public static MakeAppointmentViewModel GenerateAppointmentViewModelDropdowns(DentalBaseContext context)
        {
            var model     = new MakeAppointmentViewModel();
            var surgeries = context.Surgeries.DistinctBy(x => x.Name);

            model.Surgeries.AddRange(surgeries);
            model.Teeth.AddRange(context.Set <Tooth>());
            model.Patients.AddRange(context.Set <AppUser>());
            foreach (var item in model.Surgeries)
            {
                model.SurgeryChoice.Add(new SelectListItem {
                    Text = item.Name, Value = item.SurgeryId.ToString()
                });
            }
            foreach (var item in model.Teeth)
            {
                model.ToothChoice.Add(new SelectListItem {
                    Text = item.Name, Value = item.Id.ToString()
                });
            }
            foreach (var item in model.Patients)
            {
                if (item.FirstName != null && item.LastName != null)
                {
                    var text = $"{item.FirstName} {item.LastName}";
                    model.PatientChoice.Add(new SelectListItem {
                        Text = text, Value = item.Id.ToString()
                    });
                }
            }
            return(model);
        }
Esempio n. 2
0
        public ActionResult Save(MakeAppointmentViewModel model)
        {
            var visit = new Visit();

            visit.Date    = model.Date.Value;
            visit.Patient = _context.Set <AppUser>().Where(x => x.Id == model.PatientID.ToString()).FirstOrDefault();
            for (int i = 0; i < model.SurgeriesIDs.Count; i++)
            {
                var surgeryID     = model.SurgeriesIDs[i];
                var toothID       = model.TeethIDs[i];
                var name          = _context.Set <Surgery>().Where(x => x.SurgeryId == surgeryID).FirstOrDefault().Name;
                var estimatedTime = _context.Set <Surgery>().Where(x => x.SurgeryId == surgeryID).FirstOrDefault().EstimatedTime;
                var price         = _context.Set <Surgery>().Where(x => x.SurgeryId == surgeryID).FirstOrDefault().Price;
                var tooth         = _context.Set <Tooth>().Where(x => x.Id == toothID).FirstOrDefault();
                if (visit.Surgeries == null)
                {
                    visit.Surgeries = new List <Surgery>();
                }
                visit.Surgeries.Add(new Surgery
                {
                    Name          = name,
                    EstimatedTime = estimatedTime,
                    Price         = price,
                    Tooth         = tooth
                });
            }
            _context.Surgeries.AddRange(visit.Surgeries);
            _context.Visits.Add(visit);
            _context.SaveChanges();

            return(RedirectToAction("Index", "Home"));
        }