public IActionResult CreateStudent(StudentWithStudiesRequest request) { try { return(StatusCode(201, _dbService.CreateStudentWithStudies(request))); } catch (DbServiceException e) { if (e.Type == DbServiceExceptionTypeEnum.NotFound) { return(NotFound(e.Message)); } else if (e.Type == DbServiceExceptionTypeEnum.ValueNotUnique) { return(BadRequest(e.Message)); } else { return(StatusCode(500)); } } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, e.Message)); } }
public IActionResult EnrollStudent(StudentWithStudiesRequest request) { try { return(Ok(_service.EnrollStudent(request))); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public Enrollment CreateStudentWithStudies(StudentWithStudiesRequest request) { using (var con = new SqlConnection(ConnStr)); con.Open(); using (var transaction = con.BeginTransaction()); //check if studies exists if (!CheckIfStudiesExists(request.Studies, con, transaction)) { transaction.Rollback(); throw new DbServiceException(DbServiceExceptionTypeEnum.NotFound, "Studies does not exist."); } //get (or create and get) the enrollment var enrollment = GetNewestEnrollment(request.Studies, 1, con, transaction); if (enrollment == null) { CreateEnrollment(request.Studies, 1, DateTime.Now, con, transaction); enrollment = GetNewestEnrollment(request.Studies, 1, con, transaction); } //check if provided index number is unique if (GetStudent(request.IndexNumber) != null) { transaction.Rollback(); throw new DbServiceException(DbServiceExceptionTypeEnum.ValueNotUnique, $"Index number ({request.IndexNumber}) is not unique."); } //create a student and commit the transaction CreateStudent(request.IndexNumber, request.FirstName, request.LastName, request.BirthDate, enrollment.IdEnrollment, con, transaction); transaction.Commit(); //return Enrollment object return(enrollment); }
public StudentWithStudiesRequest EnrollStudent(StudentWithStudiesRequest request) { throw new NotImplementedException(); }