예제 #1
0
        public IActionResult Post([FromBody] ExamPost exam)
        {
            ExamValidator validator = new ExamValidator();
            List <Error>  errors    = new List <Error>();

            if (!_db.IsModelStateValid(ModelState, exam))
            {
                return(_db.ModelStateErrors);
            }

            if (_db.ExamExists(exam))
            {
                return(new UnprocessableEntityObjectResult(new Error(1301, new ErrorProps {
                    Subject = exam.Subject, Cohort = exam.Cohort, Name = exam.Name, Crebo = exam.Crebo
                })));
            }

            errors.AddRange(validator.ValidatePost(exam));
            if (errors.Any())
            {
                return(new UnprocessableEntityObjectResult(errors));
            }

            var    id       = _db.AddExam(exam);
            var    result   = _db.FetchExam(id);
            string location = Url.RouteUrl("exam", new { subject = Utils.CleanParam(exam.Subject), cohort = exam.Cohort, name = Utils.CleanParam(exam.Name) }, Request.Scheme);

            return(new CreatedResult(location, result));
        }
예제 #2
0
        public IActionResult Patch([FromBody] List <Patch> patches, string subject, string cohort, string name)
        {
            ExamValidator validator = new ExamValidator();
            List <Error>  errors    = new List <Error>();

            subject = Utils.CleanParam(subject);
            name    = Utils.CleanParam(name);

            dynamic exam = _db.FetchExam(subject, name, cohort);

            if (exam == null)
            {
                return(new HttpNotFoundResult());
            }

            if (!_db.IsModelStateValid(ModelState, patches))
            {
                return(_db.ModelStateErrors);
            }

            errors.AddRange(validator.ValidatePatches(exam.Id, patches));
            if (errors.Any())
            {
                return(new UnprocessableEntityObjectResult(errors));
            }

            _db.PatchExam(patches, exam.Id);

            var result = _db.FetchExam(exam.Id);

            return(new HttpOkObjectResult(result));
        }