├── run-order-service.sh ├── order_service_impl ├── src │ └── main │ │ ├── resources │ │ ├── db │ │ │ ├── liquibase.properties │ │ │ ├── liquibase-changelog.xml │ │ │ └── changelog │ │ │ │ ├── liquibase-insert-orders-initial-data.xml │ │ │ │ └── liquibase-create-database-structure.xml │ │ ├── banner.txt │ │ └── application.yaml │ │ └── java │ │ └── com │ │ └── zufar │ │ └── order_service_impl │ │ ├── repository │ │ └── OrderRepository.java │ │ ├── Application.java │ │ ├── converter │ │ └── OrderConverter.java │ │ ├── controller │ │ ├── CategoryController.java │ │ └── OrderController.java │ │ ├── entity │ │ └── Order.java │ │ ├── configuration │ │ └── SwaggerConfig.java │ │ ├── advice │ │ └── GlobalExceptionHandler.java │ │ └── service │ │ └── OrderService.java └── pom.xml ├── .gitignore ├── order_service_api ├── src │ └── main │ │ └── java │ │ └── com │ │ └── zufar │ │ └── order_service_api │ │ ├── endpoint │ │ ├── CategoryServiceEndPoint.java │ │ └── OrderServiceEndPoint.java │ │ └── client │ │ └── OrderFeignService.java └── pom.xml └── pom.xml /run-order-service.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "Order_service is running.." 3 | java -jar target/order_service_impl-1.0.0.jar 4 | -------------------------------------------------------------------------------- /order_service_impl/src/main/resources/db/liquibase.properties: -------------------------------------------------------------------------------- 1 | url =jdbc:postgresql://localhost:5432/order 2 | username =order_account 3 | password =order_account 4 | driver =org.postgresql.Driver 5 | changeLogFile =src/main/resources/db/liquibase-changelog.xml 6 | 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/** 5 | !**/src/test/** 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | 30 | ### VS Code ### 31 | .vscode/ 32 | -------------------------------------------------------------------------------- /order_service_impl/src/main/resources/db/liquibase-changelog.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /order_service_impl/src/main/java/com/zufar/order_service_impl/repository/OrderRepository.java: -------------------------------------------------------------------------------- 1 | package com.zufar.order_service_impl.repository; 2 | 3 | import com.zufar.order_service_impl.entity.Order; 4 | import org.springframework.data.repository.CrudRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface OrderRepository extends CrudRepository { 9 | 10 | boolean existsByClientId(Long clientId); 11 | 12 | Iterable findAllByClientIdIn(Long... clientIds); 13 | 14 | void deleteAllByClientId(Long clientId); 15 | } 16 | -------------------------------------------------------------------------------- /order_service_api/src/main/java/com/zufar/order_service_api/endpoint/CategoryServiceEndPoint.java: -------------------------------------------------------------------------------- 1 | package com.zufar.order_service_api.endpoint; 2 | 3 | import com.zufar.order_management_system_common.dto.Category; 4 | import org.springframework.http.ResponseEntity; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | 7 | @RequestMapping("/default") 8 | public interface CategoryServiceEndPoint { 9 | 10 | /** 11 | * Returns all order categories 12 | * 13 | * @return all categories 14 | */ 15 | ResponseEntity getCategories(); 16 | } 17 | -------------------------------------------------------------------------------- /order_service_impl/src/main/java/com/zufar/order_service_impl/Application.java: -------------------------------------------------------------------------------- 1 | package com.zufar.order_service_impl; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | import org.springframework.cloud.openfeign.EnableFeignClients; 7 | 8 | @EnableDiscoveryClient 9 | @EnableFeignClients(basePackages = "com.zufar.client_service_api.client") 10 | @SpringBootApplication 11 | public class Application { 12 | public static void main(String[] args) { 13 | SpringApplication.run(Application.class, args); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /order_service_impl/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | ___ _ __ __ _ _ 2 | / _ \ _ __ __| | ___ _ __ | \/ | (_) ___ _ __ ___ ___ ___ _ __ __ __ (_) ___ ___ 3 | | | | | | '__| / _` | / _ \ | '__| | |\/| | | | / __| | '__| / _ \ / __| / _ \ | '__| \ \ / / | | / __| / _ \ 4 | | |_| | | | | (_| | | __/ | | | | | | | | | (__ | | | (_) | \__ \ | __/ | | \ V / | | | (__ | __/ 5 | \___/ |_| \__,_| \___| |_| |_| |_| |_| \___| |_| \___/ |___/ \___| |_| \_/ |_| \___| \___| 6 | 7 | -------------------------------------------------------------------------------- /order_service_impl/src/main/java/com/zufar/order_service_impl/converter/OrderConverter.java: -------------------------------------------------------------------------------- 1 | package com.zufar.order_service_impl.converter; 2 | 3 | import com.zufar.order_management_system_common.dto.OrderDTO; 4 | import com.zufar.order_service_impl.entity.Order; 5 | 6 | public class OrderConverter { 7 | 8 | public static Order convertToOrder(OrderDTO orderDTO) { 9 | return new Order( 10 | orderDTO.getId(), 11 | orderDTO.getGoodsName(), 12 | orderDTO.getCategory(), 13 | orderDTO.getClientId() 14 | ); 15 | } 16 | 17 | public static OrderDTO convertToOrderDTO(Order order) { 18 | return new OrderDTO( 19 | order.getId(), 20 | order.getGoodsName(), 21 | order.getCategory(), 22 | order.getClientId() 23 | ); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /order_service_impl/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8098 3 | 4 | eureka: 5 | instance: 6 | leaseRenewalIntervalInSeconds: 1 7 | leaseExpirationDurationInSeconds: 2 8 | client: 9 | serviceUrl: 10 | defaultZone: http://localhost:8761/eureka/ 11 | 12 | spring: 13 | application: 14 | name: order-service 15 | liquibase: 16 | enabled: true 17 | change-log: classpath:/db/liquibase-changelog.xml 18 | datasource: 19 | url: 'jdbc:postgresql://localhost:5432/order' 20 | username: 'order_account' 21 | password: 'order_account' 22 | driver-class-name: org.postgresql.Driver 23 | hikari: 24 | connectionTimeout: 20000 25 | maximumPoolSize: 5 26 | jpa: 27 | hibernate: 28 | ddl-auto: validate 29 | properties: 30 | hibernate: 31 | dialect: 'org.hibernate.dialect.PostgreSQLDialect' 32 | banner: 33 | location: 'classpath:/banner.txt' 34 | 35 | -------------------------------------------------------------------------------- /order_service_impl/src/main/resources/db/changelog/liquibase-insert-orders-initial-data.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /order_service_impl/src/main/java/com/zufar/order_service_impl/controller/CategoryController.java: -------------------------------------------------------------------------------- 1 | package com.zufar.order_service_impl.controller; 2 | 3 | import com.zufar.order_management_system_common.dto.Category; 4 | import com.zufar.order_service_api.endpoint.CategoryServiceEndPoint; 5 | 6 | import io.swagger.annotations.Api; 7 | import io.swagger.annotations.ApiOperation; 8 | 9 | import org.springframework.http.HttpStatus; 10 | import org.springframework.http.ResponseEntity; 11 | import org.springframework.web.bind.annotation.GetMapping; 12 | import org.springframework.web.bind.annotation.RequestMapping; 13 | import org.springframework.web.bind.annotation.RestController; 14 | 15 | @Api(value = "Category api") 16 | @RestController 17 | @RequestMapping(value = "categories") 18 | public class CategoryController implements CategoryServiceEndPoint { 19 | 20 | @Override 21 | @GetMapping 22 | @ApiOperation(value = "View a list of order categories.", response = Category[].class, responseContainer = "List") 23 | public ResponseEntity getCategories() { 24 | return new ResponseEntity<>(Category.values(), HttpStatus.OK); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /order_service_impl/src/main/java/com/zufar/order_service_impl/entity/Order.java: -------------------------------------------------------------------------------- 1 | package com.zufar.order_service_impl.entity; 2 | 3 | import com.zufar.order_management_system_common.dto.Category; 4 | import io.swagger.annotations.ApiModel; 5 | import io.swagger.annotations.ApiModelProperty; 6 | import lombok.AllArgsConstructor; 7 | import lombok.Data; 8 | import lombok.NoArgsConstructor; 9 | 10 | import javax.persistence.*; 11 | 12 | 13 | @ApiModel(value = "Order") 14 | @Data 15 | @AllArgsConstructor 16 | @NoArgsConstructor 17 | @Entity 18 | @Table(name = "orders") 19 | public class Order { 20 | 21 | @ApiModelProperty(notes = "Order id", name = "id", required = true) 22 | @Id 23 | @SequenceGenerator(name = "order_sequence", sequenceName = "order_seq", allocationSize = 1) 24 | @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "order_sequence") 25 | private Long id; 26 | 27 | 28 | @ApiModelProperty(notes = "Goods name in an order", name = "goodsName", required = true) 29 | @Column(name = "goods_name", length = 160, nullable = false) 30 | private String goodsName; 31 | 32 | @ApiModelProperty(notes = "Goods category in an order", name = "category_id", required = true) 33 | @Enumerated(EnumType.STRING) 34 | private Category category; 35 | 36 | @ApiModelProperty(notes = "Client id in an order", name = "clientId", required = true) 37 | @Column(name = "client_id", nullable = false) 38 | private Long clientId; 39 | } 40 | -------------------------------------------------------------------------------- /order_service_api/src/main/java/com/zufar/order_service_api/client/OrderFeignService.java: -------------------------------------------------------------------------------- 1 | package com.zufar.order_service_api.client; 2 | 3 | import java.util.List; 4 | 5 | import com.zufar.order_management_system_common.dto.OrderDTO; 6 | import org.springframework.http.ResponseEntity; 7 | import org.springframework.cloud.openfeign.FeignClient; 8 | import org.springframework.web.bind.annotation.PostMapping; 9 | import org.springframework.web.bind.annotation.ResponseBody; 10 | import org.springframework.web.bind.annotation.RequestBody; 11 | import org.springframework.web.bind.annotation.DeleteMapping; 12 | import org.springframework.web.bind.annotation.PathVariable; 13 | 14 | 15 | @FeignClient(name = "order-service", path = "orders") 16 | public interface OrderFeignService { 17 | 18 | /** 19 | * Returns all orders with given client ids. 20 | * 21 | * @param clientIds must not be {@literal null}. 22 | * @return the orders with the given client ids. 23 | */ 24 | @PostMapping(value = "clients") 25 | @ResponseBody ResponseEntity> getAllByClientIds(@RequestBody Long... clientIds); 26 | 27 | /** 28 | * Deletes all orders with the given client id. 29 | * 30 | * @param clientId must not be {@literal null}. 31 | * @return the operation info. 32 | */ 33 | @DeleteMapping(value = "client/{clientId}") 34 | @ResponseBody ResponseEntity deleteAllByClientId(@PathVariable(value = "clientId") Long clientId); 35 | } 36 | -------------------------------------------------------------------------------- /order_service_impl/src/main/java/com/zufar/order_service_impl/configuration/SwaggerConfig.java: -------------------------------------------------------------------------------- 1 | package com.zufar.order_service_impl.configuration; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | import springfox.documentation.builders.ApiInfoBuilder; 7 | import springfox.documentation.builders.PathSelectors; 8 | import springfox.documentation.builders.RequestHandlerSelectors; 9 | import springfox.documentation.service.ApiInfo; 10 | import springfox.documentation.service.Contact; 11 | import springfox.documentation.spi.DocumentationType; 12 | import springfox.documentation.spring.web.plugins.Docket; 13 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 14 | 15 | @EnableSwagger2 16 | @Configuration 17 | public class SwaggerConfig { 18 | 19 | @Bean 20 | public Docket api() { 21 | return new Docket(DocumentationType.SWAGGER_2) 22 | .select() 23 | .apis(RequestHandlerSelectors.any()) 24 | .paths(PathSelectors.any()) 25 | .build().apiInfo(apiEndPointsInfo()); 26 | } 27 | 28 | private ApiInfo apiEndPointsInfo() { 29 | return new ApiInfoBuilder() 30 | .title("Order REST API") 31 | .description("Order REST API provides api for working with orders") 32 | .contact(new Contact("Zufar Sunagatov", "https://vk.com/person99", "zufar.sunagatov@gmail.com")) 33 | .version("1.0.0") 34 | .build(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /order_service_api/src/main/java/com/zufar/order_service_api/endpoint/OrderServiceEndPoint.java: -------------------------------------------------------------------------------- 1 | package com.zufar.order_service_api.endpoint; 2 | 3 | 4 | import java.util.List; 5 | 6 | import com.zufar.order_management_system_common.dto.OrderDTO; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | 10 | import javax.validation.Valid; 11 | import javax.validation.constraints.NotNull; 12 | 13 | 14 | @RequestMapping("/default") 15 | public interface OrderServiceEndPoint { 16 | 17 | /** 18 | * Returns all orders. 19 | * 20 | * @return all orders. 21 | */ 22 | ResponseEntity> getAll(); 23 | 24 | /** 25 | * Returns all orders with given client ids. 26 | * 27 | * @param clientIds must not be {@literal null}. 28 | * @return the orders with the given client ids. 29 | */ 30 | ResponseEntity> getAllByClientIds(T... clientIds); 31 | 32 | /** 33 | * Returns the order with given client id. 34 | * 35 | * @param id must not be {@literal null}. 36 | * @return the order with the given id. 37 | */ 38 | ResponseEntity getById(T id); 39 | 40 | 41 | /** 42 | * Deletes the order with the given id. 43 | * 44 | * @param id must not be {@literal null}. 45 | * @return the operation info. 46 | */ 47 | ResponseEntity deleteById(T id); 48 | 49 | /** 50 | * Deletes all orders with the given client id. 51 | * 52 | * @param clientId must not be {@literal null}. 53 | * @return the operation info. 54 | */ 55 | ResponseEntity deleteAllByClientId(T clientId); 56 | 57 | /** 58 | * Saves the given order. 59 | * 60 | * @param order must not be {@literal null}. 61 | * @return the operation info. 62 | */ 63 | ResponseEntity save(@NotNull @Valid E order); 64 | 65 | /** 66 | * Updates the given order. 67 | * 68 | * @param order must not be {@literal null}. 69 | * @return the operation info. 70 | */ 71 | ResponseEntity update(@NotNull @Valid E order); 72 | } 73 | -------------------------------------------------------------------------------- /order_service_impl/src/main/resources/db/changelog/liquibase-create-database-structure.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 4.0.0 6 | 7 | 8 | org.springframework.boot 9 | spring-boot-starter-parent 10 | 2.0.1.RELEASE 11 | 12 | 13 | 14 | 15 | order_service_api 16 | order_service_impl 17 | 18 | 19 | com.zufar 20 | order_service 21 | 1.0.0 22 | pom 23 | 24 | Order microservice provides reference information about orders. 25 | Order_Service 26 | 27 | 28 | 29 | Zufar Sunagatov 30 | zufar.sunagatov@gmail.com 31 | https://vk.com/person99 32 | 33 | architect 34 | developer 35 | tester 36 | 37 | Samara (UTC+4:00) 38 | 39 | 40 | 41 | 42 | 43 | 1.8 44 | UTF-8 45 | UTF-8 46 | https://github.com/Sunagatov/Order_Service 47 | 48 | UTF-8 49 | 1.8 50 | 1.8 51 | 52 | 53 | 54 | 55 | 56 | org.apache.maven.plugins 57 | maven-compiler-plugin 58 | 59 | 8 60 | 8 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /order_service_impl/src/main/java/com/zufar/order_service_impl/advice/GlobalExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.zufar.order_service_impl.advice; 2 | 3 | import com.zufar.order_management_system_common.dto.OperationResult; 4 | import com.zufar.order_management_system_common.exception.ClientNotFoundException; 5 | import com.zufar.order_management_system_common.exception.InternalServerException; 6 | import com.zufar.order_management_system_common.exception.OrderNotFoundException; 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.annotation.ControllerAdvice; 10 | import org.springframework.web.bind.annotation.ExceptionHandler; 11 | 12 | import java.time.LocalDateTime; 13 | import java.time.format.DateTimeFormatter; 14 | 15 | @ControllerAdvice 16 | public class GlobalExceptionHandler { 17 | 18 | private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy hh:mm:ss"); 19 | 20 | @ExceptionHandler({ClientNotFoundException.class, OrderNotFoundException.class}) 21 | public final ResponseEntity handleException(RuntimeException ex) { 22 | HttpStatus status = HttpStatus.NOT_FOUND; 23 | return ResponseEntity.status(status) 24 | .body(OperationResult.builder() 25 | .timestamp(LocalDateTime.now().format(formatter)) 26 | .message(ex.getMessage()) 27 | .status(HttpStatus.NOT_FOUND.toString()) 28 | .build()); 29 | } 30 | 31 | @ExceptionHandler({InternalServerException.class}) 32 | public final ResponseEntity handleException(InternalServerException ex) { 33 | HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR; 34 | return ResponseEntity.status(status) 35 | .body(OperationResult.builder() 36 | .timestamp(LocalDateTime.now().format(formatter)) 37 | .message(ex.getMessage() + "Try again later.") 38 | .status(HttpStatus.NOT_FOUND.toString()) 39 | .build()); 40 | } 41 | 42 | @ExceptionHandler({Exception.class}) 43 | public final ResponseEntity handleException() { 44 | HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR; 45 | return ResponseEntity.status(status) 46 | .body(OperationResult.builder() 47 | .timestamp(LocalDateTime.now().format(formatter)) 48 | .message("There is some internal error. Try again later.") 49 | .status(HttpStatus.NOT_FOUND.toString()) 50 | .build()); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /order_service_api/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 4.0.0 7 | 8 | 9 | com.zufar 10 | order_service 11 | 1.0.0 12 | 13 | 14 | com.zufar 15 | order_service_api 16 | 1.0.0 17 | 18 | order_service_api 19 | Order service api provides api of order microservice. 20 | 21 | 22 | 23 | Zufar Sunagatov 24 | zufar.sunagatov@gmail.com 25 | https://vk.com/person99 26 | 27 | architect 28 | developer 29 | tester 30 | 31 | Samara (UTC+4:00) 32 | 33 | 34 | 35 | 36 | 37 | 1.8 38 | UTF-8 39 | UTF-8 40 | https://github.com/Sunagatov/Order_Service 41 | 42 | UTF-8 43 | 1.8 44 | 1.8 45 | 46 | 2.1.5.RELEASE 47 | 2.1.3.RELEASE 48 | Finchley.SR1 49 | 50 | 1.0.0 51 | 52 | 53 | 54 | 55 | 56 | org.springframework.cloud 57 | spring-cloud-starter-openfeign 58 | 59 | 60 | 61 | com.zufar 62 | OrderManagementSystemUtil 63 | ${com.zufar.OrderManagementSystemUtil.version} 64 | 65 | 66 | 67 | 68 | 69 | 70 | org.springframework.cloud 71 | spring-cloud-dependencies 72 | ${spring-cloud.version} 73 | pom 74 | import 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | org.apache.maven.plugins 84 | maven-compiler-plugin 85 | 86 | 8 87 | 8 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /order_service_impl/src/main/java/com/zufar/order_service_impl/service/OrderService.java: -------------------------------------------------------------------------------- 1 | package com.zufar.order_service_impl.service; 2 | 3 | import com.zufar.client_service_api.client.ClientFeignService; 4 | import com.zufar.order_management_system_common.dto.OrderDTO; 5 | import com.zufar.order_management_system_common.exception.ClientNotFoundException; 6 | import com.zufar.order_management_system_common.exception.InternalServerException; 7 | import com.zufar.order_management_system_common.exception.OrderNotFoundException; 8 | import com.zufar.order_service_impl.converter.OrderConverter; 9 | import com.zufar.order_service_impl.entity.Order; 10 | import com.zufar.order_service_impl.repository.OrderRepository; 11 | import org.apache.logging.log4j.LogManager; 12 | import org.apache.logging.log4j.Logger; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.http.HttpStatus; 15 | import org.springframework.http.ResponseEntity; 16 | import org.springframework.stereotype.Service; 17 | import org.springframework.transaction.annotation.Transactional; 18 | 19 | import java.util.List; 20 | import java.util.stream.Collectors; 21 | import java.util.stream.StreamSupport; 22 | 23 | import static com.zufar.order_service_impl.converter.OrderConverter.convertToOrder; 24 | import static com.zufar.order_service_impl.converter.OrderConverter.convertToOrderDTO; 25 | 26 | @Service 27 | public class OrderService { 28 | 29 | private static final Logger LOGGER = LogManager.getLogger(OrderService.class); 30 | 31 | private final OrderRepository orderRepository; 32 | private final ClientFeignService clientService; 33 | 34 | @Autowired 35 | public OrderService(OrderRepository orderRepository, 36 | ClientFeignService clientService) { 37 | this.orderRepository = orderRepository; 38 | this.clientService = clientService; 39 | } 40 | 41 | public List getAll() { 42 | LOGGER.info("Get all orders."); 43 | return StreamSupport.stream(orderRepository.findAll().spliterator(), false) 44 | .map(OrderConverter::convertToOrderDTO) 45 | .collect(Collectors.toList()); 46 | } 47 | 48 | public List getAllByClientIds(Long... ids) { 49 | LOGGER.info("Get all orders of clients"); 50 | return StreamSupport.stream(orderRepository.findAllByClientIdIn(ids).spliterator(), false) 51 | .map(OrderConverter::convertToOrderDTO) 52 | .collect(Collectors.toList()); 53 | } 54 | 55 | public OrderDTO getById(Long id) { 56 | LOGGER.info(String.format("Get order with id=[%d]", id)); 57 | return orderRepository.findById(id).map(OrderConverter::convertToOrderDTO).orElse(null); 58 | } 59 | 60 | @Transactional 61 | public OrderDTO save(OrderDTO order) { 62 | this.isClientExists(order.getClientId(), String.format("It is impossible to save the order = [%s].", order)); 63 | Order orderEntity = convertToOrder(order); 64 | orderEntity = this.orderRepository.save(orderEntity); 65 | LOGGER.info(String.format("Order=[%s] was saved in a database successfully.", orderEntity)); 66 | return convertToOrderDTO(orderEntity); 67 | } 68 | 69 | @Transactional 70 | public OrderDTO update(OrderDTO order) { 71 | String errorMessage = String.format("It is impossible to update the order = [%s].", order); 72 | isOrderExists(order.getId(), errorMessage); 73 | this.isClientExists(order.getClientId(), "It is impossible to update the order."); 74 | Order orderEntity = convertToOrder(order); 75 | orderEntity = this.orderRepository.save(orderEntity); 76 | LOGGER.info(String.format("Order=[%s] was updated in a database successfully.", orderEntity)); 77 | return convertToOrderDTO(orderEntity); 78 | } 79 | 80 | public void deleteById(Long id) { 81 | this.orderRepository.deleteById(id); 82 | LOGGER.info(String.format("Order with id=[%d] was deleted successfully.", id)); 83 | } 84 | 85 | @Transactional 86 | public void deleteAllByClientId(Long clientId) { 87 | if (!this.orderRepository.existsByClientId(clientId)) { 88 | LOGGER.info(String.format("There are no orders with client id=[%d].", clientId)); 89 | return; 90 | } 91 | this.orderRepository.deleteAllByClientId(clientId); 92 | LOGGER.info(String.format("The orders with client id=[%d] was deleted from a database.", clientId)); 93 | } 94 | 95 | private void isOrderExists(Long orderId, String errorMessage) { 96 | if (!this.orderRepository.existsById(orderId)) { 97 | String fullErrorMessage = errorMessage + String.format("Order with id=[%d] was not found.", orderId); 98 | LOGGER.error(fullErrorMessage); 99 | throw new OrderNotFoundException(fullErrorMessage); 100 | } 101 | } 102 | 103 | private void isClientExists(Long clientId, String errorMessage) { 104 | ResponseEntity result = this.clientService.isExists(clientId); 105 | if (result == null || result.getBody() == null || result.getStatusCode() != HttpStatus.OK) { 106 | String fullErrorMessage = errorMessage + String.format("There is no info about client with id=[%d].", clientId); 107 | LOGGER.error(fullErrorMessage); 108 | throw new InternalServerException(fullErrorMessage); 109 | } 110 | if (!result.getBody()) { 111 | String fullErrorMessage = errorMessage + String.format("There is no client with id=[%d].", clientId); 112 | LOGGER.error(fullErrorMessage); 113 | throw new ClientNotFoundException(fullErrorMessage); 114 | } 115 | LOGGER.info(String.format("There is client with id=[%d].", clientId)); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /order_service_impl/src/main/java/com/zufar/order_service_impl/controller/OrderController.java: -------------------------------------------------------------------------------- 1 | package com.zufar.order_service_impl.controller; 2 | 3 | import com.zufar.order_management_system_common.dto.OperationResult; 4 | import com.zufar.order_management_system_common.dto.OrderDTO; 5 | import com.zufar.order_service_api.endpoint.OrderServiceEndPoint; 6 | import com.zufar.order_service_impl.service.OrderService; 7 | 8 | import io.swagger.annotations.Api; 9 | import io.swagger.annotations.ApiOperation; 10 | import io.swagger.annotations.ApiParam; 11 | 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.http.HttpStatus; 14 | import org.springframework.http.ResponseEntity; 15 | import org.springframework.validation.annotation.Validated; 16 | import org.springframework.web.bind.annotation.*; 17 | 18 | import javax.validation.Valid; 19 | import javax.validation.constraints.NotNull; 20 | import java.time.LocalDateTime; 21 | import java.time.format.DateTimeFormatter; 22 | import java.util.List; 23 | 24 | @Api(value = "Order api") 25 | @Validated 26 | @RestController 27 | @RequestMapping(value = "orders") 28 | public class OrderController implements OrderServiceEndPoint { 29 | 30 | private OrderService orderService; 31 | private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy hh:mm:ss"); 32 | 33 | @Autowired 34 | public OrderController(OrderService orderService) { 35 | this.orderService = orderService; 36 | } 37 | 38 | @GetMapping 39 | @Override 40 | @ApiOperation(value = "View the order list.", response = OrderDTO.class, responseContainer = "List") 41 | public @ResponseBody ResponseEntity> getAll() { 42 | return new ResponseEntity<>(this.orderService.getAll(), HttpStatus.OK); 43 | } 44 | 45 | @PostMapping(value = "clients") 46 | @Override 47 | @ApiOperation(value = "View the order list with given order ids.", response = OrderDTO.class, responseContainer = "List") 48 | public @ResponseBody ResponseEntity> getAllByClientIds(@ApiParam(value = "An order client ids which is used to get orders.", required = true) @RequestBody Long... clientIds) { 49 | return new ResponseEntity<>(this.orderService.getAllByClientIds(clientIds), HttpStatus.OK); 50 | } 51 | 52 | @GetMapping(value = "/{id}") 53 | @ApiOperation(value = "View the order with given order id.", response = OrderDTO.class) 54 | public @ResponseBody ResponseEntity getById(@ApiParam(value = "An order id which is used to retrieve an order.", required = true) @PathVariable Long id) { 55 | return new ResponseEntity<>(this.orderService.getById(id), HttpStatus.OK); 56 | } 57 | 58 | @DeleteMapping(value = "/{id}") 59 | @Override 60 | @ApiOperation(value = "Delete the order with given order id.", response = ResponseEntity.class) 61 | public @ResponseBody ResponseEntity deleteById(@ApiParam(value = "An order id which is used to delete an order.", required = true) @PathVariable Long id) { 62 | this.orderService.deleteById(id); 63 | return ResponseEntity.status(HttpStatus.OK) 64 | .body(OperationResult.builder() 65 | .timestamp(LocalDateTime.now().format(formatter)) 66 | .message(String.format("The order with the given order id=[%d] was deleted.", id)) 67 | .status(HttpStatus.OK.toString()) 68 | .build()); 69 | } 70 | 71 | @DeleteMapping(value = "client/{clientId}") 72 | @Override 73 | @ApiOperation(value = "Delete the orders of the client with given client id.", response = ResponseEntity.class) 74 | public @ResponseBody ResponseEntity deleteAllByClientId(@ApiParam(value = "A client id which is used to delete orders of specific client.", required = true) @PathVariable(value = "clientId") Long clientId) { 75 | this.orderService.deleteAllByClientId(clientId); 76 | return ResponseEntity.status(HttpStatus.OK) 77 | .body(OperationResult.builder() 78 | .timestamp(LocalDateTime.now().format(formatter)) 79 | .message(String.format("The orders of the client with given client id=[%d] were deleted.", clientId)) 80 | .status(HttpStatus.OK.toString()) 81 | .build()); 82 | } 83 | 84 | @PostMapping 85 | @Override 86 | @ApiOperation(value = "Save a new order.", response = ResponseEntity.class) 87 | public @ResponseBody ResponseEntity save(@ApiParam(value = "An order object which which will be saved.", required = true) @NotNull @Valid @RequestBody OrderDTO order) { 88 | OrderDTO savedOrder = this.orderService.save(order); 89 | return ResponseEntity.status(HttpStatus.CREATED) 90 | .body(OperationResult.builder() 91 | .timestamp(LocalDateTime.now().format(formatter)) 92 | .message((String.format("The order [%s] was saved.", savedOrder))) 93 | .status(HttpStatus.CREATED.toString()) 94 | .build()); 95 | } 96 | 97 | @PutMapping 98 | @Override 99 | @ApiOperation(value = "Update an existed order.", response = ResponseEntity.class) 100 | public @ResponseBody ResponseEntity update(@ApiParam(value = "An order object which will be used to update an existed order.", required = true) @NotNull @Valid @RequestBody OrderDTO order) { 101 | OrderDTO updatedOrder = this.orderService.update(order); 102 | return ResponseEntity.status(HttpStatus.OK) 103 | .body(OperationResult.builder() 104 | .timestamp(LocalDateTime.now().format(formatter)) 105 | .message((String.format("The order [%s] was updated.", updatedOrder))) 106 | .status(HttpStatus.OK.toString()) 107 | .build()); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /order_service_impl/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 4.0.0 7 | 8 | 9 | com.zufar 10 | order_service 11 | 1.0.0 12 | 13 | 14 | com.zufar 15 | order_service_impl 16 | 1.0.0 17 | 18 | order_service_impl 19 | Order service impl provides implementation for order microservice api. 20 | 21 | 22 | 23 | Zufar Sunagatov 24 | zufar.sunagatov@gmail.com 25 | https://vk.com/person99 26 | 27 | architect 28 | developer 29 | tester 30 | 31 | Samara (UTC+4:00) 32 | 33 | 34 | 35 | 36 | 37 | 1.8 38 | UTF-8 39 | UTF-8 40 | https://github.com/Sunagatov/Order_Service 41 | 42 | UTF-8 43 | 1.8 44 | 1.8 45 | 46 | 2.1.5.RELEASE 47 | 2.1.3.RELEASE 48 | Finchley.SR1 49 | 50 | 42.2.5 51 | 3.4.1 52 | 3.4.1 53 | 54 | 4.12 55 | 1.3 56 | 2.1.5.RELEASE 57 | 58 | 2.8.2 59 | 2.8.2 60 | 61 | 2.9.2 62 | 1.5.21 63 | 2.9.2 64 | 65 | 66 | 1.18.8 67 | 1.0.0 68 | 1.0.0 69 | 70 | 71 | 72 | 73 | 74 | 75 | org.springframework.cloud 76 | spring-cloud-dependencies 77 | ${spring-cloud.version} 78 | pom 79 | import 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | org.springframework.boot 89 | spring-boot-starter-web 90 | ${spring.framework.boot-starter.version} 91 | 92 | 93 | org.springframework.cloud 94 | spring-cloud-starter-netflix-eureka-client 95 | 96 | 97 | org.springframework.cloud 98 | spring-cloud-starter-openfeign 99 | 100 | 101 | org.springframework.boot 102 | spring-boot-starter-tomcat 103 | provided 104 | ${spring.framework.boot-starter.version} 105 | 106 | 107 | 108 | org.postgresql 109 | postgresql 110 | ${postgressql.version} 111 | 112 | 113 | org.springframework.boot 114 | spring-boot-starter-data-jpa 115 | ${spring.framework.boot-starter.version} 116 | 117 | 118 | org.liquibase 119 | liquibase-core 120 | ${liquibase-core.version} 121 | 122 | 123 | 124 | org.springframework.boot 125 | spring-boot-starter-test 126 | test 127 | ${spring-boot-starter-test.version} 128 | 129 | 130 | junit 131 | junit 132 | ${junit.version} 133 | test 134 | 135 | 136 | org.hamcrest 137 | hamcrest-all 138 | ${org.hamcrest.version} 139 | test 140 | 141 | 142 | 143 | org.apache.logging.log4j 144 | log4j-api 145 | ${log4j.api.version} 146 | 147 | 148 | org.apache.logging.log4j 149 | log4j-core 150 | ${log4j.core.version} 151 | 152 | 153 | 154 | io.springfox 155 | springfox-swagger-ui 156 | ${swagger.jersey2.jaxrs.api.version} 157 | 158 | 159 | io.springfox 160 | springfox-swagger2 161 | ${swagger.jersey2.jaxrs.api.version} 162 | 163 | 164 | io.swagger 165 | swagger-annotations 166 | 167 | 168 | io.swagger 169 | swagger-models 170 | 171 | 172 | 173 | 174 | io.springfox 175 | springfox-swagger-ui 176 | ${io.springfox-swagger-ui.version} 177 | 178 | 179 | io.swagger 180 | swagger-annotations 181 | ${io.swagger.version} 182 | 183 | 184 | io.swagger 185 | swagger-models 186 | ${io.swagger.version} 187 | 188 | 189 | 190 | org.projectlombok 191 | lombok 192 | ${org.projectlombok.version} 193 | provided 194 | 195 | 196 | com.zufar 197 | OrderManagementSystemUtil 198 | ${com.zufar.OrderManagementSystemUtil.version} 199 | 200 | 201 | com.zufar 202 | order_service_api 203 | 1.0.0 204 | 205 | 206 | com.zufar.order_service_impl 207 | client_service_impl 208 | 1.0.0 209 | 210 | 211 | 212 | 213 | 214 | 215 | org.apache.maven.plugins 216 | maven-compiler-plugin 217 | 218 | 8 219 | 8 220 | 221 | 222 | 223 | org.liquibase 224 | liquibase-maven-plugin 225 | ${liquibase-maven-plugin.version} 226 | 227 | src/main/resources/db/liquibase.properties 228 | 229 | 230 | 231 | 232 | 233 | --------------------------------------------------------------------------------