This commit is contained in:
Вячеслав Иванов 2024-03-27 20:13:25 +04:00
parent 751b9a16ab
commit 99bfffc099
23 changed files with 53 additions and 548 deletions

View File

@ -1,6 +1,6 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.3'
id 'org.springframework.boot' version '3.2.4'
id 'io.spring.dependency-management' version '1.1.4'
}

View File

@ -8,8 +8,8 @@ import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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;
@ -37,14 +37,14 @@ 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 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));
productService.create(new ProductEntity(null, type2, 900.50, 10));
productService.create(new ProductEntity(null, type2, 600.00, 6));
productService.create(new ProductEntity(null, type3, 750.00, 6));
productService.create(new ProductEntity(null, type3, 670.00, 3));
log.info("Create default product values");
productService.create(new ProductEntity(null, "test", type1, 399.00));
productService.create(new ProductEntity(null, "test", type1, 499.00));
productService.create(new ProductEntity(null, "test", type2, 450.50));
productService.create(new ProductEntity(null, "test", type2, 900.50));
productService.create(new ProductEntity(null, "test", type2, 600.00));
productService.create(new ProductEntity(null, "test", type3, 750.00));
productService.create(new ProductEntity(null, "test", type3, 670.00));
}
}
}

View File

@ -37,7 +37,12 @@ public class OrderController {
}
private OrderDto toDto(OrderEntity entity) {
return modelMapper.map(entity, OrderDto.class);
OrderDto dto = new OrderDto(productService);
dto.setId(entity.getId());
dto.setUserId(entity.getUser().getId());
dto.setProductId(entity.getProduct().getId());
dto.setCount(entity.getCount());
return dto;
}
private OrderEntity toEntity(OrderDto dto) {

View File

@ -1,5 +1,7 @@
package com.example.demo.orders.api;
import com.example.demo.product.model.ProductEntity;
import com.example.demo.product.service.ProductService;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Min;
@ -17,6 +19,16 @@ public class OrderDto {
@Min(1)
private Integer count;
transient private final ProductService productService;
public OrderDto() {
this.productService = null;
}
public OrderDto(ProductService productService) {
this.productService = productService;
}
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Long getId() {
return id;
@ -46,7 +58,13 @@ public class OrderDto {
return count;
}
public void setPrice(Integer count) {
public void setCount(Integer count) {
this.count = count;
}
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Double getSum() {
ProductEntity product = productService.get(productId);
return product.getPrice() * count;
}
}

View File

@ -4,10 +4,12 @@ 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;

View File

@ -3,12 +3,12 @@ package com.example.demo.product.api;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
public class ProductDto {
private Long id;
@NotNull
@Min(1)
@NotBlank
private String name;
@NotNull
@Min(1)

View File

@ -1,70 +0,0 @@
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 + "/products")
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

@ -1,57 +0,0 @@
package com.example.demo.products.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 Long typeId;
@NotNull
@Min(1)
private Double price;
@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 getTypeId() {
return typeId;
}
public void setTypeId(Long typeId) {
this.typeId = typeId;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
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;
}
}

View File

@ -1,65 +0,0 @@
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 ProductEntity extends BaseEntity {
private TypeEntity type;
private Double price;
private Integer count;
public ProductEntity() {
super();
}
public ProductEntity(Long id, TypeEntity type, Double price, Integer count) {
super(id);
this.type = type;
this.price = price;
this.count = count;
}
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;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
@Override
public int hashCode() {
return Objects.hash(id, type, price, count);
}
@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.getType(), type)
&& Objects.equals(other.getPrice(), price)
&& Objects.equals(other.getCount(), count);
}
}

View File

@ -1,10 +0,0 @@
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> {
}

View File

@ -1,51 +0,0 @@
package com.example.demo.products.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.products.model.ProductEntity;
import com.example.demo.products.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.setType(entity.getType());
existsEntity.setPrice(entity.getPrice());
existsEntity.setCount(entity.getCount());
return repository.update(existsEntity);
}
public ProductEntity delete(Long id) {
final ProductEntity existsEntity = get(id);
return repository.delete(existsEntity);
}
}

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -1,5 +0,0 @@
package com.example.demo.speaker.domain;
public interface Speaker {
String say();
}

View File

@ -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()");
}
}

View File

@ -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()");
}
}

View File

@ -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()");
}
}

View File

@ -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);
}
}

View File

@ -2,19 +2,15 @@ package com.example.demo.users.api;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.NotBlank;
public class UserDto {
private Long id;
@NotNull
@Min(1)
@NotBlank
private String name;
@NotNull
@Min(1)
@NotBlank
private String email;
@NotNull
@Min(1)
@NotBlank
private String password;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)

View File

@ -52,7 +52,7 @@ public class OrderServiceTests {
final OrderEntity last = orderService.create(new OrderEntity(null, one, test, 7));
Assertions.assertEquals(3, orderService.getAll(0L).size());
Assertions.assertEquals(last, orderService.get(6L));
Assertions.assertEquals(last, orderService.get(3L));
}
@Test
@ -87,7 +87,7 @@ public class OrderServiceTests {
final OrderEntity newOrder = orderService.create(new OrderEntity(null, one, test, 15));
Assertions.assertEquals(6, orderService.getAll(0L).size());
Assertions.assertEquals(7L, newOrder.getId());
Assertions.assertEquals(3, orderService.getAll(0L).size());
Assertions.assertEquals(4L, newOrder.getId());
}
}

View File

@ -1,75 +0,0 @@
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

@ -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"));
}
}

View File

@ -29,8 +29,8 @@ class UserServiceTests {
userService.create(new UserEntity(null, "User 1", "test@test", "qwerty"));
userService.create(new UserEntity(null, "User 2", "test1@test1", "12345678"));
final UserEntity last = userService.create(new UserEntity(null, "User 3", "test2@test2", "mrgurgnugrn"));
Assertions.assertEquals(3, userService.getAll().size());
Assertions.assertEquals(last, userService.get(3L));
Assertions.assertEquals(5, userService.getAll().size());
Assertions.assertEquals(last, userService.get(5L));
}
@Test
@ -42,7 +42,7 @@ class UserServiceTests {
final UserEntity entity = userService.get(3L);
final String oldName = entity.getName();
final UserEntity newEntity = userService.update(3L, new UserEntity(1L, test, email, password));
Assertions.assertEquals(3, userService.getAll().size());
Assertions.assertEquals(5, userService.getAll().size());
Assertions.assertEquals(newEntity, userService.get(3L));
Assertions.assertEquals(test, newEntity.getName());
Assertions.assertNotEquals(oldName, newEntity.getName());
@ -52,12 +52,12 @@ class UserServiceTests {
@Order(3)
void deleteTest() {
userService.delete(3L);
Assertions.assertEquals(2, userService.getAll().size());
Assertions.assertEquals(4, userService.getAll().size());
final UserEntity last = userService.get(2L);
Assertions.assertEquals(2L, last.getId());
final UserEntity newEntity = userService.create(new UserEntity(null, "TEST", "ortrigr@rbirirrgi", "rgkrgimrgirg"));
Assertions.assertEquals(3, userService.getAll().size());
Assertions.assertEquals(4L, newEntity.getId());
Assertions.assertEquals(5, userService.getAll().size());
Assertions.assertEquals(6L, newEntity.getId());
}
}