половина работы
This commit is contained in:
parent
cc1a023999
commit
b8cedd8ec3
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
@ -67,8 +67,8 @@ public class DemoApplication implements CommandLineRunner {
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
List<OrderLineEntity> lines = new ArrayList();
|
||||
lines.add(new OrderLineEntity(null, 3));
|
||||
final var user1 = userService.create(new UserEntity("Alex", "Kryukov", "akryu@mail.ru", "password"));
|
||||
orderService.create(new OrderEntity(user1, null));
|
||||
final var user1 = userService.create(new UserEntity("Misha", "Kryukov", "akryu132@mail.ru", "password"));
|
||||
orderService.create(new OrderEntity(user1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package com.example.demo.core.model;
|
||||
import com.example.demo.core.configuration.Constants;
|
||||
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
import jakarta.persistence.SequenceGenerator;
|
||||
@ -11,7 +10,7 @@ import jakarta.persistence.SequenceGenerator;
|
||||
@MappedSuperclass
|
||||
public abstract class BaseEntity {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = Constants.SEQUENCE_NAME)
|
||||
@GeneratedValue()
|
||||
@SequenceGenerator(name = Constants.SEQUENCE_NAME, sequenceName = Constants.SEQUENCE_NAME, allocationSize = 1)
|
||||
protected Long id;
|
||||
|
||||
|
@ -3,14 +3,13 @@ package com.example.demo.order_lines.api;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class OrderLineDto {
|
||||
@NotNull
|
||||
@Min(1)
|
||||
private Long productId;
|
||||
@NotBlank
|
||||
@NotNull
|
||||
private Integer count;
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
private Double totalPriceLine;
|
||||
|
@ -2,13 +2,25 @@ package com.example.demo.order_lines.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
import com.example.demo.products.model.ProductEntity;
|
||||
|
||||
@Entity
|
||||
@Table(name = "lines")
|
||||
public class OrderLineEntity extends BaseEntity {
|
||||
|
||||
@Column(nullable = false)
|
||||
private Integer count;
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "productId", nullable = false)
|
||||
private ProductEntity product;
|
||||
@Column(nullable = false)
|
||||
private Double totalPrice;
|
||||
|
||||
public OrderLineEntity() {
|
||||
|
@ -55,6 +55,7 @@ public class OrderController {
|
||||
entity.getLines().clear();
|
||||
for (OrderLineDto lineDto : dto.getLines()) {
|
||||
OrderLineEntity orderLineEntity = modelMapper.map(lineDto, OrderLineEntity.class);
|
||||
orderLineEntity.setId(null);
|
||||
// Здесь нужно установить ProductEntity для OrderLineEntity
|
||||
ProductEntity product = productService.get(lineDto.getProductId()); // Получаем продукт по его ID
|
||||
orderLineEntity.setProduct(product); // Устанавливаем продукт для строки заказа
|
||||
|
@ -17,7 +17,7 @@ public class OrderDto {
|
||||
private Long userId;
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
private Date date = getDate();
|
||||
@NotNull
|
||||
|
||||
private final List<OrderLineDto> lines = new ArrayList<>();
|
||||
|
||||
public Long getId() {
|
||||
|
@ -3,13 +3,12 @@ package com.example.demo.orders.model;
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
import com.example.demo.order_lines.model.OrderLineEntity;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
import com.fasterxml.jackson.annotation.JsonManagedReference;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToMany;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -20,28 +19,17 @@ import java.util.Objects;
|
||||
@Table(name = "orders")
|
||||
public class OrderEntity extends BaseEntity {
|
||||
@ManyToOne
|
||||
@JoinColumn()
|
||||
@JoinColumn(name = "userId", nullable = false)
|
||||
private UserEntity user;
|
||||
@JsonManagedReference
|
||||
@OneToMany
|
||||
@ManyToMany(cascade = CascadeType.ALL)
|
||||
private final List<OrderLineEntity> lines = new ArrayList<>();
|
||||
private Double totalPrice;
|
||||
|
||||
public OrderEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public OrderEntity(UserEntity user, Double totalPrice) {
|
||||
public OrderEntity(UserEntity user) {
|
||||
this.user = user;
|
||||
this.totalPrice = totalPrice;
|
||||
}
|
||||
|
||||
public Double getTotalPrice() {
|
||||
return totalPrice;
|
||||
}
|
||||
|
||||
public void setTotalPrice(Double totalPrice) {
|
||||
this.totalPrice = totalPrice;
|
||||
}
|
||||
|
||||
public UserEntity getUser() {
|
||||
@ -66,7 +54,7 @@ public class OrderEntity extends BaseEntity {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, user, lines, totalPrice);
|
||||
return Objects.hash(id, user, lines);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unlikely-arg-user")
|
||||
@ -79,12 +67,11 @@ public class OrderEntity extends BaseEntity {
|
||||
final OrderEntity other = (OrderEntity) obj;
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getUser(), user)
|
||||
&& Objects.equals(other.getLines(), lines)
|
||||
&& Objects.equals(other.getTotalPrice(), totalPrice);
|
||||
&& Objects.equals(other.getLines(), lines);
|
||||
}
|
||||
|
||||
public Double calculateTotalOrderPrice() {
|
||||
totalPrice = 0.0;
|
||||
Double totalPrice = 0.0;
|
||||
for (OrderLineEntity orderLine : lines) {
|
||||
totalPrice += orderLine.getProduct().getPrice() * orderLine.getCount();
|
||||
}
|
||||
|
@ -1,7 +0,0 @@
|
||||
package com.example.demo.orders.model;
|
||||
|
||||
public interface OrderGrouped {
|
||||
double getTotalPrice();
|
||||
|
||||
int getTotalCount();
|
||||
}
|
@ -2,27 +2,18 @@ package com.example.demo.orders.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.example.demo.orders.model.OrderEntity;
|
||||
import com.example.demo.orders.model.OrderGrouped;
|
||||
|
||||
public interface OrderRepository extends CrudRepository<OrderEntity, Long> {
|
||||
@SuppressWarnings("null")
|
||||
List<OrderEntity> findAll();
|
||||
|
||||
List<OrderEntity> findByUserIdAndLinesProductIds(long userId, List<Long> lines);
|
||||
List<OrderEntity> findByUserIdAndLinesProductId(long userId, List<Long> lines);
|
||||
|
||||
List<OrderEntity> findByLinesProductIds(List<Long> lines);
|
||||
List<OrderEntity> findByLinesProductId(List<Long> lines);
|
||||
|
||||
List<OrderEntity> findByUserId(long userId);
|
||||
|
||||
@Query("select "
|
||||
+ "t as type, "
|
||||
+ "coalesce(sum(o.price), 0) as totalPrice, "
|
||||
+ "coalesce(sum(o.count), 0) as totalCount "
|
||||
+ "from TypeEntity t left join OrderEntity o on o.type = t and o.user.id = ?1 "
|
||||
+ "group by t order by t.id")
|
||||
List<OrderGrouped> getOrdersTotalByType(long userId);
|
||||
}
|
||||
|
@ -21,10 +21,10 @@ public class OrderService {
|
||||
@Transactional(readOnly = true)
|
||||
public List<OrderEntity> getAll(Long userId, List<Long> lines) {
|
||||
if (userId != 0L && !lines.isEmpty()) {
|
||||
return repository.findByUserIdAndLinesProductIds(userId, lines);
|
||||
return repository.findByUserIdAndLinesProductId(userId, lines);
|
||||
}
|
||||
if (userId == 0L && !lines.isEmpty()) {
|
||||
return repository.findByLinesProductIds(lines);
|
||||
return repository.findByLinesProductId(lines);
|
||||
}
|
||||
if (userId != 0L && lines.isEmpty()) {
|
||||
return repository.findByUserId(userId);
|
||||
|
@ -3,24 +3,23 @@ package com.example.demo.users.api;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.Email;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class UserDto {
|
||||
private Long id;
|
||||
@NotNull
|
||||
@Min(1)
|
||||
|
||||
@NotBlank
|
||||
private String fullname;
|
||||
|
||||
@NotBlank
|
||||
private String surname;
|
||||
|
||||
@NotBlank
|
||||
@Email
|
||||
private String email;
|
||||
@NotBlank
|
||||
private String password;
|
||||
|
||||
@NotBlank
|
||||
private String password;
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public Long getId() {
|
||||
|
@ -11,13 +11,13 @@ import jakarta.persistence.Entity;
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class UserEntity extends BaseEntity {
|
||||
@Column(nullable = false, unique = true, length = 50)
|
||||
@Column(nullable = false, length = 50)
|
||||
private String fullname;
|
||||
@Column(nullable = false, unique = true, length = 50)
|
||||
@Column(nullable = false, length = 50)
|
||||
private String surname;
|
||||
@Column(nullable = false, unique = true, length = 50)
|
||||
private String email;
|
||||
@Column(nullable = false, unique = true, length = 50)
|
||||
@Column(nullable = false, length = 50)
|
||||
private String password;
|
||||
|
||||
public UserEntity() {
|
||||
|
@ -29,7 +29,7 @@ class OrderServiceTests {
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTest() {
|
||||
orderService.create(new OrderEntity(null, null));
|
||||
orderService.create(new OrderEntity(null));
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
List<OrderLineEntity> lines = new ArrayList();
|
||||
lines.add(new OrderLineEntity(null, 4));
|
||||
@ -37,7 +37,7 @@ class OrderServiceTests {
|
||||
lines.add(new OrderLineEntity(null, 6));
|
||||
lines.add(new OrderLineEntity(null, 7));
|
||||
// Создаем тестовую сущность OrderEntity
|
||||
OrderEntity testOrder = new OrderEntity(null, null);
|
||||
OrderEntity testOrder = new OrderEntity(null);
|
||||
// Вызываем метод create() и сохраняем созданную сущность
|
||||
OrderEntity createdOrder = orderService.create(testOrder);
|
||||
// Проверяем, что метод create() вернул не null
|
||||
|
Loading…
x
Reference in New Issue
Block a user