diff --git a/.gradle/8.5/executionHistory/executionHistory.bin b/.gradle/8.5/executionHistory/executionHistory.bin index f9a63c2..9fe123a 100644 Binary files a/.gradle/8.5/executionHistory/executionHistory.bin and b/.gradle/8.5/executionHistory/executionHistory.bin differ diff --git a/.gradle/8.5/executionHistory/executionHistory.lock b/.gradle/8.5/executionHistory/executionHistory.lock index 5a246e6..a858327 100644 Binary files a/.gradle/8.5/executionHistory/executionHistory.lock and b/.gradle/8.5/executionHistory/executionHistory.lock differ diff --git a/.gradle/8.5/fileHashes/fileHashes.bin b/.gradle/8.5/fileHashes/fileHashes.bin index e05da2a..3e44fff 100644 Binary files a/.gradle/8.5/fileHashes/fileHashes.bin and b/.gradle/8.5/fileHashes/fileHashes.bin differ diff --git a/.gradle/8.5/fileHashes/fileHashes.lock b/.gradle/8.5/fileHashes/fileHashes.lock index 8dc1519..eb70893 100644 Binary files a/.gradle/8.5/fileHashes/fileHashes.lock and b/.gradle/8.5/fileHashes/fileHashes.lock differ diff --git a/.gradle/8.5/fileHashes/resourceHashesCache.bin b/.gradle/8.5/fileHashes/resourceHashesCache.bin index b122b13..bcca034 100644 Binary files a/.gradle/8.5/fileHashes/resourceHashesCache.bin and b/.gradle/8.5/fileHashes/resourceHashesCache.bin differ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index 37fce3c..dd39d5d 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/build/tmp/compileJava/compileTransaction/stash-dir/DemoApplication.class.uniqueId2 b/build/tmp/compileJava/compileTransaction/stash-dir/DemoApplication.class.uniqueId2 index d949415..80b261e 100644 Binary files a/build/tmp/compileJava/compileTransaction/stash-dir/DemoApplication.class.uniqueId2 and b/build/tmp/compileJava/compileTransaction/stash-dir/DemoApplication.class.uniqueId2 differ diff --git a/build/tmp/compileJava/compileTransaction/stash-dir/OrderController.class.uniqueId0 b/build/tmp/compileJava/compileTransaction/stash-dir/OrderController.class.uniqueId0 index f86429d..e9e8aef 100644 Binary files a/build/tmp/compileJava/compileTransaction/stash-dir/OrderController.class.uniqueId0 and b/build/tmp/compileJava/compileTransaction/stash-dir/OrderController.class.uniqueId0 differ diff --git a/build/tmp/compileJava/compileTransaction/stash-dir/OrderEntity.class.uniqueId4 b/build/tmp/compileJava/compileTransaction/stash-dir/OrderEntity.class.uniqueId4 index 527662a..631ab43 100644 Binary files a/build/tmp/compileJava/compileTransaction/stash-dir/OrderEntity.class.uniqueId4 and b/build/tmp/compileJava/compileTransaction/stash-dir/OrderEntity.class.uniqueId4 differ diff --git a/build/tmp/compileJava/compileTransaction/stash-dir/OrderRepository.class.uniqueId1 b/build/tmp/compileJava/compileTransaction/stash-dir/OrderRepository.class.uniqueId1 index 5d55c82..b24c6cd 100644 Binary files a/build/tmp/compileJava/compileTransaction/stash-dir/OrderRepository.class.uniqueId1 and b/build/tmp/compileJava/compileTransaction/stash-dir/OrderRepository.class.uniqueId1 differ diff --git a/build/tmp/compileJava/compileTransaction/stash-dir/OrderService.class.uniqueId3 b/build/tmp/compileJava/compileTransaction/stash-dir/OrderService.class.uniqueId3 index 58dcd02..6349da5 100644 Binary files a/build/tmp/compileJava/compileTransaction/stash-dir/OrderService.class.uniqueId3 and b/build/tmp/compileJava/compileTransaction/stash-dir/OrderService.class.uniqueId3 differ diff --git a/build/tmp/compileJava/previous-compilation-data.bin b/build/tmp/compileJava/previous-compilation-data.bin index 3f9b479..c94757c 100644 Binary files a/build/tmp/compileJava/previous-compilation-data.bin and b/build/tmp/compileJava/previous-compilation-data.bin differ diff --git a/build/tmp/compileTestJava/previous-compilation-data.bin b/build/tmp/compileTestJava/previous-compilation-data.bin index 8ddd2a5..e0d93d4 100644 Binary files a/build/tmp/compileTestJava/previous-compilation-data.bin and b/build/tmp/compileTestJava/previous-compilation-data.bin differ diff --git a/data.mv.db b/data.mv.db index 70ef64d..a39f71e 100644 Binary files a/data.mv.db and b/data.mv.db differ diff --git a/src/main/java/com/example/demo/DemoApplication.java b/src/main/java/com/example/demo/DemoApplication.java index 4b59d03..968ad29 100644 --- a/src/main/java/com/example/demo/DemoApplication.java +++ b/src/main/java/com/example/demo/DemoApplication.java @@ -67,8 +67,8 @@ public class DemoApplication implements CommandLineRunner { @SuppressWarnings({ "rawtypes", "unchecked" }) List 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)); } } } diff --git a/src/main/java/com/example/demo/core/model/BaseEntity.java b/src/main/java/com/example/demo/core/model/BaseEntity.java index eba74ad..bda2c45 100644 --- a/src/main/java/com/example/demo/core/model/BaseEntity.java +++ b/src/main/java/com/example/demo/core/model/BaseEntity.java @@ -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; diff --git a/src/main/java/com/example/demo/order_lines/api/OrderLineDto.java b/src/main/java/com/example/demo/order_lines/api/OrderLineDto.java index 5d889d0..f9dc374 100644 --- a/src/main/java/com/example/demo/order_lines/api/OrderLineDto.java +++ b/src/main/java/com/example/demo/order_lines/api/OrderLineDto.java @@ -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; diff --git a/src/main/java/com/example/demo/order_lines/model/OrderLineEntity.java b/src/main/java/com/example/demo/order_lines/model/OrderLineEntity.java index 0bffaef..e9bcdcf 100644 --- a/src/main/java/com/example/demo/order_lines/model/OrderLineEntity.java +++ b/src/main/java/com/example/demo/order_lines/model/OrderLineEntity.java @@ -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() { diff --git a/src/main/java/com/example/demo/orders/api/OrderController.java b/src/main/java/com/example/demo/orders/api/OrderController.java index ebe057c..d49f405 100644 --- a/src/main/java/com/example/demo/orders/api/OrderController.java +++ b/src/main/java/com/example/demo/orders/api/OrderController.java @@ -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); // Устанавливаем продукт для строки заказа diff --git a/src/main/java/com/example/demo/orders/api/OrderDto.java b/src/main/java/com/example/demo/orders/api/OrderDto.java index ccbd59d..789faf4 100644 --- a/src/main/java/com/example/demo/orders/api/OrderDto.java +++ b/src/main/java/com/example/demo/orders/api/OrderDto.java @@ -17,7 +17,7 @@ public class OrderDto { private Long userId; @JsonProperty(access = JsonProperty.Access.READ_ONLY) private Date date = getDate(); - @NotNull + private final List lines = new ArrayList<>(); public Long getId() { diff --git a/src/main/java/com/example/demo/orders/model/OrderEntity.java b/src/main/java/com/example/demo/orders/model/OrderEntity.java index 1c3c95e..daf7a95 100644 --- a/src/main/java/com/example/demo/orders/model/OrderEntity.java +++ b/src/main/java/com/example/demo/orders/model/OrderEntity.java @@ -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 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(); } diff --git a/src/main/java/com/example/demo/orders/model/OrderGrouped.java b/src/main/java/com/example/demo/orders/model/OrderGrouped.java deleted file mode 100644 index 644cfb5..0000000 --- a/src/main/java/com/example/demo/orders/model/OrderGrouped.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.example.demo.orders.model; - -public interface OrderGrouped { - double getTotalPrice(); - - int getTotalCount(); -} diff --git a/src/main/java/com/example/demo/orders/repository/OrderRepository.java b/src/main/java/com/example/demo/orders/repository/OrderRepository.java index d760d1b..5b58f03 100644 --- a/src/main/java/com/example/demo/orders/repository/OrderRepository.java +++ b/src/main/java/com/example/demo/orders/repository/OrderRepository.java @@ -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 { @SuppressWarnings("null") List findAll(); - List findByUserIdAndLinesProductIds(long userId, List lines); + List findByUserIdAndLinesProductId(long userId, List lines); - List findByLinesProductIds(List lines); + List findByLinesProductId(List lines); List 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 getOrdersTotalByType(long userId); } diff --git a/src/main/java/com/example/demo/orders/service/OrderService.java b/src/main/java/com/example/demo/orders/service/OrderService.java index 34c6031..a388654 100644 --- a/src/main/java/com/example/demo/orders/service/OrderService.java +++ b/src/main/java/com/example/demo/orders/service/OrderService.java @@ -21,10 +21,10 @@ public class OrderService { @Transactional(readOnly = true) public List getAll(Long userId, List 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); diff --git a/src/main/java/com/example/demo/users/api/UserDto.java b/src/main/java/com/example/demo/users/api/UserDto.java index d1e5233..ba4dd30 100644 --- a/src/main/java/com/example/demo/users/api/UserDto.java +++ b/src/main/java/com/example/demo/users/api/UserDto.java @@ -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() { diff --git a/src/main/java/com/example/demo/users/model/UserEntity.java b/src/main/java/com/example/demo/users/model/UserEntity.java index acd6f91..0319060 100644 --- a/src/main/java/com/example/demo/users/model/UserEntity.java +++ b/src/main/java/com/example/demo/users/model/UserEntity.java @@ -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() { diff --git a/src/test/java/com/example/demo/OrderServiceTests.java b/src/test/java/com/example/demo/OrderServiceTests.java index 6b0b57b..d6f777b 100644 --- a/src/test/java/com/example/demo/OrderServiceTests.java +++ b/src/test/java/com/example/demo/OrderServiceTests.java @@ -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 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