Browse Source

1.resource api support update operation
2.Define Exception Class in common-utils
3.Add ExceptionHandler to common-utils
4.Find target common parameter in queryString or body if not exist in request header

xuchaolang 6 years ago
parent
commit
870aeb099d

+ 19 - 5
rankin-api-web/src/main/java/cn/rankin/apiweb/utils/RequestHeaderManager.java

@@ -2,19 +2,33 @@ package cn.rankin.apiweb.utils;
 
 import cn.rankin.apiweb.entity.GlobalHeader;
 import javax.servlet.http.HttpServletRequest;
+import java.util.Optional;
 
 public class RequestHeaderManager {
 
     public static GlobalHeader parseHeader(HttpServletRequest request) {
         GlobalHeader globalHeader = new GlobalHeader();
-        globalHeader.setUid(request.getHeader("uid"));
-        globalHeader.setEid(request.getHeader("eid"));
-        globalHeader.setSign(request.getHeader("sign"));
-        globalHeader.setRequestId(request.getHeader("requestId"));
-        globalHeader.setTerminal(request.getHeader("terminal").toLowerCase());
+        globalHeader.setUid(Optional.ofNullable(getRequestParameter(request, "uid")).orElse(""));
+        globalHeader.setEid(Optional.ofNullable(getRequestParameter(request, "eid")).orElse(""));
+        globalHeader.setSign(Optional.ofNullable(getRequestParameter(request, "sign")).orElse(""));
+        globalHeader.setRequestId(Optional.ofNullable(getRequestParameter(request, "requestId")).orElse(""));
+        globalHeader.setTerminal(Optional.ofNullable(getRequestParameter(request, "terminal")).orElse("").toLowerCase());
         return globalHeader;
     }
 
+    /**
+     * First get target parameter in header, if not exist, then get it in queryString or request body
+     * @param request
+     * @param key
+     * @return
+     */
+    public static String getRequestParameter(HttpServletRequest request, String key){
+        //first check header
+        if (null != request.getHeader(key)){
+            return request.getHeader(key);
+        }
+        return request.getParameter(key);
+    }
 
 
 }

+ 6 - 4
rankin-cms-web/src/main/java/cn/rankin/cmsweb/controller/resource/ResourceController.java

@@ -8,10 +8,7 @@ import cn.rankin.common.utils.dto.resource.ResourceDTO;
 import cn.rankin.common.utils.util.BeanUtil;
 import cn.rankin.common.utils.vo.resource.ResourceVo;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 @RestController
 @RequestMapping(value = "/resource")
@@ -29,4 +26,9 @@ public class ResourceController {
     public APIResult<ResourceVo> create(@RequestBody ResourceDTO resourceDTO) {
         return resourceService.create(resourceDTO);
     }
+
+    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
+    public APIResult<ResourceVo> create(@PathVariable("id") String id, @RequestBody ResourceDTO resourceDTO) {
+        return resourceService.update(id,resourceDTO);
+    }
 }

+ 5 - 4
rankin-cms-web/src/main/java/cn/rankin/cmsweb/service/resource/ResourceService.java

@@ -6,10 +6,7 @@ import cn.rankin.common.utils.dto.resource.ResourceDTO;
 import cn.rankin.common.utils.vo.resource.ResourceVo;
 import cn.rankin.data.api.resource.vo.OSSSignature;
 import org.springframework.cloud.netflix.feign.FeignClient;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
 import java.util.Map;
@@ -28,4 +25,8 @@ public interface ResourceService {
 
     @RequestMapping(value = "/oss/signature", method = RequestMethod.GET)
     APIResult<OSSSignature> getSign(@RequestParam("fileName") String fileName);
+
+    @RequestMapping(value = "/resource/{id}", method = RequestMethod.PUT)
+    APIResult<ResourceVo> update(@PathVariable("id") String id, @RequestBody ResourceDTO resourceDTO);
 }
+

+ 22 - 0
rankin-common-utils/src/main/java/cn/rankin/common/utils/api/controller/advice/GlobalExceptionHandler.java

@@ -0,0 +1,22 @@
+package cn.rankin.common.utils.api.controller.advice;
+
+import cn.rankin.common.utils.api.model.APICode;
+import cn.rankin.common.utils.api.model.APIResult;
+import cn.rankin.common.utils.api.model.BaseCode;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import javax.servlet.http.HttpServletRequest;
+
+@ControllerAdvice
+public class GlobalExceptionHandler {
+
+    @ExceptionHandler({Exception.class})
+    @ResponseBody
+    public APIResult handleException(HttpServletRequest request, Exception e){
+        e.printStackTrace();
+        return APIResult.error(new BaseCode(APICode._C_SERVER_ERROR, e.getMessage()));
+    }
+}
+

+ 7 - 0
rankin-common-utils/src/main/java/cn/rankin/common/utils/exception/DuplicateValueException.java

@@ -0,0 +1,7 @@
+package cn.rankin.common.utils.exception;
+
+public class DuplicateValueException extends ServiceException {
+    public DuplicateValueException (String message){
+        super(message);
+    }
+}

+ 8 - 0
rankin-common-utils/src/main/java/cn/rankin/common/utils/exception/NotFoundException.java

@@ -0,0 +1,8 @@
+package cn.rankin.common.utils.exception;
+
+public class NotFoundException extends ServiceException {
+
+    public NotFoundException (String message){
+        super(message);
+    }
+}

+ 7 - 0
rankin-common-utils/src/main/java/cn/rankin/common/utils/exception/ServiceException.java

@@ -0,0 +1,7 @@
+package cn.rankin.common.utils.exception;
+
+public class ServiceException extends RuntimeException{
+    public ServiceException(String message){
+        super(message);
+    }
+}

+ 8 - 0
rankin-common-utils/src/main/java/cn/rankin/common/utils/exception/UnsupportedOperationException.java

@@ -0,0 +1,8 @@
+package cn.rankin.common.utils.exception;
+
+public class UnsupportedOperationException extends ServiceException {
+
+    public UnsupportedOperationException(String message){
+        super(message);
+    }
+}

+ 2 - 1
rankin-data-api/src/main/java/cn/rankin/data/api/resource/entity/Resource.java

@@ -1,7 +1,6 @@
 package cn.rankin.data.api.resource.entity;
 
 import cn.rankin.common.utils.enums.BaseStatusEnum;
-import com.fasterxml.jackson.annotation.JsonIgnore;
 import lombok.Data;
 import lombok.ToString;
 import org.hibernate.annotations.DynamicInsert;
@@ -29,8 +28,10 @@ public class Resource implements Serializable {
     private String name;
 
     @Column
+    //type,{0,3}, 表示{视频, 图片}
     private Integer type;
 
+
     @Column
     private String format;
 

+ 3 - 0
rankin-resource-service/src/main/java/cn/rankin/resourceservice/ResourceServiceApplication.java

@@ -5,15 +5,18 @@ import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 import org.springframework.cloud.netflix.feign.EnableFeignClients;
+import org.springframework.context.annotation.ComponentScan;
 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
 
 @EnableFeignClients
 @EnableDiscoveryClient
 @SpringBootApplication
 @EnableJpaRepositories(repositoryBaseClass = SimpleJpaRepository.class)
+@ComponentScan(basePackages = {"cn.rankin.resourceservice", "cn.rankin.common.utils.api.controller"})
 public class ResourceServiceApplication {
 
 	public static void main(String[] args) {
 		SpringApplication.run(ResourceServiceApplication.class, args);
 	}
+
 }

+ 9 - 1
rankin-resource-service/src/main/java/cn/rankin/resourceservice/controller/ResourceController.java

@@ -3,9 +3,9 @@ package cn.rankin.resourceservice.controller;
 import cn.rankin.common.utils.api.model.APICode;
 import cn.rankin.common.utils.api.model.APIResult;
 import cn.rankin.common.utils.api.page.Page;
+import cn.rankin.common.utils.constant.ResourceType;
 import cn.rankin.common.utils.dto.resource.ResourceSearchDTO;
 import cn.rankin.data.api.resource.entity.Resource;
-import cn.rankin.common.utils.constant.ResourceType;
 import cn.rankin.resourceservice.service.ResourceService;
 import com.alibaba.fastjson.JSON;
 import lombok.extern.slf4j.Slf4j;
@@ -73,8 +73,16 @@ public class ResourceController {
         return APIResult.ok(result);
     }
 
+
+
     @RequestMapping(value = "/batch", method = RequestMethod.GET)
     public APIResult<Map<String, Resource>> getBatch(@RequestParam("id") List<String> ids) {
         return resourceService.findMixByIds(ids);
     }
+
+    @RequestMapping(value="/{id}", method = RequestMethod.PUT)
+    public APIResult<Resource> update(@PathVariable("id") String id, @RequestBody Resource resource) {
+        resourceService.update(resource);
+        return APIResult.ok();
+    }
 }

+ 8 - 0
rankin-resource-service/src/main/java/cn/rankin/resourceservice/repository/ResourceRepository.java

@@ -4,9 +4,17 @@ import cn.rankin.common.utils.jpa.BasicJpaRepository;
 import cn.rankin.data.api.resource.entity.Resource;
 import org.springframework.stereotype.Repository;
 
+import java.util.List;
+
 @Repository
 public interface ResourceRepository extends BasicJpaRepository<Resource, String> {
 
     Long countByCode(String code);
 
+    /**
+     * find resource by code
+      * @param code
+     * @return
+     */
+    List<Resource> findByCode(String code);
 }

+ 64 - 0
rankin-resource-service/src/main/java/cn/rankin/resourceservice/service/ResourceService.java

@@ -5,6 +5,9 @@ import cn.rankin.common.utils.api.model.APIResult;
 import cn.rankin.common.utils.api.page.Page;
 import cn.rankin.common.utils.constant.ResourceType;
 import cn.rankin.common.utils.dto.resource.ResourceSearchDTO;
+import cn.rankin.common.utils.exception.DuplicateValueException;
+import cn.rankin.common.utils.exception.NotFoundException;
+import cn.rankin.common.utils.exception.UnsupportedOperationException;
 import cn.rankin.resourceservice.dto.ResourceDetail;
 import cn.rankin.resourceservice.dto.ResourceRemote;
 import cn.rankin.data.api.resource.entity.Resource;
@@ -21,6 +24,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+
 @Slf4j
 @Service
 public class ResourceService {
@@ -171,4 +175,64 @@ public class ResourceService {
             return domain + "/" + path;
         }
     }
+
+    /**
+     *
+      * @param resource
+     * @throws DuplicateValueException
+     * @throws NotFoundException
+     */
+    public void update(Resource resource) throws DuplicateValueException, NotFoundException, UnsupportedOperationException {
+        if (null == resource){
+            log.error("Resource is null");
+            return;
+        }
+
+        if (ResourceType.IMG == resource.getType()){
+            updateImg(resource);
+        }
+        else{
+            log.error("Not-Supported-Resource-Type, id={}, type={}", resource.getId(), resource.getType());
+            throw new UnsupportedOperationException("Not-Supported-Resource-Type");
+        }
+    }
+
+    /**
+     * update Img
+     * @param resource
+     * @throws DuplicateValueException
+     * @throws NotFoundException
+     */
+    private void updateImg(Resource resource) throws DuplicateValueException, NotFoundException {
+
+        if (ResourceType.IMG != resource.getType()){
+            log.error("Not-Img-Resource");
+            return;
+        }
+
+        //find target resource
+        Resource targetResouce = resourceRepository.find(resource.getId());
+
+        if(null == targetResouce){
+            log.error("Cannot Find Resource, id={}", resource.getId());
+            throw new NotFoundException("Resource-Not-Found");
+        }
+
+        //only support code,title,path
+        //first, make sure code is not duplicated
+        List<Resource> foundedResource = resourceRepository.findByCode(resource.getCode());
+        foundedResource.forEach( (each) -> {
+            if ( !resource.getId().equals(each.getId()) ){
+                log.error("Code-Already-Exist, code={}", resource.getCode());
+                throw new DuplicateValueException("Code-Already-Exist");
+            }
+        });
+
+        //set and save
+        targetResouce.setCode(resource.getCode());
+        targetResouce.setName(resource.getName());
+        targetResouce.setPath(resource.getPath());
+        resourceRepository.save(targetResouce);
+    }
 }
+