Пример #1
0
        public HttpResponseMessage PutFormToTeacherSubject(int id, [FromBody] PutFormToTeacherSubjectDTO updated)
        {
            string userId = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value;

            logger.Info("UserId: " + userId + ": Requesting Form TO TeacherSubject Update for FTS Id: " + id);

            if (updated.Id != id)
            {
                logger.Error("Updated FormToTeacherSubject id: " + updated.Id + " doesn't match the id: " + id + " from the request (Route).");
                return(Request.CreateResponse(HttpStatusCode.BadRequest, "Updated FormToTeacherSubject " +
                                              "id: " + updated.Id + " doesn't match id: " + id + " from the request."));
            }

            try
            {
                FTSDTOForAdmin saved = formsToTeacherSubjectsService.Update(id, updated);

                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));
            }
        }
Пример #2
0
        public FTSDTOForAdmin Update(int id, PutFormToTeacherSubjectDTO updated)
        {
            FormToTeacherSubject found = GetByID(id);

            if (found == null)
            {
                throw new HttpException("The FormToTeacherSubject with id: " + id + " was not found");
            }

            Form foundForm = formsService.GetByID(updated.FormId);

            found.Form = foundForm ?? throw new HttpException("The Form with id: " + updated.FormId + " was not found");

            TeacherToSubject foundTS = teachersToSubjectsService.GetActiveTeacherToSubjectByTeacherIdAndSubjectId(updated.TeacherId, updated.SubjectId);

            found.TeacherToSubject = foundTS;

            found.Started = DateTime.UtcNow;
            found.Stopped = null;

            FormToTeacherSubject duplicate = db.FormsToTeacherSubjectsRepository.GetDuplicate(updated.FormId, foundTS.Id);

            if (duplicate != null)
            {
                throw new HttpException("The combination form-teacher-subject you are trying to create already exists - FTS Id: " + duplicate.Id);
            }
            if (foundForm.Grade != foundTS.Subject.Grade)
            {
                throw new HttpException("The subject and teacher combination with id: " + foundTS.Id + " has " +
                                        "the subject that is taught in grade " + foundTS.Subject.Grade +
                                        "and it can not be assigned to the grade " + foundForm.Grade + " in form " + foundForm.Id);
            }

            db.FormsToTeacherSubjectsRepository.Update(found);
            db.Save();

            FTSDTOForAdmin dto = toDTO.ConvertToFTSDTOForAdmin(found);

            return(dto);
        }