This commit is contained in:
Вячеслав Иванов 2024-03-25 13:40:49 +04:00
parent 34acbf7da1
commit bd0a3ef8cf
19 changed files with 677 additions and 19 deletions

View File

@ -17,6 +17,7 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'
implementation 'org.modelmapper:modelmapper:3.2.0'

View File

@ -37,7 +37,7 @@ public class DemoApplication implements CommandLineRunner {
final var type2 = typeService.create(new TypeEntity(null, "Пепперони"));
final var type3 = typeService.create(new TypeEntity(null, "Сырная"));
log.info("Create default items values");
log.info("Create default products values");
productService.create(new ProductEntity(null, type1, 399.00, 20));
productService.create(new ProductEntity(null, type1, 499.00, 3));
productService.create(new ProductEntity(null, type2, 450.50, 30));

View File

@ -0,0 +1,74 @@
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.product.service.ProductService;
import com.example.demo.users.service.UserService;
import jakarta.validation.Valid;
@RestController
@RequestMapping(Constants.API_URL + "/order")
public class OrderController {
private final UserService userService;
private final ProductService productService;
private final OrderService orderService;
private final ModelMapper modelMapper;
public OrderController(UserService userService, ProductService productService, OrderService orderService, ModelMapper modelMapper) {
this.userService = userService;
this.productService = productService;
this.orderService = orderService;
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()));
entity.setProduct(productService.get(dto.getProductId()));
return entity;
}
@GetMapping
public List<OrderDto> getAll(@RequestParam(name = "userId", defaultValue = "0") Long productId) {
return orderService.getAll(productId).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 @Valid OrderDto dto) {
return toDto(orderService.update(id, toEntity(dto)));
}
@DeleteMapping("/{id}")
public OrderDto delete(@PathVariable(name = "id") Long id) {
return toDto(orderService.delete(id));
}
}

View File

@ -0,0 +1,52 @@
package com.example.demo.orders.api;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
public class OrderDto {
private Long id;
@NotNull
@Min(1)
private Long userId;
@NotNull
@Min(1)
private Long productId;
@NotNull
@Min(1)
private Integer count;
@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 Long getProductId() {
return productId;
}
public void setProductId(Long productId) {
this.productId = productId;
}
public Integer getCount() {
return count;
}
public void setPrice(Integer count) {
this.count = count;
}
}

View File

@ -0,0 +1,66 @@
package com.example.demo.orders.model;
import java.util.Objects;
import com.example.demo.core.model.BaseEntity;
import com.example.demo.product.model.ProductEntity;
import com.example.demo.users.model.UserEntity;
public class OrderEntity extends BaseEntity {
private UserEntity user;
private ProductEntity product;
private Integer count;
public OrderEntity() {
super();
}
public OrderEntity(Long id, UserEntity user, ProductEntity product, Integer count) {
super(id);
this.user = user;
this.product = product;
this.count = count;
}
public UserEntity getUser() {
return user;
}
public void setUser(UserEntity user) {
this.user = user;
}
public ProductEntity getProduct() {
return product;
}
public void setProduct(ProductEntity product) {
this.product = product;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
@Override
public int hashCode() {
return Objects.hash(id, user, product, count);
}
@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.getProduct(), product)
&& Objects.equals(other.getCount(), count);
}
}

View File

@ -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> {
}

View File

@ -0,0 +1,48 @@
package com.example.demo.orders.service;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.orders.model.OrderEntity;
import com.example.demo.orders.repository.OrderRepository;
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(product -> product.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.setProduct(entity.getProduct());
existsEntity.setCount(entity.getCount());
return repository.update(existsEntity);
}
public OrderEntity delete(Long id) {
final OrderEntity existsEntity = get(id);
return repository.delete(existsEntity);
}
}

View File

@ -0,0 +1,70 @@
package com.example.demo.product.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.product.model.ProductEntity;
import com.example.demo.product.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 @Valid ProductDto dto) {
return toDto(productService.update(id, toEntity(dto)));
}
@DeleteMapping("/{id}")
public ProductDto delete(@PathVariable(name = "id") Long id) {
return toDto(productService.delete(id));
}
}

View File

@ -0,0 +1,52 @@
package com.example.demo.product.api;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
public class ProductDto {
private Long id;
@NotNull
@Min(1)
private String name;
@NotNull
@Min(1)
private Long typeId;
@NotNull
@Min(1)
private Double price;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getTypeId() {
return typeId;
}
public void setTypeId(Long typeId) {
this.typeId = typeId;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}

View File

@ -0,0 +1,65 @@
package com.example.demo.product.model;
import java.util.Objects;
import com.example.demo.core.model.BaseEntity;
import com.example.demo.types.model.TypeEntity;
public class ProductEntity extends BaseEntity {
private String name;
private TypeEntity type;
private Double price;
public ProductEntity() {
super();
}
public ProductEntity(Long id, String name, TypeEntity type, Double price) {
super(id);
this.name = name;
this.type = type;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public TypeEntity getType() {
return type;
}
public void setType(TypeEntity type) {
this.type = type;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public int hashCode() {
return Objects.hash(id, name, type, price);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
final ProductEntity other = (ProductEntity) obj;
return Objects.equals(other.getId(), id)
&& Objects.equals(other.getName(), name)
&& Objects.equals(other.getType(), type)
&& Objects.equals(other.getPrice(), price);
}
}

View File

@ -0,0 +1,10 @@
package com.example.demo.product.repository;
import org.springframework.stereotype.Repository;
import com.example.demo.core.repository.MapRepository;
import com.example.demo.product.model.ProductEntity;
@Repository
public class ProductRepository extends MapRepository<ProductEntity> {
}

View File

@ -0,0 +1,51 @@
package com.example.demo.product.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.product.model.ProductEntity;
import com.example.demo.product.repository.ProductRepository;
@Service
public class ProductService {
private final ProductRepository repository;
public ProductService(ProductRepository repository) {
this.repository = repository;
}
public List<ProductEntity> getAll(Long typeId) {
if (Objects.equals(typeId, 0L)) {
return repository.getAll();
}
return repository.getAll().stream()
.filter(product -> product.getType().getId().equals(typeId))
.toList();
}
public ProductEntity get(Long id) {
return Optional.ofNullable(repository.get(id))
.orElseThrow(() -> new NotFoundException(id));
}
public ProductEntity create(ProductEntity entity) {
return repository.create(entity);
}
public ProductEntity update(Long id, ProductEntity entity) {
final ProductEntity existsEntity = get(id);
existsEntity.setName(entity.getName());
existsEntity.setType(entity.getType());
existsEntity.setPrice(entity.getPrice());
return repository.update(existsEntity);
}
public ProductEntity delete(Long id) {
final ProductEntity existsEntity = get(id);
return repository.delete(existsEntity);
}
}

View File

@ -21,7 +21,7 @@ import com.example.demo.types.service.TypeService;
import jakarta.validation.Valid;
@RestController
@RequestMapping(Constants.API_URL + "/product")
@RequestMapping(Constants.API_URL + "/products")
public class ProductController {
private final ProductService productService;
private final TypeService typeService;
@ -59,7 +59,7 @@ public class ProductController {
}
@PutMapping("/{id}")
public ProductDto update(@PathVariable(name = "id") Long id, @RequestBody ProductDto dto) {
public ProductDto update(@PathVariable(name = "id") Long id, @RequestBody @Valid ProductDto dto) {
return toDto(productService.update(id, toEntity(dto)));
}

View File

@ -53,7 +53,7 @@ public class TypeController {
}
@PutMapping("/{id}")
public TypeDto update(@PathVariable(name = "id") Long id, @RequestBody TypeDto dto) {
public TypeDto update(@PathVariable(name = "id") Long id, @RequestBody @Valid TypeDto dto) {
return toDto(typeService.update(id, toEntity(dto)));
}

View File

@ -53,7 +53,7 @@ public class UserController {
}
@PutMapping("/{id}")
public UserDto update(@PathVariable(name = "id") Long id, @RequestBody UserDto dto) {
public UserDto update(@PathVariable(name = "id") Long id, @RequestBody @Valid UserDto dto) {
return toDto(userService.update(id, toEntity(dto)));
}

View File

@ -0,0 +1,84 @@
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.orders.model.OrderEntity;
import com.example.demo.orders.service.OrderService;
import com.example.demo.product.model.ProductEntity;
import com.example.demo.product.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;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
public class OrderServiceTests {
@Autowired
private OrderService orderService;
@Autowired
private UserService userService;
@Autowired
private TypeService typeService;
@Autowired
private ProductService productService;
@Test
void getTest() {
Assertions.assertThrows(NotFoundException.class, () -> orderService.get(0L));
}
@Test
@Order(1)
void createTest() {
final UserEntity one = userService.create(new UserEntity(null, "test", "test", "test"));
final TypeEntity two = typeService.create(new TypeEntity(null, "test"));
final ProductEntity test = productService.create(new ProductEntity(null, "test", two ,100.00));
orderService.create(new OrderEntity(null, one, test, 5));
orderService.create(new OrderEntity(null, one, test, 6));
final OrderEntity last = orderService.create(new OrderEntity(null, one, test, 7));
Assertions.assertEquals(3, orderService.getAll(0L).size());
Assertions.assertEquals(last, orderService.get(6L));
}
@Test
@Order(2)
void updateTest() {
final UserEntity one = userService.create(new UserEntity(null, "test", "test", "test"));
final TypeEntity two = typeService.create(new TypeEntity(null, "test"));
final ProductEntity test = productService.create(new ProductEntity(null, "test", two ,100.00));
Assertions.assertEquals(6, orderService.getAll().size());
Assertions.assertEquals(newEntity, orderService.get(3L));
Assertions.assertEquals(test, newEntity.getName());
Assertions.assertNotEquals(oldName, newEntity.getName());
}
@Test
@Order(3)
void deleteTest() {
orderService.delete(3L);
Assertions.assertEquals(5, orderService.getAll().size());
final TypeEntity last = orderService.get(2L);
Assertions.assertEquals(2L, last.getId());
final TypeEntity newEntity = orderService.create(new TypeEntity(null, "Сырная"));
Assertions.assertEquals(6, orderService.getAll().size());
Assertions.assertEquals(7L, newEntity.getId());
}
}

View File

@ -9,14 +9,14 @@ 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.product.model.ProductEntity;
import com.example.demo.product.service.ProductService;
import com.example.demo.types.model.TypeEntity;
import com.example.demo.types.service.TypeService;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class ProductServiceTests {
class ProductsServiceTests {
@Autowired
private ProductService productService;
@ -33,10 +33,10 @@ class ProductServiceTests {
void createTest() {
final TypeEntity type = typeService.create(new TypeEntity(null, "Пепперони"));
productService.create(new ProductEntity(null, type, 399.00, 20));
productService.create(new ProductEntity(null, type, 599.00, 3));
productService.create(new ProductEntity(null, "test", type, 399.00));
productService.create(new ProductEntity(null, "test1", type, 599.00));
final ProductEntity last = productService.create(new ProductEntity(null, type, 1500.00, 6));
final ProductEntity last = productService.create(new ProductEntity(null, "test2", type, 1500.0));
Assertions.assertEquals(3, productService.getAll(0L).size());
Assertions.assertEquals(last, productService.get(3L));
@ -50,7 +50,7 @@ class ProductServiceTests {
final ProductEntity entity = productService.get(3L);
final Double oldPrice = entity.getPrice();
final ProductEntity newEntity = productService.update(3L, new ProductEntity(null, type, 200.00, 6));
final ProductEntity newEntity = productService.update(3L, new ProductEntity(null, "test4", type, 200.00));
Assertions.assertEquals(3, productService.getAll(0L).size());
Assertions.assertEquals(newEntity, productService.get(3L));
@ -67,7 +67,7 @@ class ProductServiceTests {
Assertions.assertThrows(NotFoundException.class, () -> productService.get(3L));
final TypeEntity type = typeService.create(new TypeEntity(null, "Пепперони"));
final ProductEntity newEntity = productService.create(new ProductEntity(null, type, 499.00, 10));
final ProductEntity newEntity = productService.create(new ProductEntity(null, "test5", type, 499.00));
Assertions.assertEquals(3, productService.getAll(0L).size());
Assertions.assertEquals(4L, newEntity.getId());

View File

@ -0,0 +1,75 @@
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;
import com.example.demo.types.service.TypeService;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class ProductsServiceTests {
@Autowired
private ProductService productService;
@Autowired
private TypeService typeService;
@Test
void getTest() {
Assertions.assertThrows(NotFoundException.class, () -> productService.get(0L));
}
@Test
@Order(1)
void createTest() {
final TypeEntity type = typeService.create(new TypeEntity(null, "Пепперони"));
productService.create(new ProductEntity(null, type, 399.00, 20));
productService.create(new ProductEntity(null, type, 599.00, 3));
final ProductEntity last = productService.create(new ProductEntity(null, type, 1500.00, 6));
Assertions.assertEquals(3, productService.getAll(0L).size());
Assertions.assertEquals(last, productService.get(3L));
}
@Test
@Order(2)
void updateTest() {
final TypeEntity type = typeService.create(new TypeEntity(null, "Мясная"));
final ProductEntity entity = productService.get(3L);
final Double oldPrice = entity.getPrice();
final ProductEntity newEntity = productService.update(3L, new ProductEntity(null, type, 200.00, 6));
Assertions.assertEquals(3, productService.getAll(0L).size());
Assertions.assertEquals(newEntity, productService.get(3L));
Assertions.assertEquals(200.00, newEntity.getPrice());
Assertions.assertNotEquals(oldPrice, newEntity.getPrice());
}
@Test
@Order(3)
void deleteTest() {
productService.delete(3L);
Assertions.assertEquals(2, productService.getAll(0L).size());
Assertions.assertThrows(NotFoundException.class, () -> productService.get(3L));
final TypeEntity type = typeService.create(new TypeEntity(null, "Пепперони"));
final ProductEntity newEntity = productService.create(new ProductEntity(null, type, 499.00, 10));
Assertions.assertEquals(3, productService.getAll(0L).size());
Assertions.assertEquals(4L, newEntity.getId());
}
}

View File

@ -29,8 +29,8 @@ class TypeServiceTests {
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));
Assertions.assertEquals(6, typeService.getAll().size());
Assertions.assertEquals(last, typeService.get(6L));
}
@Test
@ -40,7 +40,7 @@ class TypeServiceTests {
final TypeEntity entity = typeService.get(3L);
final String oldName = entity.getName();
final TypeEntity newEntity = typeService.update(3L, new TypeEntity(1L, test));
Assertions.assertEquals(3, typeService.getAll().size());
Assertions.assertEquals(6, typeService.getAll().size());
Assertions.assertEquals(newEntity, typeService.get(3L));
Assertions.assertEquals(test, newEntity.getName());
Assertions.assertNotEquals(oldName, newEntity.getName());
@ -50,12 +50,12 @@ class TypeServiceTests {
@Order(3)
void deleteTest() {
typeService.delete(3L);
Assertions.assertEquals(2, typeService.getAll().size());
Assertions.assertEquals(5, typeService.getAll().size());
final TypeEntity last = typeService.get(2L);
Assertions.assertEquals(2L, last.getId());
final TypeEntity newEntity = typeService.create(new TypeEntity(null, "Сырная"));
Assertions.assertEquals(3, typeService.getAll().size());
Assertions.assertEquals(4L, newEntity.getId());
Assertions.assertEquals(6, typeService.getAll().size());
Assertions.assertEquals(7L, newEntity.getId());
}
}