public async Task <IActionResult> AddToCart(int?flowerId) { HouseUser currnetUser = await GetCurrentUser(); if (!flowerId.HasValue) { return(Json(new { status = 0, msg = "添加失败" })); } if (currnetUser == null) { return(Json(new { status = 0, msg = "请先登录" })); } var flowerAddToCart = await _flowerHouseContext.Flowers.Include(f => f.Stock).FirstOrDefaultAsync(f => f.FlowerId == flowerId.Value); var shoppingCart = await GetShoppingCartAsync(currnetUser); if (shoppingCart == null) { shoppingCart = new ShoppingCart { HouseUserId = currnetUser.Id, TotalPrice = 0, CartItems = new List <CartItem>() }; await _flowerHouseContext.AddAsync(shoppingCart); } var exitedCartItem = shoppingCart.CartItems.FirstOrDefault(ci => ci.FlowerId == flowerId.Value); if (exitedCartItem == null) { exitedCartItem = new CartItem { Count = 1, IsChecked = true, Flower = flowerAddToCart }; shoppingCart.CartItems.Add(exitedCartItem); } else { exitedCartItem.Count += 1; exitedCartItem.IsChecked = true; } ComputeTotalPrice(shoppingCart); await _flowerHouseContext.SaveChangesAsync(); return(Json(new { status = 1, msg = "添加成功" })); }
public async Task <IActionResult> CreateOrUpdate(Tag tag) { var updateTag = await flowerHouseContext.Tags.FirstOrDefaultAsync(t => t.TagId == tag.TagId); if (updateTag == null) { await flowerHouseContext.AddAsync(tag); } else { updateTag.TagName = tag.TagName; updateTag.TagImg = tag.TagImg; updateTag.AssortmentId = tag.AssortmentId; } await flowerHouseContext.SaveChangesAsync(); return(RedirectToAction("List")); }
public async Task<IActionResult> CreateOrUpdate(OrderItem orderItem) { //flowerHouseContext.Entry(flower).State = (flowerHouseContext.Flowers.Any(f => f.FlowerId == flower.FlowerId) ? EntityState.Modified : EntityState.Added); var updateEntity = await flowerHouseContext.OrderItems.FirstOrDefaultAsync(o => o.OrderItemId == orderItem.OrderItemId); if (updateEntity == null) { updateEntity = orderItem; await flowerHouseContext.AddAsync(updateEntity); } else { updateEntity.Count = orderItem.Count; updateEntity.FlowerId = orderItem.FlowerId; updateEntity.OrderId = orderItem.OrderId; flowerHouseContext.Update(updateEntity); } await flowerHouseContext.SaveChangesAsync(); return RedirectToAction("List"); }
public async Task <IActionResult> CreateOrUpdate(Flower flower, int[] tagid) { var updateFlower = await flowerHouseContext.Flowers.Include(f => f.Stock).FirstOrDefaultAsync(f => f.FlowerId == flower.FlowerId); if (updateFlower == null) { updateFlower = flower; updateFlower.FlowerBuy = -1; //Purchase purchase = new Purchase //{ // Flower = updateFlower, // PurchaseCount = updateFlower.FlowerCount, // PurchasePrice = updateFlower.FlowerCount * updateFlower.FlowerPrice, // PurchaseTime = DateTime.Now //}; //await flowerHouseContext.AddAsync(purchase); await flowerHouseContext.AddAsync(updateFlower); } else { updateFlower.FlowerName = flower.FlowerName; updateFlower.FlowerMaterial = flower.FlowerMaterial; updateFlower.FlowerEmblem = flower.FlowerEmblem; updateFlower.DistributionRange = flower.DistributionRange; updateFlower.FlowerBuy = flower.FlowerBuy; //updateFlower.FlowerPrice = flower.FlowerPrice; updateFlower.FlowerImg = flower.FlowerImg; flowerHouseContext.Update(updateFlower); } updateFlower.FlowerTags = new List <FlowerTag>(); foreach (int tid in tagid) { updateFlower.FlowerTags.Add(new FlowerTag { FlowerId = updateFlower.FlowerId, TagId = tid }); } await flowerHouseContext.SaveChangesAsync(); return(RedirectToAction("List")); }
public async Task <IActionResult> CreateOrUpdate(Order order) { //flowerHouseContext.Entry(flower).State = (flowerHouseContext.Flowers.Any(f => f.FlowerId == flower.FlowerId) ? EntityState.Modified : EntityState.Added); var updateEntity = await flowerHouseContext.Orders.FirstOrDefaultAsync(o => o.OrderId == order.OrderId); if (updateEntity == null) { await flowerHouseContext.AddAsync(order); } else { updateEntity.CreateTime = order.CreateTime; updateEntity.TimeOutTime = order.CreateTime.Value.AddMinutes(30); updateEntity.FinishTime = order.FinishTime; updateEntity.TotalPrice = order.TotalPrice; updateEntity.Address = order.Address; updateEntity.HouseUserId = order.HouseUserId; updateEntity.OrderStatus = order.OrderStatus; flowerHouseContext.Update(updateEntity); } await flowerHouseContext.SaveChangesAsync(); return(RedirectToAction("List")); }