test
This commit is contained in:
parent
b6afa8355f
commit
7526d3fd8b
@ -11,28 +11,24 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||||||
|
|
||||||
import com.example.demo.itemCategories.model.CategoriesEntity;
|
import com.example.demo.itemCategories.model.CategoriesEntity;
|
||||||
import com.example.demo.itemCategories.service.CategoriesService;
|
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.model.ProductsEntity;
|
||||||
import com.example.demo.itemProducts.service.ProductsService;
|
import com.example.demo.itemProducts.service.ProductsService;
|
||||||
import com.example.demo.itemUsers.model.UsersEntity;
|
|
||||||
import com.example.demo.itemUsers.service.UsersService;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class DemoApplication implements CommandLineRunner {
|
public class DemoApplication implements CommandLineRunner {
|
||||||
private final Logger log = LoggerFactory.getLogger(DemoApplication.class);
|
private final Logger log = LoggerFactory.getLogger(DemoApplication.class);
|
||||||
|
|
||||||
private final UsersService userService;
|
//private final UsersService userService;
|
||||||
private final CategoriesService categoriesService;
|
private final CategoriesService categoriesService;
|
||||||
private final ProductsService productsService;
|
private final ProductsService productsService;
|
||||||
private final OrdersService ordersService;
|
//private final OrdersService ordersService;
|
||||||
|
|
||||||
public DemoApplication(UsersService userService, CategoriesService categoriesService, ProductsService productsService, OrdersService ordersService) {
|
public DemoApplication(CategoriesService categoriesService, ProductsService productsService) {
|
||||||
|
|
||||||
this.userService = userService;
|
//this.userService = userService;
|
||||||
this.categoriesService = categoriesService;
|
this.categoriesService = categoriesService;
|
||||||
this.productsService = productsService;
|
this.productsService = productsService;
|
||||||
this.ordersService = ordersService;
|
//this.ordersService = ordersService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@ -43,9 +39,9 @@ public class DemoApplication implements CommandLineRunner {
|
|||||||
public void run(String... args) throws Exception {
|
public void run(String... args) throws Exception {
|
||||||
if (args.length > 0 && Objects.equals("--populate", args[0])) {
|
if (args.length > 0 && Objects.equals("--populate", args[0])) {
|
||||||
|
|
||||||
log.info("Создание пользователей");
|
/*log.info("Создание пользователей");
|
||||||
final var user1 = userService.create(new UsersEntity(null, "Natalia", "1234"));
|
final var user1 = userService.create(new UsersEntity(null, "Natalia", "1234"));
|
||||||
final var user2 =userService.create(new UsersEntity(null, "Revengel", "4567"));
|
final var user2 =userService.create(new UsersEntity(null, "Revengel", "4567"));*/
|
||||||
|
|
||||||
log.info("Создание категорий");
|
log.info("Создание категорий");
|
||||||
final var category1 = categoriesService.create(new CategoriesEntity(null, "Ноутбуки"));
|
final var category1 = categoriesService.create(new CategoriesEntity(null, "Ноутбуки"));
|
||||||
@ -55,12 +51,13 @@ public class DemoApplication implements CommandLineRunner {
|
|||||||
final var product1 = productsService.create(new ProductsEntity(null, category1, "Lenovo IDEA PAD 13", 15232.00));
|
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 product2 = productsService.create(new ProductsEntity(null, category1, "Acer", 20300.00));
|
||||||
final var product3 = productsService.create(new ProductsEntity(null, category2, "Iphone 13", 150000.00));
|
final var product3 = productsService.create(new ProductsEntity(null, category2, "Iphone 13", 150000.00));
|
||||||
|
/*
|
||||||
log.info("Создание заказов");
|
log.info("Создание заказов");
|
||||||
ordersService.create(user1.getId(), new OrdersEntity(null, product1, 3, new SimpleDateFormat("09/04/2024")));
|
ordersService.create(user1.getId(), new OrdersEntity(null, product1, 3, new SimpleDateFormat("09/04/2024")));
|
||||||
ordersService.create(user1.getId(), new OrdersEntity(null, product2, 2, new SimpleDateFormat("09/04/2024")));
|
ordersService.create(user1.getId(), new OrdersEntity(null, product2, 2, new SimpleDateFormat("09/04/2024")));
|
||||||
ordersService.create(user2.getId(), new OrdersEntity(null, product3, 1, new SimpleDateFormat("07/04/2024")));
|
ordersService.create(user2.getId(), new OrdersEntity(null, product3, 1, new SimpleDateFormat("07/04/2024")));
|
||||||
ordersService.create(user2.getId(), new OrdersEntity(null, product1, 4, new SimpleDateFormat("07/04/2024")));
|
ordersService.create(user2.getId(), new OrdersEntity(null, product1, 4, new SimpleDateFormat("07/04/2024")));
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,69 +0,0 @@
|
|||||||
package com.example.demo.itemOrders.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.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 jakarta.validation.Valid;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@RequestMapping(Constants.API_URL + "/user/{user}/order")
|
|
||||||
public class OrdersController {
|
|
||||||
private final OrdersService ordersService;
|
|
||||||
private final ProductsService productsService;
|
|
||||||
private final ModelMapper modelMapper;
|
|
||||||
|
|
||||||
public OrdersController(OrdersService ordersService, ProductsService productsService, ModelMapper modelMapper) {
|
|
||||||
this.ordersService = ordersService;
|
|
||||||
this.productsService = productsService;
|
|
||||||
this.modelMapper = modelMapper;
|
|
||||||
}
|
|
||||||
|
|
||||||
private OrdersDto toDto(OrdersEntity entity) {
|
|
||||||
return modelMapper.map(entity, OrdersDto.class);
|
|
||||||
}
|
|
||||||
private OrdersEntity toEntity(OrdersDto dto) {
|
|
||||||
final OrdersEntity entity = modelMapper.map(dto, OrdersEntity.class);
|
|
||||||
entity.setProduct(productsService.get(dto.getProductId()));
|
|
||||||
return entity;
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping
|
|
||||||
public List<OrdersDto> getAll(@PathVariable(name = "user") Long userId, @RequestParam(name = "productId", defaultValue = "0") ProductsEntity product) {
|
|
||||||
return ordersService.getAll(userId, product.getId()).stream().map(this::toDto).toList();
|
|
||||||
}
|
|
||||||
@GetMapping("/{id}")
|
|
||||||
public OrdersDto get(@PathVariable(name = "user") Long userId, @PathVariable(name = "id") Long id) {
|
|
||||||
return toDto(ordersService.get(userId, id));
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping
|
|
||||||
public OrdersDto create(@PathVariable(name = "user") Long userId, @RequestBody @Valid OrdersDto dto) {
|
|
||||||
return toDto(ordersService.create(userId, toEntity(dto)));
|
|
||||||
}
|
|
||||||
|
|
||||||
@PutMapping("/{id}")
|
|
||||||
public OrdersDto update(@PathVariable(name = "user") Long userId, @PathVariable(name = "id") Long id, @RequestBody OrdersDto dto) {
|
|
||||||
return toDto(ordersService.update(userId, id, toEntity(dto)));
|
|
||||||
}
|
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
|
||||||
public OrdersDto delete(@PathVariable(name = "user") Long userId, @PathVariable(name = "id") Long id) {
|
|
||||||
return toDto(ordersService.delete(userId, id));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,60 +0,0 @@
|
|||||||
package com.example.demo.itemOrders.api;
|
|
||||||
|
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
|
|
||||||
import com.example.demo.itemProducts.model.ProductsEntity;
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
||||||
|
|
||||||
import jakarta.validation.constraints.Min;
|
|
||||||
import jakarta.validation.constraints.NotNull;
|
|
||||||
|
|
||||||
public class OrdersDto {
|
|
||||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
|
||||||
private Long id;
|
|
||||||
@NotNull
|
|
||||||
private ProductsEntity product;
|
|
||||||
@NotNull
|
|
||||||
@Min(1)
|
|
||||||
private Integer count;
|
|
||||||
@NotNull
|
|
||||||
private SimpleDateFormat date;
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(Long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ProductsEntity getProduct() {
|
|
||||||
return product;
|
|
||||||
}
|
|
||||||
public void setProduct(ProductsEntity product) {
|
|
||||||
this.product = product;
|
|
||||||
}
|
|
||||||
public Long getProductId() {
|
|
||||||
return product.getId();
|
|
||||||
}
|
|
||||||
public void setProductId(Long productId) {
|
|
||||||
productId = product.getId();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer getCount() {
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
public void setCount(Integer count) {
|
|
||||||
this.count = count;
|
|
||||||
}
|
|
||||||
public SimpleDateFormat getDate() {
|
|
||||||
return date;
|
|
||||||
}
|
|
||||||
public void setDate(SimpleDateFormat date) {
|
|
||||||
this.date = date;
|
|
||||||
}
|
|
||||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
|
||||||
public Double getSum() {
|
|
||||||
return count * product.getPrice();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
package com.example.demo.itemOrders.api;
|
|
||||||
|
|
||||||
public class OrdersGroupedDto {
|
|
||||||
private Long userId;
|
|
||||||
private Long totalSum;
|
|
||||||
private Integer totalCount;
|
|
||||||
|
|
||||||
public Long getUserId() {
|
|
||||||
return userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUserId(Long userId) {
|
|
||||||
this.userId = userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getTotalSum() {
|
|
||||||
return totalSum;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTotalSum(Long totalSum) {
|
|
||||||
this.totalSum = totalSum;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer getTotalCount() {
|
|
||||||
return totalCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTotalCount(Integer totalCount) {
|
|
||||||
this.totalCount = totalCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,94 +0,0 @@
|
|||||||
package com.example.demo.itemOrders.model;
|
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.Objects;
|
|
||||||
import com.example.demo.core.model.BaseEntity;
|
|
||||||
import com.example.demo.itemProducts.model.ProductsEntity;
|
|
||||||
import com.example.demo.itemUsers.model.UsersEntity;
|
|
||||||
|
|
||||||
import jakarta.persistence.Column;
|
|
||||||
import jakarta.persistence.Entity;
|
|
||||||
import jakarta.persistence.JoinColumn;
|
|
||||||
import jakarta.persistence.ManyToOne;
|
|
||||||
import jakarta.persistence.Table;
|
|
||||||
import jakarta.persistence.Temporal;
|
|
||||||
import jakarta.persistence.TemporalType;
|
|
||||||
|
|
||||||
@Entity
|
|
||||||
@Table(name = "orders")
|
|
||||||
public class OrdersEntity extends BaseEntity {
|
|
||||||
@ManyToOne
|
|
||||||
@JoinColumn(name = "productId", nullable = false)
|
|
||||||
private ProductsEntity product;
|
|
||||||
|
|
||||||
@ManyToOne
|
|
||||||
@JoinColumn(name = "userId", nullable = false)
|
|
||||||
private UsersEntity user;
|
|
||||||
|
|
||||||
@Column(nullable = false)
|
|
||||||
private Integer count;
|
|
||||||
|
|
||||||
@Temporal(TemporalType.DATE)
|
|
||||||
private SimpleDateFormat date;
|
|
||||||
|
|
||||||
@Column(nullable = false)
|
|
||||||
private Double sum;
|
|
||||||
|
|
||||||
public OrdersEntity() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
public OrdersEntity(Long id, ProductsEntity product, Integer count, SimpleDateFormat date) {
|
|
||||||
super(id);
|
|
||||||
this.product = product;
|
|
||||||
this.count = count;
|
|
||||||
this.date = date;
|
|
||||||
}
|
|
||||||
public ProductsEntity getProduct() {
|
|
||||||
return product;
|
|
||||||
}
|
|
||||||
public void setProduct(ProductsEntity product) {
|
|
||||||
this.product = product;
|
|
||||||
}
|
|
||||||
public UsersEntity getUser() {
|
|
||||||
return user;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUser(UsersEntity user) {
|
|
||||||
this.user = user;
|
|
||||||
if (!user.getOrders().contains(this)) {
|
|
||||||
user.getOrders().add(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public Double getSum() {
|
|
||||||
sum = count * product.getPrice();
|
|
||||||
return sum;
|
|
||||||
}
|
|
||||||
public Integer getCount() {
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
public void setCount(Integer count) {
|
|
||||||
this.count = count;
|
|
||||||
}
|
|
||||||
public SimpleDateFormat getDate() {
|
|
||||||
return date;
|
|
||||||
}
|
|
||||||
public void setDate(SimpleDateFormat date) {
|
|
||||||
this.date = date;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return Objects.hash(id, product, count, date);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if (this == obj)
|
|
||||||
return true;
|
|
||||||
if (obj == null || getClass() != obj.getClass())
|
|
||||||
return false;
|
|
||||||
final OrdersEntity other = (OrdersEntity) obj;
|
|
||||||
return Objects.equals(other.getId(), id)
|
|
||||||
&& Objects.equals(other.getProduct(), product)
|
|
||||||
&& Objects.equals(other.getCount(), count)
|
|
||||||
&& Objects.equals(other.getDate(), date);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
package com.example.demo.itemOrders.model;
|
|
||||||
|
|
||||||
import com.example.demo.itemUsers.model.UsersEntity;
|
|
||||||
|
|
||||||
public interface OrdersGrouped {
|
|
||||||
UsersEntity getUser();
|
|
||||||
|
|
||||||
double getTotalSum();
|
|
||||||
|
|
||||||
int getTotalCount();
|
|
||||||
}
|
|
@ -1,31 +0,0 @@
|
|||||||
package com.example.demo.itemOrders.repository;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.Query;
|
|
||||||
import org.springframework.data.repository.CrudRepository;
|
|
||||||
import com.example.demo.itemOrders.model.OrdersEntity;
|
|
||||||
import com.example.demo.itemOrders.model.OrdersGrouped;
|
|
||||||
|
|
||||||
public interface OrdersRepository extends CrudRepository<OrdersEntity, Long> {
|
|
||||||
Optional<OrdersEntity> findOneByUserIdAndId(long userId, long id);
|
|
||||||
|
|
||||||
List<OrdersEntity> findByUserId(long userId);
|
|
||||||
|
|
||||||
List<OrdersEntity> findByUserIdAndProductId(long userId, long productId);
|
|
||||||
|
|
||||||
// select
|
|
||||||
// tpe.name,
|
|
||||||
// coalesce(sum(order.price), 0),
|
|
||||||
// coalesce(sum(order.count), 0)
|
|
||||||
// from types as tpe
|
|
||||||
// left join orders as order on tpe.id = order.type_id and order.user_id = ?
|
|
||||||
// group by tpe.name order by tpe.id
|
|
||||||
@Query("select "
|
|
||||||
+ "u as user, "
|
|
||||||
+ "coalesce(sum(o.sum), 0) as totalSum, "
|
|
||||||
+ "coalesce(sum(o.count), 0) as totalCount "
|
|
||||||
+ "from UsersEntity u left join OrderEntity o on o.user = u and o.user.id = ?1 "
|
|
||||||
+ "group by u order by u.id")
|
|
||||||
List<OrdersGrouped> getOrdersTotalByUser(long userId);
|
|
||||||
}
|
|
@ -1,70 +0,0 @@
|
|||||||
package com.example.demo.itemOrders.service;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
|
|
||||||
import com.example.demo.core.error.NotFoundException;
|
|
||||||
import com.example.demo.itemOrders.model.OrdersEntity;
|
|
||||||
import com.example.demo.itemOrders.model.OrdersGrouped;
|
|
||||||
import com.example.demo.itemOrders.repository.OrdersRepository;
|
|
||||||
import com.example.demo.itemUsers.model.UsersEntity;
|
|
||||||
import com.example.demo.itemUsers.service.UsersService;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class OrdersService {
|
|
||||||
private final OrdersRepository repository;
|
|
||||||
private final UsersService userService;
|
|
||||||
|
|
||||||
public OrdersService(OrdersRepository repository, UsersService userService) {
|
|
||||||
this.repository = repository;
|
|
||||||
this.userService = userService;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
|
||||||
public List<OrdersEntity> getAll(long userId, Long productId) {
|
|
||||||
userService.get(userId);
|
|
||||||
if (productId <= 0L) {
|
|
||||||
return repository.findByUserId(userId);
|
|
||||||
} else {
|
|
||||||
return repository.findByUserIdAndProductId(userId, productId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@Transactional(readOnly = true)
|
|
||||||
public OrdersEntity get(long userId, Long id) {
|
|
||||||
userService.get(userId);
|
|
||||||
return repository.findOneByUserIdAndId(userId, id).orElseThrow(() -> new NotFoundException(OrdersEntity.class, id));
|
|
||||||
}
|
|
||||||
|
|
||||||
public OrdersEntity create(long userId, OrdersEntity entity) {
|
|
||||||
if (entity == null) {
|
|
||||||
throw new IllegalArgumentException("Entity is null");
|
|
||||||
}
|
|
||||||
final UsersEntity existsUser = userService.get(userId);
|
|
||||||
entity.setUser(existsUser);
|
|
||||||
return repository.save(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public OrdersEntity update(long userId, Long id, OrdersEntity entity) {
|
|
||||||
userService.get(userId);
|
|
||||||
final OrdersEntity existsEntity = get(userId, id);
|
|
||||||
existsEntity.setProduct(entity.getProduct());
|
|
||||||
existsEntity.setCount(entity.getCount());
|
|
||||||
existsEntity.setDate(entity.getDate());
|
|
||||||
return repository.save(existsEntity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public OrdersEntity delete(long userId, Long id) {
|
|
||||||
userService.get(userId);
|
|
||||||
final OrdersEntity existsEntity = get(userId, id);
|
|
||||||
repository.delete(existsEntity);
|
|
||||||
return existsEntity;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
|
||||||
public List<OrdersGrouped> getTotal(long userId) {
|
|
||||||
userService.get(userId);
|
|
||||||
return repository.getOrdersTotalByUser(userId);
|
|
||||||
}
|
|
||||||
}
|
|
@ -13,7 +13,7 @@ public class ProductsDto {
|
|||||||
private String name;
|
private String name;
|
||||||
@NotNull
|
@NotNull
|
||||||
@Min(1)
|
@Min(1)
|
||||||
private Long typeId;
|
private Long categoryId;
|
||||||
@NotNull
|
@NotNull
|
||||||
@Min(1)
|
@Min(1)
|
||||||
private Double price;
|
private Double price;
|
||||||
@ -27,11 +27,11 @@ public class ProductsDto {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Long getTypeId() {
|
public Long getTypeId() {
|
||||||
return typeId;
|
return categoryId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTypeId(Long typeId) {
|
public void setTypeId(Long categoryId) {
|
||||||
this.typeId = typeId;
|
this.categoryId = categoryId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Double getPrice() {
|
public Double getPrice() {
|
||||||
|
@ -16,7 +16,7 @@ import jakarta.persistence.Table;
|
|||||||
public class ProductsEntity extends BaseEntity{
|
public class ProductsEntity extends BaseEntity{
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JoinColumn(name = "categoryId", nullable = false)
|
@JoinColumn(name = "categoryId", nullable = false)
|
||||||
private CategoriesEntity type;
|
private CategoriesEntity category;
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private String name;
|
private String name;
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
@ -26,19 +26,19 @@ public class ProductsEntity extends BaseEntity{
|
|||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProductsEntity(Long id, CategoriesEntity type, String name, Double price) {
|
public ProductsEntity(Long id, CategoriesEntity category, String name, Double price) {
|
||||||
super(id);
|
super(id);
|
||||||
this.type = type;
|
this.category = category;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.price = price;
|
this.price = price;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CategoriesEntity getType() {
|
public CategoriesEntity getType() {
|
||||||
return type;
|
return category;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setType(CategoriesEntity type) {
|
public void setType(CategoriesEntity category) {
|
||||||
this.type = type;
|
this.category = category;
|
||||||
}
|
}
|
||||||
public Double getPrice() {
|
public Double getPrice() {
|
||||||
return price;
|
return price;
|
||||||
@ -55,7 +55,7 @@ public class ProductsEntity extends BaseEntity{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(id, type, name, price);
|
return Objects.hash(id, category, name, price);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -66,7 +66,7 @@ public class ProductsEntity extends BaseEntity{
|
|||||||
return false;
|
return false;
|
||||||
final ProductsEntity other = (ProductsEntity) obj;
|
final ProductsEntity other = (ProductsEntity) obj;
|
||||||
return Objects.equals(other.getId(), id)
|
return Objects.equals(other.getId(), id)
|
||||||
&& Objects.equals(other.getType(), type)
|
&& Objects.equals(other.getType(), category)
|
||||||
&& Objects.equals(other.getName(), name)
|
&& Objects.equals(other.getName(), name)
|
||||||
&& Objects.equals(other.getPrice(), price);
|
&& Objects.equals(other.getPrice(), price);
|
||||||
}
|
}
|
||||||
|
@ -1,64 +0,0 @@
|
|||||||
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));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,37 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,80 +0,0 @@
|
|||||||
package com.example.demo.itemUsers.model;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import com.example.demo.core.model.BaseEntity;
|
|
||||||
import com.example.demo.itemOrders.model.OrdersEntity;
|
|
||||||
|
|
||||||
import jakarta.persistence.CascadeType;
|
|
||||||
import jakarta.persistence.Column;
|
|
||||||
import jakarta.persistence.Entity;
|
|
||||||
import jakarta.persistence.OneToMany;
|
|
||||||
import jakarta.persistence.OrderBy;
|
|
||||||
import jakarta.persistence.Table;
|
|
||||||
|
|
||||||
@Entity
|
|
||||||
@Table(name = "users")
|
|
||||||
public class UsersEntity extends BaseEntity {
|
|
||||||
@Column(nullable = false, unique = true, length = 20)
|
|
||||||
private String login;
|
|
||||||
|
|
||||||
@Column(nullable = false, length = 20)
|
|
||||||
private String password;
|
|
||||||
|
|
||||||
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
|
|
||||||
@OrderBy("id ASC")
|
|
||||||
private Set<OrdersEntity> orders = new HashSet<>();
|
|
||||||
|
|
||||||
public UsersEntity() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
public UsersEntity(Long id, String login, String password) {
|
|
||||||
super(id);
|
|
||||||
this.login = login;
|
|
||||||
this.password = password;
|
|
||||||
}
|
|
||||||
public Set<OrdersEntity> getOrders() {
|
|
||||||
return orders;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addOrder(OrdersEntity order) {
|
|
||||||
if (order.getUser() != this) {
|
|
||||||
order.setUser(this);
|
|
||||||
}
|
|
||||||
orders.add(order);
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return Objects.hash(id, login, password);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if (this == obj)
|
|
||||||
return true;
|
|
||||||
if (obj == null || getClass() != obj.getClass())
|
|
||||||
return false;
|
|
||||||
final UsersEntity other = (UsersEntity) obj;
|
|
||||||
return Objects.equals(other.getId(), id)
|
|
||||||
&& Objects.equals(other.getLogin(), login)
|
|
||||||
&& Objects.equals(other.getPassword(), password);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
package com.example.demo.itemUsers.repository;
|
|
||||||
|
|
||||||
import org.springframework.data.repository.CrudRepository;
|
|
||||||
|
|
||||||
import com.example.demo.itemUsers.model.UsersEntity;
|
|
||||||
|
|
||||||
public interface UsersRepository extends CrudRepository<UsersEntity, Long> {
|
|
||||||
}
|
|
@ -1,48 +0,0 @@
|
|||||||
package com.example.demo.itemUsers.service;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.StreamSupport;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
|
|
||||||
import com.example.demo.core.error.NotFoundException;
|
|
||||||
import com.example.demo.itemUsers.model.UsersEntity;
|
|
||||||
import com.example.demo.itemUsers.repository.UsersRepository;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class UsersService {
|
|
||||||
private final UsersRepository repository;
|
|
||||||
|
|
||||||
public UsersService(UsersRepository repository) {
|
|
||||||
this.repository = repository;
|
|
||||||
}
|
|
||||||
@Transactional(readOnly = true)
|
|
||||||
public List<UsersEntity> getAll() {
|
|
||||||
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
|
|
||||||
}
|
|
||||||
@Transactional(readOnly = true)
|
|
||||||
public UsersEntity get(Long id) {
|
|
||||||
return repository.findById(id).orElseThrow(() -> new NotFoundException(UsersEntity.class, id));
|
|
||||||
}
|
|
||||||
@Transactional
|
|
||||||
public UsersEntity create(UsersEntity entity) {
|
|
||||||
if (entity == null) {
|
|
||||||
throw new IllegalArgumentException("Entity is null");
|
|
||||||
}
|
|
||||||
return repository.save(entity);
|
|
||||||
}
|
|
||||||
@Transactional
|
|
||||||
public UsersEntity update(Long id, UsersEntity entity) {
|
|
||||||
final UsersEntity existsEntity = get(id);
|
|
||||||
existsEntity.setLogin(entity.getLogin());
|
|
||||||
existsEntity.setPassword(entity.getPassword());
|
|
||||||
return repository.save(existsEntity);
|
|
||||||
}
|
|
||||||
@Transactional
|
|
||||||
public UsersEntity delete(Long id) {
|
|
||||||
final UsersEntity existsEntity = get(id);
|
|
||||||
repository.delete(existsEntity);
|
|
||||||
return existsEntity;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,92 +0,0 @@
|
|||||||
package com.example.demo;
|
|
||||||
|
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
|
|
||||||
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.model.CategoriesEntity;
|
|
||||||
import com.example.demo.itemCategories.service.CategoriesService;
|
|
||||||
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.itemOrders.model.OrdersEntity;
|
|
||||||
|
|
||||||
@SpringBootTest
|
|
||||||
@TestMethodOrder(OrderAnnotation.class)
|
|
||||||
class OrdersServiceTests {
|
|
||||||
@Autowired
|
|
||||||
private OrdersService ordersService;
|
|
||||||
@Autowired
|
|
||||||
private UsersService usersService;
|
|
||||||
private UsersEntity user1;
|
|
||||||
private UsersEntity user2;
|
|
||||||
@Autowired
|
|
||||||
private CategoriesService categoriesService;
|
|
||||||
private CategoriesEntity category1;
|
|
||||||
private CategoriesEntity category2;
|
|
||||||
@Autowired
|
|
||||||
private ProductsService productsService;
|
|
||||||
private ProductsEntity product1;
|
|
||||||
private ProductsEntity product2;
|
|
||||||
private ProductsEntity product3;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getTest() {
|
|
||||||
Assertions.assertThrows(NotFoundException.class, () -> ordersService.get(1,0L));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(1)
|
|
||||||
void createTest() {
|
|
||||||
|
|
||||||
user1 = usersService.create(new UsersEntity(null, "Natalia", "1234"));
|
|
||||||
user2 = usersService.create(new UsersEntity(null, "Revengel", "4567"));
|
|
||||||
|
|
||||||
category1 = categoriesService.create(new CategoriesEntity(null, "Телефон"));
|
|
||||||
category2 = categoriesService.create(new CategoriesEntity(null, "Игровая приставка"));
|
|
||||||
|
|
||||||
product1 = productsService.create(new ProductsEntity(null, category1, "Lenovo IDEA PAD 13", 15232.00));
|
|
||||||
product2 = productsService.create(new ProductsEntity(null, category1, "Acer", 20300.00));
|
|
||||||
product3 = productsService.create(new ProductsEntity(null, category2, "Iphone 13", 150000.00));
|
|
||||||
|
|
||||||
ordersService.create(user1.getId(), new OrdersEntity(null, product1, 3, new SimpleDateFormat("09/04/2024")));
|
|
||||||
ordersService.create(user1.getId(), new OrdersEntity(null, product2, 2, new SimpleDateFormat("09/04/2024")));
|
|
||||||
ordersService.create(user2.getId(),new OrdersEntity(null, product3, 1, new SimpleDateFormat("07/04/2024")));
|
|
||||||
final OrdersEntity last = ordersService.create(user2.getId(), new OrdersEntity(null, product1, 4, new SimpleDateFormat("07/04/2024")));
|
|
||||||
Assertions.assertEquals(4, ordersService.getAll(1,0L).size());
|
|
||||||
Assertions.assertEquals(0, ordersService.getAll(1,-1L).size());
|
|
||||||
Assertions.assertEquals(last, ordersService.get(1,last.getId()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(2)
|
|
||||||
void updateTest() {
|
|
||||||
final Integer newCount = 5;
|
|
||||||
final OrdersEntity entity = ordersService.get(1,1L);
|
|
||||||
final Integer oldCount = entity.getCount();
|
|
||||||
final OrdersEntity newEntity = ordersService.update(1,1L,
|
|
||||||
new OrdersEntity(null, product1, newCount, new SimpleDateFormat("09/04/2024")));
|
|
||||||
Assertions.assertEquals(4, ordersService.getAll(1,0L).size());
|
|
||||||
Assertions.assertEquals(newEntity, ordersService.get(1,1L));
|
|
||||||
Assertions.assertEquals(newCount, newEntity.getCount());
|
|
||||||
Assertions.assertNotEquals(oldCount, newEntity.getCount());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(3)
|
|
||||||
void deleteTest() {
|
|
||||||
ordersService.delete(2,3L);
|
|
||||||
Assertions.assertEquals(3, ordersService.getAll(1,0L).size());
|
|
||||||
final OrdersEntity last = ordersService.get(1,2L);
|
|
||||||
Assertions.assertEquals(2L, last.getId());
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,57 +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.itemUsers.service.UsersService;
|
|
||||||
import com.example.demo.itemUsers.model.UsersEntity;
|
|
||||||
|
|
||||||
@SpringBootTest
|
|
||||||
@TestMethodOrder(OrderAnnotation.class)
|
|
||||||
class UsersServiceTests {
|
|
||||||
@Autowired
|
|
||||||
private UsersService usersService;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getTest() {
|
|
||||||
Assertions.assertThrows(NotFoundException.class, () -> usersService.get(0L));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(1)
|
|
||||||
void createTest() {
|
|
||||||
usersService.create(new UsersEntity(null, "Natalia", "1234"));
|
|
||||||
final UsersEntity last = usersService.create(new UsersEntity(null, "Revengel", "4567"));
|
|
||||||
Assertions.assertEquals(2, usersService.getAll().size());
|
|
||||||
Assertions.assertEquals(last, usersService.get(2L));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(2)
|
|
||||||
void updateTest() {
|
|
||||||
final String newPassword = "0000";
|
|
||||||
final UsersEntity entity = usersService.get(2L);
|
|
||||||
final String login = entity.getLogin();
|
|
||||||
final String oldPassword = entity.getPassword();
|
|
||||||
final UsersEntity newEntity = usersService.update(2L, new UsersEntity(1L, login, newPassword));
|
|
||||||
Assertions.assertEquals(2, usersService.getAll().size());
|
|
||||||
Assertions.assertEquals(newEntity, usersService.get(2L));
|
|
||||||
Assertions.assertEquals(newPassword, newEntity.getPassword());
|
|
||||||
Assertions.assertNotEquals(oldPassword, newEntity.getPassword());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(3)
|
|
||||||
void deleteTest() {
|
|
||||||
usersService.delete(2L);
|
|
||||||
Assertions.assertEquals(1, usersService.getAll().size());
|
|
||||||
final UsersEntity last = usersService.get(1L);
|
|
||||||
Assertions.assertEquals(1L, last.getId());
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user