public ActionResult BuyProducts() { var user = this.db.Users.GetById(User.Identity.GetUserId()); var shoppingCartId = user.ShoppingCart.Id; if (shoppingCartId == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Id is required"); } ShoppingCart currentCart = this.db.ShoppingCarts.All().FirstOrDefault(cart => cart.Id == shoppingCartId); if (currentCart == null) { return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Shopping cart, not found."); } if (currentCart.Auctions.Count == 0) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Cart is empty."); } int deliveryDurationTime = 0; Delivery createdDelivery = new Delivery { Id = Guid.NewGuid(), DateShipped = DateTime.Now, Receiver = currentCart.Owner, State = DeliveryState.Shipping }; int count = currentCart.Auctions.Count; foreach (Auction currentAuction in currentCart.Auctions.ToList()) { createdDelivery.Products.Add(currentAuction.Product); deliveryDurationTime += currentAuction.DeliveryDuration; this.db.Auctions.Delete(currentAuction); } int time = deliveryDurationTime / count; createdDelivery.Duration = time; currentCart.Auctions.Clear(); this.db.ShoppingCarts.Update(currentCart); this.db.Deliveries.Add(createdDelivery); this.db.SaveChanges(); return Content("Products are shipped"); }
private Delivery CreateDelivery(Auction currentAuction, string username) { ApplicationUser currentUser = this.db.Users.All().FirstOrDefault(user => user.UserName == username); if (currentUser == null) { throw new ArgumentNullException("User not found."); } Delivery createdDelivery = new Delivery { Id = Guid.NewGuid(), DateShipped = DateTime.Now, Duration = currentAuction.DeliveryDuration, Receiver = currentUser, State = DeliveryState.Shipping }; createdDelivery.Products.Add(currentAuction.Product); return createdDelivery; }