public void AddOrder(order o, IEnumerable<orderproduct> product) { context.orders.Add(o); context.SaveChanges(); decimal total = 0; //добовляем сгенерированый ID foreach (var item in product) { item.Id_Order = o.Id_Order; if (o.id_IngridientsBuyer == 1) // повар { total += item.Price; } else { total += item.PriceWithIngridients==null?0:(decimal)item.PriceWithIngridients; } } o.total = total; context.orderproducts.AddRange(product); context.SaveChanges(); }
public ActionResult MakeOrder(OrderViewModel model) { if (!ModelState.IsValid) { ViewBag.Payments = DataManager.PaymentMethod.GetPaymentMethods(); ViewBag.Deliveries = DataManager.Delivery.GetDeliveryMethods(); ViewBag.Contacts = DataManager.Contact.GetContactMethods(); ViewBag.IngridientsBuyers = DataManager.IngridientsBuyer.GetIngridientsBuyers(); ViewBag.Cities = DataManager.Geolocation.GetAllCities(); return View(model); } var userId = User.Identity.GetUserId(); var cook = DataManager.User.GetUserById(model.idCook); var dish = DataManager.Dish.GetDishById(model.idDish); //защита от скул хакеров if(cook==null || dish == null || userId == cook.Id) { return RedirectToAction("index", "home"); } order result = new order { Address = model.Address, Comment = model.Comment, CreateTime = DateTime.Now, id_IngridientsBuyer = model.ingridientBuyer, DeadLine = model.DeadLine, Email = model.Email, FirstName = model.FirstName, Id_ContactMethod = model.contactMethod, Id_Cook = model.idCook, Id_Customer = userId, Id_Delivery = model.delivery, Id_PaymentMethod = model.paymentMethod, Id_Status = 1,// новый Phone = model.Phone, Surname = model.Surname, id_city = model.City }; List<orderproduct> resultProducts = new List<orderproduct>(); resultProducts.Add(new orderproduct { Id_Dish = dish.Id_Dish, Price = dish.Price, PriceWithIngridients = dish.PriceWithIngridient }); DataManager.Order.AddOrder(result, resultProducts); return RedirectToAction("success"); }
private OrderSerializerBody GetOrderInformation(order o) { string address = string.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority); UserSerealizerBody cook = GetUserInfoBuffered(o.Id_Cook); aspnetuser cust = DataManager.User.GetUserByIdBuffered(o.Id_Customer); UserSerealizerBody customer = new UserSerealizerBody { Id = o.Id_Customer, Email = o.Email, Address = o.Address, FirstName = o.FirstName, Surname = o.Surname, Phone = o.Phone, Username = cust == null ? null : cust.UserName, AvatarUrl = cust == null ? null : (address + cust.Avatar_Url) }; OrderSerializerBody res = new OrderSerializerBody { Comment = o.Comment, Comunication = o.ordercontactmethod.Name, Deadline = o.DeadLine == null ? null : ConvertToUnixTime((DateTime)o.DeadLine).ToString(), Delivery = o.orderdelivery.Name, FinishTime = o.FinishTime == null ? null : ConvertToUnixTime((DateTime)o.FinishTime).ToString(), Payment = o.orderpaymentmethod.Name, Status = o.orderstatu.Name, Cook = cook, Id_Order = o.Id_Order, Total = o.total, IngridientsBuyer = o.orderingridientbuyer.Name, Customer = customer }; return res; }