public async Task <IActionResult> DeleteAnimal(int id)
        {
            var currentUser = HttpContext.User;

            try
            {
                if (currentUser.HasClaim(c => c.Type == "id"))
                {
                    var animal = await _context.Animals.FindAsync(id);

                    var id_log      = int.Parse(currentUser.Claims.First(c => c.Type == "id").Value);
                    var association = _context.Associations.Where(assoc => assoc.User_id == id_log).Single();
                    if (animal.Association_id == association.Id)
                    {
                        _context.Animals.Remove(animal);
                        await _context.SaveChangesAsync();

                        return(Ok(ok.TemplateResponse($"Animal com o id {id} foi eliminado!")));
                    }
                    else
                    {
                        return(BadRequest(notFound.TemplateResponse("Este animal não pertence à sua associação")));
                    }
                }
                return(NotFound(notFound.TemplateResponse("Utilizador não se encontra autenticado")));
            }
            catch
            {
                return(NotFound(notFound.TemplateResponse($"Animal com o id {id} nao encontrado!")));
            }
        }
        public async Task <IActionResult> DeletePost(int id)
        {
            var currentUser = HttpContext.User;

            try
            {
                if (currentUser.HasClaim(c => c.Type == "id"))
                {
                    var aEvent = await _context.Events.FindAsync(id);

                    var id_log      = int.Parse(currentUser.Claims.First(c => c.Type == "id").Value);
                    var association = _context.Associations.Where(assoc => assoc.User_id == id_log).Single();
                    if (aEvent.Association_id == association.Id)
                    {
                        _context.Events.Remove(aEvent);
                        await _context.SaveChangesAsync();


                        return(Ok(ok.TemplateResponse($"O evento com o id:{id} foi apagado")));
                    }
                    else
                    {
                        return(Ok(notFound.TemplateResponse($"O Evento que tentou apagar não pertence à sua associação")));
                    }
                }
                return(NotFound(notFound.TemplateResponse("Utilizador não se encontra autenticado")));
            }
            catch
            {
                return(NotFound(notFound.TemplateResponse($"Nao existe o evento com o id {id}")));
            }
        }
        public async Task <IActionResult> getAllLostAnimals()
        {
            try
            {
                var lostAnimalsList = await _context.LostAnimalPosts.OrderByDescending(b => b.Id).ToListAsync();

                if (!lostAnimalsList.Any())
                {
                    return(NotFound(notFound.TemplateResponse("Nao existe animais perdidos")));
                }

                object response = new { success = true, data = lostAnimalsList };

                return(Ok(response));
            }
            catch
            {
                return(BadRequest());
            }
        }
 public async Task <IActionResult> LoginUser(Login aLogin)
 {
     try
     {
         var user = Auth.Login(aLogin.email, aLogin.password, _context);
         if (user != null)
         {
             var    tokenString = Auth.GenerateJSONWebToken(user, _config);
             object response    = new { data = user, token = tokenString };
             return(Ok(response));
         }
         else
         {
             return(NotFound(notFound.TemplateResponse("Wrong password")));
         }
     }
     catch
     {
         return(NotFound(notFound.TemplateResponse("Wrong email")));
     }
 }
        public async Task <IActionResult> getAllPosts()
        {
            try
            {
                var postsList = await _context.Posts.ToListAsync();

                if (!postsList.Any())
                {
                    return(NotFound(notFound.TemplateResponse("Não existem posts registados")));
                }

                object response = new { success = true, data = postsList };
                return(Ok(response));
            }
            catch
            {
                return(BadRequest());
            }
        }
        public async Task <IActionResult> getAllEvents()
        {
            try
            {
                var eventsList = await _context.Events.OrderByDescending(b => b.Id).ToListAsync();

                if (!eventsList.Any())
                {
                    return(NotFound(notFound.TemplateResponse("Não existem posts registados")));
                }
                foreach (Event cEvent in eventsList)
                {
                    int result = DateTime.Compare(DateTime.Now, cEvent.DateEnd);
                }
                object response = new { success = true, data = eventsList };
                return(Ok(response));
            }
            catch
            {
                return(BadRequest());
            }
        }
        public async Task <IActionResult> DeleteAssociation(int id)
        {
            var currentUser = HttpContext.User;

            try
            {
                if (currentUser.HasClaim(c => c.Type == "id") && bool.Parse(currentUser.Claims.FirstOrDefault(c => c.Type == "admin").Value))
                {
                    var association = await _context.Associations.FindAsync(id);

                    //_context.Users.Remove(association.User);
                    _context.Associations.Remove(association);
                    await _context.SaveChangesAsync();

                    return(Ok(ok.TemplateResponse($"Associação com o id {id} foi eliminada ")));
                }

                return(NotFound(notFound.TemplateResponse("Utilizador não se encontra autenticado")));
            }
            catch
            {
                return(NotFound(notFound.TemplateResponse($"Nao foi possivel encontrar a associação com o id {id}")));
            }
        }
        public async Task <IActionResult> AllAssociations()
        {
            try
            {
                var associationsList = await _context.Associations.ToListAsync();

                foreach (Association assoc in associationsList)
                {
                    assoc.User          = _context.Users.Find(assoc.User_id);
                    assoc.User.Password = "";
                }
                if (!associationsList.Any())
                {
                    return(NotFound(notFound.TemplateResponse("Não tem associações registados")));
                }

                object response = new { success = true, data = associationsList };
                return(Ok(response));
            }
            catch
            {
                return(BadRequest());
            }
        }
        public async Task <IActionResult> Create(Animal aAnimal)
        {
            object response;
            var    currentUser = HttpContext.User;

            try
            {
                if (currentUser.HasClaim(c => c.Type == "id"))
                {
                    int         id          = int.Parse(currentUser.Claims.FirstOrDefault(c => c.Type == "id").Value);
                    Association association = _context.Associations.Single(assoc => assoc.User_id == id);
                    var         animal      = (Animal)animal_factory.CreateAnimalFromAnimalFactory(aAnimal);
                    await _context.Animals.AddAsync(animal);

                    await _context.SaveChangesAsync();

                    response = new { success = true, data = animal };
                    return(Ok(response));
                }
                return(NotFound(notFound.TemplateResponse("Utilizador não se encontra autenticado")));
            }
            catch
            {
                return(BadRequest());
            }
        }