public async Task <IActionResult> Edit(int id, AppointmentDetialsViewModel objAppointment) { if (ModelState.IsValid) { objAppointment.Appointment.AppointmentDate = objAppointment.Appointment.AppointmentDate .AddHours(objAppointment.Appointment.AppointmentTime.Hour) .AddMinutes(objAppointment.Appointment.AppointmentTime.Minute); var appointmentDb = db.Appointments.Where(a => a.Id == objAppointment.Appointment.Id).FirstOrDefault(); var ProductsList = (IEnumerable <Products>)(from p in db.Products join pa in db.ProductSelectedForAppointment on p.Id equals pa.ProductId where pa.AppointmentId == id select p).Include("ProductType"); appointmentDb.CustomerName = objAppointment.Appointment.CustomerName; appointmentDb.CustomerEmail = objAppointment.Appointment.CustomerEmail; appointmentDb.CustomerPhoneNumber = objAppointment.Appointment.CustomerPhoneNumber; appointmentDb.AppointmentDate = objAppointment.Appointment.AppointmentDate; appointmentDb.IsConfirmed = objAppointment.Appointment.IsConfirmed; foreach (var prod in ProductsList) { int oldQuantity = prod.Quantity; if (objAppointment.Appointment.IsConfirmed == true) { prod.Quantity = prod.Quantity - 1; } else { prod.Quantity = prod.Quantity + 1; } } appointmentDb.SalesPersonId = objAppointment.Appointment.SalesPersonId; await db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(objAppointment)); }
public IActionResult Delete(int?id) { if (id == null) { return(NotFound()); } var ProductsList = (IEnumerable <Products>)(from p in db.Products join pa in db.ProductSelectedForAppointment on p.Id equals pa.ProductId where pa.AppointmentId == id select p).Include("ProductType"); AppointmentDetialsViewModel appointmentDetailsView = new AppointmentDetialsViewModel() { Appointment = db.Appointments.Include(a => a.applicationUser).Where(a => a.Id == id).FirstOrDefault(), SalesPerson = db.ApplicationUser.ToList(), Products = ProductsList.ToList() }; return(View(appointmentDetailsView)); }