コード例 #1
0
        public async Task <IActionResult> Create([Bind("ID,AspNetUserId,FirstName,LastName,Username,Password,DOB,UTSID,History,Role")] User user)
        {
            if (ModelState.IsValid)
            {
                user.AspNetUserId = Common.GetUserAspNetId(User);
                _context.Add(user);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(user));
        }
コード例 #2
0
        public async Task <IActionResult> Create([Bind("ID,Notes,Location,Time,DoctorID,PatientID")] Appointment appointment)
        {
            //Link Patient ID if Patient Using System
            if (appointment.PatientID == -1)
            {
                var pat = from u in _context.Users
                          where u.AspNetUserId == Common.GetUserAspNetId(User)
                          select u.ID;
                if (pat.Count() != 0)
                {
                    appointment.PatientID = pat.FirstOrDefault();
                }
            }


            //Handle No Doctor Preference
            if (appointment.DoctorID == -1)
            {
                var busyDoctors = from s in _context.Appointments
                                  where s.Time == appointment.Time
                                  select s.DoctorID;

                var freeDoctors = from d in _context.Users
                                  where d.Role == "Doctor" && busyDoctors.All(b => b != d.ID)
                                  select d.ID;

                if (freeDoctors.Count() != 0)
                {
                    appointment.DoctorID = freeDoctors.First();
                }
            }

            if (ModelState.IsValid)
            {
                // All Appt Details Valid - Save and Redirect
                _context.Add(appointment);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            // Error - Reload Page

            List <SelectListItem> dList = _context.Users.Where(d => d.Role == "Doctor").Select(d => new SelectListItem
            {
                Value = d.ID.ToString(),
                Text  = "Dr. " + d.FirstName.Substring(0, 1) + ". " + d.LastName
            }).ToList();
            List <SelectListItem> pList = _context.Users.Where(p => p.Role == "Patient").Select(p => new SelectListItem
            {
                Value = p.ID.ToString(),
                Text  = p.LastName + ", " + p.FirstName
            }).ToList();

            dList.Insert(0, (new SelectListItem {
                Text = "No Preference", Value = "-1"
            }));

            ViewData["DoctorID"]  = dList;
            ViewData["PatientID"] = pList;

            var curUserRole = from u in _context.Users
                              where u.AspNetUserId == (Common.GetUserAspNetId(User))
                              select u.Role.ToString();

            if (!String.IsNullOrEmpty(curUserRole.FirstOrDefault()))
            {
                ViewData["Role"] = curUserRole.FirstOrDefault();
            }
            else
            {
                ViewData["Role"] = "None";
            }

            return(View());
        }