예제 #1
0
        public async Task <AlunoEditModelReturn> AddAlunoAsync(AlunoEditModel model)
        {
            var aluno = new Aluno();

            mapper.Map(model, aluno);
            await alunoRepository.AddAsync(aluno);

            await context.SaveChangesAsync();

            return(mapper.Map(aluno, new AlunoEditModelReturn()));
        }
예제 #2
0
 public async Task <IActionResult> Post(AlunoEditModel model)
 {
     try
     {
         if ((await alunoService.AddAlunoAsync(model)) is AlunoEditModelReturn alunoReturnModel)
         {
             return(Ok(alunoReturnModel));
         }
         return(BadRequest());
     }
     catch (Exception ex)
     {
         return(BadRequest($"Erro: {ex.Message}"));
     }
 }
예제 #3
0
        public async Task <AlunoEditModelReturn> UpdateAlunoAsync(int alunoId, AlunoEditModel model)
        {
            var aluno = (await alunoRepository.GetAlunoByIdAsync(alunoId));

            if (aluno == null)
            {
                throw new KeyNotFoundException();
            }

            mapper.Map(model, aluno);
            await alunoRepository.UpdateAsync(aluno);

            await context.SaveChangesAsync();

            return(mapper.Map(aluno, new AlunoEditModelReturn()));
        }
예제 #4
0
 public async Task <IActionResult> Put(int alunoId, AlunoEditModel model)
 {
     try
     {
         if ((await alunoService.UpdateAlunoAsync(alunoId, model)) is AlunoEditModelReturn alunoReturnModel)
         {
             return(Ok(alunoReturnModel));
         }
         return(BadRequest());
     }
     catch (KeyNotFoundException)
     {
         return(NotFound());
     }
     catch (Exception ex)
     {
         return(BadRequest($"Erro: {ex.Message}"));
     }
 }