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)); }
public object AddExam(ExamPost exam) { var query = @"INSERT INTO Exams (Name, NameId, Cohort, Crebo, Subject, SubjectId, Status) VALUES (@Name, @NameId, @Cohort, @Crebo, @Subject, @SubjectId, 'draft');"; var parameters = new { Name = exam.Name, NameId = Utils.CleanParam(exam.Name), Cohort = exam.Cohort, Crebo = exam.Crebo ?? string.Empty, Subject = exam.Subject, SubjectId = Utils.CleanParam(exam.Subject) }; return(_gateway.Insert(query, parameters)); }
public bool ExamExists(ExamPost exam) { var query = @"SELECT COUNT(*) as count FROM Exams WHERE NameId = @Name AND SubjectId = @Subject AND Cohort = @Cohort AND Crebo = @Crebo"; var parameters = new { Name = Utils.CleanParam(exam.Name), Subject = Utils.CleanParam(exam.Subject), Cohort = exam.Cohort, Crebo = exam.Crebo ?? string.Empty }; dynamic result = _gateway.SelectSingle(query, parameters); return(result.count > 0); }