|
|
@@ -4,20 +4,25 @@ import com.uas.cloud.base.commons.common.api.ResourceNotFoundException;
|
|
|
import com.uas.cloud.base.commons.common.api.ServiceError;
|
|
|
import com.uas.cloud.mall.carousel.domain.Carousel;
|
|
|
import com.uas.cloud.mall.carousel.service.CarouselService;
|
|
|
+import java.net.URI;
|
|
|
+import java.util.List;
|
|
|
import org.apache.log4j.LogManager;
|
|
|
import org.apache.log4j.Logger;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.cloud.client.ServiceInstance;
|
|
|
-import org.springframework.cloud.client.discovery.DiscoveryClient;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
-import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.PathVariable;
|
|
|
+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.ResponseStatus;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
import org.springframework.web.util.UriComponentsBuilder;
|
|
|
|
|
|
-import java.net.URI;
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
/**
|
|
|
* 轮播服务API
|
|
|
*
|
|
|
@@ -30,25 +35,24 @@ public class CarouselController {
|
|
|
|
|
|
private final Logger logger = LogManager.getLogger(getClass());
|
|
|
|
|
|
- @Autowired
|
|
|
- private DiscoveryClient client;
|
|
|
+ private final CarouselService carouselService;
|
|
|
|
|
|
@Autowired
|
|
|
- private CarouselService carouselService;
|
|
|
+ public CarouselController(CarouselService carouselService) {
|
|
|
+ this.carouselService = carouselService;
|
|
|
+ }
|
|
|
|
|
|
@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "application/json")
|
|
|
public Carousel get(@PathVariable Long id) {
|
|
|
- ServiceInstance instance = client.getLocalServiceInstance();
|
|
|
- logger.info("/carousels, method: get, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",carousel: " + id);
|
|
|
- Carousel carousel = carouselService.findOne(id);
|
|
|
- return carousel;
|
|
|
+ logger.info("/carousels, method: get, carousel: " + id);
|
|
|
+
|
|
|
+ return carouselService.findOne(id);
|
|
|
}
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.POST, consumes = "application/json")
|
|
|
@ResponseStatus(HttpStatus.CREATED)
|
|
|
public ResponseEntity<Carousel> save(@RequestBody Carousel carousel, UriComponentsBuilder ucb) {
|
|
|
- ServiceInstance instance = client.getLocalServiceInstance();
|
|
|
- logger.info("/carousel, method: post, host: " + instance.getHost() + ", serviceId: " + instance.getServiceId() + "usedFor: " + carousel.getUsedFor());
|
|
|
+ logger.info("/carousel, method: post, usedFor: " + carousel.getUsedFor());
|
|
|
|
|
|
Carousel saved = carouselService.save(carousel);
|
|
|
|
|
|
@@ -59,42 +63,37 @@ public class CarouselController {
|
|
|
.toUri();
|
|
|
headers.setLocation(locationUri);
|
|
|
|
|
|
- ResponseEntity<Carousel> responseEntity = new ResponseEntity<>(saved, headers, HttpStatus.CREATED);
|
|
|
- return responseEntity;
|
|
|
+ return new ResponseEntity<>(saved, headers, HttpStatus.CREATED);
|
|
|
}
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.PUT, consumes = "application/json")
|
|
|
@ResponseStatus(HttpStatus.OK)
|
|
|
public ResponseEntity<Carousel> update(@RequestBody Carousel carousel) {
|
|
|
- ServiceInstance instance = client.getLocalServiceInstance();
|
|
|
- logger.info("/carousel, method: put, host: " + instance.getHost() + ", serviceId: " + instance.getServiceId() + "usedFor: " + carousel.getUsedFor());
|
|
|
+ logger.info("/carousel, method: put, usedFor: " + carousel.getUsedFor());
|
|
|
|
|
|
Carousel saved = carouselService.save(carousel);
|
|
|
|
|
|
- ResponseEntity<Carousel> responseEntity = new ResponseEntity<>(saved, HttpStatus.OK);
|
|
|
- return responseEntity;
|
|
|
+ return new ResponseEntity<>(saved, HttpStatus.OK);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
|
|
|
public void delete(@PathVariable Long id) {
|
|
|
- ServiceInstance instance = client.getLocalServiceInstance();
|
|
|
- logger.info("/carousels, method: delete, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",carousel: " + id);
|
|
|
+ logger.info("/carousels, method: delete, carousel: " + id);
|
|
|
+
|
|
|
carouselService.delete(id);
|
|
|
}
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.GET, produces = "application/json")
|
|
|
public List<Carousel> carousels(@RequestParam(value = "usedFor", defaultValue = "home") String usedFor) {
|
|
|
- ServiceInstance instance = client.getLocalServiceInstance();
|
|
|
- logger.info("/carousel, get, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",usedFor: " + usedFor);
|
|
|
- List<Carousel> carousels = carouselService.findByUsedFor(usedFor);
|
|
|
- return carousels;
|
|
|
+ logger.info("/carousel, get, usedFor: " + usedFor);
|
|
|
+
|
|
|
+ return carouselService.findByUsedFor(usedFor);
|
|
|
}
|
|
|
|
|
|
@GetMapping(value = "/usedFor", produces = "application/json")
|
|
|
public List<String> getUsedFor() {
|
|
|
- ServiceInstance instance = client.getLocalServiceInstance();
|
|
|
- logger.info("/carousel/usedFor, get, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId());
|
|
|
+ logger.info("/carousel/usedFor, get");
|
|
|
+
|
|
|
return carouselService.getUsedFor();
|
|
|
}
|
|
|
|
|
|
@@ -102,6 +101,7 @@ public class CarouselController {
|
|
|
@ResponseStatus(HttpStatus.NOT_FOUND)
|
|
|
public ServiceError resourceNotFound(ResourceNotFoundException e) {
|
|
|
Long resourceId = e.getResourceId();
|
|
|
+
|
|
|
return new ServiceError(4, "Carousel [" + resourceId + "] not found");
|
|
|
}
|
|
|
}
|