diff --git a/.gradle/8.5/executionHistory/executionHistory.bin b/.gradle/8.5/executionHistory/executionHistory.bin index b7bef7d..d59e707 100644 Binary files a/.gradle/8.5/executionHistory/executionHistory.bin and b/.gradle/8.5/executionHistory/executionHistory.bin differ diff --git a/.gradle/8.5/executionHistory/executionHistory.lock b/.gradle/8.5/executionHistory/executionHistory.lock index 8ca11a9..4395df6 100644 Binary files a/.gradle/8.5/executionHistory/executionHistory.lock and b/.gradle/8.5/executionHistory/executionHistory.lock differ diff --git a/.gradle/8.5/fileHashes/fileHashes.bin b/.gradle/8.5/fileHashes/fileHashes.bin index 1f9f115..34397a4 100644 Binary files a/.gradle/8.5/fileHashes/fileHashes.bin and b/.gradle/8.5/fileHashes/fileHashes.bin differ diff --git a/.gradle/8.5/fileHashes/fileHashes.lock b/.gradle/8.5/fileHashes/fileHashes.lock index d45f28a..4c3ec78 100644 Binary files a/.gradle/8.5/fileHashes/fileHashes.lock and b/.gradle/8.5/fileHashes/fileHashes.lock differ diff --git a/.gradle/8.5/fileHashes/resourceHashesCache.bin b/.gradle/8.5/fileHashes/resourceHashesCache.bin index 7fe8d3a..1f1a381 100644 Binary files a/.gradle/8.5/fileHashes/resourceHashesCache.bin and b/.gradle/8.5/fileHashes/resourceHashesCache.bin differ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index 4a71871..9386d8d 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/.gradle/file-system.probe b/.gradle/file-system.probe index 7020664..828e72a 100644 Binary files a/.gradle/file-system.probe and b/.gradle/file-system.probe differ diff --git a/.vscode/launch.json b/.vscode/launch.json index 8a4c9bd..238dd94 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -2,23 +2,13 @@ "configurations": [ { "type": "java", - "name": "Demo", + "name": "Spring Boot-DemoApplication", "request": "launch", "cwd": "${workspaceFolder}", "mainClass": "com.example.demo.DemoApplication", - "projectName": "demo", + "projectName": "IP_PIbd-21_Kryukov_Backend", "args": "--populate", "envFile": "${workspaceFolder}/.env" - }, - { - "type": "java", - "name": "Spring Boot-DemoApplication", - "request": "launch", - "cwd": "${workspaceFolder}", - "mainClass": "com.example.demo.DemoApplication", - "projectName": "lab_2", - "args": "", - "envFile": "${workspaceFolder}/.env" } ] } \ No newline at end of file diff --git a/build/tmp/compileJava/compileTransaction/stash-dir/DemoApplication.class.uniqueId5 b/build/tmp/compileJava/compileTransaction/stash-dir/DemoApplication.class.uniqueId5 new file mode 100644 index 0000000..ff4eabd Binary files /dev/null and b/build/tmp/compileJava/compileTransaction/stash-dir/DemoApplication.class.uniqueId5 differ diff --git a/build/tmp/compileJava/compileTransaction/stash-dir/OrderLineController.class.uniqueId3 b/build/tmp/compileJava/compileTransaction/stash-dir/OrderLineController.class.uniqueId3 new file mode 100644 index 0000000..2c88e57 Binary files /dev/null and b/build/tmp/compileJava/compileTransaction/stash-dir/OrderLineController.class.uniqueId3 differ diff --git a/build/tmp/compileJava/compileTransaction/stash-dir/OrderLineEntity.class.uniqueId0 b/build/tmp/compileJava/compileTransaction/stash-dir/OrderLineEntity.class.uniqueId0 new file mode 100644 index 0000000..2f4bfba Binary files /dev/null and b/build/tmp/compileJava/compileTransaction/stash-dir/OrderLineEntity.class.uniqueId0 differ diff --git a/build/tmp/compileJava/compileTransaction/stash-dir/OrderLineRepository.class.uniqueId8 b/build/tmp/compileJava/compileTransaction/stash-dir/OrderLineRepository.class.uniqueId8 new file mode 100644 index 0000000..6030791 Binary files /dev/null and b/build/tmp/compileJava/compileTransaction/stash-dir/OrderLineRepository.class.uniqueId8 differ diff --git a/build/tmp/compileJava/compileTransaction/stash-dir/OrderLineService.class.uniqueId9 b/build/tmp/compileJava/compileTransaction/stash-dir/OrderLineService.class.uniqueId9 new file mode 100644 index 0000000..8ccd40b Binary files /dev/null and b/build/tmp/compileJava/compileTransaction/stash-dir/OrderLineService.class.uniqueId9 differ diff --git a/build/tmp/compileJava/compileTransaction/stash-dir/ProductController.class.uniqueId7 b/build/tmp/compileJava/compileTransaction/stash-dir/ProductController.class.uniqueId7 new file mode 100644 index 0000000..e6c3f9a Binary files /dev/null and b/build/tmp/compileJava/compileTransaction/stash-dir/ProductController.class.uniqueId7 differ diff --git a/build/tmp/compileJava/compileTransaction/stash-dir/ProductDto.class.uniqueId2 b/build/tmp/compileJava/compileTransaction/stash-dir/ProductDto.class.uniqueId2 new file mode 100644 index 0000000..d192c3b Binary files /dev/null and b/build/tmp/compileJava/compileTransaction/stash-dir/ProductDto.class.uniqueId2 differ diff --git a/build/tmp/compileJava/compileTransaction/stash-dir/ProductEntity.class.uniqueId6 b/build/tmp/compileJava/compileTransaction/stash-dir/ProductEntity.class.uniqueId6 new file mode 100644 index 0000000..c0e5faa Binary files /dev/null and b/build/tmp/compileJava/compileTransaction/stash-dir/ProductEntity.class.uniqueId6 differ diff --git a/build/tmp/compileJava/compileTransaction/stash-dir/ProductRepository.class.uniqueId1 b/build/tmp/compileJava/compileTransaction/stash-dir/ProductRepository.class.uniqueId1 new file mode 100644 index 0000000..7f8127d Binary files /dev/null and b/build/tmp/compileJava/compileTransaction/stash-dir/ProductRepository.class.uniqueId1 differ diff --git a/build/tmp/compileJava/compileTransaction/stash-dir/ProductService.class.uniqueId4 b/build/tmp/compileJava/compileTransaction/stash-dir/ProductService.class.uniqueId4 new file mode 100644 index 0000000..cf1c50c Binary files /dev/null and b/build/tmp/compileJava/compileTransaction/stash-dir/ProductService.class.uniqueId4 differ diff --git a/build/tmp/compileJava/compileTransaction/stash-dir/UserController.class.uniqueId0 b/build/tmp/compileJava/compileTransaction/stash-dir/UserController.class.uniqueId0 deleted file mode 100644 index 1f91c39..0000000 Binary files a/build/tmp/compileJava/compileTransaction/stash-dir/UserController.class.uniqueId0 and /dev/null differ diff --git a/build/tmp/compileJava/compileTransaction/stash-dir/UserService.class.uniqueId1 b/build/tmp/compileJava/compileTransaction/stash-dir/UserService.class.uniqueId1 deleted file mode 100644 index 9471d97..0000000 Binary files a/build/tmp/compileJava/compileTransaction/stash-dir/UserService.class.uniqueId1 and /dev/null differ diff --git a/build/tmp/compileJava/previous-compilation-data.bin b/build/tmp/compileJava/previous-compilation-data.bin index 93eee6a..7f22257 100644 Binary files a/build/tmp/compileJava/previous-compilation-data.bin and b/build/tmp/compileJava/previous-compilation-data.bin differ diff --git a/build/tmp/compileTestJava/compileTransaction/stash-dir/UserServiceTests.class.uniqueId0 b/build/tmp/compileTestJava/compileTransaction/stash-dir/UserServiceTests.class.uniqueId0 deleted file mode 100644 index 026053e..0000000 Binary files a/build/tmp/compileTestJava/compileTransaction/stash-dir/UserServiceTests.class.uniqueId0 and /dev/null differ diff --git a/build/tmp/compileTestJava/previous-compilation-data.bin b/build/tmp/compileTestJava/previous-compilation-data.bin index f34352f..a5646ec 100644 Binary files a/build/tmp/compileTestJava/previous-compilation-data.bin and b/build/tmp/compileTestJava/previous-compilation-data.bin differ diff --git a/src/main/java/com/example/demo/DemoApplication.java b/src/main/java/com/example/demo/DemoApplication.java index b365b54..c4afea8 100644 --- a/src/main/java/com/example/demo/DemoApplication.java +++ b/src/main/java/com/example/demo/DemoApplication.java @@ -8,21 +8,30 @@ import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import com.example.demo.items.model.ItemEntity; -import com.example.demo.items.service.ItemService; +import com.example.demo.orders.model.OrderEntity; +import com.example.demo.orders.service.OrderService; +import com.example.demo.products.model.ProductEntity; +import com.example.demo.products.service.ProductService; import com.example.demo.types.model.TypeEntity; import com.example.demo.types.service.TypeService; +import com.example.demo.users.model.UserEntity; +import com.example.demo.users.service.UserService; @SpringBootApplication public class DemoApplication implements CommandLineRunner { private final Logger log = LoggerFactory.getLogger(DemoApplication.class); private final TypeService typeService; - private final ItemService itemService; + private final ProductService productService; + private final UserService userService; + private final OrderService orderService; - public DemoApplication(TypeService typeService, ItemService itemService) { + public DemoApplication(TypeService typeService, ProductService productService, UserService userService, + OrderService orderService) { this.typeService = typeService; - this.itemService = itemService; + this.productService = productService; + this.userService = userService; + this.orderService = orderService; } public static void main(String[] args) { @@ -33,18 +42,27 @@ public class DemoApplication implements CommandLineRunner { public void run(String... args) throws Exception { if (args.length > 0 && Objects.equals("--populate", args[0])) { log.info("Create default types values"); - final var type1 = typeService.create(new TypeEntity(null, "Ноутбук")); - final var type2 = typeService.create(new TypeEntity(null, "Телефон")); - final var type3 = typeService.create(new TypeEntity(null, "Игровая приставка")); + final var type1 = typeService.create(new TypeEntity(null, "Пицца")); + final var type2 = typeService.create(new TypeEntity(null, "Напиток")); + final var type3 = typeService.create(new TypeEntity(null, "Закуска")); - log.info("Create default items values"); - itemService.create(new ItemEntity(null, type1, 49999.00, 20)); - itemService.create(new ItemEntity(null, type1, 129999.00, 3)); - itemService.create(new ItemEntity(null, type2, 15450.50, 30)); - itemService.create(new ItemEntity(null, type2, 69900.50, 10)); - itemService.create(new ItemEntity(null, type2, 150000.00, 6)); - itemService.create(new ItemEntity(null, type3, 75000.00, 6)); - itemService.create(new ItemEntity(null, type3, 67800.00, 3)); + log.info("Create default products values"); + productService.create(new ProductEntity(null, "Маргарита", type1, 499.00)); + productService.create(new ProductEntity(null, "Эль Дьябло", type1, 699.00)); + productService.create(new ProductEntity(null, "Гавайская", type1, 399.00)); + productService.create(new ProductEntity(null, "Лимонад", type2, 99.00)); + productService.create(new ProductEntity(null, "Сок", type2, 99.00)); + productService.create(new ProductEntity(null, "Чай", type2, 49.00)); + productService.create(new ProductEntity(null, "Картошка фри", type3, 199.00)); + productService.create(new ProductEntity(null, "Нагетсы", type3, 199.00)); + + log.info("Create default users values"); + userService.create(new UserEntity(null, "Alex", "Kryukov", "akryu@mail.ru", "password123")); + userService.create(new UserEntity(null, "Oleg", "Zyngin", "@mail.ru", "password")); + + log.info("Create default orders values"); + final var user1 = userService.create(new UserEntity(null, "Alex", "Kryukov", "akryu@mail.ru", "password")); + orderService.create(new OrderEntity(null, user1, "31-03-2024")); } } } diff --git a/src/main/java/com/example/demo/items/api/ItemController.java b/src/main/java/com/example/demo/items/api/ItemController.java deleted file mode 100644 index 8de1407..0000000 --- a/src/main/java/com/example/demo/items/api/ItemController.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.example.demo.items.api; - -import java.util.List; - -import org.modelmapper.ModelMapper; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; -import com.example.demo.core.configuration.Constants; -import com.example.demo.items.model.ItemEntity; -import com.example.demo.items.service.ItemService; -import com.example.demo.types.service.TypeService; - -import jakarta.validation.Valid; - -@RestController -@RequestMapping(Constants.API_URL + "/item") -public class ItemController { - private final ItemService itemService; - private final TypeService typeService; - private final ModelMapper modelMapper; - - public ItemController(ItemService itemService, TypeService typeService, ModelMapper modelMapper) { - this.itemService = itemService; - this.typeService = typeService; - this.modelMapper = modelMapper; - } - - private ItemDto toDto(ItemEntity entity) { - return modelMapper.map(entity, ItemDto.class); - } - - private ItemEntity toEntity(ItemDto dto) { - final ItemEntity entity = modelMapper.map(dto, ItemEntity.class); - entity.setType(typeService.get(dto.getTypeId())); - return entity; - } - - @GetMapping - public List getAll(@RequestParam(name = "typeId", defaultValue = "0") Long typeId) { - return itemService.getAll(typeId).stream().map(this::toDto).toList(); - } - - @GetMapping("/{id}") - public ItemDto get(@PathVariable(name = "id") Long id) { - return toDto(itemService.get(id)); - } - - @PostMapping - public ItemDto create(@RequestBody @Valid ItemDto dto) { - return toDto(itemService.create(toEntity(dto))); - } - - @PutMapping("/{id}") - public ItemDto update(@PathVariable(name = "id") Long id, @RequestBody ItemDto dto) { - return toDto(itemService.update(id, toEntity(dto))); - } - - @DeleteMapping("/{id}") - public ItemDto delete(@PathVariable(name = "id") Long id) { - return toDto(itemService.delete(id)); - } -} diff --git a/src/main/java/com/example/demo/items/repository/ItemRepository.java b/src/main/java/com/example/demo/items/repository/ItemRepository.java deleted file mode 100644 index 6488c26..0000000 --- a/src/main/java/com/example/demo/items/repository/ItemRepository.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.example.demo.items.repository; - -import org.springframework.stereotype.Repository; - -import com.example.demo.core.repository.MapRepository; -import com.example.demo.items.model.ItemEntity; - -@Repository -public class ItemRepository extends MapRepository { -} diff --git a/src/main/java/com/example/demo/order_lines/api/OrderLineController.java b/src/main/java/com/example/demo/order_lines/api/OrderLineController.java new file mode 100644 index 0000000..49c7c2f --- /dev/null +++ b/src/main/java/com/example/demo/order_lines/api/OrderLineController.java @@ -0,0 +1,85 @@ +package com.example.demo.order_lines.api; + +import java.util.List; + +import org.modelmapper.ModelMapper; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import com.example.demo.core.configuration.Constants; +import com.example.demo.order_lines.model.OrderLineEntity; +import com.example.demo.order_lines.service.OrderLineService; +import com.example.demo.orders.model.OrderEntity; +import com.example.demo.orders.service.OrderService; +import com.example.demo.products.model.ProductEntity; +import com.example.demo.products.service.ProductService; + +import jakarta.validation.Valid; + +@RestController +@RequestMapping(Constants.API_URL + "/orderLine") +public class OrderLineController { + private final OrderService orderService; + private final OrderLineService orderLineService; + private final ProductService productService; + private final ModelMapper modelMapper; + + public OrderLineController(OrderLineService orderLineService, OrderService orderService, + ProductService productService, ModelMapper modelMapper) { + this.orderService = orderService; + this.orderLineService = orderLineService; + this.productService = productService; + this.modelMapper = modelMapper; + } + + private OrderLineDto toDto(OrderLineEntity orderLineEntity) { + return modelMapper.map(orderLineEntity, OrderLineDto.class); + } + + private OrderLineEntity toEntity(OrderLineDto dto) { + // Получаем заказ и товар из соответствующих сервисов по их идентификаторам из + // OrderLineDto + OrderEntity order = orderService.get(dto.getOrderId()); + ProductEntity product = productService.get(dto.getProductId()); + + // Создаем новый экземпляр OrderLineEntity с помощью модельного маппера + OrderLineEntity entity = modelMapper.map(dto, OrderLineEntity.class); + + // Устанавливаем полученные заказ и товар в OrderLineEntity + entity.setOrder(order); + entity.setProduct(product); + + return entity; + } + + @GetMapping + public List getAll(@RequestParam(name = "orderId", defaultValue = "0") Long orderId) { + return orderLineService.getAll(orderId).stream().map(this::toDto).toList(); + } + + @GetMapping("/{id}") + public OrderLineDto get(@PathVariable(name = "id") Long id) { + return toDto(orderLineService.get(id)); + } + + @PostMapping + public OrderLineDto create(@RequestBody @Valid OrderLineDto dto) { + return toDto(orderLineService.create(toEntity(dto))); + } + + @PutMapping("/{id}") + public OrderLineDto update(@PathVariable(name = "id") Long id, @RequestBody OrderLineDto dto) { + return toDto(orderLineService.update(id, toEntity(dto))); + } + + @DeleteMapping("/{id}") + public OrderLineDto delete(@PathVariable(name = "id") Long id) { + return toDto(orderLineService.delete(id)); + } +} diff --git a/src/main/java/com/example/demo/order_lines/api/OrderLineDto.java b/src/main/java/com/example/demo/order_lines/api/OrderLineDto.java new file mode 100644 index 0000000..d686e57 --- /dev/null +++ b/src/main/java/com/example/demo/order_lines/api/OrderLineDto.java @@ -0,0 +1,52 @@ +package com.example.demo.order_lines.api; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; + +public class OrderLineDto { + private Long id; + @NotNull + @Min(1) + private Long orderId; + @NotNull + @Min(1) + private Long productId; + @NotBlank + private Integer count; + + @JsonProperty(access = JsonProperty.Access.READ_ONLY) + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Long getOrderId() { + return orderId; + } + + public void setOrderId(Long orderId) { + this.orderId = orderId; + } + + public Integer getCount() { + return count; + } + + public void setCount(Integer count) { + this.count = count; + } + + public Long getProductId() { + return productId; + } + + public void setProductId(Long productId) { + this.productId = productId; + } +} diff --git a/src/main/java/com/example/demo/order_lines/model/OrderLineEntity.java b/src/main/java/com/example/demo/order_lines/model/OrderLineEntity.java new file mode 100644 index 0000000..de79b9c --- /dev/null +++ b/src/main/java/com/example/demo/order_lines/model/OrderLineEntity.java @@ -0,0 +1,68 @@ +package com.example.demo.order_lines.model; + +import java.util.Objects; + +import com.example.demo.core.model.BaseEntity; +import com.example.demo.orders.model.OrderEntity; +import com.example.demo.products.model.ProductEntity; + +public class OrderLineEntity extends BaseEntity { + + private OrderEntity order; + private Integer count; + private ProductEntity product; + + public OrderLineEntity() { + + } + + public OrderLineEntity(Long id, OrderEntity order, ProductEntity product, Integer count) { + super(id); + this.order = order; + this.count = count; + this.product = product; + } + + public ProductEntity getProduct() { + return product; + } + + public void setProduct(ProductEntity product) { + this.product = product; + } + + public OrderEntity getOrder() { + return order; + } + + public void setOrder(OrderEntity order) { + this.order = order; + } + + public Integer getCount() { + return count; + } + + public void setCount(Integer count) { + this.count = count; + } + + @Override + public int hashCode() { + return Objects.hash(id, order, product, count); + } + + @SuppressWarnings("unlikely-arg-order-product") + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null || getClass() != obj.getClass()) + return false; + final OrderLineEntity other = (OrderLineEntity) obj; + return Objects.equals(other.getId(), id) + && Objects.equals(other.getOrder(), order) + && Objects.equals(other.getProduct(), product) + && Objects.equals(other.getCount(), count); + } +} diff --git a/src/main/java/com/example/demo/order_lines/repository/OrderLineRepository.java b/src/main/java/com/example/demo/order_lines/repository/OrderLineRepository.java new file mode 100644 index 0000000..3b0bb25 --- /dev/null +++ b/src/main/java/com/example/demo/order_lines/repository/OrderLineRepository.java @@ -0,0 +1,10 @@ +package com.example.demo.order_lines.repository; + +import org.springframework.stereotype.Repository; + +import com.example.demo.core.repository.MapRepository; +import com.example.demo.order_lines.model.OrderLineEntity; + +@Repository +public class OrderLineRepository extends MapRepository { +} diff --git a/src/main/java/com/example/demo/order_lines/service/OrderLineService.java b/src/main/java/com/example/demo/order_lines/service/OrderLineService.java new file mode 100644 index 0000000..2b701a0 --- /dev/null +++ b/src/main/java/com/example/demo/order_lines/service/OrderLineService.java @@ -0,0 +1,51 @@ +package com.example.demo.order_lines.service; + +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +import org.springframework.stereotype.Service; + +import com.example.demo.core.error.NotFoundException; +import com.example.demo.order_lines.model.OrderLineEntity; +import com.example.demo.order_lines.repository.OrderLineRepository; + +@Service +public class OrderLineService { + private final OrderLineRepository repository; + + public OrderLineService(OrderLineRepository repository) { + this.repository = repository; + } + + public List getAll(Long orderLineId) { + if (Objects.equals(orderLineId, 0L)) { + return repository.getAll(); + } + return repository.getAll().stream() + .filter(orderLine -> orderLine.getOrder().getId().equals(orderLineId)) + .toList(); + } + + public OrderLineEntity get(Long id) { + return Optional.ofNullable(repository.get(id)) + .orElseThrow(() -> new NotFoundException(id)); + } + + public OrderLineEntity create(OrderLineEntity entity) { + return repository.create(entity); + } + + public OrderLineEntity update(Long id, OrderLineEntity entity) { + final OrderLineEntity existsEntity = get(id); + existsEntity.setOrder(entity.getOrder()); + existsEntity.setProduct(entity.getProduct()); + existsEntity.setCount(entity.getCount()); + return repository.update(existsEntity); + } + + public OrderLineEntity delete(Long id) { + final OrderLineEntity existsEntity = get(id); + return repository.delete(existsEntity); + } +} diff --git a/src/main/java/com/example/demo/orders/api/OrderController.java b/src/main/java/com/example/demo/orders/api/OrderController.java new file mode 100644 index 0000000..9ef168d --- /dev/null +++ b/src/main/java/com/example/demo/orders/api/OrderController.java @@ -0,0 +1,69 @@ +package com.example.demo.orders.api; + +import java.util.List; + +import org.modelmapper.ModelMapper; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import com.example.demo.core.configuration.Constants; +import com.example.demo.orders.model.OrderEntity; +import com.example.demo.orders.service.OrderService; +import com.example.demo.users.service.UserService; + +import jakarta.validation.Valid; + +@RestController +@RequestMapping(Constants.API_URL + "/order") +public class OrderController { + private final OrderService orderService; + private final UserService userService; + private final ModelMapper modelMapper; + + public OrderController(OrderService orderService, UserService userService, ModelMapper modelMapper) { + this.orderService = orderService; + this.userService = userService; + this.modelMapper = modelMapper; + } + + private OrderDto toDto(OrderEntity entity) { + return modelMapper.map(entity, OrderDto.class); + } + + private OrderEntity toEntity(OrderDto dto) { + final OrderEntity entity = modelMapper.map(dto, OrderEntity.class); + entity.setUser(userService.get(dto.getUserId())); + return entity; + } + + @GetMapping + public List getAll(@RequestParam(name = "userId", defaultValue = "0") Long userId) { + return orderService.getAll(userId).stream().map(this::toDto).toList(); + } + + @GetMapping("/{id}") + public OrderDto get(@PathVariable(name = "id") Long id) { + return toDto(orderService.get(id)); + } + + @PostMapping + public OrderDto create(@RequestBody @Valid OrderDto dto) { + return toDto(orderService.create(toEntity(dto))); + } + + @PutMapping("/{id}") + public OrderDto update(@PathVariable(name = "id") Long id, @RequestBody OrderDto dto) { + return toDto(orderService.update(id, toEntity(dto))); + } + + @DeleteMapping("/{id}") + public OrderDto delete(@PathVariable(name = "id") Long id) { + return toDto(orderService.delete(id)); + } +} diff --git a/src/main/java/com/example/demo/orders/api/OrderDto.java b/src/main/java/com/example/demo/orders/api/OrderDto.java new file mode 100644 index 0000000..09293eb --- /dev/null +++ b/src/main/java/com/example/demo/orders/api/OrderDto.java @@ -0,0 +1,41 @@ +package com.example.demo.orders.api; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; + +public class OrderDto { + private Long id; + @NotNull + @Min(1) + private Long userId; + @NotBlank + private String date; + + @JsonProperty(access = JsonProperty.Access.READ_ONLY) + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Long getUserId() { + return userId; + } + + public void setUserId(Long userId) { + this.userId = userId; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } +} diff --git a/src/main/java/com/example/demo/orders/model/OrderEntity.java b/src/main/java/com/example/demo/orders/model/OrderEntity.java new file mode 100644 index 0000000..e3406f1 --- /dev/null +++ b/src/main/java/com/example/demo/orders/model/OrderEntity.java @@ -0,0 +1,55 @@ +package com.example.demo.orders.model; + +import java.util.Objects; + +import com.example.demo.core.model.BaseEntity; +import com.example.demo.users.model.UserEntity; + +public class OrderEntity extends BaseEntity { + private UserEntity user; + private String date; + + public OrderEntity() { + // Пустой конструктор + } + + public OrderEntity(Long id, UserEntity user, String date) { + super(id); + this.user = user; + this.date = date; + } + + public UserEntity getUser() { + return user; + } + + public void setUser(UserEntity user) { + this.user = user; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + @Override + public int hashCode() { + return Objects.hash(id, user, date); + } + + @SuppressWarnings("unlikely-arg-user") + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null || getClass() != obj.getClass()) + return false; + final OrderEntity other = (OrderEntity) obj; + return Objects.equals(other.getId(), id) + && Objects.equals(other.getUser(), user) + && Objects.equals(other.getDate(), date); + } +} diff --git a/src/main/java/com/example/demo/orders/repository/OrderRepository.java b/src/main/java/com/example/demo/orders/repository/OrderRepository.java new file mode 100644 index 0000000..ae96b81 --- /dev/null +++ b/src/main/java/com/example/demo/orders/repository/OrderRepository.java @@ -0,0 +1,10 @@ +package com.example.demo.orders.repository; + +import org.springframework.stereotype.Repository; + +import com.example.demo.core.repository.MapRepository; +import com.example.demo.orders.model.OrderEntity; + +@Repository +public class OrderRepository extends MapRepository { +} diff --git a/src/main/java/com/example/demo/orders/service/OrderService.java b/src/main/java/com/example/demo/orders/service/OrderService.java new file mode 100644 index 0000000..f325cf8 --- /dev/null +++ b/src/main/java/com/example/demo/orders/service/OrderService.java @@ -0,0 +1,50 @@ +package com.example.demo.orders.service; + +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +import org.springframework.stereotype.Service; + +import com.example.demo.core.error.NotFoundException; +import com.example.demo.orders.model.OrderEntity; +import com.example.demo.orders.repository.OrderRepository; + +@Service +public class OrderService { + private final OrderRepository repository; + + public OrderService(OrderRepository repository) { + this.repository = repository; + } + + public List getAll(Long userId) { + if (Objects.equals(userId, 0L)) { + return repository.getAll(); + } + return repository.getAll().stream() + .filter(order -> order.getUser().getId().equals(userId)) + .toList(); + } + + public OrderEntity get(Long id) { + return Optional.ofNullable(repository.get(id)) + .orElseThrow(() -> new NotFoundException(id)); + } + + public OrderEntity create(OrderEntity entity) { + return repository.create(entity); + } + + public OrderEntity update(Long id, OrderEntity entity) { + final OrderEntity existsEntity = get(id); + existsEntity.setUser(entity.getUser()); + existsEntity.setDate(entity.getDate()); + return repository.update(existsEntity); + } + + public OrderEntity delete(Long id) { + final OrderEntity existsEntity = get(id); + return repository.delete(existsEntity); + } +} diff --git a/src/main/java/com/example/demo/products/api/ProductController.java b/src/main/java/com/example/demo/products/api/ProductController.java new file mode 100644 index 0000000..409eca7 --- /dev/null +++ b/src/main/java/com/example/demo/products/api/ProductController.java @@ -0,0 +1,69 @@ +package com.example.demo.products.api; + +import java.util.List; + +import org.modelmapper.ModelMapper; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import com.example.demo.core.configuration.Constants; +import com.example.demo.products.model.ProductEntity; +import com.example.demo.products.service.ProductService; +import com.example.demo.types.service.TypeService; + +import jakarta.validation.Valid; + +@RestController +@RequestMapping(Constants.API_URL + "/product") +public class ProductController { + private final ProductService productService; + private final TypeService typeService; + private final ModelMapper modelMapper; + + public ProductController(ProductService productService, TypeService typeService, ModelMapper modelMapper) { + this.productService = productService; + this.typeService = typeService; + this.modelMapper = modelMapper; + } + + private ProductDto toDto(ProductEntity entity) { + return modelMapper.map(entity, ProductDto.class); + } + + private ProductEntity toEntity(ProductDto dto) { + final ProductEntity entity = modelMapper.map(dto, ProductEntity.class); + entity.setType(typeService.get(dto.getTypeId())); + return entity; + } + + @GetMapping + public List getAll(@RequestParam(name = "typeId", defaultValue = "0") Long typeId) { + return productService.getAll(typeId).stream().map(this::toDto).toList(); + } + + @GetMapping("/{id}") + public ProductDto get(@PathVariable(name = "id") Long id) { + return toDto(productService.get(id)); + } + + @PostMapping + public ProductDto create(@RequestBody @Valid ProductDto dto) { + return toDto(productService.create(toEntity(dto))); + } + + @PutMapping("/{id}") + public ProductDto update(@PathVariable(name = "id") Long id, @RequestBody ProductDto dto) { + return toDto(productService.update(id, toEntity(dto))); + } + + @DeleteMapping("/{id}") + public ProductDto delete(@PathVariable(name = "id") Long id) { + return toDto(productService.delete(id)); + } +} diff --git a/src/main/java/com/example/demo/items/api/ItemDto.java b/src/main/java/com/example/demo/products/api/ProductDto.java similarity index 73% rename from src/main/java/com/example/demo/items/api/ItemDto.java rename to src/main/java/com/example/demo/products/api/ProductDto.java index 023ffc4..368d3eb 100644 --- a/src/main/java/com/example/demo/items/api/ItemDto.java +++ b/src/main/java/com/example/demo/products/api/ProductDto.java @@ -1,21 +1,21 @@ -package com.example.demo.items.api; +package com.example.demo.products.api; import com.fasterxml.jackson.annotation.JsonProperty; import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; -public class ItemDto { +public class ProductDto { private Long id; @NotNull @Min(1) private Long typeId; + @NotBlank + private String name; @NotNull @Min(1) private Double price; - @NotNull - @Min(1) - private Integer count; @JsonProperty(access = JsonProperty.Access.READ_ONLY) public Long getId() { @@ -34,6 +34,14 @@ public class ItemDto { this.typeId = typeId; } + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + public Double getPrice() { return price; } @@ -42,16 +50,8 @@ public class ItemDto { this.price = price; } - public Integer getCount() { - return count; - } - - public void setCount(Integer count) { - this.count = count; - } - @JsonProperty(access = JsonProperty.Access.READ_ONLY) public Double getSum() { - return price * count; + return price; } } diff --git a/src/main/java/com/example/demo/items/model/ItemEntity.java b/src/main/java/com/example/demo/products/model/ProductEntity.java similarity index 59% rename from src/main/java/com/example/demo/items/model/ItemEntity.java rename to src/main/java/com/example/demo/products/model/ProductEntity.java index 55951d9..3b43ea8 100644 --- a/src/main/java/com/example/demo/items/model/ItemEntity.java +++ b/src/main/java/com/example/demo/products/model/ProductEntity.java @@ -1,24 +1,25 @@ -package com.example.demo.items.model; +package com.example.demo.products.model; import java.util.Objects; import com.example.demo.core.model.BaseEntity; import com.example.demo.types.model.TypeEntity; -public class ItemEntity extends BaseEntity { +public class ProductEntity extends BaseEntity { private TypeEntity type; + private String name; private Double price; - private Integer count; - public ItemEntity() { + public ProductEntity() { super(); } - public ItemEntity(Long id, TypeEntity type, Double price, Integer count) { + public ProductEntity(Long id, String name, TypeEntity type, Double price) { super(id); this.type = type; + this.name = name; this.price = price; - this.count = count; + } public TypeEntity getType() { @@ -29,6 +30,14 @@ public class ItemEntity extends BaseEntity { this.type = type; } + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + public Double getPrice() { return price; } @@ -37,29 +46,22 @@ public class ItemEntity extends BaseEntity { this.price = price; } - public Integer getCount() { - return count; - } - - public void setCount(Integer count) { - this.count = count; - } - @Override public int hashCode() { - return Objects.hash(id, type, price, count); + return Objects.hash(id, type, name, price); } + @SuppressWarnings("unlikely-arg-type") @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; - final ItemEntity other = (ItemEntity) obj; + final ProductEntity other = (ProductEntity) obj; return Objects.equals(other.getId(), id) && Objects.equals(other.getType(), type) - && Objects.equals(other.getPrice(), price) - && Objects.equals(other.getCount(), count); + && Objects.equals(other.getType(), name) + && Objects.equals(other.getPrice(), price); } } diff --git a/src/main/java/com/example/demo/products/repository/ProductRepository.java b/src/main/java/com/example/demo/products/repository/ProductRepository.java new file mode 100644 index 0000000..2dbfa07 --- /dev/null +++ b/src/main/java/com/example/demo/products/repository/ProductRepository.java @@ -0,0 +1,10 @@ +package com.example.demo.products.repository; + +import org.springframework.stereotype.Repository; + +import com.example.demo.core.repository.MapRepository; +import com.example.demo.products.model.ProductEntity; + +@Repository +public class ProductRepository extends MapRepository { +} diff --git a/src/main/java/com/example/demo/items/service/ItemService.java b/src/main/java/com/example/demo/products/service/ProductService.java similarity index 50% rename from src/main/java/com/example/demo/items/service/ItemService.java rename to src/main/java/com/example/demo/products/service/ProductService.java index 7678a2c..31c83c3 100644 --- a/src/main/java/com/example/demo/items/service/ItemService.java +++ b/src/main/java/com/example/demo/products/service/ProductService.java @@ -1,4 +1,4 @@ -package com.example.demo.items.service; +package com.example.demo.products.service; import java.util.List; import java.util.Objects; @@ -7,45 +7,45 @@ import java.util.Optional; import org.springframework.stereotype.Service; import com.example.demo.core.error.NotFoundException; -import com.example.demo.items.model.ItemEntity; -import com.example.demo.items.repository.ItemRepository; +import com.example.demo.products.model.ProductEntity; +import com.example.demo.products.repository.ProductRepository; @Service -public class ItemService { - private final ItemRepository repository; +public class ProductService { + private final ProductRepository repository; - public ItemService(ItemRepository repository) { + public ProductService(ProductRepository repository) { this.repository = repository; } - public List getAll(Long typeId) { + public List getAll(Long typeId) { if (Objects.equals(typeId, 0L)) { return repository.getAll(); } return repository.getAll().stream() - .filter(item -> item.getType().getId().equals(typeId)) + .filter(order -> order.getType().getId().equals(typeId)) .toList(); } - public ItemEntity get(Long id) { + public ProductEntity get(Long id) { return Optional.ofNullable(repository.get(id)) .orElseThrow(() -> new NotFoundException(id)); } - public ItemEntity create(ItemEntity entity) { + public ProductEntity create(ProductEntity entity) { return repository.create(entity); } - public ItemEntity update(Long id, ItemEntity entity) { - final ItemEntity existsEntity = get(id); + public ProductEntity update(Long id, ProductEntity entity) { + final ProductEntity existsEntity = get(id); + existsEntity.setName(entity.getName()); existsEntity.setType(entity.getType()); existsEntity.setPrice(entity.getPrice()); - existsEntity.setCount(entity.getCount()); return repository.update(existsEntity); } - public ItemEntity delete(Long id) { - final ItemEntity existsEntity = get(id); + public ProductEntity delete(Long id) { + final ProductEntity existsEntity = get(id); return repository.delete(existsEntity); } } diff --git a/src/main/java/com/example/demo/speaker/api/SpeakerController.java b/src/main/java/com/example/demo/speaker/api/SpeakerController.java deleted file mode 100644 index 293296d..0000000 --- a/src/main/java/com/example/demo/speaker/api/SpeakerController.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.example.demo.speaker.api; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -import com.example.demo.speaker.service.SpeakerService; - -@RestController -public class SpeakerController { - private final SpeakerService speakerService; - - public SpeakerController(SpeakerService speakerService) { - this.speakerService = speakerService; - } - - @GetMapping - public String hello( - @RequestParam(value = "name", defaultValue = "Мир") String name, - @RequestParam(value = "lang", defaultValue = "ru") String lang) { - return speakerService.say(name, lang); - } -} diff --git a/src/main/java/com/example/demo/speaker/configuration/SpeakerConfiguration.java b/src/main/java/com/example/demo/speaker/configuration/SpeakerConfiguration.java deleted file mode 100644 index 0ef7cda..0000000 --- a/src/main/java/com/example/demo/speaker/configuration/SpeakerConfiguration.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.example.demo.speaker.configuration; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import com.example.demo.speaker.domain.Speaker; -import com.example.demo.speaker.domain.SpeakerEng; -import com.example.demo.speaker.domain.SpeakerRus; - -@Configuration -public class SpeakerConfiguration { - private final Logger log = LoggerFactory.getLogger(SpeakerConfiguration.class); - - @Bean(value = "ru", initMethod = "init", destroyMethod = "destroy") - public SpeakerRus createRusSpeaker() { - log.info("Call createRusSpeaker()"); - return new SpeakerRus(); - } - - @Bean(value = "en") - public Speaker createEngSpeaker() { - log.info("Call createEngSpeaker()"); - return new SpeakerEng(); - } -} diff --git a/src/main/java/com/example/demo/speaker/domain/Speaker.java b/src/main/java/com/example/demo/speaker/domain/Speaker.java deleted file mode 100644 index 27a0a29..0000000 --- a/src/main/java/com/example/demo/speaker/domain/Speaker.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.example.demo.speaker.domain; - -public interface Speaker { - String say(); -} diff --git a/src/main/java/com/example/demo/speaker/domain/SpeakerDeu.java b/src/main/java/com/example/demo/speaker/domain/SpeakerDeu.java deleted file mode 100644 index 5909864..0000000 --- a/src/main/java/com/example/demo/speaker/domain/SpeakerDeu.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.example.demo.speaker.domain; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - -import jakarta.annotation.PostConstruct; -import jakarta.annotation.PreDestroy; - -@Component(value = "de") -public class SpeakerDeu implements Speaker { - private final Logger log = LoggerFactory.getLogger(SpeakerDeu.class); - - @Override - public String say() { - return "Hallo"; - } - - @PostConstruct - public void init() { - log.info("SpeakerDeu.init()"); - } - - @PreDestroy - public void destroy() { - log.info("SpeakerDeu.destroy()"); - } -} diff --git a/src/main/java/com/example/demo/speaker/domain/SpeakerEng.java b/src/main/java/com/example/demo/speaker/domain/SpeakerEng.java deleted file mode 100644 index a310403..0000000 --- a/src/main/java/com/example/demo/speaker/domain/SpeakerEng.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.example.demo.speaker.domain; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.DisposableBean; -import org.springframework.beans.factory.InitializingBean; - -public class SpeakerEng implements Speaker, InitializingBean, DisposableBean { - private final Logger log = LoggerFactory.getLogger(SpeakerEng.class); - - @Override - public String say() { - return "Hello"; - } - - @Override - public void afterPropertiesSet() { - log.info("SpeakerEng.afterPropertiesSet()"); - } - - @Override - public void destroy() { - log.info("SpeakerEng.destroy()"); - - } -} diff --git a/src/main/java/com/example/demo/speaker/domain/SpeakerRus.java b/src/main/java/com/example/demo/speaker/domain/SpeakerRus.java deleted file mode 100644 index d6ed18e..0000000 --- a/src/main/java/com/example/demo/speaker/domain/SpeakerRus.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.example.demo.speaker.domain; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class SpeakerRus implements Speaker { - private final Logger log = LoggerFactory.getLogger(SpeakerRus.class); - - @Override - public String say() { - return "Привет"; - } - - public void init() { - log.info("SpeakerRus.init()"); - } - - public void destroy() { - log.info("SpeakerRus.destroy()"); - } -} diff --git a/src/main/java/com/example/demo/speaker/service/SpeakerService.java b/src/main/java/com/example/demo/speaker/service/SpeakerService.java deleted file mode 100644 index db81ba4..0000000 --- a/src/main/java/com/example/demo/speaker/service/SpeakerService.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.example.demo.speaker.service; - -import org.springframework.context.ApplicationContext; -import org.springframework.stereotype.Service; - -import com.example.demo.speaker.domain.Speaker; - -@Service -public class SpeakerService { - private final ApplicationContext applicationContext; - - public SpeakerService(ApplicationContext applicationContext) { - this.applicationContext = applicationContext; - } - - public String say(String name, String lang) { - final Speaker speaker = (Speaker) applicationContext.getBean(lang); - return String.format("%s, %s!", speaker.say(), name); - } -} diff --git a/src/main/java/com/example/demo/users/api/UserDto.java b/src/main/java/com/example/demo/users/api/UserDto.java index 31fabd8..d1e5233 100644 --- a/src/main/java/com/example/demo/users/api/UserDto.java +++ b/src/main/java/com/example/demo/users/api/UserDto.java @@ -2,6 +2,7 @@ package com.example.demo.users.api; import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.validation.constraints.Email; import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; @@ -14,6 +15,7 @@ public class UserDto { @NotBlank private String surname; @NotBlank + @Email private String email; @NotBlank private String password; diff --git a/src/test/java/com/example/demo/OrderLineServiceTests.java b/src/test/java/com/example/demo/OrderLineServiceTests.java new file mode 100644 index 0000000..282e229 --- /dev/null +++ b/src/test/java/com/example/demo/OrderLineServiceTests.java @@ -0,0 +1,69 @@ +package com.example.demo; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import com.example.demo.core.error.NotFoundException; +import com.example.demo.order_lines.model.OrderLineEntity; +import com.example.demo.order_lines.service.OrderLineService; + +@SpringBootTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class OrderLineServiceTests { + @Autowired + private OrderLineService orderLineService; + + @Test + void getTest() { + Assertions.assertThrows(NotFoundException.class, () -> orderLineService.get(0L)); + } + + @Test + @Order(1) + void createTest() { + // Создаем тестовую сущность OrderLineEntity + OrderLineEntity testOrderLine = new OrderLineEntity(null, null, null, 5); + // Вызываем метод create() и сохраняем созданную сущность + OrderLineEntity createdOrderLine = orderLineService.create(testOrderLine); + // Проверяем, что метод create() вернул не null + Assertions.assertNotNull(createdOrderLine); + // Проверяем, что созданная сущность имеет назначенный ID + Assertions.assertNotNull(createdOrderLine.getId()); + // Проверяем, что созданная сущность соответствует той, которую мы передали + Assertions.assertEquals(testOrderLine.getCount(), createdOrderLine.getCount()); + } + + @Test + @Order(2) + void updateTest() { + // Получаем сущность OrderLineEntity для обновления + OrderLineEntity existingOrderLine = orderLineService.get(1L); + // Создаем новое количество для обновления + int newCount = 10; + // Устанавливаем новое количество в сущность OrderLineEntity + existingOrderLine.setCount(newCount); + // Вызываем метод update() и сохраняем обновленную сущность + OrderLineEntity updatedOrderLine = orderLineService.update(1L, existingOrderLine); + // Проверяем, что метод update() вернул не null + Assertions.assertNotNull(updatedOrderLine); + // Проверяем, что обновленная сущность имеет то же количество, что и заданное + Assertions.assertEquals(newCount, updatedOrderLine.getCount()); + } + + @Test + @Order(3) + void deleteTest() { + // Удаляем сущность OrderLineEntity по ее ID + OrderLineEntity deletedOrderLine = orderLineService.delete(1L); + // Проверяем, что метод delete() вернул не null + Assertions.assertNotNull(deletedOrderLine); + // Проверяем, что удаленная сущность имеет тот же ID, что и удаленная + Assertions.assertEquals(1L, deletedOrderLine.getId()); + // Проверяем, что сущность больше не существует в репозитории + Assertions.assertThrows(NotFoundException.class, () -> orderLineService.get(1L)); + } +} diff --git a/src/test/java/com/example/demo/OrderServiceTests.java b/src/test/java/com/example/demo/OrderServiceTests.java new file mode 100644 index 0000000..c299ad7 --- /dev/null +++ b/src/test/java/com/example/demo/OrderServiceTests.java @@ -0,0 +1,69 @@ +package com.example.demo; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import com.example.demo.core.error.NotFoundException; +import com.example.demo.orders.model.OrderEntity; +import com.example.demo.orders.service.OrderService; + +@SpringBootTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class OrderServiceTests { + @Autowired + private OrderService orderService; + + @Test + void getTest() { + Assertions.assertThrows(NotFoundException.class, () -> orderService.get(0L)); + } + + @Test + @Order(1) + void createTest() { + // Создаем тестовую сущность OrderEntity + OrderEntity testOrder = new OrderEntity(null, null, "2024-04-01"); + // Вызываем метод create() и сохраняем созданную сущность + OrderEntity createdOrder = orderService.create(testOrder); + // Проверяем, что метод create() вернул не null + Assertions.assertNotNull(createdOrder); + // Проверяем, что созданная сущность имеет назначенный ID + Assertions.assertNotNull(createdOrder.getId()); + // Проверяем, что созданная сущность соответствует той, которую мы передали + Assertions.assertEquals(testOrder.getDate(), createdOrder.getDate()); + } + + @Test + @Order(2) + void updateTest() { + // Получаем сущность OrderEntity для обновления + OrderEntity existingOrder = orderService.get(1L); + // Создаем новую дату для обновления + String newDate = "2024-04-02"; + // Устанавливаем новую дату в сущность OrderEntity + existingOrder.setDate(newDate); + // Вызываем метод update() и сохраняем обновленную сущность + OrderEntity updatedOrder = orderService.update(1L, existingOrder); + // Проверяем, что метод update() вернул не null + Assertions.assertNotNull(updatedOrder); + // Проверяем, что обновленная сущность имеет ту же дату, что и заданная + Assertions.assertEquals(newDate, updatedOrder.getDate()); + } + + @Test + @Order(3) + void deleteTest() { + // Удаляем сущность OrderEntity по ее ID + OrderEntity deletedOrder = orderService.delete(1L); + // Проверяем, что метод delete() вернул не null + Assertions.assertNotNull(deletedOrder); + // Проверяем, что удаленная сущность имеет тот же ID, что и удаленная + Assertions.assertEquals(1L, deletedOrder.getId()); + // Проверяем, что сущность больше не существует в репозитории + Assertions.assertThrows(NotFoundException.class, () -> orderService.get(1L)); + } +} diff --git a/src/test/java/com/example/demo/ProductServiceTests.java b/src/test/java/com/example/demo/ProductServiceTests.java new file mode 100644 index 0000000..8a545be --- /dev/null +++ b/src/test/java/com/example/demo/ProductServiceTests.java @@ -0,0 +1,57 @@ +package com.example.demo; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import com.example.demo.core.error.NotFoundException; +import com.example.demo.products.model.ProductEntity; +import com.example.demo.products.service.ProductService; +import com.example.demo.types.model.TypeEntity; + +@SpringBootTest +@TestMethodOrder(OrderAnnotation.class) +class ProductServiceTests { + @Autowired + private ProductService productService; + + @Test + void getTest() { + Assertions.assertThrows(NotFoundException.class, () -> productService.get(0L)); + } + + @Test + @Order(1) + void createTest() { + + final var type1 = new TypeEntity(null, "Пицца"); + productService.create(new ProductEntity(null, "Mocarela", type1, 20.00)); + productService.create(new ProductEntity(null, "El'Diablo", type1, 20.00)); + productService.create(new ProductEntity(null, "Маргарита", type1, 499.00)); + productService.create(new ProductEntity(null, "Эль Дьябло", type1, 699.00)); + productService.create(new ProductEntity(null, "Гавайская", type1, 399.00)); + Assertions.assertEquals(5, productService.getAll(0L).size()); + } + + @Test + @Order(2) + void updateTest() { + final ProductEntity newProduct = new ProductEntity(null, "El'Diablo", new TypeEntity(null, "Пицца"), 20.00); + final ProductEntity updProduct = productService.update(1L, newProduct); + Assertions.assertEquals(5, productService.getAll(0L).size()); + Assertions.assertEquals(updProduct, productService.get(1L)); + Assertions.assertEquals(newProduct.getName(), updProduct.getName()); + Assertions.assertEquals(newProduct.getPrice(), updProduct.getPrice()); + } + + @Test + @Order(3) + void deleteTest() { + productService.delete(2L); + Assertions.assertEquals(4L, productService.getAll(0L).size()); + } +} \ No newline at end of file diff --git a/src/test/java/com/example/demo/SpeakerSrviceTests.java b/src/test/java/com/example/demo/SpeakerSrviceTests.java deleted file mode 100644 index d8624d3..0000000 --- a/src/test/java/com/example/demo/SpeakerSrviceTests.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.example.demo; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.NoSuchBeanDefinitionException; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; - -import com.example.demo.speaker.service.SpeakerService; - -@SpringBootTest -class SpeakerSrviceTests { - @Autowired - SpeakerService speakerService; - - @Test - void testSpeakerRus() { - final String res = speakerService.say("Мир", "ru"); - Assertions.assertEquals("Привет, Мир!", res); - } - - @Test - void testSpeakerEng() { - final String res = speakerService.say("World", "en"); - Assertions.assertEquals("Hello, World!", res); - } - - @Test - void testSpeakerDeu() { - final String res = speakerService.say("Welt", "de"); - Assertions.assertEquals("Hallo, Welt!", res); - } - - @Test - void testSpeakerErrorWired() { - Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> speakerService.say("Мир", "rus")); - } -} diff --git a/src/test/java/com/example/demo/TypeServiceTests.java b/src/test/java/com/example/demo/TypeServiceTests.java index 409d4d9..6c372b6 100644 --- a/src/test/java/com/example/demo/TypeServiceTests.java +++ b/src/test/java/com/example/demo/TypeServiceTests.java @@ -26,9 +26,9 @@ class TypeServiceTests { @Test @Order(1) void createTest() { - typeService.create(new TypeEntity(null, "Ноутбук")); - typeService.create(new TypeEntity(null, "Телефон")); - final TypeEntity last = typeService.create(new TypeEntity(null, "Игровая приставка")); + typeService.create(new TypeEntity(null, "Пицца")); + typeService.create(new TypeEntity(null, "Напиток")); + final TypeEntity last = typeService.create(new TypeEntity(null, "Закуска")); Assertions.assertEquals(3, typeService.getAll().size()); Assertions.assertEquals(last, typeService.get(3L)); } @@ -54,7 +54,7 @@ class TypeServiceTests { final TypeEntity last = typeService.get(2L); Assertions.assertEquals(2L, last.getId()); - final TypeEntity newEntity = typeService.create(new TypeEntity(null, "Игровая приставка")); + final TypeEntity newEntity = typeService.create(new TypeEntity(null, "Закуска")); Assertions.assertEquals(3, typeService.getAll().size()); Assertions.assertEquals(4L, newEntity.getId()); } diff --git a/src/test/java/com/example/demo/UserServiceTests.java b/src/test/java/com/example/demo/UserServiceTests.java index f65096d..9366084 100644 --- a/src/test/java/com/example/demo/UserServiceTests.java +++ b/src/test/java/com/example/demo/UserServiceTests.java @@ -37,7 +37,6 @@ class UserServiceTests { @Test @Order(2) void updateTest() { - final String test = "TEST"; final UserEntity entity = userService.get(3L); final String oldName = entity.getFullname(); final UserEntity newEntity = userService.update(3L, new UserEntity(1L, "test", "test", "test", "test"));