public async Task <IActionResult> Update(int id, [FromBody] BasketItemInputModel model) { var oldBasketItem = ctx.BasketItems .Where(b => b.Id == id && b.AppUserId == UserId) .Include(b => b.Product) .FirstOrDefault(); if (oldBasketItem == null) { return(NotFound()); } if (model.Units <= 0) { return(BadRequest("Units should be more than zero.")); } oldBasketItem.Units = model.Units; if (await ctx.SaveChangesAsync() > 0) { return(Ok(mapper.Map <BasketItemModel>(oldBasketItem))); } return(BadRequest()); }
public async Task <IActionResult> Create([FromBody] BasketItemInputModel model) { var entity = mapper.Map <BasketItem>(model); entity.AppUserId = UserId; ctx.Add(entity); if (await ctx.SaveChangesAsync() > 0) { var url = Url.Link("BasketItemGet", new { id = entity.Id }); entity = ctx.BasketItems.Where(i => i.Id == entity.Id).Include(i => i.Product).First(); return(Created(url, mapper.Map <BasketItemModel>(entity))); } return(BadRequest()); }