Exemplo n.º 1
0
        public async Task <IActionResult> CancelEvent(EventViewModel eventViewModel)
        {
            int eventId = eventViewModel.eventId;
            var user    = await userManager.GetUserAsync(User);

            VolunteerRepository volRepo = new VolunteerRepository(configModel.ConnectionString);
            CalendarRepository  repo    = new CalendarRepository(configModel.ConnectionString);
            VolunteerModel      volunteer;
            EventModel          eventModel;
            EventSignupModel    signup;

            // Ensure that ONLY volunteer, volunteer captain, and bus driver accounts have access to this API endpoint
            if (user == null ||
                !(await userManager.IsInRoleAsync(user, UserHelpers.UserRoles.Volunteer.ToString()) ||
                  await userManager.IsInRoleAsync(user, UserHelpers.UserRoles.VolunteerCaptain.ToString()) ||
                  await userManager.IsInRoleAsync(user, UserHelpers.UserRoles.BusDriver.ToString())))
            {
                return(Utilities.ErrorJson("Event signup is available only to volunteers, volunteer captains, and bus drivers"));
            }

            volunteer = volRepo.GetVolunteer(user.VolunteerId);

            if (volunteer == null)
            {
                return(Utilities.ErrorJson("Unable to find volunteer profile"));
            }

            eventModel = repo.GetEvent(eventId);

            // Verify that the specified event exists
            if (eventModel == null)
            {
                return(Utilities.ErrorJson("Specified event does not exist."));
            }

            // Check if there is already a record of this user having signed up
            signup = repo.GetEventSignup(eventId, volunteer.Id);

            if (signup == null)
            {
                // If no record exists of the user signing up, or they have already cancelled, then let them know
                return(Utilities.ErrorJson("You are not signed up for this event"));
            }
            else
            {
                // Otherwise, update the record to indicate non-attendance
                repo.DeleteEventSignup(eventId, volunteer.Id);
            }

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