обновление 3
This commit is contained in:
parent
5f9c0f7aa3
commit
3a6a49598c
Binary file not shown.
@ -6,55 +6,29 @@ import org.modelmapper.ModelMapper;
|
||||
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.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.example.qpickshop.core.configuration.Constants;
|
||||
import com.example.qpickshop.orders.model.OrderEntity;
|
||||
import com.example.qpickshop.orders.model.OrderItemEntity;
|
||||
import com.example.qpickshop.orders.service.OrderService;
|
||||
import com.example.qpickshop.products.service.ProductService;
|
||||
import com.example.qpickshop.users.service.UserService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL + "/user/{user}/order")
|
||||
public class OrderController {
|
||||
private final OrderService orderService;
|
||||
private final ProductService productService;
|
||||
private final UserService userService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public OrderController(OrderService orderService, UserService userService, ProductService productService,
|
||||
public OrderController(OrderService orderService,
|
||||
ModelMapper modelMapper) {
|
||||
this.orderService = orderService;
|
||||
this.productService = productService;
|
||||
this.userService = userService;
|
||||
this.modelMapper = modelMapper;
|
||||
}
|
||||
|
||||
private OrderItemEntity getOrderItemEntity(OrderItemDto dto) {
|
||||
OrderItemEntity orderItem = modelMapper.map(dto, OrderItemEntity.class);
|
||||
orderItem.setProduct(productService.get(orderItem.getProduct().getId()));
|
||||
return orderItem;
|
||||
}
|
||||
|
||||
private List<OrderItemEntity> getOrderItems(OrderDto dto) {
|
||||
return dto.getOrderItems().stream().map(this::getOrderItemEntity).toList();
|
||||
}
|
||||
|
||||
private OrderDto toDto(OrderEntity entity) {
|
||||
return modelMapper.map(entity, OrderDto.class);
|
||||
}
|
||||
|
||||
private OrderEntity toEntity(OrderDto dto) {
|
||||
OrderEntity entity = modelMapper.map(dto, OrderEntity.class);
|
||||
entity.setItems(getOrderItems(dto));
|
||||
return entity;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<OrderDto> getAll(
|
||||
@PathVariable(name = "user") Long userId) {
|
||||
@ -70,7 +44,7 @@ public class OrderController {
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public OrderDto create(@PathVariable(name = "user") Long userId, @RequestBody @Valid OrderDto dto) {
|
||||
return toDto(orderService.create(toEntity(dto), userId));
|
||||
public OrderDto create(@PathVariable(name = "user") Long userId) {
|
||||
return toDto(orderService.create(userId));
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.example.qpickshop.core.error.NotFoundException;
|
||||
import com.example.qpickshop.orders.model.OrderEntity;
|
||||
import com.example.qpickshop.orders.model.OrderItemEntity;
|
||||
import com.example.qpickshop.orders.repository.OrderItemRepository;
|
||||
|
||||
@ -31,14 +30,6 @@ public class OrderItemService {
|
||||
|
||||
@Transactional
|
||||
public List<OrderItemEntity> createMany(List<OrderItemEntity> orderItems) {
|
||||
// if (order == null) {
|
||||
// throw new IllegalArgumentException("Order is null");
|
||||
// }
|
||||
|
||||
// for (int i = 0; i < orderItems.size(); i++) {
|
||||
// orderItems.get(i).setOrder(order);
|
||||
// }
|
||||
|
||||
repository.saveAll(orderItems);
|
||||
return orderItems;
|
||||
}
|
||||
|
@ -1,14 +1,16 @@
|
||||
package com.example.qpickshop.orders.service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.example.qpickshop.cartItems.model.CartItemEntity;
|
||||
import com.example.qpickshop.cartItems.service.CartItemService;
|
||||
import com.example.qpickshop.core.error.NotFoundException;
|
||||
import com.example.qpickshop.orders.model.OrderEntity;
|
||||
import com.example.qpickshop.orders.model.OrderItemEntity;
|
||||
import com.example.qpickshop.orders.repository.OrderRepository;
|
||||
import com.example.qpickshop.users.service.UserService;
|
||||
|
||||
@ -41,16 +43,19 @@ public class OrderService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public OrderEntity create(OrderEntity entity, Long userId) {
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException("Entity is null");
|
||||
public OrderEntity create(Long userId) {
|
||||
OrderEntity entity = new OrderEntity(userService.get(userId));
|
||||
|
||||
List<CartItemEntity> cartItems = cartItemService.getAll(userId);
|
||||
List<OrderItemEntity> orderItems = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < cartItems.size(); i++) {
|
||||
OrderItemEntity oie = new OrderItemEntity(cartItems.get(i).getProduct(), cartItems.get(i).getQuantity());
|
||||
oie.setOrder(entity);
|
||||
orderItems.add(oie);
|
||||
}
|
||||
|
||||
entity.setUser(userService.get(userId));
|
||||
|
||||
for (int i = 0; i < entity.getItems().size(); i++) {
|
||||
entity.getItems().get(i).setOrder(entity);
|
||||
}
|
||||
entity.setItems(orderItems);
|
||||
|
||||
OrderEntity order = repository.save(entity);
|
||||
orderItemService.createMany(order.getItems());
|
||||
|
@ -1,72 +1,75 @@
|
||||
// package com.example.qpickshop;
|
||||
package com.example.qpickshop;
|
||||
|
||||
// import java.util.ArrayList;
|
||||
// import java.util.List;
|
||||
import java.util.List;
|
||||
|
||||
// import org.junit.jupiter.api.AfterEach;
|
||||
// import org.junit.jupiter.api.Assertions;
|
||||
// import org.junit.jupiter.api.BeforeEach;
|
||||
// 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 org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
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.qpickshop.categories.model.CategoryEntity;
|
||||
// import com.example.qpickshop.categories.service.CategoryService;
|
||||
// import com.example.qpickshop.core.error.NotFoundException;
|
||||
// import com.example.qpickshop.orders.model.OrderEntity;
|
||||
// import com.example.qpickshop.orders.model.OrderItemEntity;
|
||||
// import com.example.qpickshop.orders.service.OrderItemService;
|
||||
// import com.example.qpickshop.orders.service.OrderService;
|
||||
// import com.example.qpickshop.products.model.ProductEntity;
|
||||
// import com.example.qpickshop.products.service.ProductService;
|
||||
// import com.example.qpickshop.users.service.UserService;
|
||||
import com.example.qpickshop.categories.model.CategoryEntity;
|
||||
import com.example.qpickshop.categories.service.CategoryService;
|
||||
import com.example.qpickshop.core.error.NotFoundException;
|
||||
import com.example.qpickshop.orders.model.OrderEntity;
|
||||
import com.example.qpickshop.orders.model.OrderItemEntity;
|
||||
import com.example.qpickshop.orders.service.OrderItemService;
|
||||
import com.example.qpickshop.orders.service.OrderService;
|
||||
import com.example.qpickshop.products.model.ProductEntity;
|
||||
import com.example.qpickshop.products.service.ProductService;
|
||||
import com.example.qpickshop.users.service.UserService;
|
||||
|
||||
// import com.example.qpickshop.users.model.UserEntity;
|
||||
// @SpringBootTest
|
||||
// @TestMethodOrder(OrderAnnotation.class)
|
||||
// public class OrderItemServiceTests {
|
||||
// @Autowired
|
||||
// private UserService userService;
|
||||
// @Autowired
|
||||
// private ProductService productService;
|
||||
// @Autowired
|
||||
// private CategoryService categoryService;
|
||||
// @Autowired
|
||||
// private OrderService orderService;
|
||||
// @Autowired
|
||||
// private OrderItemService orderItemService;
|
||||
import com.example.qpickshop.users.model.UserEntity;
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
public class OrderItemServiceTests {
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
@Autowired
|
||||
private ProductService productService;
|
||||
@Autowired
|
||||
private CategoryService categoryService;
|
||||
@Autowired
|
||||
private OrderService orderService;
|
||||
@Autowired
|
||||
private OrderItemService orderItemService;
|
||||
|
||||
// private OrderEntity order;
|
||||
// private OrderEntity order2;
|
||||
// private OrderEntity order3;
|
||||
private OrderEntity order;
|
||||
private OrderEntity order2;
|
||||
private OrderEntity order3;
|
||||
|
||||
// @Test
|
||||
// void getTest() {
|
||||
// Assertions.assertThrows(NotFoundException.class, () -> orderItemService.get(0L));
|
||||
// }
|
||||
@Test
|
||||
void getTest() {
|
||||
Assertions.assertThrows(NotFoundException.class, () -> orderItemService.get(0L));
|
||||
}
|
||||
|
||||
// @Test
|
||||
// @Order(1)
|
||||
// void createTest() {
|
||||
// CategoryEntity category = categoryService.create(new CategoryEntity("category"));
|
||||
// ProductEntity product = productService.create(new ProductEntity("name3", category, "description3", 11.1, 1.1));
|
||||
// ProductEntity product2 = productService.create(new ProductEntity("name2", category, "description2", 11.1, 1.1));
|
||||
@Test
|
||||
void createTest() {
|
||||
CategoryEntity category = categoryService.create(new CategoryEntity("testcategory"));
|
||||
ProductEntity product = productService.create(new ProductEntity("testproductin", category, "description3", 11.1, 1.1));
|
||||
ProductEntity product2 = productService.create(new ProductEntity("testproductin2", category, "description2", 11.1, 1.1));
|
||||
|
||||
// UserEntity user = userService.create(new UserEntity("email", "password"));
|
||||
UserEntity user = userService.create(new UserEntity("emailll", "password"));
|
||||
|
||||
// order = orderService.create(new OrderEntity(user), user.getId());
|
||||
// order2 = orderService.create(new OrderEntity(user), user.getId());
|
||||
// order3 = orderService.create(new OrderEntity(user), user.getId());
|
||||
order = orderService.create(user.getId());
|
||||
order2 = orderService.create(user.getId());
|
||||
order3 = orderService.create(user.getId());
|
||||
|
||||
// orderItemService.createMany(List.of(new OrderItemEntity(product, 3), new OrderItemEntity(product2, 5)), order);
|
||||
// orderItemService.createMany(List.of(new OrderItemEntity(product, 8)), order2);
|
||||
// orderItemService.createMany(new ArrayList<>(), order2);
|
||||
|
||||
// Assertions.assertEquals(2, orderItemService.getAll(order.getId()).size());
|
||||
// Assertions.assertEquals(1, orderItemService.getAll(order2.getId()).size());
|
||||
// Assertions.assertEquals(0, orderItemService.getAll(order3.getId()).size());
|
||||
// }
|
||||
// }
|
||||
OrderItemEntity oie = new OrderItemEntity(product, 3);
|
||||
OrderItemEntity oie2 = new OrderItemEntity(product2, 5);
|
||||
OrderItemEntity oie3 = new OrderItemEntity(product, 8);
|
||||
oie.setOrder(order);
|
||||
oie2.setOrder(order);
|
||||
|
||||
oie3.setOrder(order2);
|
||||
|
||||
orderItemService.createMany(List.of(oie, oie2));
|
||||
orderItemService.createMany(List.of(oie3));
|
||||
|
||||
Assertions.assertEquals(2, orderItemService.getAll(order.getId()).size());
|
||||
Assertions.assertEquals(1, orderItemService.getAll(order2.getId()).size());
|
||||
Assertions.assertEquals(0, orderItemService.getAll(order3.getId()).size());
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
package com.example.qpickshop;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@ -13,7 +11,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import com.example.qpickshop.core.error.NotFoundException;
|
||||
import com.example.qpickshop.orders.model.OrderEntity;
|
||||
import com.example.qpickshop.orders.service.OrderService;
|
||||
import com.example.qpickshop.users.model.UserEntity;
|
||||
import com.example.qpickshop.users.service.UserService;
|
||||
@ -35,9 +32,9 @@ public class OrderServiceTests {
|
||||
user = userService.create(new UserEntity("email", "password"));
|
||||
user2 = userService.create(new UserEntity("email2", "password2"));
|
||||
|
||||
List.of(
|
||||
new OrderEntity(user), new OrderEntity(user), new OrderEntity(user),
|
||||
new OrderEntity(user)).forEach(item -> orderService.create(item, user.getId()));
|
||||
for (int i = 0; i < 4; i++) {
|
||||
orderService.create(user.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
|
Loading…
Reference in New Issue
Block a user