public async Task <ActivityCart> AddToCart(int activityId, string userEmail = null) { var cartItem = _dbContext.ActivityCart.Any(x => x.Username == userEmail) ? _dbContext.ActivityCart.AsNoTracking().Where(x => x.Username == userEmail).FirstOrDefault() : null; List <ActivityCartDTO> dtoLst = new List <ActivityCartDTO>(); if (cartItem != null) { if (!string.IsNullOrEmpty(cartItem.ItemDetails)) { dtoLst = JsonConvert.DeserializeObject <List <ActivityCartDTO> >(cartItem.ItemDetails); } } var activityDetails = _dbContext.Activities.Where(x => x.activity_id == activityId).FirstOrDefault(); ActivityCartDTO dto = new ActivityCartDTO { ActivityID = activityDetails.activity_id, ActivityDescription = activityDetails.activity_description, ActivityFee = activityDetails.activity_fee, ActivityName = activityDetails.activity_name, ActivityImage = activityDetails.activity_image_path, Quantity = 1 }; dtoLst.Add(dto); string itemJson = JsonConvert.SerializeObject(dtoLst); if (string.IsNullOrEmpty(userEmail)) { return(null); } ActivityCart entity = new ActivityCart { ItemDetails = itemJson, Username = userEmail }; ActivityCart result = new ActivityCart(); if (cartItem != null) { entity.Id = cartItem.Id; var exeResult = _dbContext.ActivityCart.Update(entity); result = exeResult.Entity; } else { var exeResult = await _dbContext.ActivityCart.AddAsync(entity); result = exeResult.Entity; } await _dbContext.SaveChangesAsync(); return(result); }
private ActivityCartDTO FetchCartDTO(int activityID) { var activityDetails = _dbContext.Activities.Where(x => x.activity_id == activityID).FirstOrDefault(); ActivityCartDTO dto = new ActivityCartDTO { ActivityID = activityDetails.activity_id, ActivityDescription = activityDetails.activity_description, ActivityFee = activityDetails.activity_fee, ActivityName = activityDetails.activity_name, Quantity = 1 }; return(dto); }
//TODO : create view -> go to cart / continue shopping (home) public async Task <ActionResult> AddToCart(int activityId) { string userEmail = string.Empty; if (!string.IsNullOrEmpty(HttpContext.Session.GetString("CurrentUser"))) { userEmail = HttpContext.Session.GetString("CurrentUser"); } var result = await _shopping.AddToCart(activityId, userEmail); var vmObj = ActivityCartDTO.TransformcartItem(result, activityId); return(View(vmObj)); }