public IHttpActionResult ChangeEditor(int applicationID, [FromBody] ApartmentApplicationViewModel applicationDetails) { // Verify Input if (!ModelState.IsValid) { string errors = ""; foreach (ModelState modelstate in ModelState.Values) { foreach (ModelError error in modelstate.Errors) { errors += "|" + error.ErrorMessage + "|" + error.Exception; } } throw new BadInputException() { ExceptionMessage = errors }; } //get token data from context, username is the username of current logged in person ClaimsPrincipal authenticatedUser = ActionContext.RequestContext.Principal as ClaimsPrincipal; string username = authenticatedUser.Claims.FirstOrDefault(x => x.Type == "user_name").Value; string newEditorUsername = applicationDetails?.EditorProfile?.AD_Username ?? applicationDetails?.EditorUsername; bool result = _housingService.ChangeApplicationEditor(username, applicationID, newEditorUsername); return(Ok(result)); }
public IHttpActionResult GetApartmentApplication(int applicationID) { //get token data from context, username is the username of current logged in person ClaimsPrincipal authenticatedUser = ActionContext.RequestContext.Principal as ClaimsPrincipal; string username = authenticatedUser.Claims.FirstOrDefault(x => x.Type == "user_name").Value; string userID = _accountService.GetAccountByUsername(username).GordonID; bool isAdmin = false; try { ADMIN adminModel = _administratorService.Get(userID); isAdmin = (adminModel != null); } catch { isAdmin = _housingService.CheckIfHousingAdmin(userID); } ApartmentApplicationViewModel result = _housingService.GetApartmentApplication(applicationID, isAdmin); if (result != null) { return(Ok(result)); } else { return(NotFound()); } }
/// <returns>Array of ApartmentApplicationViewModel Objects</returns> public ApartmentApplicationViewModel[] GetAllApartmentApplication() { IEnumerable <ApartmentAppIDViewModel> appIDsResult = null; appIDsResult = RawSqlQuery <ApartmentAppIDViewModel> .query("GET_AA_CURRENT_APP_IDS"); if (appIDsResult == null || !appIDsResult.Any()) { throw new ResourceNotFoundException() { ExceptionMessage = "The application could not be found." }; } List <ApartmentApplicationViewModel> applicationList = new List <ApartmentApplicationViewModel>(); foreach (ApartmentAppIDViewModel appIDModel in appIDsResult) { ApartmentApplicationViewModel apartmentApplicationModel = null; try { apartmentApplicationModel = GetApartmentApplication(appIDModel.AprtAppID, true); if (apartmentApplicationModel != null) { applicationList.Add(apartmentApplicationModel); } } catch { // Do nothing, simply skip this application // TODO: condsider how to report this error for the case where the application exists but something went wrong getting the editor student info (See the GetApartmentApplication method for what I am referring to) // We want to report the error, but not stop the entire process. It is better to have some or most applications rather than none of them } } ApartmentApplicationViewModel[] apartmentApplicationArray = null; if (applicationList.Any()) { apartmentApplicationArray = applicationList.ToArray(); } return(apartmentApplicationArray); }
public IHttpActionResult EditApplication(int applicationID, [FromBody] ApartmentApplicationViewModel applicationDetails) { // Verify Input if (!ModelState.IsValid) { string errors = ""; foreach (ModelState modelstate in ModelState.Values) { foreach (ModelError error in modelstate.Errors) { errors += "|" + error.ErrorMessage + "|" + error.Exception; } } throw new BadInputException() { ExceptionMessage = errors }; } //get token data from context, username is the username of current logged in person ClaimsPrincipal authenticatedUser = ActionContext.RequestContext.Principal as ClaimsPrincipal; string username = authenticatedUser.Claims.FirstOrDefault(x => x.Type == "user_name").Value; string sessionID = Helpers.GetCurrentSession().SessionCode; string newEditorUsername = applicationDetails?.EditorProfile?.AD_Username ?? applicationDetails?.EditorUsername; ApartmentApplicantViewModel[] newApartmentApplicants = applicationDetails.Applicants; foreach (ApartmentApplicantViewModel applicant in newApartmentApplicants) { if (applicant.Profile == null) { applicant.Profile = _profileService.GetStudentProfileByUsername(applicant.Username); } } ApartmentChoiceViewModel[] newApartmentChoices = applicationDetails.ApartmentChoices; int result = _housingService.EditApplication(username, sessionID, applicationID, newEditorUsername, newApartmentApplicants, newApartmentChoices); return(Created("Status of application saving: ", result)); }
/// <param name="applicationID">application ID number of the apartment application</param> /// <param name="isAdmin">boolean indicating whether the current user is an admin, permits access to restricted information such as birth date</param> /// <returns>Object of type ApartmentApplicationViewModel</returns> public ApartmentApplicationViewModel GetApartmentApplication(int applicationID, bool isAdmin = false) { SqlParameter appIDParam = new SqlParameter("@APPLICATION_ID", applicationID); IEnumerable <GET_AA_APPLICATIONS_BY_ID_Result> applicationResult = RawSqlQuery <GET_AA_APPLICATIONS_BY_ID_Result> .query("GET_AA_APPLICATIONS_BY_ID @APPLICATION_ID", appIDParam); if (applicationResult == null || !applicationResult.Any()) { throw new ResourceNotFoundException() { ExceptionMessage = "The application could not be found." }; } else if (applicationResult.Count() > 1) { // Somehow there was more than one application for this session code and applcation ID.... THis should not be possible // We will have to decide what is the best course of action in this case } GET_AA_APPLICATIONS_BY_ID_Result applicationDBModel = applicationResult.FirstOrDefault(x => x.AprtAppID == applicationID); // Assign the values from the database to the custom view model for the frontend ApartmentApplicationViewModel apartmentApplicationModel = applicationDBModel; //implicit conversion if (apartmentApplicationModel.EditorProfile == null) { throw new ResourceNotFoundException() { ExceptionMessage = "The student information about the editor of this application could not be found." }; } // Get the applicants that match this application ID appIDParam = new SqlParameter("@APPLICATION_ID", applicationID); IEnumerable <GET_AA_APPLICANTS_BY_APPID_Result> applicantsResult = RawSqlQuery <GET_AA_APPLICANTS_BY_APPID_Result> .query("GET_AA_APPLICANTS_BY_APPID @APPLICATION_ID", appIDParam); if (applicantsResult != null && applicantsResult.Any()) { // Only attempt to parse the data if the collection of applicants is not empty // It is possible for a valid saved application to not contain any applicants yet, so we do not want to throw an error in the case where no applicants were found List <ApartmentApplicantViewModel> applicantsList = new List <ApartmentApplicantViewModel>(); foreach (GET_AA_APPLICANTS_BY_APPID_Result applicantDBModel in applicantsResult) { ApartmentApplicantViewModel applicantModel = applicantDBModel; //implicit conversion // If the student information is found, create a new ApplicationViewModel and fill in its properties if (applicantModel.Profile != null && applicantDBModel.AprtAppID == applicationID) { if (isAdmin) // if the current user is a housing admin or super admin { // Only add the birthdate, probabtion, and points if the user is authorized to view that information applicantModel.BirthDate = new UnitOfWork().AccountRepository.FirstOrDefault(x => x.AD_Username.ToLower() == applicantDBModel.Username.ToLower()).Birth_Date; // The probation data is already in the database, we just need to write a stored procedure to get it // applicantModel.Probation = ... // TBD // Calculate application points int points = 0; if (!string.IsNullOrEmpty(applicantModel.Class)) { points += int.Parse(applicantModel.Class); } if (applicantModel.Age >= 23) { points += 1; } if (!string.IsNullOrEmpty(applicantModel.OffCampusProgram)) { points += 1; } if (applicantModel.Probation) { points -= 3; } applicantModel.Points = Math.Max(0, points);; // Set the resulting points to zero if the sum gave a value less than zero } // Add this new ApplicantViewModel object to the list of applicants for this application applicantsList.Add(applicantModel); } } if (applicantsList.Any()) { // Add this list of applicants to the application model as an array apartmentApplicationModel.Applicants = applicantsList.OrderBy(x => x.Username).ToArray(); } } // Get the apartment choices that match this application ID appIDParam = new SqlParameter("@APPLICATION_ID", applicationID); IEnumerable <GET_AA_APARTMENT_CHOICES_BY_APP_ID_Result> apartmentChoicesResult = RawSqlQuery <GET_AA_APARTMENT_CHOICES_BY_APP_ID_Result> .query("GET_AA_APARTMENT_CHOICES_BY_APP_ID @APPLICATION_ID", appIDParam); if (apartmentChoicesResult != null && apartmentChoicesResult.Any()) { // Only attempt to parse the data if the collection of apartment choices is not empty // It is possible for a valid saved application to not contain any apartment choices yet, so we do not want to throw an error in the case where no apartment choices were found List <ApartmentChoiceViewModel> apartmentChoicesList = new List <ApartmentChoiceViewModel>(); foreach (GET_AA_APARTMENT_CHOICES_BY_APP_ID_Result apartmentChoiceDBModel in apartmentChoicesResult) { ApartmentChoiceViewModel apartmentChoiceModel = apartmentChoiceDBModel; //implicit conversion // Add this new ApartmentChoiceModel object to the list of apartment choices for this application apartmentChoicesList.Add(apartmentChoiceModel); } if (apartmentChoicesList.Any()) { // Sort the apartment choices by their ranking number and Add this list of apartment choices to the application model as an array apartmentApplicationModel.ApartmentChoices = apartmentChoicesList.OrderBy(x => x.HallRank).ThenBy(x => x.HallName).ToArray(); } } return(apartmentApplicationModel); }