Skip to content

Commit e0bbac2

Browse files
authored
Merge pull request #45
Actualizado: Entidad y tabla "product"
2 parents 131560e + d2ddc42 commit e0bbac2

File tree

5 files changed

+53
-22
lines changed

5 files changed

+53
-22
lines changed

src/main/java/dev/nmarulo/despensa_app/app/pantry/products/Product.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,6 @@ public class Product {
3838
@Column(name = "price", nullable = false, precision = 2)
3939
private BigDecimal price;
4040

41-
/**
42-
* @deprecated Use {@link ProductImage} instead
43-
*/
44-
@Basic
45-
@Column(name = "img_url", nullable = false)
46-
@Deprecated
47-
private String imgUrl;
48-
4941
@Basic
5042
@Column(name = "calories", nullable = false, precision = 2)
5143
private BigDecimal calories;
@@ -112,11 +104,6 @@ public boolean equals(final Object o) {
112104
if (!Objects.equals(this$price, other$price)) {
113105
return false;
114106
}
115-
final Object this$imgUrl = this.getImgUrl();
116-
final Object other$imgUrl = other.getImgUrl();
117-
if (!Objects.equals(this$imgUrl, other$imgUrl)) {
118-
return false;
119-
}
120107
final Object this$calories = this.getCalories();
121108
final Object other$calories = other.getCalories();
122109
if (!Objects.equals(this$calories, other$calories)) {
@@ -148,8 +135,6 @@ public int hashCode() {
148135
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
149136
final Object $price = this.getPrice();
150137
result = result * PRIME + ($price == null ? 43 : $price.hashCode());
151-
final Object $imgUrl = this.getImgUrl();
152-
result = result * PRIME + ($imgUrl == null ? 43 : $imgUrl.hashCode());
153138
final Object $calories = this.getCalories();
154139
result = result * PRIME + ($calories == null ? 43 : $calories.hashCode());
155140
final Object $description = this.getDescription();

src/main/java/dev/nmarulo/despensa_app/app/pantry/products/ProductMapper.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.nmarulo.despensa_app.app.pantry.products;
22

3+
import dev.nmarulo.despensa_app.app.pantry.product_images.ProductImage;
34
import dev.nmarulo.despensa_app.app.pantry.product_shopping_list.ProductHasShoppingList;
45
import dev.nmarulo.despensa_app.app.pantry.products.dtos.FindAllProductRes;
56
import dev.nmarulo.despensa_app.app.pantry.products.dtos.FindAllShoppingListProductRes;
@@ -16,11 +17,12 @@ private ProductMapper() {
1617

1718
public static FindAllShoppingListProductRes.Product toFindAllShoppingListProductResProduct(final Product product) {
1819
final var response = new FindAllShoppingListProductRes.Product();
20+
final var productImage = getFirstProductImage(product);
1921

2022
response.setId(product.getId());
2123
response.setName(product.getName());
2224
response.setPrice(product.getPrice());
23-
response.setImgUrl(product.getImgUrl());
25+
response.setImgUrl(productImage.getUrl());
2426

2527
return response;
2628
}
@@ -48,11 +50,12 @@ public static SaveShoppingListProductRes toSaveShoppingListProductRes(final Prod
4850

4951
public static FindAllProductRes.Product toFindAllProductResProduct(final Product product) {
5052
final var response = new FindAllProductRes.Product();
53+
final var productImage = getFirstProductImage(product);
5154

5255
response.setId(product.getId());
5356
response.setName(product.getName());
5457
response.setPrice(product.getPrice());
55-
response.setImgUrl(product.getImgUrl());
58+
response.setImgUrl(productImage.getUrl());
5659
response.setCalories(product.getCalories());
5760
response.setDescription(product.getDescription());
5861

@@ -67,4 +70,11 @@ public static FindAllProductRes toFindAllProductRes(final Page<Product> page) {
6770
return pageTo(page, FindAllProductRes::new, ProductMapper::toFindAllProductResProduct);
6871
}
6972

73+
private static ProductImage getFirstProductImage(Product product) {
74+
return product.getProductImages()
75+
.stream()
76+
.toList()
77+
.getFirst();
78+
}
79+
7080
}

src/main/java/dev/nmarulo/despensa_app/app/pantry/shopping_list/ShoppingListMapper.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dev.nmarulo.despensa_app.app.pantry.shopping_list;
22

3+
import dev.nmarulo.despensa_app.app.pantry.product_images.ProductImage;
34
import dev.nmarulo.despensa_app.app.pantry.product_shopping_list.ProductHasShoppingList;
5+
import dev.nmarulo.despensa_app.app.pantry.products.Product;
46
import dev.nmarulo.despensa_app.app.pantry.shopping_list.dtos.*;
57
import dev.nmarulo.despensa_app.commons.mapper.CommonMapper;
68
import org.springframework.data.domain.Page;
@@ -26,12 +28,13 @@ public static FindByIdProductShoppingListRest toFindByIdProductShoppingListRest(
2628
final var product = productHasShoppingList.getProduct();
2729
final var unitType = productHasShoppingList.getUnitType();
2830
final var unitTypeRes = new FindByIdProductShoppingListRest.UnitTypeRes(unitType.getId(), unitType.getName());
31+
final var productImage = getFirtProductImage(product);
2932

3033
response.setName(product.getName());
3134
response.setPrice(product.getPrice());
3235
response.setUnitsPerProduct(productHasShoppingList.getUnitsPerProduct());
3336
response.setTotalPrice(productHasShoppingList.getTotalPrice());
34-
response.setImgUrl(product.getImgUrl());
37+
response.setImgUrl(productImage.getUrl());
3538
response.setUnitType(unitTypeRes);
3639

3740
return response;
@@ -72,14 +75,15 @@ private static FindByIdProductListRes.ProductListRes toFindByIdProductListResPro
7275
final var unitTypeRes = new FindByIdProductListRes.ProductListRes.UnitTypeRes();
7376
final var unitType = productHasShoppingList.getUnitType();
7477
final var product = productHasShoppingList.getProduct();
78+
final var productImage = getFirtProductImage(product);
7579

7680
unitTypeRes.setId(unitType.getId());
7781
unitTypeRes.setName(unitType.getName());
7882

7983
productRes.setId(product.getId());
8084
productRes.setName(product.getName());
8185
productRes.setPrice(product.getPrice());
82-
productRes.setImgUrl(product.getImgUrl());
86+
productRes.setImgUrl(productImage.getUrl());
8387

8488
response.setUnitsPerProduct(productHasShoppingList.getUnitsPerProduct());
8589
response.setSelected(productHasShoppingList.isSelected());
@@ -90,4 +94,11 @@ private static FindByIdProductListRes.ProductListRes toFindByIdProductListResPro
9094
return response;
9195
}
9296

97+
private static ProductImage getFirtProductImage(Product product) {
98+
return product.getProductImages()
99+
.stream()
100+
.toList()
101+
.getFirst();
102+
}
103+
93104
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ALTER TABLE products
2+
MODIFY product_categories_id BIGINT NOT NULL;
3+
4+
ALTER TABLE products
5+
MODIFY supermarkets_id BIGINT NOT NULL;
6+
7+
ALTER TABLE products
8+
DROP COLUMN img_url;

src/test/java/dev/nmarulo/despensa_app/app/pantry/products/ProductServiceTestUtil.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.nmarulo.despensa_app.app.pantry.products;
22

33
import dev.nmarulo.despensa_app.FakeTestUtil;
4+
import dev.nmarulo.despensa_app.app.pantry.product_images.ProductImage;
45
import dev.nmarulo.despensa_app.app.pantry.products.dtos.FindAllShoppingListProductRes;
56
import dev.nmarulo.despensa_app.app.users.User;
67
import lombok.Getter;
@@ -40,11 +41,15 @@ private FindAllShoppingListProductRes initFindAllShoppingListProductRes(Page<Pro
4041
final var productRes = new FindAllShoppingListProductRes.Product();
4142
final var product = productsPage.getContent()
4243
.getFirst();
44+
final var productImage = product.getProductImages()
45+
.stream()
46+
.toList()
47+
.getFirst();
4348

4449
productRes.setId(product.getId());
4550
productRes.setName(product.getName());
4651
productRes.setPrice(product.getPrice());
47-
productRes.setImgUrl(product.getImgUrl());
52+
productRes.setImgUrl(productImage.getUrl());
4853

4954
findAllShoppingListProductRes.setCurrentPage(productsPage.getNumber());
5055
findAllShoppingListProductRes.setPageSize(productsPage.getNumberOfElements());
@@ -62,22 +67,34 @@ private Page<Product> initProductsPage(Product product) {
6267
return new PageImpl<>(products, pageable, FakeTestUtil.randomInteger());
6368
}
6469

65-
private static Product initProduct() {
70+
private Product initProduct() {
6671
final var product = new Product();
72+
final var productImage = initProductImage();
6773

6874
product.setId(FakeTestUtil.randomLong());
6975
product.setName(FakeTestUtil.randomWord());
7076
product.setPrice(FakeTestUtil.randomBigDecimal());
71-
product.setImgUrl(FakeTestUtil.randomImage());
7277
product.setCalories(FakeTestUtil.randomBigDecimal());
7378
product.setDescription(FakeTestUtil.randomSentence());
7479
product.setCreatedAt(FakeTestUtil.randomPast());
7580
product.setUpdatedAt(FakeTestUtil.randomFuture());
7681
product.setProductHasShoppingList(Collections.emptySet());
82+
product.setProductImages(Collections.singleton(productImage));
7783

7884
return product;
7985
}
8086

87+
private ProductImage initProductImage() {
88+
final var productImage = new ProductImage();
89+
90+
productImage.setId(FakeTestUtil.randomLong());
91+
productImage.setUrl(FakeTestUtil.randomImage());
92+
productImage.setCreatedAt(FakeTestUtil.randomPast());
93+
productImage.setUpdatedAt(FakeTestUtil.randomFuture());
94+
95+
return productImage;
96+
}
97+
8198
private User initUser() {
8299
final var user = new User();
83100

0 commit comments

Comments
 (0)