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