// GET: api/Programmes/s50001 public async Task <ActionResult <ProgrammeDto> > GetProgrammeAsync(string id) { var programme = await _programmeRepository.FindByIdAsync(id); if (programme != null) { return(Ok(_mapper.Map <ProgrammeDto>(programme))); } else { _logger.LogWarning($"Get programme. Id {id} not found."); return(NotFound()); } }
public async Task <ActionResult <StudentDto> > PostStudentAsync([FromBody] StudentDto student) { // Api automatically returns BadRequest if StudentDto == null try { // Check if programme exists var programme = _programmeRepository.FindByIdAsync(student.ProgrammeId); if (programme == null) { _logger.LogWarning($"Programme : {student.ProgrammeId} not found."); return(NotFound($"Programmee {student.ProgrammeId} does not exist")); } // map StudentDto to Student var studentToDb = _mapper.Map <Student>(student); var addedStudent = await _studentRepository.AddAsync(studentToDb); await _studentRepository.SaveChangesAsync(); var studentToReturn = _mapper.Map <StudentDto>(addedStudent); return(CreatedAtAction("GetStudent", new { id = addedStudent.StudentId }, studentToReturn)); } catch (DataAccessException e) { _logger.LogError(e, "Db error."); return(BadRequest("Post student failed.")); } catch (Exception e) { _logger.LogError(e, "Db error."); return(BadRequest("Post student failed.")); } }
[HttpGet]// GET: Programme/Details/5 public async Task <IActionResult> Details(string id) { if (id == null) { ViewBag.ErrorMsg = $"Invalid programme ID: {id}"; return(NotFound()); } var programme = await programmeRepository.FindByIdAsync(id); if (programme == null) { ViewBag.ErrorMsg = $"Invalid programme {id}"; return(NotFound()); } return(View(programme)); }