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:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
- dev-*
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
release:
|
release:
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
name: Java CI with Maven
|
name: Java CI with Maven
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-and-test:
|
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 class InventoryItemEntity extends BaseEntity {
|
||||||
|
|
||||||
public String name;
|
public String name;
|
||||||
public int quantity;
|
|
||||||
public String unit;
|
public String unit;
|
||||||
|
|
||||||
|
private InventoryItemEntity() {}
|
||||||
|
|
||||||
// Create
|
// Create
|
||||||
@Transactional
|
@Transactional
|
||||||
public void add(String name, int quantity, String unit) {
|
public static InventoryItemEntity create(String name, String unit) {
|
||||||
this.name = name;
|
InventoryItemEntity item = new InventoryItemEntity();
|
||||||
this.quantity = quantity;
|
|
||||||
this.unit = unit;
|
|
||||||
|
|
||||||
this.persist();
|
item.name = name;
|
||||||
|
item.unit = unit;
|
||||||
|
|
||||||
|
item.persist();
|
||||||
|
|
||||||
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void add(String name, int quantity) {
|
public static InventoryItemEntity create(String name) {
|
||||||
add(name, quantity, "unit");
|
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;
|
public UUID eateryId;
|
||||||
|
|
||||||
|
private ReviewEntity(ReviewModel input) {
|
||||||
|
super(input);
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void receive(UUID reviewerId, UUID eateryId, ReviewModel review) {
|
public static ReviewEntity create(
|
||||||
this.reviewerId = reviewerId;
|
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;
|
return review;
|
||||||
|
|
||||||
this.persist();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,12 +10,10 @@ import jakarta.ws.rs.core.Response;
|
|||||||
public class InventoryService {
|
public class InventoryService {
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
@Path("/add")
|
@Path("/create")
|
||||||
@Transactional
|
@Transactional
|
||||||
public Response add(AddInventory input) {
|
public Response create(AddInventory input) {
|
||||||
InventoryItemEntity newItem = new InventoryItemEntity();
|
InventoryItemEntity.create(input.name, input.unit);
|
||||||
|
|
||||||
newItem.add(input.name, input.quantity, input.unit);
|
|
||||||
|
|
||||||
return Response.ok().build();
|
return Response.ok().build();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,16 +6,14 @@ import jakarta.transaction.Transactional;
|
|||||||
import jakarta.ws.rs.*;
|
import jakarta.ws.rs.*;
|
||||||
import jakarta.ws.rs.core.Response;
|
import jakarta.ws.rs.core.Response;
|
||||||
|
|
||||||
@Path("/api")
|
@Path("/api/review")
|
||||||
public class ReviewService {
|
public class ReviewService {
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
@Path("/leaveReview")
|
@Path("/create")
|
||||||
@Transactional
|
@Transactional
|
||||||
public Response leaveReview(SendReview input) {
|
public Response create(SendReview input) {
|
||||||
ReviewEntity newReview = new ReviewEntity();
|
ReviewEntity.create(input.reviewerId, input.orderId, input.review);
|
||||||
|
|
||||||
newReview.receive(input.reviewerId, input.orderId, input.review);
|
|
||||||
|
|
||||||
return Response.status(201).build();
|
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 com.drinkool.entities.InventoryItemEntity;
|
||||||
import io.quarkus.test.junit.QuarkusTest;
|
import io.quarkus.test.junit.QuarkusTest;
|
||||||
import io.restassured.http.ContentType;
|
import io.restassured.http.ContentType;
|
||||||
import java.util.Random;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
@@ -16,19 +15,16 @@ public class InventoryServiceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void add() {
|
void add() {
|
||||||
Random random = new Random();
|
|
||||||
|
|
||||||
String name = UUID.randomUUID().toString();
|
String name = UUID.randomUUID().toString();
|
||||||
|
|
||||||
AddInventory input = new AddInventory();
|
AddInventory input = new AddInventory();
|
||||||
input.name = name;
|
input.name = name;
|
||||||
input.quantity = random.nextInt(100);
|
|
||||||
|
|
||||||
given()
|
given()
|
||||||
.contentType(ContentType.JSON)
|
.contentType(ContentType.JSON)
|
||||||
.body(input)
|
.body(input)
|
||||||
.when()
|
.when()
|
||||||
.post("/api/inventory/add")
|
.post("/api/inventory/create")
|
||||||
.then();
|
.then();
|
||||||
|
|
||||||
assertEquals(1, InventoryItemEntity.find("name", name).count());
|
assertEquals(1, InventoryItemEntity.find("name", name).count());
|
||||||
|
|||||||
@@ -27,16 +27,14 @@ public class ReviewServiceTest {
|
|||||||
SendReview input = new SendReview();
|
SendReview input = new SendReview();
|
||||||
input.reviewerId = reviewerId;
|
input.reviewerId = reviewerId;
|
||||||
input.orderId = orderId;
|
input.orderId = orderId;
|
||||||
ReviewModel reviewModel = new ReviewModel();
|
ReviewModel reviewModel = new ReviewModel(rating, comment);
|
||||||
reviewModel.comment = comment;
|
|
||||||
reviewModel.rating = rating;
|
|
||||||
input.review = reviewModel;
|
input.review = reviewModel;
|
||||||
|
|
||||||
given()
|
given()
|
||||||
.contentType(ContentType.JSON)
|
.contentType(ContentType.JSON)
|
||||||
.body(input)
|
.body(input)
|
||||||
.when()
|
.when()
|
||||||
.post("/api/leaveReview")
|
.post("/api/review/create")
|
||||||
.then();
|
.then();
|
||||||
|
|
||||||
assertEquals(1, ReviewEntity.find("reviewerId", reviewerId).count());
|
assertEquals(1, ReviewEntity.find("reviewerId", reviewerId).count());
|
||||||
|
|||||||
@@ -3,6 +3,5 @@ package com.drinkool.dtos;
|
|||||||
public class AddInventory {
|
public class AddInventory {
|
||||||
|
|
||||||
public String name;
|
public String name;
|
||||||
public int quantity;
|
|
||||||
public String unit;
|
public String unit;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,18 @@ public class ReviewModel extends BaseEntity {
|
|||||||
@Column
|
@Column
|
||||||
public String comment;
|
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
|
@org.hibernate.annotations.CreationTimestamp
|
||||||
@Column(name = "review_date", updatable = false)
|
@Column(name = "review_date", updatable = false)
|
||||||
protected LocalDateTime reviewDate;
|
public LocalDateTime reviewDate;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user