Ejemplo n.º 1
0
        public FTSDTOForAdmin Create(int formId, string teacherId, int subjectId)
        {
            Form foundForm = formsService.GetByID(formId);

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

            if (foundForm.Started.AddDays(360).CompareTo(DateTime.UtcNow) < 0)
            {
                throw new HttpException("The Form with id: " + formId + " was not created for this shool year. " +
                                        "This form is from: " + foundForm.Started.Year + ". Classes must be assign to a form from this school year.");
            }

            TeacherToSubject foundTeacherToSubject = teachersToSubjectsService.GetActiveTeacherToSubjectByTeacherIdAndSubjectId(teacherId, subjectId);

            if (foundForm.Grade != foundTeacherToSubject.Subject.Grade)
            {
                throw new HttpException("The subject and teacher combination with id: " + foundTeacherToSubject.Id + " has " +
                                        "the subject that is taught in grade " + foundTeacherToSubject.Subject.Grade +
                                        "and it can not be assigned to the grade " + foundForm.Grade + " in form " + foundForm.Id);
            }

            FormToTeacherSubject duplicate = db.FormsToTeacherSubjectsRepository.GetDuplicate(foundForm.Id, foundTeacherToSubject.Id);

            if (duplicate != null)
            {
                throw new HttpException("The combination form-teacher-subject you are trying to create already exists - FTS Id: " + duplicate.Id);
            }

            FormToTeacherSubject fts = new FormToTeacherSubject
            {
                Form             = foundForm,
                TeacherToSubject = foundTeacherToSubject,
                Started          = DateTime.UtcNow,
                Stopped          = null
            };

            db.FormsToTeacherSubjectsRepository.Insert(fts);
            db.Save();

            FTSDTOForAdmin dto = toDTO.ConvertToFTSDTOForAdmin(fts);

            return(dto);
        }
Ejemplo n.º 2
0
        public HttpResponseMessage GetFormsToTeachersSubjectsById(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 FormToTeacherSubject by id: " + id);

            try
            {
                FormToTeacherSubject formToTeacherSubject = formsToTeacherSubjectsService.GetByID(id);

                if (formToTeacherSubject == null)
                {
                    logger.Info("The formToTeacherSubject with id: " + id + " was not found.");
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "The formToTeacherSubject with id: " + id + " was not found."));
                }
                if (userRole == "admin")
                {
                    logger.Info("Requesting found FormToTeacherSubject convert for " + userRole + "role.");
                    FTSDTOForAdmin dto = toDTO.ConvertToFTSDTOForAdmin(formToTeacherSubject);
                    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 FormToTeacherSubject convert for " + userRole + "role.");
                    FTSDTOForTeacher dto = toDTO.ConvertToFTSDTOForTeacher(formToTeacherSubject);
                    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));
            }
        }