Co-authored-by: TakahashiNg <83152264+TakahashiNguyen@users.noreply.github.com> Reviewed-on: #57
This commit was merged in pull request #57.
This commit is contained in:
@@ -150,11 +150,11 @@ public class CartService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Mutation("addItem")
|
@Mutation("addItem")
|
||||||
public Uni<Cart> addItem(
|
public Cart addItem(
|
||||||
@Name("cartId") UUID cartId,
|
@Name("cartId") UUID cartId,
|
||||||
@Name("menuItemId") UUID menuItemId,
|
@Name("menuItemId") UUID menuItemId,
|
||||||
@Name("quantity") long quantity
|
@Name("quantity") long quantity
|
||||||
) {
|
) throws GraphQLException {
|
||||||
String key = "cart:" + cartId;
|
String key = "cart:" + cartId;
|
||||||
String priceKey = "cart_prices:" + cartId;
|
String priceKey = "cart_prices:" + cartId;
|
||||||
|
|
||||||
@@ -163,18 +163,18 @@ public class CartService {
|
|||||||
|
|
||||||
UUID eateryId = UUID.fromString(eateryIdStr);
|
UUID eateryId = UUID.fromString(eateryIdStr);
|
||||||
|
|
||||||
return eateryController
|
Response response = eateryController
|
||||||
.checkMenuItemExists(eateryId, menuItemId)
|
.checkMenuItemExists(eateryId, menuItemId)
|
||||||
.onItem()
|
.await()
|
||||||
.transform(response -> {
|
.indefinitely();
|
||||||
|
|
||||||
if (response.getStatus() != Response.Status.OK.getStatusCode()) {
|
if (response.getStatus() != Response.Status.OK.getStatusCode()) {
|
||||||
throw new RuntimeException("NotFoundMenuItemInEatery");
|
throw new GraphQLException("NotFoundMenuItemInEatery");
|
||||||
}
|
}
|
||||||
|
|
||||||
Double menuItemPrice = response.readEntity(Double.class);
|
Double menuItemPrice = response.readEntity(Double.class);
|
||||||
|
|
||||||
hashCommands.hincrby(key, menuItemId.toString(), quantity);
|
hashCommands.hincrby(key, menuItemId.toString(), quantity);
|
||||||
|
|
||||||
hashCommands.hset(
|
hashCommands.hset(
|
||||||
priceKey,
|
priceKey,
|
||||||
menuItemId.toString(),
|
menuItemId.toString(),
|
||||||
@@ -182,16 +182,8 @@ public class CartService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
hashCommands.getDataSource().key().expire(key, Duration.ofDays(30));
|
hashCommands.getDataSource().key().expire(key, Duration.ofDays(30));
|
||||||
hashCommands
|
hashCommands.getDataSource().key().expire(priceKey, Duration.ofDays(30));
|
||||||
.getDataSource()
|
|
||||||
.key()
|
|
||||||
.expire(priceKey, Duration.ofDays(30));
|
|
||||||
|
|
||||||
try {
|
|
||||||
return getCart(cartId);
|
return getCart(cartId);
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException(e.getMessage());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -124,7 +124,7 @@ public class CartServiceTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testAddItem_Success() {
|
void testAddItem_Success() throws GraphQLException {
|
||||||
UUID cartId = UUID.randomUUID();
|
UUID cartId = UUID.randomUUID();
|
||||||
UUID eateryId = UUID.randomUUID();
|
UUID eateryId = UUID.randomUUID();
|
||||||
UUID menuItemId = UUID.randomUUID();
|
UUID menuItemId = UUID.randomUUID();
|
||||||
@@ -137,10 +137,7 @@ public class CartServiceTest {
|
|||||||
Uni.createFrom().item(Response.ok(17.0).build())
|
Uni.createFrom().item(Response.ok(17.0).build())
|
||||||
);
|
);
|
||||||
|
|
||||||
Cart result = cartService
|
Cart result = cartService.addItem(cartId, menuItemId, 2L);
|
||||||
.addItem(cartId, menuItemId, 2L)
|
|
||||||
.await()
|
|
||||||
.atMost(Duration.ofSeconds(5));
|
|
||||||
|
|
||||||
assertNotNull(result);
|
assertNotNull(result);
|
||||||
assertEquals(cartId, result.getId());
|
assertEquals(cartId, result.getId());
|
||||||
|
|||||||
Reference in New Issue
Block a user