Пример #1
0
        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()));
        }
Пример #2
0
 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}"));
     }
 }
Пример #3
0
 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}"));
     }
 }
Пример #4
0
        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()));
        }