lab2 5
This commit is contained in:
parent
f087bc5e96
commit
431abe9c99
@ -1,5 +1,6 @@
|
||||
package com.example.demo;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
@ -10,26 +11,24 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import com.example.demo.itemCategories.model.CategoriesEntity;
|
||||
import com.example.demo.itemCategories.service.CategoriesService;
|
||||
import com.example.demo.itemOrders.model.OrdersEntity;
|
||||
import com.example.demo.itemOrders.service.OrdersService;
|
||||
import com.example.demo.itemProducts.model.ProductsEntity;
|
||||
import com.example.demo.itemProducts.service.ProductsService;
|
||||
import com.example.demo.itemUsers.model.UsersEntity;
|
||||
import com.example.demo.itemUsers.service.UsersService;
|
||||
import com.example.demo.types.model.TypeEntity;
|
||||
import com.example.demo.types.service.TypeService;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoApplication implements CommandLineRunner {
|
||||
private final Logger log = LoggerFactory.getLogger(DemoApplication.class);
|
||||
|
||||
private final TypeService typeService;
|
||||
private final UsersService userService;
|
||||
private final CategoriesService categoriesService;
|
||||
private final ProductsService productsService;
|
||||
private final OrdersService ordersService;
|
||||
|
||||
public DemoApplication(TypeService typeService, UsersService userService, CategoriesService categoriesService, ProductsService productsService, OrdersService ordersService) {
|
||||
this.typeService = typeService;
|
||||
public DemoApplication(UsersService userService, CategoriesService categoriesService, ProductsService productsService, OrdersService ordersService) {
|
||||
|
||||
this.userService = userService;
|
||||
this.categoriesService = categoriesService;
|
||||
this.productsService = productsService;
|
||||
@ -45,30 +44,23 @@ public class DemoApplication implements CommandLineRunner {
|
||||
if (args.length > 0 && Objects.equals("--populate", args[0])) {
|
||||
|
||||
log.info("Создание пользователей");
|
||||
final var user1 = userService.create(new UsersEntity(null, "Natalia", "1234"));
|
||||
final var user2 = userService.create(new UsersEntity(null, "Revengel", "4567"));
|
||||
userService.create(new UsersEntity(null, "Natalia", "1234"));
|
||||
userService.create(new UsersEntity(null, "Revengel", "4567"));
|
||||
|
||||
log.info("Создание категорий");
|
||||
final var category1 = categoriesService.create(new CategoriesEntity(null, "Ноутбуки"));
|
||||
final var category2 = categoriesService.create(new CategoriesEntity(null, "Телефоны"));
|
||||
|
||||
log.info("Создание продуктов");
|
||||
final var products1 = productsService.create(new ProductsEntity(null, "Ноутбуки"));
|
||||
final var products2 = productsService.create(new ProductsEntity(null, "Телефоны"));
|
||||
final var product1 = productsService.create(new ProductsEntity(null, category1, "Lenovo IDEA PAD 13", 15232.00));
|
||||
final var product2 = productsService.create(new ProductsEntity(null, category1, "Acer", 20300.00));
|
||||
final var product3 = productsService.create(new ProductsEntity(null, category2, "Iphone 13", 150000.00));
|
||||
|
||||
log.info("Create default types values");
|
||||
final var type1 = typeService.create(new TypeEntity(null, "Ноутбук"));
|
||||
final var type2 = typeService.create(new TypeEntity(null, "Телефон"));
|
||||
final var type3 = typeService.create(new TypeEntity(null, "Игровая приставка"));
|
||||
|
||||
log.info("Create default items values");
|
||||
itemService.create(new OrderEntity(null, type1, 49999.00, 20));
|
||||
itemService.create(new OrderEntity(null, type1, 129999.00, 3));
|
||||
itemService.create(new OrderEntity(null, type2, 15450.50, 30));
|
||||
itemService.create(new OrderEntity(null, type2, 69900.50, 10));
|
||||
itemService.create(new OrderEntity(null, type2, 150000.00, 6));
|
||||
itemService.create(new OrderEntity(null, type3, 75000.00, 6));
|
||||
itemService.create(new OrderEntity(null, type3, 67800.00, 3));
|
||||
log.info("Создание заказов");
|
||||
ordersService.create(new OrdersEntity(null, product1, 3, new SimpleDateFormat("09/04/2024")));
|
||||
ordersService.create(new OrdersEntity(null, product2, 2, new SimpleDateFormat("09/04/2024")));
|
||||
ordersService.create(new OrdersEntity(null, product3, 1, new SimpleDateFormat("07/04/2024")));
|
||||
ordersService.create(new OrdersEntity(null, product1, 4, new SimpleDateFormat("07/04/2024")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,64 @@
|
||||
package com.example.demo.itemCategories.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.itemCategories.model.CategoriesEntity;
|
||||
import com.example.demo.itemCategories.service.CategoriesService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL + "/Categories")
|
||||
public class CategoriesController {
|
||||
private final CategoriesService CategoriesService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public CategoriesController(CategoriesService CategoriesService, ModelMapper modelMapper) {
|
||||
this.CategoriesService = CategoriesService;
|
||||
this.modelMapper = modelMapper;
|
||||
}
|
||||
|
||||
private CategoriesDto toDto(CategoriesEntity entity) {
|
||||
return modelMapper.map(entity, CategoriesDto.class);
|
||||
}
|
||||
|
||||
private CategoriesEntity toEntity(CategoriesDto dto) {
|
||||
return modelMapper.map(dto, CategoriesEntity.class);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<CategoriesDto> getAll() {
|
||||
return CategoriesService.getAll().stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public CategoriesDto get(@PathVariable(name = "id") Long id) {
|
||||
return toDto(CategoriesService.get(id));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public CategoriesDto create(@RequestBody @Valid CategoriesDto dto) {
|
||||
return toDto(CategoriesService.create(toEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public CategoriesDto update(@PathVariable(name = "id") Long id, @RequestBody CategoriesDto dto) {
|
||||
return toDto(CategoriesService.update(id, toEntity(dto)));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public CategoriesDto delete(@PathVariable(name = "id") Long id) {
|
||||
return toDto(CategoriesService.delete(id));
|
||||
}
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
package com.example.demo.types.api;
|
||||
package com.example.demo.itemCategories.api;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
public class TypeDto {
|
||||
public class CategoriesDto {
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
private Long id;
|
||||
@NotBlank
|
@ -1,7 +1,6 @@
|
||||
package com.example.demo.itemCategories.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -18,10 +17,7 @@ public class CategoriesService {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public List<CategoriesEntity> getAll(Long typeId) {
|
||||
if (Objects.equals(typeId, 0L)) {
|
||||
return repository.getAll();
|
||||
}
|
||||
public List<CategoriesEntity> getAll() {
|
||||
return repository.getAll();
|
||||
}
|
||||
|
||||
|
@ -1,59 +1,49 @@
|
||||
package com.example.demo.itemOrders.model;
|
||||
import java.sql.Date;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Objects;
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
import com.example.demo.itemCategories.model.CategoriesEntity;
|
||||
import com.example.demo.itemProducts.model.ProductsEntity;
|
||||
|
||||
public class OrdersEntity extends BaseEntity {
|
||||
private CategoriesEntity type;
|
||||
private Double price;
|
||||
private ProductsEntity product;
|
||||
private Integer count;
|
||||
private Date date;
|
||||
private SimpleDateFormat date;
|
||||
private Double sum;
|
||||
|
||||
public OrdersEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public OrdersEntity(Long id, CategoriesEntity type, Double price, Integer count, Date date) {
|
||||
public OrdersEntity(Long id, ProductsEntity product, Integer count, SimpleDateFormat date) {
|
||||
super(id);
|
||||
this.type = type;
|
||||
this.price = price;
|
||||
this.product = product;
|
||||
this.count = count;
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public CategoriesEntity getType() {
|
||||
return type;
|
||||
public ProductsEntity getProduct() {
|
||||
return product;
|
||||
}
|
||||
|
||||
public void setType(CategoriesEntity type) {
|
||||
this.type = type;
|
||||
public void setProduct(ProductsEntity product) {
|
||||
this.product = product;
|
||||
}
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
public Double getSum() {
|
||||
sum = count * product.getPrice();
|
||||
return sum;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public Integer getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void setCount(Integer count) {
|
||||
this.count = count;
|
||||
}
|
||||
public Date getDate() {
|
||||
public SimpleDateFormat getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(Date date) {
|
||||
public void setDate(SimpleDateFormat date) {
|
||||
this.date = date;
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, type, price, count);
|
||||
return Objects.hash(id, product, count, date);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -64,8 +54,7 @@ public class OrdersEntity extends BaseEntity {
|
||||
return false;
|
||||
final OrdersEntity other = (OrdersEntity) obj;
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getType(), type)
|
||||
&& Objects.equals(other.getPrice(), price)
|
||||
&& Objects.equals(other.getProduct(), product)
|
||||
&& Objects.equals(other.getCount(), count)
|
||||
&& Objects.equals(other.getDate(), date);
|
||||
}
|
||||
|
@ -18,11 +18,11 @@ public class OrdersService {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public List<OrdersEntity> getAll(Long typeId) {
|
||||
if (Objects.equals(typeId, 0L)) {
|
||||
public List<OrdersEntity> getAll(Long productId) {
|
||||
if (Objects.equals(productId, 0L)) {
|
||||
return repository.getAll();
|
||||
}
|
||||
return repository.getAll().stream().filter(item -> item.getType().getId().equals(typeId)).toList();
|
||||
return repository.getAll().stream().filter(item -> item.getProduct().getId().equals(productId)).toList();
|
||||
}
|
||||
|
||||
public OrdersEntity get(Long id) {
|
||||
@ -35,8 +35,7 @@ public class OrdersService {
|
||||
|
||||
public OrdersEntity update(Long id, OrdersEntity entity) {
|
||||
final OrdersEntity existsEntity = get(id);
|
||||
existsEntity.setType(entity.getType());
|
||||
existsEntity.setPrice(entity.getPrice());
|
||||
existsEntity.setProduct(entity.getProduct());
|
||||
existsEntity.setCount(entity.getCount());
|
||||
existsEntity.setDate(entity.getDate());
|
||||
return repository.update(existsEntity);
|
||||
|
@ -7,20 +7,18 @@ import com.example.demo.itemCategories.model.CategoriesEntity;
|
||||
|
||||
public class ProductsEntity extends BaseEntity{
|
||||
private CategoriesEntity type;
|
||||
private String name;
|
||||
private Double price;
|
||||
private Integer count;
|
||||
private Double sum;
|
||||
|
||||
public ProductsEntity(Object object, String string) {
|
||||
super();
|
||||
}
|
||||
|
||||
public ProductsEntity(Long id, CategoriesEntity type, Double price, Integer count, Double sum) {
|
||||
public ProductsEntity(Long id, CategoriesEntity type, String name, Double price) {
|
||||
super(id);
|
||||
this.type = type;
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
this.count = count;
|
||||
this.sum = sum;
|
||||
}
|
||||
|
||||
public CategoriesEntity getType() {
|
||||
@ -34,27 +32,19 @@ public class ProductsEntity extends BaseEntity{
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public Integer getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void setCount(Integer count) {
|
||||
this.count = count;
|
||||
}
|
||||
public Double getSum() {
|
||||
return sum;
|
||||
}
|
||||
|
||||
public void setSum(Double sum) {
|
||||
this.sum = sum;
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, type, price, count, sum);
|
||||
return Objects.hash(id, type, name, price);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -66,8 +56,7 @@ public class ProductsEntity extends BaseEntity{
|
||||
final ProductsEntity other = (ProductsEntity) obj;
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getType(), type)
|
||||
&& Objects.equals(other.getPrice(), price)
|
||||
&& Objects.equals(other.getCount(), count)
|
||||
&& Objects.equals(other.getSum(), sum);
|
||||
&& Objects.equals(other.getName(), name)
|
||||
&& Objects.equals(other.getPrice(), price);
|
||||
}
|
||||
}
|
||||
|
@ -36,9 +36,8 @@ public class ProductsService {
|
||||
public ProductsEntity update(Long id, ProductsEntity entity) {
|
||||
final ProductsEntity existsEntity = get(id);
|
||||
existsEntity.setType(entity.getType());
|
||||
existsEntity.setName(entity.getName());
|
||||
existsEntity.setPrice(entity.getPrice());
|
||||
existsEntity.setCount(entity.getCount());
|
||||
existsEntity.setSum(entity.getSum());
|
||||
return repository.update(existsEntity);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,64 @@
|
||||
package com.example.demo.itemUsers.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.itemUsers.model.UsersEntity;
|
||||
import com.example.demo.itemUsers.service.UsersService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL + "/Users")
|
||||
public class UsersController {
|
||||
private final UsersService UsersService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public UsersController(UsersService UsersService, ModelMapper modelMapper) {
|
||||
this.UsersService = UsersService;
|
||||
this.modelMapper = modelMapper;
|
||||
}
|
||||
|
||||
private UsersDto toDto(UsersEntity entity) {
|
||||
return modelMapper.map(entity, UsersDto.class);
|
||||
}
|
||||
|
||||
private UsersEntity toEntity(UsersDto dto) {
|
||||
return modelMapper.map(dto, UsersEntity.class);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<UsersDto> getAll() {
|
||||
return UsersService.getAll().stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public UsersDto get(@PathVariable(name = "id") Long id) {
|
||||
return toDto(UsersService.get(id));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public UsersDto create(@RequestBody @Valid UsersDto dto) {
|
||||
return toDto(UsersService.create(toEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public UsersDto update(@PathVariable(name = "id") Long id, @RequestBody UsersDto dto) {
|
||||
return toDto(UsersService.update(id, toEntity(dto)));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public UsersDto delete(@PathVariable(name = "id") Long id) {
|
||||
return toDto(UsersService.delete(id));
|
||||
}
|
||||
}
|
37
src/main/java/com/example/demo/itemUsers/api/UsersDto.java
Normal file
37
src/main/java/com/example/demo/itemUsers/api/UsersDto.java
Normal file
@ -0,0 +1,37 @@
|
||||
package com.example.demo.itemUsers.api;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
public class UsersDto {
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
private Long id;
|
||||
@NotBlank
|
||||
private String login;
|
||||
private String password;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
|
||||
public void setLogin(String login) {
|
||||
this.login = login;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
@ -18,10 +18,7 @@ public class UsersService {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public List<UsersEntity> getAll(Long typeId) {
|
||||
if (Objects.equals(typeId, 0L)) {
|
||||
return repository.getAll();
|
||||
}
|
||||
public List<UsersEntity> getAll() {
|
||||
return repository.getAll();
|
||||
}
|
||||
|
||||
|
@ -1,64 +0,0 @@
|
||||
package com.example.demo.types.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.types.model.TypeEntity;
|
||||
import com.example.demo.types.service.TypeService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL + "/type")
|
||||
public class TypeController {
|
||||
private final TypeService typeService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public TypeController(TypeService typeService, ModelMapper modelMapper) {
|
||||
this.typeService = typeService;
|
||||
this.modelMapper = modelMapper;
|
||||
}
|
||||
|
||||
private TypeDto toDto(TypeEntity entity) {
|
||||
return modelMapper.map(entity, TypeDto.class);
|
||||
}
|
||||
|
||||
private TypeEntity toEntity(TypeDto dto) {
|
||||
return modelMapper.map(dto, TypeEntity.class);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<TypeDto> getAll() {
|
||||
return typeService.getAll().stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public TypeDto get(@PathVariable(name = "id") Long id) {
|
||||
return toDto(typeService.get(id));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public TypeDto create(@RequestBody @Valid TypeDto dto) {
|
||||
return toDto(typeService.create(toEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public TypeDto update(@PathVariable(name = "id") Long id, @RequestBody TypeDto dto) {
|
||||
return toDto(typeService.update(id, toEntity(dto)));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public TypeDto delete(@PathVariable(name = "id") Long id) {
|
||||
return toDto(typeService.delete(id));
|
||||
}
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
package com.example.demo.types.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
|
||||
public class TypeEntity extends BaseEntity {
|
||||
private String name;
|
||||
|
||||
public TypeEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public TypeEntity(Long id, String name) {
|
||||
super(id);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null || getClass() != obj.getClass())
|
||||
return false;
|
||||
final TypeEntity other = (TypeEntity) obj;
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getName(), name);
|
||||
}
|
||||
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
package com.example.demo.types.repository;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.example.demo.core.repository.MapRepository;
|
||||
import com.example.demo.types.model.TypeEntity;
|
||||
|
||||
@Repository
|
||||
public class TypeRepository extends MapRepository<TypeEntity> {
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
package com.example.demo.types.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.types.model.TypeEntity;
|
||||
import com.example.demo.types.repository.TypeRepository;
|
||||
|
||||
@Service
|
||||
public class TypeService {
|
||||
private final TypeRepository repository;
|
||||
|
||||
public TypeService(TypeRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public List<TypeEntity> getAll() {
|
||||
return repository.getAll();
|
||||
}
|
||||
|
||||
public TypeEntity get(Long id) {
|
||||
return Optional.ofNullable(repository.get(id))
|
||||
.orElseThrow(() -> new NotFoundException(id));
|
||||
}
|
||||
|
||||
public TypeEntity create(TypeEntity entity) {
|
||||
return repository.create(entity);
|
||||
}
|
||||
|
||||
public TypeEntity update(Long id, TypeEntity entity) {
|
||||
final TypeEntity existsEntity = get(id);
|
||||
existsEntity.setName(entity.getName());
|
||||
return repository.update(existsEntity);
|
||||
}
|
||||
|
||||
public TypeEntity delete(Long id) {
|
||||
final TypeEntity existsEntity = get(id);
|
||||
return repository.delete(existsEntity);
|
||||
}
|
||||
}
|
60
src/test/java/com/example/demo/CategoriesServiceTests.java
Normal file
60
src/test/java/com/example/demo/CategoriesServiceTests.java
Normal file
@ -0,0 +1,60 @@
|
||||
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.itemCategories.service.CategoriesService;
|
||||
import com.example.demo.itemCategories.model.CategoriesEntity;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
class CategoriesServiceTests {
|
||||
@Autowired
|
||||
private CategoriesService CategoriesService;
|
||||
|
||||
@Test
|
||||
void getTest() {
|
||||
Assertions.assertThrows(NotFoundException.class, () -> CategoriesService.get(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTest() {
|
||||
CategoriesService.create(new CategoriesEntity(null, "Ноутбуки"));
|
||||
final CategoriesEntity last = CategoriesService.create(new CategoriesEntity(null, "Телефоны"));
|
||||
Assertions.assertEquals(3, CategoriesService.getAll().size());
|
||||
Assertions.assertEquals(last, CategoriesService.get(3L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void updateTest() {
|
||||
final String test = "TEST";
|
||||
final CategoriesEntity entity = CategoriesService.get(3L);
|
||||
final String oldName = entity.getName();
|
||||
final CategoriesEntity newEntity = CategoriesService.update(3L, new CategoriesEntity(1L, test));
|
||||
Assertions.assertEquals(3, CategoriesService.getAll().size());
|
||||
Assertions.assertEquals(newEntity, CategoriesService.get(3L));
|
||||
Assertions.assertEquals(test, newEntity.getName());
|
||||
Assertions.assertNotEquals(oldName, newEntity.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void deleteTest() {
|
||||
CategoriesService.delete(3L);
|
||||
Assertions.assertEquals(2, CategoriesService.getAll().size());
|
||||
final CategoriesEntity last = CategoriesService.get(2L);
|
||||
Assertions.assertEquals(2L, last.getId());
|
||||
|
||||
final CategoriesEntity newEntity = CategoriesService.create(new CategoriesEntity(null, "Игровая приставка"));
|
||||
Assertions.assertEquals(3, CategoriesService.getAll().size());
|
||||
Assertions.assertEquals(4L, newEntity.getId());
|
||||
}
|
||||
}
|
@ -1,61 +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.types.model.TypeEntity;
|
||||
import com.example.demo.types.service.TypeService;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
class TypeServiceTests {
|
||||
@Autowired
|
||||
private TypeService typeService;
|
||||
|
||||
@Test
|
||||
void getTest() {
|
||||
Assertions.assertThrows(NotFoundException.class, () -> typeService.get(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTest() {
|
||||
typeService.create(new TypeEntity(null, "Ноутбук"));
|
||||
typeService.create(new TypeEntity(null, "Телефон"));
|
||||
final TypeEntity last = typeService.create(new TypeEntity(null, "Игровая приставка"));
|
||||
Assertions.assertEquals(3, typeService.getAll().size());
|
||||
Assertions.assertEquals(last, typeService.get(3L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void updateTest() {
|
||||
final String test = "TEST";
|
||||
final TypeEntity entity = typeService.get(3L);
|
||||
final String oldName = entity.getName();
|
||||
final TypeEntity newEntity = typeService.update(3L, new TypeEntity(1L, test));
|
||||
Assertions.assertEquals(3, typeService.getAll().size());
|
||||
Assertions.assertEquals(newEntity, typeService.get(3L));
|
||||
Assertions.assertEquals(test, newEntity.getName());
|
||||
Assertions.assertNotEquals(oldName, newEntity.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void deleteTest() {
|
||||
typeService.delete(3L);
|
||||
Assertions.assertEquals(2, typeService.getAll().size());
|
||||
final TypeEntity last = typeService.get(2L);
|
||||
Assertions.assertEquals(2L, last.getId());
|
||||
|
||||
final TypeEntity newEntity = typeService.create(new TypeEntity(null, "Игровая приставка"));
|
||||
Assertions.assertEquals(3, typeService.getAll().size());
|
||||
Assertions.assertEquals(4L, newEntity.getId());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user