public async Task <Option <CartItemServiceModel, Error> > CreateAsync(CartItemBm model) { model.CartId = await GetCurrentCartIdByUserIdAsync(model.UserId); return(await ExistsAsync(model) ? Option.None <CartItemServiceModel, Error>(CartItemExistsErrMsg.ToError()) : (await SaveAsync(model)).Some <CartItemServiceModel, Error>()); }
private async Task <CartItemServiceModel> SaveAsync(CartItemBm model) { var entity = Mapper.Map <CartItemBm, CartItem>(model); await DbContext.CartItems.AddAsync(entity); await DbContext.SaveChangesAsync(); return(Mapper.Map <CartItem, CartItemServiceModel>(entity)); }
private async Task <bool> ExistsAsync(CartItemBm model) => await DbContext .CartItems .AnyAsync(ci => ci.CartId == model.CartId && ci.ProductId == model.ProductId);
public async Task <IActionResult> Create([FromBody] CartItemBm model) { model.UserId = User.Identity.GetUserId(); return((await _cartItemsService.CreateAsync(model)) .Match(ci => CreatedAtAction(nameof(Create), ci), ErrorContent)); }