Пример #1
0
        public IActionResult Post([FromBody]
                                  AlunoRequest alunoRequest)
        {
            var aluno = new Aluno
            {
                IdPessoa      = alunoRequest.IdPessoa,
                Matricula     = alunoRequest.Matricula,
                RegistroAtivo = alunoRequest.RegistroAtivo,
                IdCurso       = alunoRequest.IdCurso
            };

            contexto.Aluno.Add(aluno);
            contexto.SaveChanges();

            var alunoRetorno = contexto.Aluno.Where
                                   (x => x.IdPessoa == aluno.IdPessoa)
                               .FirstOrDefault();

            AlunoResponse response = new AlunoResponse();

            if (alunoRetorno != null)
            {
                response.IdPessoa      = alunoRetorno.IdPessoa;
                response.Matricula     = alunoRetorno.Matricula;
                response.RegistroAtivo = alunoRetorno.RegistroAtivo;
                response.IdCurso       = alunoRetorno.IdCurso;
            }

            return(StatusCode(200, response));
        }
Пример #2
0
        public IActionResult Put(int id, [FromBody] AlunoRequest alunoRequest)
        {
            try
            {
                var aluno = contexto.Aluno.Where(x => x.IdPessoa == id)
                            .FirstOrDefault();

                if (aluno != null)
                {
                    aluno.Matricula = alunoRequest.Matricula;
                }
                contexto.Entry(aluno).State =
                    Microsoft.EntityFrameworkCore.EntityState.Modified;

                contexto.SaveChanges();
            }
            catch (Exception ex)
            {
                return(StatusCode(400, ex.InnerException.Message.FirstOrDefault()));
            }

            var alunoRetorno = contexto.Aluno.FirstOrDefault(
                x => x.IdPessoa == id
                );

            AlunoResponse retorno = new AlunoResponse()
            {
                IdPessoa      = alunoRetorno.IdPessoa,
                Matricula     = alunoRetorno.Matricula,
                RegistroAtivo = alunoRetorno.RegistroAtivo,
            };

            return(StatusCode(200, retorno));
        }