This commit is contained in:
Вячеслав Иванов 2024-03-27 20:15:17 +04:00
parent 717aa9e938
commit b350bf150c
29 changed files with 541 additions and 369 deletions

BIN
2 семестр/lab2.rar Normal file

Binary file not shown.

View File

@ -1,6 +1,6 @@
plugins { plugins {
id 'java' 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' id 'io.spring.dependency-management' version '1.1.4'
} }
@ -17,6 +17,7 @@ repositories {
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web' 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.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'
implementation 'org.modelmapper:modelmapper:3.2.0' implementation 'org.modelmapper:modelmapper:3.2.0'

View File

@ -8,8 +8,8 @@ import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.example.demo.items.model.ItemEntity; import com.example.demo.product.model.ProductEntity;
import com.example.demo.items.service.ItemService; import com.example.demo.product.service.ProductService;
import com.example.demo.types.model.TypeEntity; import com.example.demo.types.model.TypeEntity;
import com.example.demo.types.service.TypeService; import com.example.demo.types.service.TypeService;
@ -18,11 +18,11 @@ public class DemoApplication implements CommandLineRunner {
private final Logger log = LoggerFactory.getLogger(DemoApplication.class); private final Logger log = LoggerFactory.getLogger(DemoApplication.class);
private final TypeService typeService; private final TypeService typeService;
private final ItemService itemService; private final ProductService productService;
public DemoApplication(TypeService typeService, ItemService itemService) { public DemoApplication(TypeService typeService, ProductService productService) {
this.typeService = typeService; this.typeService = typeService;
this.itemService = itemService; this.productService = productService;
} }
public static void main(String[] args) { public static void main(String[] args) {
@ -37,14 +37,14 @@ public class DemoApplication implements CommandLineRunner {
final var type2 = typeService.create(new TypeEntity(null, "Пепперони")); final var type2 = typeService.create(new TypeEntity(null, "Пепперони"));
final var type3 = typeService.create(new TypeEntity(null, "Сырная")); final var type3 = typeService.create(new TypeEntity(null, "Сырная"));
log.info("Create default items values"); log.info("Create default product values");
itemService.create(new ItemEntity(null, type1, 399.00, 20)); productService.create(new ProductEntity(null, "test", type1, 399.00));
itemService.create(new ItemEntity(null, type1, 499.00, 3)); productService.create(new ProductEntity(null, "test", type1, 499.00));
itemService.create(new ItemEntity(null, type2, 450.50, 30)); productService.create(new ProductEntity(null, "test", type2, 450.50));
itemService.create(new ItemEntity(null, type2, 900.50, 10)); productService.create(new ProductEntity(null, "test", type2, 900.50));
itemService.create(new ItemEntity(null, type2, 600.00, 6)); productService.create(new ProductEntity(null, "test", type2, 600.00));
itemService.create(new ItemEntity(null, type3, 750.00, 6)); productService.create(new ProductEntity(null, "test", type3, 750.00));
itemService.create(new ItemEntity(null, type3, 670.00, 3)); productService.create(new ProductEntity(null, "test", type3, 670.00));
} }
} }
} }

View File

@ -1,70 +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));
}
}

View File

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

View File

@ -0,0 +1,79 @@
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) {
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) {
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,70 @@
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;
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;
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;
}
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 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

@ -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,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(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

@ -1,21 +1,21 @@
package com.example.demo.items.api; package com.example.demo.product.api;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Min; import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
public class ItemDto { public class ProductDto {
private Long id; private Long id;
@NotBlank
private String name;
@NotNull @NotNull
@Min(1) @Min(1)
private Long typeId; private Long typeId;
@NotNull @NotNull
@Min(1) @Min(1)
private Double price; private Double price;
@NotNull
@Min(1)
private Integer count;
@JsonProperty(access = JsonProperty.Access.READ_ONLY) @JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Long getId() { public Long getId() {
@ -26,6 +26,14 @@ public class ItemDto {
this.id = id; this.id = id;
} }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getTypeId() { public Long getTypeId() {
return typeId; return typeId;
} }
@ -40,18 +48,5 @@ public class ItemDto {
public void setPrice(Double price) { public void setPrice(Double price) {
this.price = 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,24 +1,32 @@
package com.example.demo.items.model; package com.example.demo.product.model;
import java.util.Objects; import java.util.Objects;
import com.example.demo.core.model.BaseEntity; import com.example.demo.core.model.BaseEntity;
import com.example.demo.types.model.TypeEntity; import com.example.demo.types.model.TypeEntity;
public class ItemEntity extends BaseEntity { public class ProductEntity extends BaseEntity {
private String name;
private TypeEntity type; private TypeEntity type;
private Double price; private Double price;
private Integer count;
public ItemEntity() { public ProductEntity() {
super(); super();
} }
public ItemEntity(Long id, TypeEntity type, Double price, Integer count) { public ProductEntity(Long id, String name, TypeEntity type, Double price) {
super(id); super(id);
this.name = name;
this.type = type; this.type = type;
this.price = price; this.price = price;
this.count = count; }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }
public TypeEntity getType() { public TypeEntity getType() {
@ -37,17 +45,9 @@ public class ItemEntity extends BaseEntity {
this.price = price; this.price = price;
} }
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(id, type, price, count); return Objects.hash(id, name, type, price);
} }
@Override @Override
@ -56,10 +56,10 @@ public class ItemEntity extends BaseEntity {
return true; return true;
if (obj == null || getClass() != obj.getClass()) if (obj == null || getClass() != obj.getClass())
return false; return false;
final ItemEntity other = (ItemEntity) obj; final ProductEntity other = (ProductEntity) obj;
return Objects.equals(other.getId(), id) return Objects.equals(other.getId(), id)
&& Objects.equals(other.getName(), name)
&& Objects.equals(other.getType(), type) && Objects.equals(other.getType(), type)
&& Objects.equals(other.getPrice(), price) && Objects.equals(other.getPrice(), price);
&& Objects.equals(other.getCount(), count);
} }
} }

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

@ -1,4 +1,4 @@
package com.example.demo.items.service; package com.example.demo.product.service;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -7,45 +7,45 @@ import java.util.Optional;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.example.demo.core.error.NotFoundException; import com.example.demo.core.error.NotFoundException;
import com.example.demo.items.model.ItemEntity; import com.example.demo.product.model.ProductEntity;
import com.example.demo.items.repository.ItemRepository; import com.example.demo.product.repository.ProductRepository;
@Service @Service
public class ItemService { public class ProductService {
private final ItemRepository repository; private final ProductRepository repository;
public ItemService(ItemRepository repository) { public ProductService(ProductRepository repository) {
this.repository = repository; this.repository = repository;
} }
public List<ItemEntity> getAll(Long typeId) { public List<ProductEntity> getAll(Long typeId) {
if (Objects.equals(typeId, 0L)) { if (Objects.equals(typeId, 0L)) {
return repository.getAll(); return repository.getAll();
} }
return repository.getAll().stream() return repository.getAll().stream()
.filter(item -> item.getType().getId().equals(typeId)) .filter(product -> product.getType().getId().equals(typeId))
.toList(); .toList();
} }
public ItemEntity get(Long id) { public ProductEntity get(Long id) {
return Optional.ofNullable(repository.get(id)) return Optional.ofNullable(repository.get(id))
.orElseThrow(() -> new NotFoundException(id)); .orElseThrow(() -> new NotFoundException(id));
} }
public ItemEntity create(ItemEntity entity) { public ProductEntity create(ProductEntity entity) {
return repository.create(entity); return repository.create(entity);
} }
public ItemEntity update(Long id, ItemEntity entity) { public ProductEntity update(Long id, ProductEntity entity) {
final ItemEntity existsEntity = get(id); final ProductEntity existsEntity = get(id);
existsEntity.setName(entity.getName());
existsEntity.setType(entity.getType()); existsEntity.setType(entity.getType());
existsEntity.setPrice(entity.getPrice()); existsEntity.setPrice(entity.getPrice());
existsEntity.setCount(entity.getCount());
return repository.update(existsEntity); return repository.update(existsEntity);
} }
public ItemEntity delete(Long id) { public ProductEntity delete(Long id) {
final ItemEntity existsEntity = get(id); final ProductEntity existsEntity = get(id);
return repository.delete(existsEntity); 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

@ -53,7 +53,7 @@ public class TypeController {
} }
@PutMapping("/{id}") @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))); return toDto(typeService.update(id, toEntity(dto)));
} }

View File

@ -53,7 +53,7 @@ public class UserController {
} }
@PutMapping("/{id}") @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))); return toDto(userService.update(id, toEntity(dto)));
} }

View File

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

View File

@ -0,0 +1,93 @@
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(3L));
}
@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));
final OrderEntity order = orderService.get(3L);
final Integer oldPrice = order.getCount();
final OrderEntity newOrder = orderService.update(3L, new OrderEntity(3L, one, test, 10));
Assertions.assertEquals(3, orderService.getAll(0L).size());
Assertions.assertEquals(newOrder, orderService.get(3L));
Assertions.assertEquals(10, newOrder.getCount());
Assertions.assertNotEquals(oldPrice, newOrder.getCount());
}
@Test
@Order(3)
void deleteTest() {
orderService.delete(3L);
Assertions.assertEquals(2, orderService.getAll(0L).size());
Assertions.assertThrows(NotFoundException.class, () -> orderService.get(3L));
final UserEntity one = userService.get(2L);
final TypeEntity two = typeService.create(new TypeEntity(null, "test"));
final ProductEntity test = productService.create(new ProductEntity(null, "test", two ,100.00));
final OrderEntity newOrder = orderService.create(new OrderEntity(null, one, test, 15));
Assertions.assertEquals(3, orderService.getAll(0L).size());
Assertions.assertEquals(4L, newOrder.getId());
}
}

View File

@ -9,23 +9,23 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import com.example.demo.core.error.NotFoundException; import com.example.demo.core.error.NotFoundException;
import com.example.demo.items.model.ItemEntity; import com.example.demo.product.model.ProductEntity;
import com.example.demo.items.service.ItemService; import com.example.demo.product.service.ProductService;
import com.example.demo.types.model.TypeEntity; import com.example.demo.types.model.TypeEntity;
import com.example.demo.types.service.TypeService; import com.example.demo.types.service.TypeService;
@SpringBootTest @SpringBootTest
@TestMethodOrder(OrderAnnotation.class) @TestMethodOrder(OrderAnnotation.class)
class ItemServiceTests { class ProductsServiceTests {
@Autowired @Autowired
private ItemService itemService; private ProductService productService;
@Autowired @Autowired
private TypeService typeService; private TypeService typeService;
@Test @Test
void getTest() { void getTest() {
Assertions.assertThrows(NotFoundException.class, () -> itemService.get(0L)); Assertions.assertThrows(NotFoundException.class, () -> productService.get(0L));
} }
@Test @Test
@ -33,13 +33,13 @@ class ItemServiceTests {
void createTest() { void createTest() {
final TypeEntity type = typeService.create(new TypeEntity(null, "Пепперони")); final TypeEntity type = typeService.create(new TypeEntity(null, "Пепперони"));
itemService.create(new ItemEntity(null, type, 399.00, 20)); productService.create(new ProductEntity(null, "test", type, 399.00));
itemService.create(new ItemEntity(null, type, 599.00, 3)); productService.create(new ProductEntity(null, "test1", type, 599.00));
final ItemEntity last = itemService.create(new ItemEntity(null, type, 1500.00, 6)); final ProductEntity last = productService.create(new ProductEntity(null, "test2", type, 1500.0));
Assertions.assertEquals(3, itemService.getAll(0L).size()); Assertions.assertEquals(3, productService.getAll(0L).size());
Assertions.assertEquals(last, itemService.get(3L)); Assertions.assertEquals(last, productService.get(3L));
} }
@Test @Test
@ -47,13 +47,13 @@ class ItemServiceTests {
void updateTest() { void updateTest() {
final TypeEntity type = typeService.create(new TypeEntity(null, "Мясная")); final TypeEntity type = typeService.create(new TypeEntity(null, "Мясная"));
final ItemEntity entity = itemService.get(3L); final ProductEntity entity = productService.get(3L);
final Double oldPrice = entity.getPrice(); final Double oldPrice = entity.getPrice();
final ItemEntity newEntity = itemService.update(3L, new ItemEntity(null, type, 200.00, 6)); final ProductEntity newEntity = productService.update(3L, new ProductEntity(null, "test4", type, 200.00));
Assertions.assertEquals(3, itemService.getAll(0L).size()); Assertions.assertEquals(3, productService.getAll(0L).size());
Assertions.assertEquals(newEntity, itemService.get(3L)); Assertions.assertEquals(newEntity, productService.get(3L));
Assertions.assertEquals(200.00, newEntity.getPrice()); Assertions.assertEquals(200.00, newEntity.getPrice());
Assertions.assertNotEquals(oldPrice, newEntity.getPrice()); Assertions.assertNotEquals(oldPrice, newEntity.getPrice());
} }
@ -61,15 +61,15 @@ class ItemServiceTests {
@Test @Test
@Order(3) @Order(3)
void deleteTest() { void deleteTest() {
itemService.delete(3L); productService.delete(3L);
Assertions.assertEquals(2, itemService.getAll(0L).size()); Assertions.assertEquals(2, productService.getAll(0L).size());
Assertions.assertThrows(NotFoundException.class, () -> itemService.get(3L)); Assertions.assertThrows(NotFoundException.class, () -> productService.get(3L));
final TypeEntity type = typeService.create(new TypeEntity(null, "Пепперони")); final TypeEntity type = typeService.create(new TypeEntity(null, "Пепперони"));
final ItemEntity newEntity = itemService.create(new ItemEntity(null, type, 499.00, 10)); final ProductEntity newEntity = productService.create(new ProductEntity(null, "test5", type, 499.00));
Assertions.assertEquals(3, itemService.getAll(0L).size()); Assertions.assertEquals(3, productService.getAll(0L).size());
Assertions.assertEquals(4L, newEntity.getId()); 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 1", "test@test", "qwerty"));
userService.create(new UserEntity(null, "User 2", "test1@test1", "12345678")); userService.create(new UserEntity(null, "User 2", "test1@test1", "12345678"));
final UserEntity last = userService.create(new UserEntity(null, "User 3", "test2@test2", "mrgurgnugrn")); final UserEntity last = userService.create(new UserEntity(null, "User 3", "test2@test2", "mrgurgnugrn"));
Assertions.assertEquals(3, userService.getAll().size()); Assertions.assertEquals(5, userService.getAll().size());
Assertions.assertEquals(last, userService.get(3L)); Assertions.assertEquals(last, userService.get(5L));
} }
@Test @Test
@ -42,7 +42,7 @@ class UserServiceTests {
final UserEntity entity = userService.get(3L); final UserEntity entity = userService.get(3L);
final String oldName = entity.getName(); final String oldName = entity.getName();
final UserEntity newEntity = userService.update(3L, new UserEntity(1L, test, email, password)); 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(newEntity, userService.get(3L));
Assertions.assertEquals(test, newEntity.getName()); Assertions.assertEquals(test, newEntity.getName());
Assertions.assertNotEquals(oldName, newEntity.getName()); Assertions.assertNotEquals(oldName, newEntity.getName());
@ -52,12 +52,12 @@ class UserServiceTests {
@Order(3) @Order(3)
void deleteTest() { void deleteTest() {
userService.delete(3L); userService.delete(3L);
Assertions.assertEquals(2, userService.getAll().size()); Assertions.assertEquals(4, userService.getAll().size());
final UserEntity last = userService.get(2L); final UserEntity last = userService.get(2L);
Assertions.assertEquals(2L, last.getId()); Assertions.assertEquals(2L, last.getId());
final UserEntity newEntity = userService.create(new UserEntity(null, "TEST", "ortrigr@rbirirrgi", "rgkrgimrgirg")); final UserEntity newEntity = userService.create(new UserEntity(null, "TEST", "ortrigr@rbirirrgi", "rgkrgimrgirg"));
Assertions.assertEquals(3, userService.getAll().size()); Assertions.assertEquals(5, userService.getAll().size());
Assertions.assertEquals(4L, newEntity.getId()); Assertions.assertEquals(6L, newEntity.getId());
} }
} }