Ejemplo n.º 1
0
        public async Task <IActionResult> SignupSingle(DateModel date)
        {
            var user = await userManager.GetUserAsync(User);

            CalendarRepository repo       = new CalendarRepository(configModel.ConnectionString);
            AttendanceModel    attendance = repo.GetSingleAttendance(user.VolunteerId, date.Date);

            if (date.Date.DayOfWeek != DayOfWeek.Saturday)
            {
                return(Utilities.ErrorJson("Provided date was not a Saturday"));
            }

            if (attendance != null && attendance.Scheduled)
            {
                return(Utilities.ErrorJson("You are already scheduled on this date"));
            }

            if (attendance == null)
            {
                try
                {
                    repo.InsertAttendance(new AttendanceModel
                    {
                        VolunteerId = user.VolunteerId,
                        DayAttended = date.Date,
                        Scheduled   = true,
                        Attended    = false,
                    });
                }
                catch (Exception e)
                {
                    return(Utilities.ErrorJson(e.Message));
                }
            }
            else
            {
                try
                {
                    repo.UpdateScheduled(attendance.Id, true);
                }
                catch (Exception e)
                {
                    return(Utilities.ErrorJson(e.Message));
                }
            }

            return(new JsonResult(new
            {
                Error = ""
            }));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> CancelSingle(DateModel date)
        {
            var user = await userManager.GetUserAsync(User);

            CalendarRepository  repo          = new CalendarRepository(configModel.ConnectionString);
            VolunteerRepository volunteerRepo = new VolunteerRepository(configModel.ConnectionString);
            AttendanceModel     attendance    = repo.GetSingleAttendance(user.VolunteerId, date.Date);
            VolunteerModel      profile;

            if (date.Date.DayOfWeek != DayOfWeek.Saturday)
            {
                return(Utilities.ErrorJson("Provided date was not a Saturday"));
            }

            if (User.IsInRole(UserHelpers.UserRoles.BusDriver.ToString()))
            {
                try
                {
                    profile = volunteerRepo.GetVolunteer(user.VolunteerId);
                    await EmailHelpers.SendEmail("*****@*****.**", "Bus Driver Cancellation",
                                                 $"A bus driver ({profile.FirstName} {profile.LastName}) has been marked as not attending on {date.Date.ToString("dd/MM/yyyy")}.  Please update bus assignments accordingly.",
                                                 configModel.EmailOptions);
                }
                catch (Exception e)
                {
                    return(Utilities.ErrorJson(e.Message));
                }
            }
            // If the user is a volunteer, has not previously scheduled themselves for this day, and is not a class teacher, no need to hit the DB,
            // Since volunteers are assumed to not be present until they indicate otherwise
            else if (User.IsInRole(UserHelpers.UserRoles.Volunteer.ToString()) && (attendance == null || !attendance.Scheduled) && !volunteerRepo.VolunteerIsClassTeacher(user.VolunteerId))
            {
                return(Utilities.ErrorJson("You are not currently scheduled on this date"));
            }

            try
            {
                if (attendance != null)
                {
                    repo.UpdateScheduled(attendance.Id, false);
                }
                else
                {
                    repo.InsertAttendance(new AttendanceModel
                    {
                        VolunteerId = user.VolunteerId,
                        DayAttended = date.Date,
                        Scheduled   = false,
                        Attended    = false,
                    });
                }
            }
            catch (Exception e)
            {
                return(Utilities.ErrorJson(e.Message));
            }

            return(new JsonResult(new
            {
                Error = ""
            }));
        }