public async Task<IHttpActionResult> Put(long id, CartItem entity) { var user = await _authRepository.FindUser(HttpContext.Current.User as ClaimsPrincipal); var cartItem = await _cartRepository.GetAsync(id); if (cartItem == null) { return NotFound(); } if (cartItem.UserId != user.Id) { return StatusCode(HttpStatusCode.Forbidden); } if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != entity.Id) { return BadRequest(); } await _cartRepository.Update(entity); await _unitOfWork.CompleteAsync(); return StatusCode(HttpStatusCode.NoContent); }
public async Task<IHttpActionResult> Post(CartItem entity) { var user = await _authRepository.FindUser(HttpContext.Current.User as ClaimsPrincipal); var cart = new CartItem { ProductId = entity.ProductId, Product = await _unitOfWork.Products.GetAsync(entity.ProductId), UserId = user.Id, User = user }; if (cart.Product == null) { return StatusCode(HttpStatusCode.BadRequest); } if (!ModelState.IsValid) { return BadRequest(ModelState); } _cartRepository.Add(cart); await _unitOfWork.CompleteAsync(); var viewModel = new CartViewModel(); viewModel.Create(cart); return CreatedAtRoute("DefaultApi", new { id = entity.Id }, viewModel); }