public async Task <bool> CreateAsync(ShoppingCartCreateDto dto) { var book = repository.context.Books.Find(dto.BookId); if (book.QuantityInStock < dto.Quantity) { return(false); } var cart = await GetExistingOrCreateNewCart(dto.AccountId); cart.AddItem(book.Id, book, unitPrice: book.Price, quantity: dto.Quantity); repository.context.ShoppingCarts.Update(cart); var result = await repository.context.SaveChangesAsync() > 0; return(result); }
public async Task <ActionResult <bool> > Create(ShoppingCartCreateDto ShoppingCartCreateDto) { try { var result = await service.CreateAsync(ShoppingCartCreateDto); if (result == false) { return(BadRequest("Out of quantity in stock")); } return(result); } catch (Exception error) { return(Conflict(error.Message)); } }
public async Task <IActionResult> CreateShoppingCartItems([FromBody] ShoppingCartCreateDto touristRoute) { //1. 先获取用户登录ID信息 var userId = httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; //2.(判断用户是否存,这个jwt框架帮你做过了) 获取用户购物车 var shoppingCart = await repository.GetShoopingCartByUserIdAsync(userId); if (shoppingCart == null) { return(NotFound($"用户{userId}的购物车不存在")); } //3.获取旅游路线信息 var tourist = await repository.GetTouristRoutAsync(touristRoute.TouristRouteID); if (tourist == null) { return(NotFound($"旅游线路{touristRoute.TouristRouteID}不存在")); } CartLineItem lineItems = new CartLineItem() { OriginalPrice = tourist.OriginalPrice, DiscountPresent = tourist.DiscountPresent, ShoppingCartId = shoppingCart.Id, TouristID = tourist.ID, TouristRout = tourist, }; //5. 异步调用上下文添加 await repository.AddCartLineItemAsync(lineItems); //6.保存 await repository.SaveAsync(); //7. 返回 return(Ok(mapper.Map <ShoppingCartDto>(shoppingCart))); }