public async Task <IActionResult> CreateSchoolStudent([FromBody] SchoolStudentDto schoolStudentDto) { try { if (schoolStudentDto == null) { _loggerManager.LogError("schoolStudentDto object sent from client is null."); return(BadRequest("schoolStudentDto object is null")); } if (!ModelState.IsValid) { _loggerManager.LogError("Invalid schoolStudentDto object sent from client."); return(BadRequest("Invalid schoolStudentDto model object")); } var schoolStudent = _mapper.Map <SchoolStudent>(schoolStudentDto); await _repositoryWrapper.SchoolStudent.CreateSchoolStudent(schoolStudent); if (schoolStudent.IsEmptyObject()) { _loggerManager.LogError($"Save operation failed inside CreateSchoolStudent action"); return(StatusCode(500, "Internal server error while saving School ")); } var dbObject = await _repositoryWrapper.SchoolStudent.GetSchoolStudentById(schoolStudent.Id); return(Ok(dbObject)); } catch (Exception ex) { _loggerManager.LogError($"Something went wrong inside CreateSchoolStudent action: {ex.Message}"); return(StatusCode(500, "Internal server error")); } }
public async Task <IActionResult> UpdateSchoolStudent([FromQuery] Guid id, [FromBody] SchoolStudentDto schoolStudentDto) { try { if (schoolStudentDto == null) { _loggerManager.LogError("SchoolStudentDto object sent from client is null."); return(BadRequest("SchoolStudentDto object is null")); } if (!ModelState.IsValid) { _loggerManager.LogError("Invalid SchoolStudentDto object sent from client."); return(BadRequest("Invalid SchoolStudentDto model object")); } var dbSchoolStudentDto = await _repositoryWrapper.SchoolStudent.GetSchoolStudentById(id); if (dbSchoolStudentDto.IsEmptyObject()) { _loggerManager.LogError($"School with id: {id}, hasn't been found in db."); return(NotFound()); } var schoolStudent = _mapper.Map <SchoolStudent>(schoolStudentDto); await _repositoryWrapper.SchoolStudent.UpdateSchoolStudent(dbSchoolStudentDto, schoolStudent); return(NoContent()); } catch (Exception ex) { _loggerManager.LogError($"Something went wrong inside UpdateSchoolStudent action: {ex.Message}"); return(StatusCode(500, "Internal server error")); } }
public void UpdateSchoolStudent() { // Arrange var schoolStudentDto = new SchoolStudentDto { GradeLevel = "9th", RegistrationDate = DateTime.UtcNow, SchoolId = DataSeeder.SchoolSeeder.Objects[0].Id, StudentId = DataSeeder.StudentSeeder.Objects[0].Id }; var schoolStudent = _mapper.Map <SchoolStudent>(schoolStudentDto); Mock.Get(_repositoryWrapper.SchoolStudent).Setup(x => x.UpdateSchoolStudent(schoolStudent, schoolStudent)); Mock.Get(_repositoryWrapper.SchoolStudent).Setup(x => x.GetSchoolStudentById(schoolStudent.Id)).ReturnsAsync(schoolStudent); var controller = new SchoolStudentController(_loggerManager, _mapper, _repositoryWrapper); // Act var actionResult = controller.UpdateSchoolStudent(schoolStudent.Id, schoolStudentDto).Result; // Assert var noContentResult = actionResult as NoContentResult; Assert.IsNotNull(noContentResult); }