Example #1
0
        public IActionResult Index()
        {
            UserScheduleViewModel model = new UserScheduleViewModel();

            // Find our user with the auth token.
            var user = _userManager.GetUserAsync(User).Result;

            // Grab schedule from db.
            var sectionIds = _context.UserSections
                             .Where(us => us.User == user)
                             .Select(us => us.SectionId);

            // If we have no courses to show, why don't we just stop here?
            if (sectionIds.Count() == 0)
            {
                var emptyReturn = new UserScheduleViewModel
                {
                    CourseListItems   = new List <CourseListItemViewModel>(),
                    ScheduleViewModel = new ScheduleViewModel()
                };
                return(View(emptyReturn));
            }

            var schedule = _context.Sections
                           .Include(s => s.Course)
                           .ThenInclude(c => c.User)
                           .Include(s => s.Course)
                           .ThenInclude(c => c.Sections)
                           .ThenInclude(s => s.Professor)
                           .Include(s => s.Course)
                           .ThenInclude(c => c.Sections)
                           .ThenInclude(s => s.Meetings)
                           .ThenInclude(m => m.Location)
                           .Include(s => s.Course)
                           .ThenInclude(c => c.Cape)
                           .ThenInclude(ca => ca.Professor)
                           .ThenInclude(p => p.RateMyProfessor)

                           .Where(s => sectionIds.Contains(s.Id))
                           .ToList();

            // Populate model with schedule info.
            if (schedule != null)
            {
                model.ScheduleViewModel = FormatRepo.FormatSectionsToCalendarEvent(schedule);
                model.CourseListItems   = schedule.Select(s => new CourseListItemViewModel
                {
                    CourseId           = s.Course.Id,
                    CourseAbbreviation = s.Course.CourseAbbreviation,
                    IsCustomEvent      = s.Course.User != null
                }).ToList();
            }
            // We shouldn't ever get here, because if a user has a user section, then schedule shouldn't be null.
            else
            {
                model.ScheduleViewModel = new ScheduleViewModel();
            }

            return(View(model));
        }
        private async Task <UserScheduleViewModel> PrepareResult(int week)
        {
            string userId       = this.userManager.GetUserId(User);
            var    appointments = await this.appointments.GetAllByUser(userId);

            var appointmentsViewModel = Mapper.Map <IEnumerable <AppointmentServiceModel>, IList <AppointmentViewModel> >(appointments);
            var weekViewModel         = new WeeksViewModel
            {
                FirstDayOfWeek = GetDateByWeek(week),
                WeekNumber     = week,
            };

            var userSchedule = new UserScheduleViewModel
            {
                Week         = weekViewModel,
                Appointments = appointmentsViewModel
            };

            return(userSchedule);
        }
        public async Task <IActionResult> EditAppointment(UserScheduleViewModel model)
        {
            var newDateTime = new DateTime(
                model.EditAppointment.NewStartDay.Year,
                model.EditAppointment.NewStartDay.Month,
                model.EditAppointment.NewStartDay.Day,
                model.EditAppointment.NewStartTime.Hour,
                model.EditAppointment.NewStartTime.Minute,
                0);

            newDateTime = RoundTime(newDateTime);

            var currentStartDateTime = Convert.ToDateTime(model.EditAppointment.StartTime);

            if (newDateTime != currentStartDateTime)
            {
                string userId           = this.userManager.GetUserId(User);
                var    validationChecks = await this.appointments.AlreadyScheduled(newDateTime, userId);

                if (validationChecks)
                {
                    this.TempData.AddErrorMessage(WebConstants.AppointmentDateTimeReservedMessage);
                    TempData["appointmentId"] = model.EditAppointment.Id;
                    return(RedirectToAction(nameof(ByWeek)));
                }
            }

            await this.appointments.Edit(
                model.EditAppointment.Id,
                model.EditAppointment.Title,
                model.EditAppointment.Description,
                newDateTime,
                model.EditAppointment.PatientId);

            this.TempData.AddSuccessMessage(WebConstants.AppointmentEditSuccessMessage);

            var week         = GetWeek(Convert.ToDateTime(model.EditAppointment.StartTime));
            var userSchedule = await PrepareResult(week);

            return(View(nameof(ByWeek), userSchedule));
        }
        public async Task <IActionResult> NewAppointment(UserScheduleViewModel model)
        {
            if (String.IsNullOrEmpty(model.EditAppointment.Title) ||
                String.IsNullOrEmpty(model.EditAppointment.Description) ||
                String.IsNullOrEmpty(model.EditAppointment.StartTime))
            {
                this.TempData.AddErrorMessage(WebConstants.AppointmentCreateFailMessage);

                return(RedirectToAction(nameof(ByWeek)));
            }

            var modelPatientId = model.EditAppointment.PatientId;

            string patientId = null;

            if (!String.IsNullOrEmpty(modelPatientId))
            {
                var patient = await this.userManager.FindByIdAsync(modelPatientId);

                patientId = patient.Id;
            }

            var userId = this.userManager.GetUserId(User);

            var startTime = Convert.ToDateTime(model.EditAppointment.StartTime);

            startTime = RoundTime(startTime);
            var result = await this.appointments.AddAppointment(
                model.EditAppointment.Title,
                model.EditAppointment.Description,
                startTime,
                userId,
                patientId
                );

            this.TempData.AddSuccessMessage(WebConstants.AppointmentCreateSuccessMessage);


            return(RedirectToAction(nameof(ByWeek)));
        }
        public async Task <IActionResult> ByWeek(UserScheduleViewModel model)
        {
            var userId = this.userManager.GetUserId(User);

            var scheduleUser = await this.appointments.CheckUserForSchedule(userId);

            if (!scheduleUser)
            {
                return(RedirectToAction(nameof(UsersController.CreateSchedule), "Users", new { area = string.Empty }));
            }

            var week = model.Week.WeekNumber;

            if (week == 0)
            {
                week = GetWeek(DateTime.UtcNow);
            }

            var userSchedule = await PrepareResult(week);

            return(View(userSchedule));
        }