Exemple #1
0
        public AddAlunoResponse Add(AddAlunoRequest aluno)
        {
            var entity = new Aluno(aluno.Nome, aluno.Matricula);

            entity = _repository.Create(entity);
            var alunoTurma = new AlunoTurma(entity.IdAluno, aluno.IdTurma);

            _repositoryAlunoTurma.AddAluno(alunoTurma);
            return((AddAlunoResponse)entity);
        }
        public ContentResult PostAluno([FromBody] AddAlunoRequest req)
        {
            ContentResult response = new ContentResult {
                ContentType = "application/json"
            };

            try
            {
                var checkIfTurmaExist = _serviceTurma.LoadTurma(req.IdTurma);
                if (checkIfTurmaExist == null)
                {
                    response.StatusCode = (int)HttpStatusCode.BadRequest;
                    response.Content    = JsonConvert.SerializeObject("Turma não existe");
                    return(response);
                }

                var checkIfTurmaIsAvailable = _serviceTurma.CheckIfTurmaIsAvailable(req.IdTurma);
                if (!checkIfTurmaIsAvailable)
                {
                    response.StatusCode = (int)HttpStatusCode.BadRequest;
                    response.Content    = JsonConvert.SerializeObject("Turma Cheia");
                    return(response);
                }

                var checkIfMatriculaExists = _service.CheckIfExists(req.Matricula);
                if (checkIfMatriculaExists)
                {
                    response.StatusCode = (int)HttpStatusCode.BadRequest;
                    response.Content    = JsonConvert.SerializeObject("Matricula já existe");
                    return(response);
                }
                var result = _service.Add(req);
                _uof.Commit();

                response.StatusCode = (int)HttpStatusCode.OK;
                response.Content    = JsonConvert.SerializeObject(result);
                return(response);
            }
            catch (Exception ex)
            {
                response.StatusCode = (int)HttpStatusCode.BadRequest;
                response.Content    = JsonConvert.SerializeObject(ex.Message);
                return(response);
            }
        }