in process....

This commit is contained in:
Вячеслав Иванов 2024-04-25 13:09:17 +04:00
parent d09f2ec19c
commit 19fad7ac71
9 changed files with 43 additions and 70 deletions

View File

@ -41,3 +41,7 @@ dependencies {
tasks.named('test') {
useJUnitPlatform()
}
bootRun {
args = ['--populate']
}

View File

@ -47,12 +47,12 @@ public class DemoApplication implements CommandLineRunner {
final var type3 = typeService.create(new TypeEntity("Сырная"));
log.info("Create default product values");
final var product1 = productService.create(new ProductEntity("test", type1, 399.00, 15));
final var product2 = productService.create(new ProductEntity("test", type1, 499.00, 10));
final var product3 = productService.create(new ProductEntity("test", type2, 450.50, 5));
final var product4 = productService.create(new ProductEntity("test", type2, 900.50, 6));
final var product5 = productService.create(new ProductEntity("test", type2, 600.00, 8));
final var product6 = productService.create(new ProductEntity("test", type3, 750.00, 9));
final var product1 = productService.create(new ProductEntity("test", type1, 399.00));
final var product2 = productService.create(new ProductEntity("test", type1, 499.00));
final var product3 = productService.create(new ProductEntity("test", type2, 450.50));
final var product4 = productService.create(new ProductEntity("test", type2, 900.50));
final var product5 = productService.create(new ProductEntity("test", type2, 600.00));
final var product6 = productService.create(new ProductEntity("test", type3, 750.00));
log.info("Create default user values");
final var user1 = userService.create(new UserEntity("kruviii", "Павел", "Крылов", "test@test.ru", "11-11-2023", "+79876543210", "qwerty123"));

View File

@ -81,7 +81,7 @@ public class OrderService {
public List<ProductEntity> getOrderProducts(long userId, long id) {
userService.get(userId);
return get(userId, id).getOrderProducts().stream()
.map(OrderProductEntity::getProduct)
.map(orderProduct -> orderProduct.getProduct())
.toList();
}
@ -114,7 +114,7 @@ public class OrderService {
result.forEach(product -> {
final OrderProductEntity orderProduct = new OrderProductEntity(existsOrder, product, productsIdsCount.get(product.getId()));
orderProduct.setOrder(existsOrder);
orderProduct.setProduct(product);
// orderProduct.setProduct(product);
});
repository.save(existsOrder);
return result;

View File

@ -61,13 +61,6 @@ public class OrderProductEntity {
return product;
}
public void setProduct(ProductEntity product) {
this.product = product;
if (!product.getOrderProducts().contains(this)) {
product.getOrderProducts().add(this);
}
}
public Integer getCount() {
return count;
}

View File

@ -17,9 +17,6 @@ public class ProductDto {
@NotNull
@Min(1)
private Double price;
@NotNull
@Min(0)
private Integer count;
public Long getId() {
return id;
@ -52,12 +49,4 @@ public class ProductDto {
public void setPrice(Double price) {
this.price = price;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
}

View File

@ -1,17 +1,17 @@
package com.example.demo.product.model;
import java.util.HashSet;
// import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
// import java.util.Set;
import com.example.demo.types.model.TypeEntity;
import com.example.demo.ordersproducts.model.OrderProductEntity;
// import com.example.demo.ordersproducts.model.OrderProductEntity;
import com.example.demo.core.model.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OrderBy;
// import jakarta.persistence.OneToMany;
// import jakarta.persistence.OrderBy;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
@ -26,21 +26,18 @@ public class ProductEntity extends BaseEntity {
private TypeEntity type;
@Column(nullable = false)
private Double price;
@Column(nullable = false)
private Integer count;
@OneToMany(mappedBy = "product")
@OrderBy("id ASC")
private Set<OrderProductEntity> orderProducts = new HashSet<>();
// @OneToMany(mappedBy = "product")
// @OrderBy("id ASC")
// private Set<OrderProductEntity> orderProducts = new HashSet<>();
public ProductEntity() {
}
public ProductEntity(String name, TypeEntity type, Double price, Integer count) {
public ProductEntity(String name, TypeEntity type, Double price) {
this.name = name;
this.type = type;
this.price = price;
this.count = count;
}
public String getName() {
@ -67,28 +64,20 @@ public class ProductEntity extends BaseEntity {
this.price = price;
}
public Integer getCount() {
return count;
}
// public Set<OrderProductEntity> getOrderProducts() {
// return orderProducts;
// }
public void setCount(Integer count) {
this.count = count;
}
public Set<OrderProductEntity> getOrderProducts() {
return orderProducts;
}
public void addOrder(OrderProductEntity orderProduct) {
if (orderProduct.getProduct() != this) {
orderProduct.setProduct(this);
}
orderProducts.add(orderProduct);
}
// public void addOrder(OrderProductEntity orderProduct) {
// if (orderProduct.getProduct() != this) {
// orderProduct.setProduct(this);
// }
// orderProducts.add(orderProduct);
// }
@Override
public int hashCode() {
return Objects.hash(id, name, type, price, count, orderProducts);
return Objects.hash(id, name, type, price/* , orderProducts*/);
}
@Override
@ -101,8 +90,7 @@ public class ProductEntity extends BaseEntity {
return Objects.equals(other.getId(), id)
&& Objects.equals(other.getName(), name)
&& Objects.equals(other.getType(), type)
&& Objects.equals(other.getPrice(), price)
&& Objects.equals(other.getCount(), count)
&& Objects.equals(other.getOrderProducts(), orderProducts);
&& Objects.equals(other.getPrice(), price);
// && Objects.equals(other.getOrderProducts(), orderProducts);
}
}

View File

@ -58,7 +58,6 @@ public class ProductService {
existsEntity.setName(entity.getName());
existsEntity.setType(entity.getType());
existsEntity.setPrice(entity.getPrice());
existsEntity.setCount(entity.getCount());
return repository.save(existsEntity);
}

View File

@ -49,9 +49,9 @@ public class OrderServiceTests {
removeData();
final var type = typeService.create(new TypeEntity("Пепперони"));
product1 = productService.create(new ProductEntity("test1", type, 399.00, 15));
product2 = productService.create(new ProductEntity("test2", type, 499.00, 10));
product3 = productService.create(new ProductEntity("test3", type, 450.50, 5));
product1 = productService.create(new ProductEntity("test1", type, 399.00));
product2 = productService.create(new ProductEntity("test2", type, 499.00));
product3 = productService.create(new ProductEntity("test3", type, 450.50));
user = userService.create(new UserEntity("kruviii", "Павел", "Крылов", "test@test.ru", "11-11-2023", "+79876543210", "qwerty123"));
order = orderService.create(user.getId(), new OrderEntity("ул. Северный венец, 32"));
orderService.create(user.getId(), new OrderEntity("ул. Северный венец, 32"));
@ -116,7 +116,7 @@ public class OrderServiceTests {
@Test
void AddOrderProductsTest() {
final var type = typeService.create(new TypeEntity("3 сыра"));
final var product = productService.create(new ProductEntity("test1", type, 100.00, 5));
final var product = productService.create(new ProductEntity("test1", type, 100.00));
Assertions.assertEquals(3, orderService.getOrderProducts(user.getId(), order.getId()).size());
orderService.addOrderProducts(user.getId(), order.getId(), Map.of(product.getId(), 5));
Assertions.assertEquals(4, orderService.getOrderProducts(user.getId(), order.getId()).size());

View File

@ -37,9 +37,9 @@ class ProductsServiceTests {
final var type3 = typeService.create(new TypeEntity("Сырная"));
product = productService.create(new ProductEntity("test", type, 399.00, 15));
productService.create(new ProductEntity("test", type2, 499.00, 10));
productService.create(new ProductEntity("test", type3, 450.50, 5));
product = productService.create(new ProductEntity("test", type, 399.00));
productService.create(new ProductEntity("test", type2, 499.00));
productService.create(new ProductEntity("test", type3, 450.50));
}
@AfterEach
@ -62,7 +62,7 @@ class ProductsServiceTests {
@Test
void createNullableTest() {
final ProductEntity nullableProduct = new ProductEntity(null, type, 399.00, 15);;
final ProductEntity nullableProduct = new ProductEntity(null, type, 399.00);;
Assertions.assertThrows(DataIntegrityViolationException.class, () -> productService.create(nullableProduct));
}
@ -72,7 +72,7 @@ class ProductsServiceTests {
final String test = "TEST";
final TypeEntity newType = typeService.create(new TypeEntity("Фреш"));
final TypeEntity oldType = product.getType();
final ProductEntity newEntity = productService.update(product.getId(), new ProductEntity(test, newType, 100.00, 15));
final ProductEntity newEntity = productService.update(product.getId(), new ProductEntity(test, newType, 100.00));
Assertions.assertEquals(3, productService.getAll(0L).size());
Assertions.assertEquals(newEntity, productService.get(product.getId()));
Assertions.assertEquals(test, newEntity.getName());
@ -85,7 +85,7 @@ class ProductsServiceTests {
productService.delete(product.getId());
Assertions.assertEquals(2, productService.getAll(0L).size());
final ProductEntity newEntity = productService.create(new ProductEntity(product.getName(), product.getType(), product.getPrice(), product.getCount()));
final ProductEntity newEntity = productService.create(new ProductEntity(product.getName(), product.getType(), product.getPrice()));
Assertions.assertEquals(3, typeService.getAll().size());
Assertions.assertNotEquals(product.getId(), newEntity.getId());
}