// PUT api/values/5
 public HttpResponseMessage Put(Guid id, [FromBody] Voluntary voluntary)
 {
     Console.WriteLine("PUT CONTROLLER");
     try
     {
         //Neste local faria a alteracao do voluntario no repositorio
         //Antes de fazer a alteracao, o sistema deve verificar se existe o voluntario
         //Se existir aplica as mudanças
         VoluntaryDTO voluntaryDto = Find(id);
         if (voluntaryDto == null)
         {
             return(Request.CreateResponse(HttpStatusCode.NotFound, "Voluntario não encontrado"));
         }
         else
         {
             Alter(voluntary);
             return(Request.CreateResponse(HttpStatusCode.OK, id));
         }
     }
     catch (ApplicationException ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message));
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message));
     }
 }
 // DELETE api/values/5
 public HttpResponseMessage Delete(Guid id)
 {
     try
     {
         VoluntaryDTO voluntaryDto = Find(id);
         if (voluntaryDto == null)
         {
             return(Request.CreateResponse(HttpStatusCode.NotFound, "Voluntario não encontrado"));
         }
         else
         {
             bool removed = Remove(id);
             if (removed)
             {
                 return(Request.CreateResponse(HttpStatusCode.OK, id));
             }
         }
     }
     catch (ApplicationException ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message));
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message));
     }
     return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Erro ao excluir voluntário"));
 }
Example #3
0
 public static Voluntary ToDomain(VoluntaryDTO voluntary)
 {
     return(new Voluntary()
     {
         VoluntaryId = voluntary.VoluntaryId,
         UserId = voluntary.UserId,
         IsApproved = voluntary.IsApproved,
         Name = voluntary.Name,
         Email = voluntary.Email,
         Phone = voluntary.Phone,
         Password = voluntary.Password,
         SocialNetwork = voluntary.SocialNetwork,
         Affinities = AffinityAdapter.ListToDomain(voluntary.Affinities),
         Address = new Address()
         {
             AddressId = voluntary.Address.AddressId,
             CEP = voluntary.Address.CEP,
             Avenue = voluntary.Address.Avenue,
             Number = voluntary.Address.Number,
             Neighborhood = voluntary.Address.Neighborhood,
             City = voluntary.Address.City,
             State = voluntary.Address.State
         }
     });
 }
        public Guid Insert(VoluntaryDTO voluntaryDto)
        {
            voluntaryDto.VoluntaryId       = Guid.NewGuid();
            voluntaryDto.UserId            = Guid.NewGuid();
            voluntaryDto.Address.AddressId = Guid.NewGuid();
            var voluntary = VoluntaryAdapter.ToDomain(voluntaryDto);

            return(voluntaryRepository.Insert(voluntary));
        }
        private Guid Insert(Voluntary voluntary)
        {
            Console.WriteLine("POST METHOD CONTROLLER");
            //return Guid.NewGuid();
            //executa o mapeamento

            List <AffinityDTO> affinities = new List <AffinityDTO>();

            foreach (var affinity in voluntary.Affinities)
            {
                AffinityDTO affinityDTO = new AffinityDTO()
                {
                    AffinityId = affinity.AffinityId,
                    Name       = affinity.Name
                };
                affinities.Add(affinityDTO);
            }

            VoluntaryDTO voluntaryDTO = new VoluntaryDTO()
            {
                VoluntaryId   = voluntary.VoluntaryId,
                UserId        = voluntary.UserId,
                IsApproved    = voluntary.IsApproved,
                IsVoluntary   = voluntary.IsVoluntary,
                Name          = voluntary.Name,
                Email         = voluntary.Email,
                Phone         = voluntary.Phone,
                Password      = voluntary.Password,
                SocialNetwork = voluntary.SocialNetwork,
                Affinities    = affinities,
                Address       = new AddressDTO()
                {
                    CEP          = voluntary.Address.CEP,
                    Avenue       = voluntary.Address.Avenue,
                    Number       = voluntary.Address.Number,
                    Neighborhood = voluntary.Address.Neighborhood,
                    City         = voluntary.Address.City,
                    State        = voluntary.Address.State
                }
            };

            return(voluntaryApplication.Insert(voluntaryDTO));
        }
 // GET api/values/5
 public HttpResponseMessage Get(Guid id)
 {
     try
     {
         VoluntaryDTO voluntariesDTO = Find(id);
         //Neste local faria uma busca na base de dados
         // Se encontrar o voluntario retorno o voluntario, caso contrário retorna código de nao encontrado
         if (voluntariesDTO == null)
         {
             return(Request.CreateResponse(HttpStatusCode.NotFound, "Voluntário não encontrado"));
         }
         else
         {
             return(Request.CreateResponse(HttpStatusCode.OK, voluntariesDTO));
         }
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message));
     }
 }
        private void Alter(Voluntary voluntary)
        {
            List <AffinityDTO> affinities = new List <AffinityDTO>();

            foreach (var affinity in voluntary.Affinities)
            {
                AffinityDTO affinityDTO = new AffinityDTO()
                {
                    AffinityId = affinity.AffinityId,
                    Name       = affinity.Name
                };
                affinities.Add(affinityDTO);
            }

            VoluntaryDTO voluntaryDTO = new VoluntaryDTO()
            {
                VoluntaryId   = voluntary.VoluntaryId,
                UserId        = voluntary.UserId,
                IsApproved    = voluntary.IsApproved,
                IsVoluntary   = voluntary.IsVoluntary,
                Name          = voluntary.Name,
                Email         = voluntary.Email,
                Phone         = voluntary.Phone,
                Password      = voluntary.Password,
                SocialNetwork = voluntary.SocialNetwork,
                Affinities    = affinities,
                Address       = new AddressDTO()
                {
                    CEP          = voluntary.Address.CEP,
                    Avenue       = voluntary.Address.Avenue,
                    Number       = voluntary.Address.Number,
                    Neighborhood = voluntary.Address.Neighborhood,
                    City         = voluntary.Address.City,
                    State        = voluntary.Address.State
                }
            };

            voluntaryApplication.Update(voluntaryDTO);
        }
        public Guid Update(VoluntaryDTO voluntaryDto)
        {
            var voluntary = VoluntaryAdapter.ToDomain(voluntaryDto);

            return(voluntaryRepository.Update(voluntary));
        }