fix
This commit is contained in:
parent
cafeab358b
commit
34acbf7da1
@ -8,10 +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.products.model.ProductEntity;
|
||||||
import com.example.demo.items.service.ItemService;
|
import com.example.demo.products.service.ProductService;
|
||||||
import com.example.demo.stocks.model.StockEntity;
|
|
||||||
import com.example.demo.stocks.service.StockService;
|
|
||||||
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;
|
||||||
|
|
||||||
@ -20,13 +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;
|
||||||
private final StockService stockService;
|
|
||||||
|
|
||||||
public DemoApplication(TypeService typeService, ItemService itemService, StockService stockService) {
|
public DemoApplication(TypeService typeService, ProductService productService) {
|
||||||
this.typeService = typeService;
|
this.typeService = typeService;
|
||||||
this.itemService = itemService;
|
this.productService = productService;
|
||||||
this.stockService = stockService;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@ -41,18 +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, "Сырная"));
|
||||||
|
|
||||||
final var stock1 = stockService.create(new StockEntity(null, "без акции", 0));
|
|
||||||
final var stock2 = stockService.create(new StockEntity(null, "50% до вечера", 50));
|
|
||||||
final var stock3 = stockService.create(new StockEntity(null, "День рождения", 25));
|
|
||||||
|
|
||||||
log.info("Create default items values");
|
log.info("Create default items values");
|
||||||
itemService.create(new ItemEntity(null, type1, 399.00, 20, stock1));
|
productService.create(new ProductEntity(null, type1, 399.00, 20));
|
||||||
itemService.create(new ItemEntity(null, type1, 499.00, 3, stock2));
|
productService.create(new ProductEntity(null, type1, 499.00, 3));
|
||||||
itemService.create(new ItemEntity(null, type2, 450.50, 30, stock3));
|
productService.create(new ProductEntity(null, type2, 450.50, 30));
|
||||||
itemService.create(new ItemEntity(null, type2, 900.50, 10, stock1));
|
productService.create(new ProductEntity(null, type2, 900.50, 10));
|
||||||
itemService.create(new ItemEntity(null, type2, 600.00, 6, stock2));
|
productService.create(new ProductEntity(null, type2, 600.00, 6));
|
||||||
itemService.create(new ItemEntity(null, type3, 750.00, 6, stock3));
|
productService.create(new ProductEntity(null, type3, 750.00, 6));
|
||||||
itemService.create(new ItemEntity(null, type3, 670.00, 3, stock1));
|
productService.create(new ProductEntity(null, type3, 670.00, 3));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,74 +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.stocks.service.StockService;
|
|
||||||
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 StockService stockService;
|
|
||||||
private final ModelMapper modelMapper;
|
|
||||||
|
|
||||||
public ItemController(ItemService itemService, TypeService typeService, StockService stockService, ModelMapper modelMapper) {
|
|
||||||
this.itemService = itemService;
|
|
||||||
this.typeService = typeService;
|
|
||||||
this.stockService = stockService;
|
|
||||||
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()));
|
|
||||||
entity.setStock(stockService.get(dto.getStockId()));
|
|
||||||
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,70 @@
|
|||||||
|
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,14 +1,14 @@
|
|||||||
package com.example.demo.items.api;
|
package com.example.demo.products.api;
|
||||||
|
|
||||||
import com.example.demo.stocks.service.StockService;
|
|
||||||
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.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
|
||||||
public class ItemDto {
|
public class ProductDto {
|
||||||
private Long id;
|
private Long id;
|
||||||
@NotNull
|
@NotNull
|
||||||
|
@Min(1)
|
||||||
private Long typeId;
|
private Long typeId;
|
||||||
@NotNull
|
@NotNull
|
||||||
@Min(1)
|
@Min(1)
|
||||||
@ -16,9 +16,6 @@ public class ItemDto {
|
|||||||
@NotNull
|
@NotNull
|
||||||
@Min(1)
|
@Min(1)
|
||||||
private Integer count;
|
private Integer count;
|
||||||
@NotNull
|
|
||||||
private Long stockId;
|
|
||||||
|
|
||||||
|
|
||||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
@ -53,19 +50,8 @@ public class ItemDto {
|
|||||||
this.count = count;
|
this.count = count;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getStockId() {
|
|
||||||
return stockId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setStockId(Long stockId) {
|
|
||||||
this.stockId = stockId;
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
public Double getSum(StockService stockService) {
|
public Double getSum() {
|
||||||
Integer stockValue = stockService.getStockValueById(stockId);
|
return price * count;
|
||||||
|
|
||||||
double multiplier = (stockValue != null && stockValue != 0) ? stockValue / 100.0 : 1.0;
|
|
||||||
return price * count * multiplier;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,27 +1,24 @@
|
|||||||
package com.example.demo.items.model;
|
package com.example.demo.products.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.stocks.model.StockEntity;
|
|
||||||
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 TypeEntity type;
|
private TypeEntity type;
|
||||||
private Double price;
|
private Double price;
|
||||||
private Integer count;
|
private Integer count;
|
||||||
private StockEntity stock;
|
|
||||||
|
|
||||||
public ItemEntity() {
|
public ProductEntity() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemEntity(Long id, TypeEntity type, Double price, Integer count, StockEntity stock) {
|
public ProductEntity(Long id, TypeEntity type, Double price, Integer count) {
|
||||||
super(id);
|
super(id);
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.price = price;
|
this.price = price;
|
||||||
this.count = count;
|
this.count = count;
|
||||||
this.stock = stock;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public TypeEntity getType() {
|
public TypeEntity getType() {
|
||||||
@ -48,17 +45,9 @@ public class ItemEntity extends BaseEntity {
|
|||||||
this.count = count;
|
this.count = count;
|
||||||
}
|
}
|
||||||
|
|
||||||
public StockEntity getStock() {
|
|
||||||
return stock;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setStock(StockEntity stock) {
|
|
||||||
this.stock = stock;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(id, type, price, count, stock);
|
return Objects.hash(id, type, price, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -67,11 +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.getType(), type)
|
&& Objects.equals(other.getType(), type)
|
||||||
&& Objects.equals(other.getPrice(), price)
|
&& Objects.equals(other.getPrice(), price)
|
||||||
&& Objects.equals(other.getCount(), count)
|
&& Objects.equals(other.getCount(), count);
|
||||||
&& Objects.equals(other.getStock(), stock);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@ -7,46 +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.products.model.ProductEntity;
|
||||||
import com.example.demo.items.repository.ItemRepository;
|
import com.example.demo.products.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.setType(entity.getType());
|
existsEntity.setType(entity.getType());
|
||||||
existsEntity.setPrice(entity.getPrice());
|
existsEntity.setPrice(entity.getPrice());
|
||||||
existsEntity.setCount(entity.getCount());
|
existsEntity.setCount(entity.getCount());
|
||||||
existsEntity.setStock(entity.getStock());
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,63 +0,0 @@
|
|||||||
package com.example.demo.stocks.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.RestController;
|
|
||||||
|
|
||||||
import com.example.demo.core.configuration.Constants;
|
|
||||||
import com.example.demo.stocks.model.StockEntity;
|
|
||||||
import com.example.demo.stocks.service.StockService;
|
|
||||||
import jakarta.validation.Valid;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@RequestMapping(Constants.API_URL + "/stock")
|
|
||||||
public class StockController {
|
|
||||||
private final StockService stockService;
|
|
||||||
private final ModelMapper modelMapper;
|
|
||||||
|
|
||||||
public StockController(StockService stockService, ModelMapper modelMapper) {
|
|
||||||
this.stockService = stockService;
|
|
||||||
this.modelMapper = modelMapper;
|
|
||||||
}
|
|
||||||
|
|
||||||
private StockDto toDto(StockEntity entity) {
|
|
||||||
return modelMapper.map(entity, StockDto.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
private StockEntity toEntity(StockDto dto) {
|
|
||||||
return modelMapper.map(dto, StockEntity.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping
|
|
||||||
public List<StockDto> getAll() {
|
|
||||||
return stockService.getAll().stream().map(this::toDto).toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
|
||||||
public StockDto get(@PathVariable(name = "id") Long id) {
|
|
||||||
return toDto(stockService.get(id));
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping
|
|
||||||
public StockDto create(@RequestBody @Valid StockDto dto) {
|
|
||||||
return toDto(stockService.create(toEntity(dto)));
|
|
||||||
}
|
|
||||||
|
|
||||||
@PutMapping("/{id}")
|
|
||||||
public StockDto update(@PathVariable(name = "id") Long id, @RequestBody StockDto dto) {
|
|
||||||
return toDto(stockService.update(id, toEntity(dto)));
|
|
||||||
}
|
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
|
||||||
public StockDto delete(@PathVariable(name = "id") Long id) {
|
|
||||||
return toDto(stockService.delete(id));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
package com.example.demo.stocks.api;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
||||||
|
|
||||||
import jakarta.validation.constraints.NotBlank;
|
|
||||||
|
|
||||||
public class StockDto {
|
|
||||||
private Long id;
|
|
||||||
@NotBlank
|
|
||||||
private String name;
|
|
||||||
@NotBlank
|
|
||||||
private Integer value;
|
|
||||||
|
|
||||||
@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 Integer getValue() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setValue(Integer value) {
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,53 +0,0 @@
|
|||||||
package com.example.demo.stocks.model;
|
|
||||||
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
import com.example.demo.core.model.BaseEntity;
|
|
||||||
|
|
||||||
public class StockEntity extends BaseEntity {
|
|
||||||
private String name;
|
|
||||||
private Integer value;
|
|
||||||
|
|
||||||
public StockEntity() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
public StockEntity(Long id, String name, Integer value) {
|
|
||||||
super(id);
|
|
||||||
this.name = name;
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer getValue() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setValue(Integer value) {
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return Objects.hash(id, name, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if (this == obj)
|
|
||||||
return true;
|
|
||||||
if (obj == null || getClass() != obj.getClass())
|
|
||||||
return false;
|
|
||||||
final StockEntity other = (StockEntity) obj;
|
|
||||||
return Objects.equals(other.getId(), id)
|
|
||||||
&& Objects.equals(other.getName(), name)
|
|
||||||
&& Objects.equals(other.getValue(), value);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
package com.example.demo.stocks.repository;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
import com.example.demo.core.repository.MapRepository;
|
|
||||||
import com.example.demo.stocks.model.StockEntity;
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
public class StockRepository extends MapRepository<StockEntity> {
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
package com.example.demo.stocks.service;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import com.example.demo.core.error.NotFoundException;
|
|
||||||
import com.example.demo.stocks.model.StockEntity;
|
|
||||||
import com.example.demo.stocks.repository.StockRepository;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class StockService {
|
|
||||||
private final StockRepository repository;
|
|
||||||
|
|
||||||
public StockService(StockRepository repository) {
|
|
||||||
this.repository = repository;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer getStockValueById(Long stockId) {
|
|
||||||
StockEntity stockEntity = repository.get(stockId);
|
|
||||||
return (stockEntity != null) ? stockEntity.getValue() : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<StockEntity> getAll() {
|
|
||||||
return repository.getAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
public StockEntity get(Long id) {
|
|
||||||
return Optional.ofNullable(repository.get(id))
|
|
||||||
.orElseThrow(() -> new NotFoundException(id));
|
|
||||||
}
|
|
||||||
|
|
||||||
public StockEntity create(StockEntity entity) {
|
|
||||||
return repository.create(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public StockEntity update(Long id, StockEntity entity) {
|
|
||||||
final StockEntity existsEntity = get(id);
|
|
||||||
existsEntity.setName(entity.getName());
|
|
||||||
existsEntity.setValue(entity.getValue());
|
|
||||||
return repository.update(existsEntity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public StockEntity delete(Long id) {
|
|
||||||
final StockEntity existsEntity = get(id);
|
|
||||||
return repository.delete(existsEntity);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,83 +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.items.model.ItemEntity;
|
|
||||||
import com.example.demo.items.service.ItemService;
|
|
||||||
import com.example.demo.stocks.model.StockEntity;
|
|
||||||
import com.example.demo.stocks.service.StockService;
|
|
||||||
import com.example.demo.types.model.TypeEntity;
|
|
||||||
import com.example.demo.types.service.TypeService;
|
|
||||||
|
|
||||||
@SpringBootTest
|
|
||||||
@TestMethodOrder(OrderAnnotation.class)
|
|
||||||
class ItemServiceTests {
|
|
||||||
@Autowired
|
|
||||||
private ItemService itemService;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private TypeService typeService;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private StockService stockService;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getTest() {
|
|
||||||
Assertions.assertThrows(NotFoundException.class, () -> itemService.get(0L));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(1)
|
|
||||||
void createTest() {
|
|
||||||
final TypeEntity type = typeService.create(new TypeEntity(null, "Пепперони"));
|
|
||||||
final StockEntity stock = stockService.create(new StockEntity(null, "Две по цене одной", 50));
|
|
||||||
|
|
||||||
itemService.create(new ItemEntity(null, type, 399.00, 20, stock));
|
|
||||||
itemService.create(new ItemEntity(null, type, 599.00, 3, stock));
|
|
||||||
|
|
||||||
final ItemEntity last = itemService.create(new ItemEntity(null, type, 1500.00, 6, stock));
|
|
||||||
|
|
||||||
Assertions.assertEquals(3, itemService.getAll(0L).size());
|
|
||||||
Assertions.assertEquals(last, itemService.get(3L));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(2)
|
|
||||||
void updateTest() {
|
|
||||||
final TypeEntity type = typeService.create(new TypeEntity(null, "Мясная"));
|
|
||||||
final StockEntity stock = stockService.create(new StockEntity(null, "День рождения", 25));
|
|
||||||
|
|
||||||
final ItemEntity entity = itemService.get(3L);
|
|
||||||
final Double oldPrice = entity.getPrice();
|
|
||||||
|
|
||||||
final ItemEntity newEntity = itemService.update(3L, new ItemEntity(null, type, 200.00, 6, stock));
|
|
||||||
|
|
||||||
Assertions.assertEquals(3, itemService.getAll(0L).size());
|
|
||||||
Assertions.assertEquals(newEntity, itemService.get(3L));
|
|
||||||
Assertions.assertEquals(200.00, newEntity.getPrice());
|
|
||||||
Assertions.assertNotEquals(oldPrice, newEntity.getPrice());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(3)
|
|
||||||
void deleteTest() {
|
|
||||||
itemService.delete(3L);
|
|
||||||
|
|
||||||
Assertions.assertEquals(2, itemService.getAll(0L).size());
|
|
||||||
Assertions.assertThrows(NotFoundException.class, () -> itemService.get(3L));
|
|
||||||
|
|
||||||
final TypeEntity type = typeService.create(new TypeEntity(null, "Пепперони"));
|
|
||||||
final StockEntity stock = stockService.create(new StockEntity(null, "День рождения", 25));
|
|
||||||
final ItemEntity newEntity = itemService.create(new ItemEntity(null, type, 499.00, 10, stock));
|
|
||||||
|
|
||||||
Assertions.assertEquals(3, itemService.getAll(0L).size());
|
|
||||||
Assertions.assertEquals(4L, newEntity.getId());
|
|
||||||
}
|
|
||||||
}
|
|
75
demo/src/test/java/com/example/demo/ProductServiceTests.java
Normal file
75
demo/src/test/java/com/example/demo/ProductServiceTests.java
Normal 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 ProductServiceTests {
|
||||||
|
@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());
|
||||||
|
}
|
||||||
|
}
|
@ -1,62 +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.stocks.model.StockEntity;
|
|
||||||
import com.example.demo.stocks.service.StockService;
|
|
||||||
|
|
||||||
@SpringBootTest
|
|
||||||
@TestMethodOrder(OrderAnnotation.class)
|
|
||||||
class StockServiceTests {
|
|
||||||
@Autowired
|
|
||||||
private StockService stockService;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getTest() {
|
|
||||||
Assertions.assertThrows(NotFoundException.class, () -> stockService.get(0L));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(1)
|
|
||||||
void createTest() {
|
|
||||||
stockService.create(new StockEntity(null, "Stock 1", 50));
|
|
||||||
stockService.create(new StockEntity(null, "Stock 2", 0));
|
|
||||||
final StockEntity last = stockService.create(new StockEntity(null, "Stock 3", 25));
|
|
||||||
Assertions.assertEquals(3, stockService.getAll().size());
|
|
||||||
Assertions.assertEquals(last, stockService.get(3L));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(2)
|
|
||||||
void updateTest() {
|
|
||||||
final String test = "TEST";
|
|
||||||
final Integer valueTest = 15;
|
|
||||||
final StockEntity entity = stockService.get(3L);
|
|
||||||
final String oldName = entity.getName();
|
|
||||||
final StockEntity newEntity = stockService.update(3L, new StockEntity(1L, test, valueTest));
|
|
||||||
Assertions.assertEquals(3, stockService.getAll().size());
|
|
||||||
Assertions.assertEquals(newEntity, stockService.get(3L));
|
|
||||||
Assertions.assertEquals(test, newEntity.getName());
|
|
||||||
Assertions.assertNotEquals(oldName, newEntity.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(3)
|
|
||||||
void deleteTest() {
|
|
||||||
stockService.delete(3L);
|
|
||||||
Assertions.assertEquals(2, stockService.getAll().size());
|
|
||||||
final StockEntity last = stockService.get(2L);
|
|
||||||
Assertions.assertEquals(2L, last.getId());
|
|
||||||
|
|
||||||
final StockEntity newEntity = stockService.create(new StockEntity(null, "TEST", 15));
|
|
||||||
Assertions.assertEquals(3, stockService.getAll().size());
|
|
||||||
Assertions.assertEquals(4L, newEntity.getId());
|
|
||||||
}
|
|
||||||
}
|
|
@ -29,8 +29,8 @@ class TypeServiceTests {
|
|||||||
typeService.create(new TypeEntity(null, "Пепперони"));
|
typeService.create(new TypeEntity(null, "Пепперони"));
|
||||||
typeService.create(new TypeEntity(null, "Мясная"));
|
typeService.create(new TypeEntity(null, "Мясная"));
|
||||||
final TypeEntity last = typeService.create(new TypeEntity(null, "Сырная"));
|
final TypeEntity last = typeService.create(new TypeEntity(null, "Сырная"));
|
||||||
Assertions.assertEquals(6, typeService.getAll().size());
|
Assertions.assertEquals(3, typeService.getAll().size());
|
||||||
Assertions.assertEquals(last, typeService.get(6L));
|
Assertions.assertEquals(last, typeService.get(3L));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -40,7 +40,7 @@ class TypeServiceTests {
|
|||||||
final TypeEntity entity = typeService.get(3L);
|
final TypeEntity entity = typeService.get(3L);
|
||||||
final String oldName = entity.getName();
|
final String oldName = entity.getName();
|
||||||
final TypeEntity newEntity = typeService.update(3L, new TypeEntity(1L, test));
|
final TypeEntity newEntity = typeService.update(3L, new TypeEntity(1L, test));
|
||||||
Assertions.assertEquals(6, typeService.getAll().size());
|
Assertions.assertEquals(3, typeService.getAll().size());
|
||||||
Assertions.assertEquals(newEntity, typeService.get(3L));
|
Assertions.assertEquals(newEntity, typeService.get(3L));
|
||||||
Assertions.assertEquals(test, newEntity.getName());
|
Assertions.assertEquals(test, newEntity.getName());
|
||||||
Assertions.assertNotEquals(oldName, newEntity.getName());
|
Assertions.assertNotEquals(oldName, newEntity.getName());
|
||||||
@ -50,12 +50,12 @@ class TypeServiceTests {
|
|||||||
@Order(3)
|
@Order(3)
|
||||||
void deleteTest() {
|
void deleteTest() {
|
||||||
typeService.delete(3L);
|
typeService.delete(3L);
|
||||||
Assertions.assertEquals(5, typeService.getAll().size());
|
Assertions.assertEquals(2, typeService.getAll().size());
|
||||||
final TypeEntity last = typeService.get(2L);
|
final TypeEntity last = typeService.get(2L);
|
||||||
Assertions.assertEquals(2L, last.getId());
|
Assertions.assertEquals(2L, last.getId());
|
||||||
|
|
||||||
final TypeEntity newEntity = typeService.create(new TypeEntity(null, "Сырная"));
|
final TypeEntity newEntity = typeService.create(new TypeEntity(null, "Сырная"));
|
||||||
Assertions.assertEquals(6, typeService.getAll().size());
|
Assertions.assertEquals(3, typeService.getAll().size());
|
||||||
Assertions.assertEquals(7L, newEntity.getId());
|
Assertions.assertEquals(4L, newEntity.getId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user