This commit is contained in:
Вячеслав Иванов 2024-03-15 13:57:29 +04:00
parent 05dbc2902f
commit 717aa9e938
12 changed files with 20 additions and 342 deletions

View File

@ -10,8 +10,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
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;
@ -21,12 +19,10 @@ public class DemoApplication implements CommandLineRunner {
private final TypeService typeService;
private final ItemService itemService;
private final StockService stockService;
public DemoApplication(TypeService typeService, ItemService itemService, StockService stockService) {
public DemoApplication(TypeService typeService, ItemService itemService) {
this.typeService = typeService;
this.itemService = itemService;
this.stockService = stockService;
}
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 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");
itemService.create(new ItemEntity(null, type1, 399.00, 20, stock1));
itemService.create(new ItemEntity(null, type1, 499.00, 3, stock2));
itemService.create(new ItemEntity(null, type2, 450.50, 30, stock3));
itemService.create(new ItemEntity(null, type2, 900.50, 10, stock1));
itemService.create(new ItemEntity(null, type2, 600.00, 6, stock2));
itemService.create(new ItemEntity(null, type3, 750.00, 6, stock3));
itemService.create(new ItemEntity(null, type3, 670.00, 3, stock1));
itemService.create(new ItemEntity(null, type1, 399.00, 20));
itemService.create(new ItemEntity(null, type1, 499.00, 3));
itemService.create(new ItemEntity(null, type2, 450.50, 30));
itemService.create(new ItemEntity(null, type2, 900.50, 10));
itemService.create(new ItemEntity(null, type2, 600.00, 6));
itemService.create(new ItemEntity(null, type3, 750.00, 6));
itemService.create(new ItemEntity(null, type3, 670.00, 3));
}
}
}

View File

@ -16,7 +16,6 @@ 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;
@ -26,13 +25,11 @@ import jakarta.validation.Valid;
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) {
public ItemController(ItemService itemService, TypeService typeService, ModelMapper modelMapper) {
this.itemService = itemService;
this.typeService = typeService;
this.stockService = stockService;
this.modelMapper = modelMapper;
}
@ -43,7 +40,6 @@ public class ItemController {
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;
}

View File

@ -1,6 +1,5 @@
package com.example.demo.items.api;
import com.example.demo.stocks.service.StockService;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Min;
@ -9,6 +8,7 @@ import jakarta.validation.constraints.NotNull;
public class ItemDto {
private Long id;
@NotNull
@Min(1)
private Long typeId;
@NotNull
@Min(1)
@ -16,9 +16,6 @@ public class ItemDto {
@NotNull
@Min(1)
private Integer count;
@NotNull
private Long stockId;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Long getId() {
@ -53,19 +50,8 @@ public class ItemDto {
this.count = count;
}
public Long getStockId() {
return stockId;
}
public void setStockId(Long stockId) {
this.stockId = stockId;
}
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Double getSum(StockService stockService) {
Integer stockValue = stockService.getStockValueById(stockId);
double multiplier = (stockValue != null && stockValue != 0) ? stockValue / 100.0 : 1.0;
return price * count * multiplier;
public Double getSum() {
return price * count;
}
}

View File

@ -3,25 +3,22 @@ package com.example.demo.items.model;
import java.util.Objects;
import com.example.demo.core.model.BaseEntity;
import com.example.demo.stocks.model.StockEntity;
import com.example.demo.types.model.TypeEntity;
public class ItemEntity extends BaseEntity {
private TypeEntity type;
private Double price;
private Integer count;
private StockEntity stock;
public ItemEntity() {
super();
}
public ItemEntity(Long id, TypeEntity type, Double price, Integer count, StockEntity stock) {
public ItemEntity(Long id, TypeEntity type, Double price, Integer count) {
super(id);
this.type = type;
this.price = price;
this.count = count;
this.stock = stock;
}
public TypeEntity getType() {
@ -48,17 +45,9 @@ public class ItemEntity extends BaseEntity {
this.count = count;
}
public StockEntity getStock() {
return stock;
}
public void setStock(StockEntity stock) {
this.stock = stock;
}
@Override
public int hashCode() {
return Objects.hash(id, type, price, count, stock);
return Objects.hash(id, type, price, count);
}
@Override
@ -71,7 +60,6 @@ public class ItemEntity extends BaseEntity {
return Objects.equals(other.getId(), id)
&& Objects.equals(other.getType(), type)
&& Objects.equals(other.getPrice(), price)
&& Objects.equals(other.getCount(), count)
&& Objects.equals(other.getStock(), stock);
&& Objects.equals(other.getCount(), count);
}
}

View File

@ -41,7 +41,6 @@ public class ItemService {
existsEntity.setType(entity.getType());
existsEntity.setPrice(entity.getPrice());
existsEntity.setCount(entity.getCount());
existsEntity.setStock(entity.getStock());
return repository.update(existsEntity);
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -11,8 +11,6 @@ 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;
@ -25,9 +23,6 @@ class ItemServiceTests {
@Autowired
private TypeService typeService;
@Autowired
private StockService stockService;
@Test
void getTest() {
Assertions.assertThrows(NotFoundException.class, () -> itemService.get(0L));
@ -37,12 +32,11 @@ class ItemServiceTests {
@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));
itemService.create(new ItemEntity(null, type, 399.00, 20));
itemService.create(new ItemEntity(null, type, 599.00, 3));
final ItemEntity last = itemService.create(new ItemEntity(null, type, 1500.00, 6, stock));
final ItemEntity last = itemService.create(new ItemEntity(null, type, 1500.00, 6));
Assertions.assertEquals(3, itemService.getAll(0L).size());
Assertions.assertEquals(last, itemService.get(3L));
@ -52,12 +46,11 @@ class ItemServiceTests {
@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));
final ItemEntity newEntity = itemService.update(3L, new ItemEntity(null, type, 200.00, 6));
Assertions.assertEquals(3, itemService.getAll(0L).size());
Assertions.assertEquals(newEntity, itemService.get(3L));
@ -74,8 +67,7 @@ class ItemServiceTests {
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));
final ItemEntity newEntity = itemService.create(new ItemEntity(null, type, 499.00, 10));
Assertions.assertEquals(3, itemService.getAll(0L).size());
Assertions.assertEquals(4L, newEntity.getId());

View File

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