lab_2 done ( new entityes order orderline, refresh entity product)
This commit is contained in:
parent
7995d3d885
commit
176d2bbbb4
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
14
.vscode/launch.json
vendored
14
.vscode/launch.json
vendored
@ -2,23 +2,13 @@
|
||||
"configurations": [
|
||||
{
|
||||
"type": "java",
|
||||
"name": "Demo",
|
||||
"name": "Spring Boot-DemoApplication<IP_PIbd-21_Kryukov_Backend>",
|
||||
"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<lab_2>",
|
||||
"request": "launch",
|
||||
"cwd": "${workspaceFolder}",
|
||||
"mainClass": "com.example.demo.DemoApplication",
|
||||
"projectName": "lab_2",
|
||||
"args": "",
|
||||
"envFile": "${workspaceFolder}/.env"
|
||||
}
|
||||
]
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<ItemDto> 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));
|
||||
}
|
||||
}
|
@ -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<ItemEntity> {
|
||||
}
|
@ -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<OrderLineDto> 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));
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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<OrderLineEntity> {
|
||||
}
|
@ -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<OrderLineEntity> 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);
|
||||
}
|
||||
}
|
@ -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<OrderDto> 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));
|
||||
}
|
||||
}
|
41
src/main/java/com/example/demo/orders/api/OrderDto.java
Normal file
41
src/main/java/com/example/demo/orders/api/OrderDto.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
55
src/main/java/com/example/demo/orders/model/OrderEntity.java
Normal file
55
src/main/java/com/example/demo/orders/model/OrderEntity.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
@ -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<OrderEntity> {
|
||||
}
|
@ -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<OrderEntity> 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);
|
||||
}
|
||||
}
|
@ -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<ProductDto> 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));
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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<ProductEntity> {
|
||||
}
|
@ -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<ItemEntity> getAll(Long typeId) {
|
||||
public List<ProductEntity> 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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
package com.example.demo.speaker.domain;
|
||||
|
||||
public interface Speaker {
|
||||
String say();
|
||||
}
|
@ -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()");
|
||||
}
|
||||
}
|
@ -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()");
|
||||
|
||||
}
|
||||
}
|
@ -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()");
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
69
src/test/java/com/example/demo/OrderLineServiceTests.java
Normal file
69
src/test/java/com/example/demo/OrderLineServiceTests.java
Normal file
@ -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));
|
||||
}
|
||||
}
|
69
src/test/java/com/example/demo/OrderServiceTests.java
Normal file
69
src/test/java/com/example/demo/OrderServiceTests.java
Normal file
@ -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));
|
||||
}
|
||||
}
|
57
src/test/java/com/example/demo/ProductServiceTests.java
Normal file
57
src/test/java/com/example/demo/ProductServiceTests.java
Normal file
@ -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());
|
||||
}
|
||||
}
|
@ -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"));
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
||||
|
@ -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"));
|
||||
|
Loading…
x
Reference in New Issue
Block a user