Пример #1
0
        /*public IEnumerable<Aluno> Get() {
         *  return _repositorioAlunos.Selecionar();
         * }
         *
         * public HttpResponseMessage Get(int? id) { //Precisa fazer match com o formato da rota
         *  if (!id.HasValue)
         *      return Request.CreateResponse(HttpStatusCode.BadRequest);
         *
         *  Aluno aluno = _repositorioAlunos.SelecionarPorId(id.Value);
         *
         *  if (aluno == null)
         *      return Request.CreateResponse(HttpStatusCode.NotFound);
         *
         *  return Request.CreateResponse(HttpStatusCode.Found, aluno);
         * }
         *
         * public HttpResponseMessage Post([FromBody]Aluno aluno) {
         *
         *  try
         *  {
         *      _repositorioAlunos.Inserir(aluno);
         *      return Request.CreateResponse(HttpStatusCode.Created);
         *  }
         *  catch (Exception ex) {
         *      return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
         *  }
         * }*/

        //Sem automapper

        /*public IHttpActionResult Get()
         * {
         *  return Ok(_repositorioAlunos.Selecionar());
         * }
         *
         * public IHttpActionResult Get(int? id) // IHttpActionResult - Mais recomendado
         * { //Precisa fazer match com o formato da rota
         *  if (!id.HasValue) return BadRequest();
         *
         *  Aluno aluno = _repositorioAlunos.SelecionarPorId(id.Value);
         *
         *  if (aluno == null) return NotFound();
         *
         *  return Content(HttpStatusCode.Found, aluno);
         * }
         *
         * public IHttpActionResult Post([FromBody]Aluno aluno) //Mais recomendado com IHttpActionResult
         * {
         *
         *  try
         *  {
         *      _repositorioAlunos.Inserir(aluno);
         *      return Created($"{Request.RequestUri}/{aluno.Id}", aluno);
         *  }
         *  catch (Exception ex)
         *  {
         *      return InternalServerError(ex);
         *  }
         * }
         *
         * public IHttpActionResult Put(int? id, [FromBody] Aluno aluno) {
         *  try
         *  {
         *      if (!id.HasValue)
         *          return BadRequest();
         *
         *      aluno.Id = id.Value;
         *      _repositorioAlunos.Atualizar(aluno);
         *      return Ok();
         *  }
         *  catch (Exception ex) {
         *      return InternalServerError();
         *  }
         * }
         *
         * public IHttpActionResult Delete(int? id)
         * {
         *  try {
         *      if (!id.HasValue)
         *          return BadRequest();
         *
         *      Aluno aluno = _repositorioAlunos.SelecionarPorId(id.Value);
         *
         *      if (aluno == null)
         *          return NotFound();
         *
         *      _repositorioAlunos.ExcluirPorId(id.Value);
         *      return Ok();
         *
         *  } catch (Exception ex) {
         *      return InternalServerError();
         *  }
         * }*/

        //Com automapper
        public IHttpActionResult Get()
        {
            List <Aluno>    alunos    = _repositorioAlunos.Selecionar();
            List <AlunoDTO> alunosDTO = AutoMapperManager.Instance.Mapper.Map <List <Aluno>, List <AlunoDTO> >(alunos); //Já está carregando com opções de mapeamento

            return(Ok(alunosDTO));
        }