Пример #1
0
        public void Remove(InterestedAnimal entity)
        {
            User user = entity.User;

            _userRepository.DecreaseAdoptionCount(user);
            _interestedAnimalRepository.Remove(entity);
        }
Пример #2
0
        public async Task <InterestedAnimal> RemoveInterest(Animal animal, System.Security.Claims.ClaimsPrincipal currentUser)
        {
            InterestedAnimal interest = await FindInterest(animal, currentUser);

            using (var httpClient = new HttpClient())
            {
                var builder = new UriBuilder(apiBaseUrl + "/api/interest/" + interest.ID);
                var query   = HttpUtility.ParseQueryString(builder.Query);
                builder.Query = query.ToString();
                string url = builder.ToString();

                var stringContent = new StringContent(JsonConvert.SerializeObject(interest), Encoding.UTF8, "application/json");
                stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                try
                {
                    using (var response = await httpClient.PostAsync(url, stringContent))
                    {
                        string apiResponse = await response.Content.ReadAsStringAsync();

                        if (response.ReasonPhrase == "Bad Request")
                        {
                            throw new InvalidOperationException("Interest already shown");
                        }
                        var res = response.Content.ReadAsStringAsync().Result;
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            return(interest);
        }
Пример #3
0
        public void Add(InterestedAnimal entity)
        {
            if (entity.Animal == null)
            {
                entity.Animal = _animalRepository.FindByID(entity.AnimalID);
            }

            if (entity.User == null)
            {
                entity.User = _userRepository.FindByID(entity.UserID);
            }

            // Check if not more then three animals
            // If so allow to add
            if (entity.User.InterestCount < 3)
            {
                entity.Animal = null;
                try
                {
                    _userRepository.IncreaseAdoptionCount(entity.User);
                    entity.User = null;
                    _interestedAnimalRepository.Add(entity);
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            else
            {
                throw new InvalidOperationException("No more then three animals with interest");
            }
        }
Пример #4
0
        public IActionResult GetInterest(int id, [FromQuery] string userName)
        {
            User             user   = _userService.FindByUsername(userName);
            InterestedAnimal animal = _interestedAnimalService.FindByIDAndUser(id, user.ID);

            return(Ok(animal));
        }
Пример #5
0
 public ActionResult <Animal> ShowInterest(InterestedAnimal animal)
 {
     try
     {
         _interestedAnimalService.Add(animal);
         // Update user view count
         return(Ok(animal));
     } catch (Exception e)
     {
         return(BadRequest());
     }
 }
Пример #6
0
        public async Task <InterestedAnimal> ShowInterest(Animal animal, System.Security.Claims.ClaimsPrincipal currentUser)
        {
            InterestedAnimal interstedAnimal = new InterestedAnimal();
            User             user            = await GetUserByHttp(currentUser);

            using (var httpClient = new HttpClient())
            {
                // TODO: Find cleaner way to do this
                var builder = new UriBuilder(apiBaseUrl + "/api/interest");
                var query   = HttpUtility.ParseQueryString(builder.Query);
                builder.Query = query.ToString();
                string url = builder.ToString();

                InterestedAnimal interest = new InterestedAnimal
                {
                    Animal   = animal,
                    AnimalID = animal.ID,
                    UserID   = user.ID,
                    User     = user
                };

                var stringContent = new StringContent(JsonConvert.SerializeObject(interest), Encoding.UTF8, "application/json");
                stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                try
                {
                    using (var response = await httpClient.PostAsync(url, stringContent))
                    {
                        string apiResponse = await response.Content.ReadAsStringAsync();

                        if (response.ReasonPhrase == "Bad Request")
                        {
                            throw new InvalidOperationException("Interest already shown");
                        }
                        var res = response.Content.ReadAsStringAsync().Result;
                        interstedAnimal = JsonConvert.DeserializeObject <InterestedAnimal>(apiResponse);
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            return(interstedAnimal);
        }
Пример #7
0
        public async Task <InterestedAnimal> FindInterest(Animal animal, System.Security.Claims.ClaimsPrincipal currentUser)
        {
            InterestedAnimal foundInterest = new InterestedAnimal();

            using (var httpClient = new HttpClient())
            {
                var builder = new UriBuilder(apiBaseUrl + "/api/interest/" + animal.ID);
                var query   = HttpUtility.ParseQueryString(builder.Query);
                query["userName"] = currentUser.Identity.Name;
                builder.Query     = query.ToString();
                string url = builder.ToString();

                using (var response = await httpClient.GetAsync(url))
                {
                    string apiResponse = await response.Content.ReadAsStringAsync();

                    foundInterest = JsonConvert.DeserializeObject <InterestedAnimal>(apiResponse);
                }
            }

            return(foundInterest);
        }
Пример #8
0
 public void SaveInterestedAnimal(InterestedAnimal interestedAnimal)
 {
     throw new System.NotImplementedException();
 }
Пример #9
0
 public IActionResult Delete(int id, [FromBody] InterestedAnimal animal)
 {
     _interestedAnimalService.Remove(animal);
     return(Ok(animal));
 }