feat(eatery-service): init menu item (#9)
Release package / release (push) Failing after 3m24s
Release package / release (push) Failing after 3m24s
Co-authored-by: TakahashiNg <83152264+TakahashiNguyen@users.noreply.github.com> Reviewed-on: #9
This commit was merged in pull request #9.
This commit is contained in:
@@ -4,7 +4,6 @@ on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- dev-*
|
||||
|
||||
jobs:
|
||||
release:
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
name: Java CI with Maven
|
||||
|
||||
on:
|
||||
push:
|
||||
pull_request:
|
||||
branches:
|
||||
- main
|
||||
|
||||
jobs:
|
||||
build-and-test:
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.drinkool.entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.transaction.Transactional;
|
||||
|
||||
@Entity
|
||||
@Table
|
||||
public class EateryInventoryItemEntity extends BaseEntity {
|
||||
|
||||
public double quantity;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "inventory_item_id")
|
||||
public InventoryItemEntity inventoryItem;
|
||||
|
||||
private EateryInventoryItemEntity() {}
|
||||
|
||||
// Create
|
||||
@Transactional
|
||||
public static EateryInventoryItemEntity create(
|
||||
String name,
|
||||
double quantity,
|
||||
String unit
|
||||
) {
|
||||
String itemName = name.toLowerCase();
|
||||
|
||||
InventoryItemEntity item = InventoryItemEntity.find(
|
||||
"name",
|
||||
itemName
|
||||
).firstResult();
|
||||
|
||||
if (item == null || item.unit != unit) {
|
||||
if (unit != null || unit == "") {
|
||||
item = InventoryItemEntity.create(itemName, unit);
|
||||
} else {
|
||||
item = InventoryItemEntity.create(itemName);
|
||||
}
|
||||
}
|
||||
|
||||
EateryInventoryItemEntity eateryInventoryItem =
|
||||
new EateryInventoryItemEntity();
|
||||
|
||||
eateryInventoryItem.quantity = quantity;
|
||||
eateryInventoryItem.inventoryItem = item;
|
||||
|
||||
eateryInventoryItem.persist();
|
||||
|
||||
return eateryInventoryItem;
|
||||
}
|
||||
}
|
||||
@@ -9,21 +9,25 @@ import jakarta.transaction.Transactional;
|
||||
public class InventoryItemEntity extends BaseEntity {
|
||||
|
||||
public String name;
|
||||
public int quantity;
|
||||
public String unit;
|
||||
|
||||
private InventoryItemEntity() {}
|
||||
|
||||
// Create
|
||||
@Transactional
|
||||
public void add(String name, int quantity, String unit) {
|
||||
this.name = name;
|
||||
this.quantity = quantity;
|
||||
this.unit = unit;
|
||||
public static InventoryItemEntity create(String name, String unit) {
|
||||
InventoryItemEntity item = new InventoryItemEntity();
|
||||
|
||||
this.persist();
|
||||
item.name = name;
|
||||
item.unit = unit;
|
||||
|
||||
item.persist();
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void add(String name, int quantity) {
|
||||
add(name, quantity, "unit");
|
||||
public static InventoryItemEntity create(String name) {
|
||||
return InventoryItemEntity.create(name, "unit");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.drinkool.entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.transaction.Transactional;
|
||||
import java.util.*;
|
||||
|
||||
@Entity
|
||||
@Table
|
||||
public class MenuItemEntity extends BaseEntity {
|
||||
|
||||
public String name;
|
||||
public double price;
|
||||
|
||||
// Danh sách các nguyên liệu cấu thành món ăn này
|
||||
@OneToMany(
|
||||
mappedBy = "menuItem",
|
||||
cascade = CascadeType.ALL,
|
||||
orphanRemoval = true
|
||||
)
|
||||
public List<MenuItemIngredientEntity> ingredients = new ArrayList<>();
|
||||
|
||||
private MenuItemEntity() {}
|
||||
|
||||
/**
|
||||
* Thêm nguyên liệu vào món ăn với định lượng cụ thể
|
||||
*/
|
||||
@Transactional
|
||||
public void addIngredient(
|
||||
EateryInventoryItemEntity inventoryItem,
|
||||
double requiredQty
|
||||
) {
|
||||
MenuItemIngredientEntity ingredient = MenuItemIngredientEntity.create(
|
||||
this,
|
||||
inventoryItem,
|
||||
requiredQty
|
||||
);
|
||||
|
||||
this.ingredients.add(ingredient);
|
||||
|
||||
this.persist();
|
||||
}
|
||||
|
||||
/**
|
||||
* Phương thức tiện ích để tạo nhanh Menu Item từ tên
|
||||
*/
|
||||
@Transactional
|
||||
public static MenuItemEntity create(String name, double price) {
|
||||
MenuItemEntity menu = new MenuItemEntity();
|
||||
|
||||
menu.name = name;
|
||||
menu.price = price;
|
||||
|
||||
menu.persist();
|
||||
|
||||
return menu;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.drinkool.entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "menu_item_ingredients")
|
||||
public class MenuItemIngredientEntity extends BaseEntity {
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "menu_item_id")
|
||||
public MenuItemEntity menuItem;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "inventory_item_id")
|
||||
public EateryInventoryItemEntity inventoryItem;
|
||||
|
||||
public double requiredQuantity;
|
||||
|
||||
private MenuItemIngredientEntity() {}
|
||||
|
||||
public static MenuItemIngredientEntity create(
|
||||
MenuItemEntity menuItem,
|
||||
EateryInventoryItemEntity inventoryItem,
|
||||
double requiredQuantity
|
||||
) {
|
||||
MenuItemIngredientEntity ingredient = new MenuItemIngredientEntity();
|
||||
|
||||
ingredient.menuItem = menuItem;
|
||||
ingredient.inventoryItem = inventoryItem;
|
||||
ingredient.requiredQuantity = requiredQuantity;
|
||||
|
||||
ingredient.persist();
|
||||
|
||||
return ingredient;
|
||||
}
|
||||
}
|
||||
@@ -13,16 +13,23 @@ public class ReviewEntity extends ReviewModel {
|
||||
|
||||
public UUID eateryId;
|
||||
|
||||
private ReviewEntity(ReviewModel input) {
|
||||
super(input);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void receive(UUID reviewerId, UUID eateryId, ReviewModel review) {
|
||||
this.reviewerId = reviewerId;
|
||||
public static ReviewEntity create(
|
||||
UUID reviewerId,
|
||||
UUID eateryId,
|
||||
ReviewModel reviewInput
|
||||
) {
|
||||
ReviewEntity review = new ReviewEntity(reviewInput);
|
||||
|
||||
this.eateryId = eateryId;
|
||||
review.reviewerId = reviewerId;
|
||||
review.eateryId = eateryId;
|
||||
|
||||
this.rating = review.rating;
|
||||
review.persist();
|
||||
|
||||
this.comment = review.comment;
|
||||
|
||||
this.persist();
|
||||
return review;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,12 +10,10 @@ import jakarta.ws.rs.core.Response;
|
||||
public class InventoryService {
|
||||
|
||||
@POST
|
||||
@Path("/add")
|
||||
@Path("/create")
|
||||
@Transactional
|
||||
public Response add(AddInventory input) {
|
||||
InventoryItemEntity newItem = new InventoryItemEntity();
|
||||
|
||||
newItem.add(input.name, input.quantity, input.unit);
|
||||
public Response create(AddInventory input) {
|
||||
InventoryItemEntity.create(input.name, input.unit);
|
||||
|
||||
return Response.ok().build();
|
||||
}
|
||||
|
||||
@@ -6,16 +6,14 @@ import jakarta.transaction.Transactional;
|
||||
import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
@Path("/api")
|
||||
@Path("/api/review")
|
||||
public class ReviewService {
|
||||
|
||||
@POST
|
||||
@Path("/leaveReview")
|
||||
@Path("/create")
|
||||
@Transactional
|
||||
public Response leaveReview(SendReview input) {
|
||||
ReviewEntity newReview = new ReviewEntity();
|
||||
|
||||
newReview.receive(input.reviewerId, input.orderId, input.review);
|
||||
public Response create(SendReview input) {
|
||||
ReviewEntity.create(input.reviewerId, input.orderId, input.review);
|
||||
|
||||
return Response.status(201).build();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.drinkool.entities;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@QuarkusTest
|
||||
public class EateryInventoryItemEntityTest {
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
void testAdd_WhenItemExists_ShouldUseExistingItem() {
|
||||
String itemName = "Coca Cola";
|
||||
int quantity = 10;
|
||||
|
||||
InventoryItemEntity.create(itemName);
|
||||
|
||||
EateryInventoryItemEntity eateryItem = EateryInventoryItemEntity.create(
|
||||
itemName,
|
||||
quantity,
|
||||
""
|
||||
);
|
||||
|
||||
EateryInventoryItemEntity savedEntity = EateryInventoryItemEntity.find(
|
||||
"id",
|
||||
eateryItem.id
|
||||
).firstResult();
|
||||
assertNotNull(savedEntity);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
void testAdd_WhenItemDoesNotExist_ShouldCreateNewItem() {
|
||||
String itemName = "Pepsi";
|
||||
int quantity = 5;
|
||||
String unit = "Bottle";
|
||||
|
||||
EateryInventoryItemEntity eateryItem = EateryInventoryItemEntity.create(
|
||||
itemName,
|
||||
quantity,
|
||||
unit
|
||||
);
|
||||
|
||||
EateryInventoryItemEntity savedEntity = EateryInventoryItemEntity.find(
|
||||
"id",
|
||||
eateryItem.id
|
||||
).firstResult();
|
||||
assertNotNull(savedEntity);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.drinkool.entities;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@QuarkusTest
|
||||
public class MenuItemIngredientEntityTest {
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
void testCreate_ShouldPersistIngredientSuccessfully() {
|
||||
// 1. Chuẩn bị dữ liệu giả định
|
||||
|
||||
String menuItemName = "Milk Tea";
|
||||
double menuItemPrice = 40;
|
||||
|
||||
MenuItemEntity menuItem = MenuItemEntity.create(
|
||||
menuItemName,
|
||||
menuItemPrice
|
||||
);
|
||||
|
||||
String inventoryItemName = "Water";
|
||||
double inventoryItemQuantity = 10;
|
||||
String inventoryItemUnit = "l";
|
||||
|
||||
EateryInventoryItemEntity inventoryItem = EateryInventoryItemEntity.create(
|
||||
inventoryItemName,
|
||||
inventoryItemQuantity,
|
||||
inventoryItemUnit
|
||||
);
|
||||
|
||||
double requiredQuantity = 0.5;
|
||||
|
||||
// 2. Gọi hàm static create cần test
|
||||
MenuItemIngredientEntity createdIngredient =
|
||||
MenuItemIngredientEntity.create(
|
||||
menuItem,
|
||||
inventoryItem,
|
||||
requiredQuantity
|
||||
);
|
||||
|
||||
// 3. Kiểm tra kết quả
|
||||
assertNotNull(createdIngredient.id, "ID không được null sau khi persist");
|
||||
|
||||
// Truy vấn lại từ Database để đảm bảo dữ liệu đã được lưu đúng
|
||||
MenuItemIngredientEntity savedEntity = MenuItemIngredientEntity.findById(
|
||||
createdIngredient.id
|
||||
);
|
||||
|
||||
assertNotNull(savedEntity);
|
||||
assertEquals(menuItem.id, savedEntity.menuItem.id);
|
||||
assertEquals(inventoryItem.id, savedEntity.inventoryItem.id);
|
||||
assertEquals(requiredQuantity, savedEntity.requiredQuantity);
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ import com.drinkool.dtos.AddInventory;
|
||||
import com.drinkool.entities.InventoryItemEntity;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.http.ContentType;
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -16,19 +15,16 @@ public class InventoryServiceTest {
|
||||
|
||||
@Test
|
||||
void add() {
|
||||
Random random = new Random();
|
||||
|
||||
String name = UUID.randomUUID().toString();
|
||||
|
||||
AddInventory input = new AddInventory();
|
||||
input.name = name;
|
||||
input.quantity = random.nextInt(100);
|
||||
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body(input)
|
||||
.when()
|
||||
.post("/api/inventory/add")
|
||||
.post("/api/inventory/create")
|
||||
.then();
|
||||
|
||||
assertEquals(1, InventoryItemEntity.find("name", name).count());
|
||||
|
||||
@@ -27,16 +27,14 @@ public class ReviewServiceTest {
|
||||
SendReview input = new SendReview();
|
||||
input.reviewerId = reviewerId;
|
||||
input.orderId = orderId;
|
||||
ReviewModel reviewModel = new ReviewModel();
|
||||
reviewModel.comment = comment;
|
||||
reviewModel.rating = rating;
|
||||
ReviewModel reviewModel = new ReviewModel(rating, comment);
|
||||
input.review = reviewModel;
|
||||
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body(input)
|
||||
.when()
|
||||
.post("/api/leaveReview")
|
||||
.post("/api/review/create")
|
||||
.then();
|
||||
|
||||
assertEquals(1, ReviewEntity.find("reviewerId", reviewerId).count());
|
||||
|
||||
@@ -3,6 +3,5 @@ package com.drinkool.dtos;
|
||||
public class AddInventory {
|
||||
|
||||
public String name;
|
||||
public int quantity;
|
||||
public String unit;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,18 @@ public class ReviewModel extends BaseEntity {
|
||||
@Column
|
||||
public String comment;
|
||||
|
||||
public ReviewModel() {}
|
||||
|
||||
public ReviewModel(int rating, String comment) {
|
||||
this.rating = rating;
|
||||
this.comment = comment;
|
||||
}
|
||||
|
||||
public ReviewModel(ReviewModel input) {
|
||||
this(input.rating, input.comment);
|
||||
}
|
||||
|
||||
@org.hibernate.annotations.CreationTimestamp
|
||||
@Column(name = "review_date", updatable = false)
|
||||
protected LocalDateTime reviewDate;
|
||||
public LocalDateTime reviewDate;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user