public static void SavingParts(PartsEditViewModel viewmodel, bool accessed, int Id, DbAccesPoint db) { using (var context = new ApplicationDbContext()) { var partA = (AmountPartsInStorage)null; var Part = (part)null; if (accessed == true && Id == 0) { partA = new AmountPartsInStorage(); Part = new part(); } else { partA = db.getAmountById(Id); Part = db.GetPartInfoByAmountId(Id); } partA.AmountInStorage = viewmodel.amountparts.AmountInStorage; Part.Brand = viewmodel.amountparts.Part.Brand; Part.Name = viewmodel.amountparts.Part.Name; Part.Type = viewmodel.amountparts.Part.Type; Part.Price = viewmodel.amountparts.Part.Price; context.parts.AddOrUpdate(Part); partA.Part = Part; context.amountParts.AddOrUpdate(partA); context.SaveChanges(); } }
public ActionResult CreateOrder(DetailsEditViewModel viewmodel) { using (var context = new ApplicationDbContext()) { PartsNeeded needed = new PartsNeeded(); var currentuser = User.Identity.Name; // Here is where a customer is made if an user doesnt have a customer linked to it already// if (context.customers.Where(c => c.user.UserName == currentuser) == null) { Customer AdminCustomer = new Customer(); AdminCustomer.user = context.Users.FirstOrDefault(c => c.UserName == currentuser); context.customers.AddOrUpdate(AdminCustomer); } if (User.IsInRole("Customer")) { // here the needed part with amount is set // needed.PartNeeded.Id = db.GetPartInfoByAmountId(viewmodel.order.parts.PartNeeded.Id).Id; needed.PartNeeded.Name = db.GetPartInfoByAmountId(viewmodel.order.parts.PartNeeded.Id).Name; needed.PartNeeded.Brand = db.GetPartInfoByAmountId(viewmodel.order.parts.PartNeeded.Id).Brand; needed.PartNeeded.Type = db.GetPartInfoByAmountId(viewmodel.order.parts.PartNeeded.Id).Type; needed.PartNeeded.Price = db.GetPartInfoByAmountId(viewmodel.order.parts.PartNeeded.Id).Price; needed.AmountNeeded = viewmodel.order.parts.AmountNeeded; needed.inStorage.AmountInStorage = context.amountParts.FirstOrDefault(p => p.Part.Id == needed.PartNeeded.Id).AmountInStorage; needed.inStorage.Part = needed.PartNeeded; //added part and amount that is needed // context.partsNeeded.AddOrUpdate(needed); // filling in the rest // viewmodel.order.parts = needed; viewmodel.order.customer = db.GetCustomerByUser(currentuser); viewmodel.order.EndDate = viewmodel.order.StartDate.AddDays(7); viewmodel.order.status = Status.Awaiting; viewmodel.order.StartDate = viewmodel.order.StartDate; viewmodel.order.EndDate = viewmodel.order.StartDate.AddDays(7); viewmodel.order.Description = viewmodel.order.Description; // sh*ts not working // if (ModelState.IsValid) { context.repairOrders.AddOrUpdate(viewmodel.order); context.SaveChanges(); return(RedirectToAction("Index")); } } else { needed.PartNeeded = db.GetPartInfoByAmountId(viewmodel.order.parts.PartNeeded.Id); needed.PartNeeded.Name = db.GetPartInfoByAmountId(viewmodel.order.parts.PartNeeded.Id).Name; needed.PartNeeded.Brand = db.GetPartInfoByAmountId(viewmodel.order.parts.PartNeeded.Id).Brand; needed.PartNeeded.Type = db.GetPartInfoByAmountId(viewmodel.order.parts.PartNeeded.Id).Type; needed.PartNeeded.Price = db.GetPartInfoByAmountId(viewmodel.order.parts.PartNeeded.Id).Price; needed.AmountNeeded = viewmodel.order.parts.AmountNeeded; needed.inStorage = context.amountParts.FirstOrDefault(p => p.Part.Id == needed.PartNeeded.Id); needed.inStorage.AmountInStorage = context.amountParts.FirstOrDefault(p => p.Part.Id == needed.PartNeeded.Id).AmountInStorage; needed.inStorage.Part = needed.PartNeeded; viewmodel.order.parts = needed; viewmodel.order.status = Status.Awaiting; viewmodel.order.StartDate = viewmodel.order.StartDate; viewmodel.order.EndDate = viewmodel.order.StartDate.AddDays(7); viewmodel.order.Description = viewmodel.order.Description; viewmodel.order.customer = viewmodel.order.customer; // Again, sh*ts not working // if (ModelState.IsValid) { context.repairOrders.AddOrUpdate(viewmodel.order); context.SaveChanges(); } return(RedirectToAction("Index")); } return(View()); } }