public FormDTOForAdmin Update(int id, PutFormDTO updated) { Form found = GetByID(id); if (found == null) { throw new HttpException("The Form with id: " + updated.Id + " was not found."); } if (updated.Grade != null) { found.Grade = (int)updated.Grade; } if (updated.Tag != null) { found.Tag = updated.Tag; } if (updated.Started != null) { found.Started = (DateTime)updated.Started; } if (updated.AttendingTeacherId != null) { Teacher foundTeacher = db.TeachersRepository.GetByID(updated.AttendingTeacherId); if (foundTeacher == null) { throw new HttpException("Attending teacher with id: " + updated.AttendingTeacherId + " was not found."); } if (foundTeacher.FormAttending != null && foundTeacher.FormAttending.Id != found.Id) { throw new HttpException("The teacher id " + updated.AttendingTeacherId + " is already assigned to the form " + "with id: " + foundTeacher.FormAttending.Id + ". The teacher can only attend one form at a time."); } if (foundTeacher.IsStillWorking == false) { throw new HttpException("The teacher id " + foundTeacher.Id + " is no longer working in this shool. " + "You must assing someone who is still working."); } found.AttendingTeacher = foundTeacher; } db.FormsRepository.Update(found); Form duplicate = db.FormsRepository.GetDuplicate(found.Grade, found.Tag, found.Started.Year); if (duplicate != null && duplicate.Id != found.Id) { throw new HttpException("The form you are creating by this update is already in the system. " + "The form id:" + duplicate.Id); } db.Save(); FormDTOForAdmin updatedDTO = new FormDTOForAdmin(); updatedDTO = toDTO.ConvertToFormDTOForAdmin(found); return(updatedDTO); }
public HttpResponseMessage GetFormByAttendingTeacherUserName([FromUri] string teacherUserName) { string userId = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value; logger.Info("UserId: " + userId + ": Requesting Form by attending teacher's user name: " + teacherUserName); try { FormDTOForAdmin form = formsService.GetFormByAttendingTeacherLastName(teacherUserName); if (form == null) { logger.Info("The Form by attending teacher's last name: " + teacherUserName + " was not found."); return(Request.CreateResponse(HttpStatusCode.BadRequest, "The Form by attending teacher's last name: " + teacherUserName + " was not found.")); } logger.Info("Success! Form by id: " + form.Id); return(Request.CreateResponse(HttpStatusCode.OK, form)); } catch (Exception e) { logger.Error(e); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e)); } }
public HttpResponseMessage PutChangeAttendingTeacher(int id, string teacherId) { string userId = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value; logger.Info("UserId: " + userId + ": Requesting Form Update For Form Id: " + id + ", Change Attending Teacher Id to: " + teacherId); try { FormDTOForAdmin saved = formsService.ChangeAttendingTeacher(id, teacherId); if (saved == null) { logger.Info("Failed!"); return(Request.CreateResponse(HttpStatusCode.BadRequest, "Failed!")); } logger.Info("Success!"); return(Request.CreateResponse(HttpStatusCode.OK, saved)); } catch (Exception e) { logger.Error(e); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e)); } }
public HttpResponseMessage PostForm([FromBody] PostFormDTO newForm) { string userId = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value; logger.Info("UserId: " + userId + ": Requesting Form Insert"); try { FormDTOForAdmin saved = formsService.Create(newForm); if (saved == null) { logger.Info("Failed!"); return(Request.CreateResponse(HttpStatusCode.BadRequest, "Failed! Something went wrong.")); } logger.Info("Success!"); return(Request.CreateResponse(HttpStatusCode.OK, saved)); } catch (Exception e) { logger.Error(e); return(Request.CreateResponse(HttpStatusCode.BadRequest, e)); } }
public HttpResponseMessage PutForm(int id, [FromBody] PutFormDTO updated) { string userId = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value; logger.Info("UserId: " + userId + ": Requesting Update for Form Id: " + id); if (updated.Id != id) { logger.Error("Updated Form id " + updated.Id + " doesn't match the id " + id + " from the request (route)."); return(Request.CreateResponse(HttpStatusCode.BadRequest, "Updated " + "Form id " + updated.Id + " doesn't match the id " + id + " from the request (route).")); } try { FormDTOForAdmin saved = formsService.Update(id, updated); if (saved == null) { logger.Info("Failed!"); return(Request.CreateResponse(HttpStatusCode.BadRequest, "Failed! Something went wrong.")); } logger.Info("Success!"); return(Request.CreateResponse(HttpStatusCode.OK, saved)); } catch (Exception e) { logger.Error(e); return(Request.CreateResponse(HttpStatusCode.BadRequest, e)); } }
public IList <FormDTOForAdmin> ConvertToFormDTOListForAdmin(List <Form> forms) { IList <FormDTOForAdmin> dtos = new List <FormDTOForAdmin>(); foreach (var form in forms) { FormDTOForAdmin dto = ConvertToFormDTOForAdmin(form); dtos.Add(dto); } return(dtos); }
public FormDTOForAdmin ConvertToFormDTOForAdmin(Form x) { FormDTOForAdmin dto = new FormDTOForAdmin { Id = x.Id, Grade = x.Grade, Tag = x.Tag, Started = x.Started, AttendingTeacher = teacherToDTO.ConvertToTeacherDTOForAdmin(x.AttendingTeacher, (List <IdentityUserRole>)x.AttendingTeacher.Roles), }; return(dto); }
public FormDTOForAdmin GetFormByAttendingTeacherLastName(string teacherUserName) { Teacher foundTeacher = db.TeachersRepository.GetByUserName(teacherUserName); if (foundTeacher == null) { throw new HttpException("The Teacher with user name: " + teacherUserName + " was not found."); } Form foundForm = db.FormsRepository.GetByAttendingTeacherId(foundTeacher.Id); if (foundForm == null) { throw new HttpException("Teacher " + foundTeacher.FirstName + " " + foundTeacher.LastName + " is currenty not assign to any form."); } FormDTOForAdmin dto = toDTO.ConvertToFormDTOForAdmin(foundForm); return(dto); }
public FormDTOForAdmin Create(PostFormDTO newForm) { Teacher attendingTeacher = db.TeachersRepository.GetByID(newForm.AttendingTeacherId); if (attendingTeacher == null) { throw new HttpException("Attending teacher with id: " + newForm.AttendingTeacherId + " was not found."); } if (attendingTeacher.FormAttending != null) { throw new HttpException("The teacher id " + newForm.AttendingTeacherId + " is already assigned to a form " + "with id: " + attendingTeacher.FormAttending.Id + ". The teacher can only attend one form at a time."); } if (attendingTeacher.IsStillWorking == false) { throw new HttpException("The teacher id " + attendingTeacher.Id + " is no longer working in this shool. " + "You must assing someone who is still working."); } Form form = ConvertFromDTO(newForm, attendingTeacher); db.FormsRepository.Insert(form); Form duplicate = db.FormsRepository.GetDuplicate(form.Grade, form.Tag, form.Started.Year); if (duplicate != null) { throw new HttpException("The form you are trying to create is already in the system. " + "The form id:" + duplicate.Id); } db.Save(); FormDTOForAdmin dto = toDTO.ConvertToFormDTOForAdmin(form); return(dto); }
public FormDTOForAdmin ChangeAttendingTeacher(int id, string teacherId) { Form found = GetByID(id); if (found == null) { throw new HttpException("The Form with id: " + id + " was not found."); } Teacher foundTeacher = db.TeachersRepository.GetByID(teacherId); if (foundTeacher == null) { throw new HttpException("Attending teacher with id: " + teacherId + " was not found."); } if (foundTeacher.FormAttending != null && foundTeacher.FormAttending.Id != found.Id) { throw new HttpException("The teacher id " + teacherId + " is already assigned to a form " + "with id: " + foundTeacher.FormAttending.Id + ". The teacher can only attend one form at a time."); } if (foundTeacher.IsStillWorking == false) { throw new HttpException("The teacher id " + foundTeacher.Id + " is no longer working in this shool. " + "You must assing someone who is still working."); } found.AttendingTeacher = foundTeacher; db.FormsRepository.Update(found); db.Save(); FormDTOForAdmin dto = toDTO.ConvertToFormDTOForAdmin(found); return(dto); }
public HttpResponseMessage GetForm(int id) { string userId = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value; string userRole = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == ClaimTypes.Role).Value; logger.Info("UserRole: " + userRole + ", UserId: " + userId + ": Requesting Form by id: " + id); try { Form form = formsService.GetByID(id); if (form == null) { logger.Info("The form with id: " + id + " was not found."); return(Request.CreateResponse(HttpStatusCode.BadRequest, "The form with id: " + id + " was not found.")); } if (userRole == "admin") { logger.Info("Requesting found form convert for " + userRole + "role."); FormDTOForAdmin dto = toDTO.ConvertToFormDTOForAdmin(form); if (dto == null) { logger.Info("Failed!"); return(Request.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong.")); } logger.Info("Success!"); return(Request.CreateResponse(HttpStatusCode.OK, dto)); } else if (userRole == "teacher") { logger.Info("Requesting found form convert for " + userRole + "role."); FormDTOForTeacher dto = toDTO.ConvertToFormDTOForTeacher(form); if (dto == null) { logger.Info("Failed!"); return(Request.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong.")); } logger.Info("Success!"); return(Request.CreateResponse(HttpStatusCode.OK, dto)); } else if (form.Students.Any(x => x.Id == userId) == true || form.Students.Any(x => x.Parent.Id == userId) == true) { logger.Info("Requesting found form convert for " + userRole + " role."); FormDTOForStudentAndParents dto = toDTO.ConvertToFormDTOForStudentAndParent(form); if (dto == null) { logger.Info("Failed!"); return(Request.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong.")); } logger.Info("Success!"); return(Request.CreateResponse(HttpStatusCode.OK, dto)); } else { logger.Info("Authorisation failure. User " + userId + " is not authorised for this request."); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Access Denied. " + "We’re sorry, but you are not authorized to perform the requested operation.")); } } catch (Exception e) { logger.Error(e); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e)); } }