public async Task <ProfessorEditModelReturn> AddProfessorAsync(ProfessorEditModel model) { var professor = new Professor(); mapper.Map(model, professor); await professorRepository.AddAsync(professor); await context.SaveChangesAsync(); return(mapper.Map(professor, new ProfessorEditModelReturn())); }
public async Task <IActionResult> Post(ProfessorEditModel model) { try { if ((await professorService.AddProfessorAsync(model)) is ProfessorEditModelReturn professorReturnModel) { return(Ok(professorReturnModel)); } return(BadRequest()); } catch (Exception ex) { return(BadRequest($"Erro: {ex.Message}")); } }
public async Task <IActionResult> Put(int professorId, ProfessorEditModel model) { try { if ((await professorService.UpdateProfessorAsync(professorId, model)) is ProfessorEditModelReturn professorModelReturn) { return(Ok(professorModelReturn)); } return(BadRequest()); } catch (KeyNotFoundException) { return(NotFound()); } catch (Exception ex) { return(BadRequest($"Erro: {ex.Message}")); } }
public async Task <ProfessorEditModelReturn> UpdateProfessorAsync(int professorId, ProfessorEditModel model) { var professor = (await professorRepository.GetProfessorByIdAsync(professorId)); if (professor == null) { throw new KeyNotFoundException(); } mapper.Map(model, professor); await professorRepository.UpdateAsync(professor); await context.SaveChangesAsync(); return(mapper.Map(professor, new ProfessorEditModelReturn())); }