public async Task <UserSessionCartDto> SaveItemToCart(UserSessionCartDto userSessionDto) { var session = _appDbContext.UserSessions.FirstOrDefault(a => a.Id == userSessionDto.UserSessionId); if (session == null) { var userSession = new UserSession() { Id = userSessionDto.UserSessionId }; _appDbContext.UserSessions.Add(userSession); await _appDbContext.SaveChangesAsync(); } var item = _mapper.Map <CartItem>(userSessionDto); var item2 = _appDbContext.CartItems.Where(a => a.UserSessionId == userSessionDto.UserSessionId && a.ProductId == userSessionDto.ProductId && a.Size.Contains(userSessionDto.Size) && a.OrderId == null).FirstOrDefault(); if (item2 != null) { item2.Count += userSessionDto.Count; item2.Sum += userSessionDto.Sum; _appDbContext.CartItems.Update(item2); await _appDbContext.SaveChangesAsync(); userSessionDto.Count = item2.Count; userSessionDto.Sum = item2.Sum; } else { _appDbContext.CartItems.Add(item); await _appDbContext.SaveChangesAsync(); } return(userSessionDto); }
public async Task <UserSessionCartDto> SaveToCart(UserSessionCartDto userSessionDto, List <UserTextSessionDto> userTextSessionDto) { var session = _appDbContext.UserSessions.FirstOrDefault(a => a.Id == userSessionDto.UserSessionId); if (session == null) { var userSession = new UserSession() { Id = userSessionDto.UserSessionId }; _appDbContext.UserSessions.Add(userSession); await _appDbContext.SaveChangesAsync(); } var item = _mapper.Map <CartItem>(userSessionDto); _appDbContext.CartItems.Add(item); await _appDbContext.SaveChangesAsync(); var textList = _mapper.Map <List <TextProperty> >(userTextSessionDto); textList.ForEach(a => a.CartItemId = item.Id); _appDbContext.TextProperties.AddRange(textList); await _appDbContext.SaveChangesAsync(); return(userSessionDto); }
public async Task DeleteItemFromCart(string sessionId, UserSessionCartDto userSessionDto) { var item = _appDbContext.CartItems.Where(a => a.UserSessionId == sessionId && a.ProductId == userSessionDto.ProductId && a.Size.Contains(userSessionDto.Size) && a.OrderId == null).FirstOrDefault(); if (item != null) { var textPropertiesList = await _appDbContext.TextProperties.Where(a => a.UserSessionId == sessionId && a.CartItemId == item.Id && a.OrderId == null).ToListAsync(); _appDbContext.CartItems.Remove(item); _appDbContext.TextProperties.RemoveRange(textPropertiesList); await _appDbContext.SaveChangesAsync(); } }
public async Task <UserSessionCartDto> UpdateCart(string sessionId, UserSessionCartDto userSessionCartDto) { var item = _appDbContext.CartItems.Where(a => a.UserSessionId == sessionId && a.ProductId == userSessionCartDto.ProductId && a.Size == userSessionCartDto.Size && a.OrderId == null).FirstOrDefault(); if (item != null) { item.Count = userSessionCartDto.Count; item.Sum = item.Price * item.Count; userSessionCartDto.Price = item.Price; userSessionCartDto.Sum = item.Sum; userSessionCartDto.Size = item.Size; } await _appDbContext.SaveChangesAsync(); return(userSessionCartDto); }