public async Task <ResponseListWrapper> GetWishList(string shopperId)
        {
            // GET https://{{accountName}}.vtexcommercestable.com.br/api/dataentities/{{data_entity_name}}/documents/{{id}}
            // GET https://{{accountName}}.vtexcommercestable.com.br/api/dataentities/{{data_entity_name}}/search

            var request = new HttpRequestMessage
            {
                Method     = HttpMethod.Get,
                RequestUri = new Uri($"http://{this._httpContextAccessor.HttpContext.Request.Headers[WishListConstants.VTEX_ACCOUNT_HEADER_NAME]}.vtexcommercestable.com.br/api/dataentities/{WishListConstants.DATA_ENTITY}/search?_fields=id,email,ListItemsWrapper&_schema={WishListConstants.SCHEMA}&email={shopperId}")
            };

            string authToken = this._httpContextAccessor.HttpContext.Request.Headers[WishListConstants.HEADER_VTEX_CREDENTIAL];

            if (authToken != null)
            {
                request.Headers.Add(WishListConstants.AUTHORIZATION_HEADER_NAME, authToken);
                request.Headers.Add(WishListConstants.VtexIdCookie, authToken);
                request.Headers.Add(WishListConstants.PROXY_AUTHORIZATION_HEADER_NAME, authToken);
            }

            var client   = _clientFactory.CreateClient();
            var response = await client.SendAsync(request);

            string responseContent = await response.Content.ReadAsStringAsync();

            Console.WriteLine($"Get:{response.StatusCode} ");
            ResponseListWrapper responseListWrapper = new ResponseListWrapper();

            try
            {
                JArray searchResult = JArray.Parse(responseContent);
                for (int l = 0; l < searchResult.Count; l++)
                {
                    JToken listWrapper = searchResult[l];
                    if (l == 0)
                    {
                        responseListWrapper = JsonConvert.DeserializeObject <ResponseListWrapper>(listWrapper.ToString());
                    }
                    else
                    {
                        ResponseListWrapper listToRemove = JsonConvert.DeserializeObject <ResponseListWrapper>(listWrapper.ToString());
                        bool removed = await this.DeleteWishList(listToRemove.Id);
                    }
                }
            }
            catch (Exception ex)
            {
                responseListWrapper.message = $"Error:{ex.Message}: Rsp = {responseContent} ";
                Console.WriteLine($"Error:{ex.Message}: Rsp = {responseContent} ");
            }

            if (!response.IsSuccessStatusCode)
            {
                responseListWrapper.message = $"Get:{response.StatusCode}: Rsp = {responseContent}";
            }

            return(responseListWrapper);
        }
Exemplo n.º 2
0
        public Query(IWishListService wishListService)
        {
            Name = "Query";

            FieldAsync <ListResponseType>(
                "viewList",
                arguments: new QueryArguments(
                    new QueryArgument <StringGraphType> {
                Name = "shopperId", Description = "Shopper Id"
            },
                    new QueryArgument <StringGraphType> {
                Name = "name", Description = "List Name"
            },
                    new QueryArgument <IntGraphType> {
                Name = "from", Description = "From"
            },
                    new QueryArgument <IntGraphType> {
                Name = "to", Description = "To"
            }
                    ),
                resolve: async context =>
            {
                string shopperId            = context.GetArgument <string>("shopperId");
                string name                 = context.GetArgument <string>("name");
                int from                    = context.GetArgument <int>("from");
                int to                      = context.GetArgument <int>("to");
                IList <ListItem> resultList = new List <ListItem>();
                int totalCount              = 0;
                var resultListsWrapper      = await wishListService.GetList(shopperId, name);
                var resultListWrapper       = resultListsWrapper.ListItemsWrapper.FirstOrDefault();
                if (resultListWrapper != null)
                {
                    if (resultListWrapper.ListItems != null)
                    {
                        resultList = resultListWrapper.ListItems;
                        totalCount = resultList.Count;

                        if (from > 0 && to > 0)
                        {
                            resultList = await wishListService.LimitList(resultList, from, to);
                        }

                        // Normalize Title field
                        foreach (ListItem listItem in resultList)
                        {
                            if (string.IsNullOrWhiteSpace(listItem.Title))
                            {
                                listItem.Title = string.Empty;
                            }
                        }
                    }
                    else
                    {
                        resultList = new List <ListItem>();
                    }
                }

                ListResponse listResponse = new ListResponse
                {
                    Data = new DataElement {
                        data = resultList
                    },
                    Range = new ResultRange {
                        From = from, To = to, Total = totalCount
                    },
                    Public = resultListWrapper.IsPublic ?? false,
                    Name   = resultListWrapper.Name
                };

                return(listResponse);
            }
                );

            FieldAsync <ListGraphType <ListResponseType> >(
                "viewLists",
                arguments: new QueryArguments(
                    new QueryArgument <StringGraphType> {
                Name = "shopperId", Description = "Shopper Id"
            },
                    new QueryArgument <IntGraphType> {
                Name = "from", Description = "From"
            },
                    new QueryArgument <IntGraphType> {
                Name = "to", Description = "To"
            }
                    ),
                resolve: async context =>
            {
                string shopperId                 = context.GetArgument <string>("shopperId");
                int from                         = context.GetArgument <int>("from");
                int to                           = context.GetArgument <int>("to");
                IList <ListItem> resultList      = new List <ListItem>();
                IList <ListResponse> resultLists = new List <ListResponse>();
                int totalCount                   = 0;
                var resultListsWrapper           = await wishListService.GetLists(shopperId);
                if (resultListsWrapper != null && resultListsWrapper.ListItemsWrapper != null)
                {
                    foreach (ListItemsWrapper listItemsWrapper in resultListsWrapper.ListItemsWrapper)
                    {
                        if (listItemsWrapper != null)
                        {
                            if (listItemsWrapper.ListItems != null)
                            {
                                resultList = listItemsWrapper.ListItems;
                                totalCount = resultList.Count;

                                if (from > 0 && to > 0)
                                {
                                    resultList = await wishListService.LimitList(resultList, from, to);
                                }

                                // Normalize Title field
                                foreach (ListItem listItem in resultList)
                                {
                                    if (string.IsNullOrWhiteSpace(listItem.Title))
                                    {
                                        listItem.Title = string.Empty;
                                    }
                                }
                            }
                            else
                            {
                                resultList = new List <ListItem>();
                            }
                        }

                        ListResponse listResponse = new ListResponse
                        {
                            Data = new DataElement {
                                data = resultList
                            },
                            Range = new ResultRange {
                                From = from, To = to, Total = totalCount
                            },
                            Public = listItemsWrapper.IsPublic ?? false,
                            Name   = listItemsWrapper.Name
                        };

                        resultLists.Add(listResponse);
                    }
                }

                return(resultLists);
            }
                );

            FieldAsync <CheckListType>(
                "checkList",
                arguments: new QueryArguments(
                    new QueryArgument <StringGraphType> {
                Name = "shopperId", Description = "Shopper Id"
            },
                    new QueryArgument <StringGraphType> {
                Name = "productId", Description = "Product Id"
            },
                    new QueryArgument <StringGraphType> {
                Name = "sku", Description = "Product Sku"
            }
                    ),
                resolve: async context =>
            {
                string shopperId = context.GetArgument <string>("shopperId");
                string productId = context.GetArgument <string>("productId");
                string sku       = context.GetArgument <string>("sku");
                ResponseListWrapper resultListWrapper = await wishListService.GetLists(shopperId);
                List <string> namesList             = new List <string>();
                List <int> idsList                  = new List <int>();
                CheckListResponse checkListResponse = null;
                if (resultListWrapper != null && resultListWrapper.ListItemsWrapper != null)
                {
                    foreach (ListItemsWrapper listItemsWrapper in resultListWrapper.ListItemsWrapper)
                    {
                        ListItem listItem = new ListItem();
                        if (!string.IsNullOrEmpty(sku))
                        {
                            listItem = listItemsWrapper.ListItems.FirstOrDefault(l => l.ProductId.Equals(productId, StringComparison.OrdinalIgnoreCase) && l.Sku.Equals(sku, StringComparison.OrdinalIgnoreCase));
                        }
                        else
                        {
                            listItem = listItemsWrapper.ListItems.FirstOrDefault(l => l.ProductId.Equals(productId, StringComparison.OrdinalIgnoreCase));
                        }

                        if (listItem != null)
                        {
                            namesList.Add(listItemsWrapper.Name);
                            idsList.Add(listItem.Id ?? -1);
                        }
                    }

                    checkListResponse = new CheckListResponse
                    {
                        InList    = namesList.Count > 0,
                        ListNames = namesList.ToArray(),
                        ListIds   = idsList.ToArray(),
                        message   = resultListWrapper.message
                    };
                }
                else
                {
                    checkListResponse = new CheckListResponse
                    {
                        InList    = false,
                        ListNames = new string[0],
                        ListIds   = new int[0],
                        message   = resultListWrapper != null ? resultListWrapper.message : "No records returned."
                    };
                }

                return(checkListResponse);
            }
                );

            FieldAsync <StringGraphType>(
                "listNames",
                arguments: new QueryArguments(
                    new QueryArgument <StringGraphType> {
                Name = "shopperId", Description = "Shopper Id"
            }
                    ),
                resolve: async context =>
            {
                string shopperId             = context.GetArgument <string>("shopperId");
                ResponseListWrapper allLists = await wishListService.GetLists(shopperId);
                IList <ListItemsWrapper> listItemsWrappers = allLists.ListItemsWrapper;
                var distinctListNames = listItemsWrappers.GroupBy(elem => elem.Name).Select(group => group.First());
                var listName          = distinctListNames.Select(n => n.Name);

                return(listName.ToArray());
            }
                );
        }