public TeacherToSubjectDTOForAdmin Create(string teacherId, int subjectId) { Teacher foundTeacher = db.TeachersRepository.GetByID(teacherId); if (foundTeacher == null) { throw new HttpException("The teacher with id: " + teacherId + " was not found"); } if (foundTeacher.IsStillWorking == false) { throw new HttpException("The teacher with id: " + teacherId + " is currently not an active faculty member. " + "You cannot assign this teacher to a subject."); } Subject foundSubject = subjectsService.GetByID(subjectId); if (foundSubject == null) { throw new HttpException("The subject with id: " + subjectId + " was found"); } TeacherToSubject found = db.TeachersToSubjectsRepository.GetByTeacherIdAndSubjectId(foundTeacher.Id, foundSubject.Id); if (found != null) { throw new HttpException("The subject with id: " + subjectId + " is already assign to " + "the teacher with id: " + teacherId + ". You can find the existing Teacher-Subject combination (" + found.Id + ") with HttpGet at route: http://localhost:54164/project/teachers-to-subjects/" + found.Id); } TeacherToSubject ts = new TeacherToSubject { Teacher = foundTeacher, Subject = foundSubject, StartedTeaching = DateTime.UtcNow, StoppedTeaching = null }; db.TeachersToSubjectsRepository.Insert(ts); db.Save(); TeacherToSubjectDTOForAdmin dto = toDTO.ConvertToTeacherToSubjectDTOForAdmin(ts); return(dto); }
public HttpResponseMessage GetTeacherToSubject(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 Teacher To Subject by id: " + id); try { TeacherToSubject ts = teachersToSubjectsService.GetByID(id); if (ts == null) { logger.Info("The teacher with id: " + id + " was not found."); return(Request.CreateResponse(HttpStatusCode.BadRequest, "The TeacherToSubject with id: " + id + " was not found.")); } if (userRole == "admin") { logger.Info("Requesting found TeacherToSubject convert for " + userRole + "role."); TeacherToSubjectDTOForAdmin dto = toDTO.ConvertToTeacherToSubjectDTOForAdmin(ts); 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 TeacherToSubject convert for " + userRole + "role."); TeacherToSubjectDTOForTeacher dto = toDTO.ConvertToTeacherToSubjectDTOForTeacher(ts); 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 == "student" || userRole == "parent") { logger.Info("Requesting found TeacherToSubject convert for " + userRole + "role."); TeacherToSubjectDTOForStudentAndParent dto = toDTO.ConvertToTeacherToSubjectDTOForStudentAndParent(ts); if (dto == null) { logger.Info("Failed!"); return(Request.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong.")); } logger.Info("Success!"); return(Request.CreateResponse(HttpStatusCode.OK, dto)); } } catch (Exception e) { logger.Error(e); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e)); } }