Beispiel #1
0
        public async Task <IActionResult> SignupEvent(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 there is no record of the user signing up, insert a new record into the database
                try
                {
                    repo.CreateEventSignup(eventId, volunteer.Id);
                }
                catch (Exception e)
                {
                    return(Utilities.ErrorJson(e.Message));
                }
            }
            else
            {
                // Otherwise the user is already signed up, so we want to tell them
                return(Utilities.ErrorJson("You are already signed up for this event"));
            }

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