public async Task <DataAccessResponse <IEnumerable <PurchasedItem> > > GetAll(string friendId)
        {
            DataAccessResponse <IEnumerable <PurchasedItem> > response = new DataAccessResponse <IEnumerable <PurchasedItem> >();

            if (friendId == null)
            {
                return(response.NotFound());
            }

            DataAccessResponse <Friend> friendResponse = await _friendRepo.Get(friendId);

            if (friendResponse.Status != HttpStatusCode.OK)
            {
                return(response.NotFound());
            }

            Friend friend = friendResponse.Payload;

            if (friend.PurchasedItems == null)
            {
                return(response.NotFound());
            }

            return(response.Ok(friendResponse.Payload.PurchasedItems));
        }
Exemple #2
0
        public async Task <DataAccessResponse <IEnumerable <Friend> > > GetAll()
        {
            DataAccessResponse <IEnumerable <Friend> > response = new DataAccessResponse <IEnumerable <Friend> >();

            ISearchResponse <Friend> searchResponse = await _esClient.SearchAsync <Friend>(s => s);

            if (searchResponse.Total == 0)
            {
                return(response.NotFound());
            }

            if (!searchResponse.IsValid)
            {
                return(response.InternalServerError());
            }

            IEnumerable <Friend> friends = searchResponse.Hits.Select(h => {
                Friend source = h.Source;
                source.Id     = h.Id;
                return(source);
            });

            if (friends == null)
            {
                return(response.NotFound());
            }
            if (friends.Count() == 0)
            {
                return(response.NotFound());
            }

            return(response.Ok(friends));
        }
Exemple #3
0
        public async Task <DataAccessResponse <string> > Remove(string id)
        {
            DataAccessResponse <string> response = new DataAccessResponse <string>();

            if (id == null)
            {
                return(response.NotFound());
            }

            IDeleteResponse deleteResponse = await _esClient.DeleteAsync <Friend>(id);

            if (deleteResponse.Result == Result.NotFound)
            {
                return(response.NotFound());
            }

            if (!deleteResponse.IsValid)
            {
                return(response.InternalServerError());
            }
            if (deleteResponse.Id == null)
            {
                return(response.InternalServerError());
            }

            return(response.NoContent(deleteResponse.Id));
        }
Exemple #4
0
        public async Task <DataAccessResponse <string> > Update(string id, Friend friend)
        {
            DataAccessResponse <string> response = new DataAccessResponse <string>();

            if (friend == null)
            {
                return(response.NotFound());
            }

            if (friend.PurchasedItems != null)
            {
                friend.PurchasedItems.ForEach(p => p.Id = Guid.NewGuid());
            }

            IUpdateResponse <Friend> updateResponse = await _esClient.UpdateAsync <Friend>(friend,
                                                                                           d => d
                                                                                           .Index(Index)
                                                                                           .Type(Type)
                                                                                           .Doc(friend));

            if (!updateResponse.IsValid)
            {
                return(response.InternalServerError());
            }

            return(response.NoContent(updateResponse.Id));
        }
        public async Task <DataAccessResponse <string> > Update(string friendId, Guid purchasedItemId, PurchasedItem purchasedItem)
        {
            DataAccessResponse <string> response = new DataAccessResponse <string>();

            if (friendId == null)
            {
                return(response.NotFound());
            }
            if (purchasedItemId == null)
            {
                return(response.NotFound());
            }

            DataAccessResponse <Friend> friendResponse = await _friendRepo.Get(friendId);

            if (friendResponse.Status != HttpStatusCode.OK)
            {
                return(response.NotFound());
            }

            Friend friend = friendResponse.Payload;

            if (friend.PurchasedItems == null)
            {
                return(response.BadRequest());
            }

            PurchasedItem purchasedItemToUpdate = friend.PurchasedItems.FirstOrDefault(p => p.Id == purchasedItemId);

            if (purchasedItemToUpdate == null)
            {
                return(response.NotFound());
            }

            purchasedItem.Id = purchasedItemId;

            friend.PurchasedItems.Remove(purchasedItemToUpdate);
            friend.PurchasedItems.Add(purchasedItem);

            IUpdateResponse <Friend> updateResponse = await _esClient.UpdateAsync <Friend>(friendId,
                                                                                           d => d
                                                                                           .Index(FriendRepository.Index)
                                                                                           .Type(FriendRepository.Type)
                                                                                           .Doc(friend));

            if (!updateResponse.IsValid)
            {
                return(response.InternalServerError());
            }
            if (updateResponse.Id == null)
            {
                return(response.InternalServerError());
            }

            return(response.NoContent(updateResponse.Id));
        }
        public async Task <DataAccessResponse <string> > Add(string friendId, PurchasedItem item)
        {
            DataAccessResponse <string> response = new DataAccessResponse <string>();

            if (friendId == null)
            {
                return(response.NotFound());
            }
            if (item == null)
            {
                return(response.NotFound());
            }

            DataAccessResponse <Friend> friendResponse = await _friendRepo.Get(friendId);

            if (friendResponse.Status != HttpStatusCode.OK)
            {
                return(response.NotFound());
            }

            Friend friend = friendResponse.Payload;

            item.Id = Guid.NewGuid();

            friend.PurchasedItems = friend.PurchasedItems == null ? new List <PurchasedItem>() : friend.PurchasedItems;

            friend.PurchasedItems.Add(item);

            IUpdateResponse <Friend> updateResponse = await _esClient.UpdateAsync <Friend>(friendId,
                                                                                           d => d
                                                                                           .Index(FriendRepository.Index)
                                                                                           .Type(FriendRepository.Type)
                                                                                           .Doc(friend));

            if (!updateResponse.IsValid)
            {
                return(response.InternalServerError());
            }
            if (updateResponse.Id == null)
            {
                return(response.InternalServerError());
            }

            return(response.Created(item.Id.ToString()));
        }
Exemple #7
0
        public async Task <DataAccessResponse <Friend> > Get(string id)
        {
            DataAccessResponse <Friend> response = new DataAccessResponse <Friend>();

            IGetResponse <Friend> getResponse = await _esClient.GetAsync <Friend>(id);

            if (!getResponse.Found)
            {
                return(response.NotFound());
            }

            if (!getResponse.IsValid)
            {
                return(response.InternalServerError());
            }

            Friend friend = getResponse.Source;

            friend.Id = getResponse.Id;

            return(response.Ok(friend));;
        }
        public async Task <DataAccessResponse <PurchasedItem> > Get(string friendId, Guid purchasedItemId)
        {
            DataAccessResponse <PurchasedItem> response = new DataAccessResponse <PurchasedItem>();

            if (friendId == null)
            {
                return(response.NotFound());
            }
            if (purchasedItemId == null)
            {
                return(response.NotFound());
            }

            DataAccessResponse <Friend> friendResponse = await _friendRepo.Get(friendId);

            if (friendResponse.Status != HttpStatusCode.OK)
            {
                return(response.NotFound());
            }

            Friend friend = friendResponse.Payload;

            if (friend.PurchasedItems == null)
            {
                return(response.BadRequest());
            }

            PurchasedItem purchasedItem = friend.PurchasedItems.FirstOrDefault(p => p.Id == purchasedItemId);

            if (purchasedItem == null)
            {
                return(response.NotFound());
            }

            purchasedItem.Id = purchasedItemId;

            return(response.Ok(purchasedItem));
        }
Exemple #9
0
        public async Task <DataAccessResponse <string> > Add(Friend friend)
        {
            DataAccessResponse <string> response = new DataAccessResponse <string>();

            if (friend == null)
            {
                return(response.NotFound());
            }

            if (friend.PurchasedItems != null)
            {
                friend.PurchasedItems.ForEach(p => p.Id = Guid.NewGuid());
            }

            IIndexResponse indexResponse = await _esClient.IndexAsync <Friend>(friend, d => d.Index(Index).Type(Type));

            if (!indexResponse.IsValid)
            {
                return(response.InternalServerError());
            }

            return(response.Created(indexResponse.Id));
        }