private void MapForUpdateEntity(BL.CartLine entity, DA.CartLine daEntity) { daEntity.Id = entity.Id; daEntity.Price = entity.Price; daEntity.Quantity = entity.Quantity; daEntity.UserId = entity.UserId; //daEntity.User = _mapper.Map<DA.User>(entity); daEntity.ProductId = entity.ProductId; //daEntity.Product = _mapper.Map<DA.Product>(entity); }
public async Task <IActionResult> Post(int id) { try { var product = await _productRepo.GetByIdAsync(id); if (product == null) { return(NotFound("Item is not found.")); } var user = _userServ.GetUser(); //if (user == null) // return Unauthorized("User is not authentificated."); var cartLineList = await _cartLineRepo.FindByAsync(c => c.UserId == user.Id && c.ProductId == product.Id); var cartLine = cartLineList.FirstOrDefault(); if (cartLine == null) { BL.CartLine newItem = new BL.CartLine { Quantity = 1, ProductId = product.Id, Price = product.Price, UserId = user.Id }; var newItemId = await _cartLineRepo.SaveAsync(newItem); newItem = await _cartLineRepo.GetByIdAsync(newItemId); return(Ok(newItem)); } else { cartLine.Quantity += 1; await _cartLineRepo.SaveAsync(cartLine); return(Ok(cartLine)); } } catch (Exception ex) { return(BadRequest($"{ex.Message}")); } }
public async Task <IActionResult> Delete(int id) { try { BL.CartLine cartLine = await _cartLineRepo.GetByIdAsync(id); if (cartLine != null) { await _cartLineRepo.DeleteAsync(id); return(Ok(cartLine)); } return(BadRequest("Item not found.")); } catch (Exception ex) { return(BadRequest($"{ex.Message}")); } }
public async Task <int> SaveAsync(BL.CartLine entity) { try { if (entity == null) { return(0); } using (var context = _contextFactory.GetProductContext()) { var entityModel = await context .CartLines .FirstOrDefaultAsync(item => item.Id.Equals(entity.Id)); if (entityModel == null) { entityModel = new DA.CartLine(); MapForUpdateEntity(entity, entityModel); await context.CartLines.AddAsync(entityModel); } else { MapForUpdateEntity(entity, entityModel); } context.SaveChanges(); return(entityModel.Id); } } catch (Exception ex) { throw ex; } }