public async Task <IActionResult> VolunteerJobAssignment(JobAssignmentViewModel vm) { var user = await userManager.GetUserAsync(User); VolunteerRepository repo = new VolunteerRepository(configModel.ConnectionString); VolunteerJobModel job; // This endpoint should only be accessible if the user is a staff member or if the user is trying to sign themselves up for a job if (user.VolunteerId != vm.VolunteerId && !User.IsInRole(UserHelpers.UserRoles.Staff.ToString())) { return(Utilities.ErrorJson("Unauthorized")); } if (vm.Date == DateTime.MinValue) { return(Utilities.ErrorJson("Must specify a date")); } if (vm.Date.DayOfWeek != DayOfWeek.Saturday) { return(Utilities.ErrorJson("Jobs can only be signed up for for Saturdays")); } // The first check is so we can skip a call to the database if the user is signing up for a job on their own - clearly the user id is valid in that case if (vm.VolunteerId != user.VolunteerId && repo.GetVolunteer(vm.VolunteerId) == null) { return(Utilities.ErrorJson("Invalid volunteer id")); } try { job = repo.GetVolunteerJob(vm.JobId, vm.Date); } catch (Exception e) { return(Utilities.ErrorJson(e.Message)); } if (job == null) { return(Utilities.ErrorJson("Invalid volunteer job id")); } if (repo.CheckSignedUpForJob(vm.JobId, vm.VolunteerId, vm.Date)) { return(Utilities.ErrorJson("Already signed up")); } if (job.CurrentNumber >= job.Max) { return(Utilities.ErrorJson("Too many people are already signed up for this job")); } try { repo.AddVolunteerJobAssignment(vm.VolunteerId, vm.JobId, vm.Date); } catch (Exception e) { return(Utilities.ErrorJson(e.Message)); } return(Utilities.NoErrorJson()); }