예제 #1
0
        public async Task <IActionResult> Novo([FromBody] NovoAlunoInputModel input)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var aluno = Aluno.Criar(input.Nome, input.Email, input.DataNascimento, input.Sexo.ToEnum <Aluno.ESexo>(),
                                        new Endereco(input.Rua, input.Numero, input.Complemento, input.Bairro, input.Cidade, input.Cep, input.DistanciaAteEscola));
                await _alunosRepositorio.AdicionarAsync(aluno);

                await _escolasContexto.SaveChangesAsync();

                return(Ok(aluno));
            }
            catch (InvalidOperationException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (Exception e)
            {
                return(StatusCode(500, new { error = e.Message }));
            }
        }
예제 #2
0
        public async Task <ActionResult> CriarAsync([FromBody] NovoAlunoInputModel novoAlunoRequest)
        {
            var aluno = Aluno.Criar(
                novoAlunoRequest.PrimeiroNome,
                novoAlunoRequest.Sobrenome,
                novoAlunoRequest.Email,
                novoAlunoRequest.DataNascimento,
                novoAlunoRequest.Sexo);

            if (aluno.IsFailure)
            {
                return(BadRequest(aluno.Error));
            }

            if (await _alunosRepositorio.RecuperarPorEmailAsync(aluno.Value.Email) is var alunoExistente && alunoExistente.HasValue)
            {
                return(BadRequest("Já existe um aluno cadastrado com este e-mail"));
            }

            await _alunosRepositorio.AdicionarAsync(aluno.Value);

            await _unitOfWork.CommitAsync();

            return(CreatedAtAction(nameof(AlunoPorId), new { id = aluno.Value.Id }, null));
        }