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)); } }
private Form ConvertFromDTO(PostFormDTO newForm, Teacher attendingTeacher) { Form x = new Form { Grade = newForm.Grade, Tag = newForm.Tag, Started = newForm.Started, AttendingTeacher = attendingTeacher }; return(x); }
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); }