public async Task <WishListWrapper> GetList(string shopperId, string listName) { ListItemsWrapper listItemsWrapper = new ListItemsWrapper(); WishListWrapper wishListWrapper = await _wishListRepository.GetWishList(shopperId); if (wishListWrapper != null && wishListWrapper.ListItemsWrapper != null) { // TODO: Check for more than instance of a document for shopperid (email) listItemsWrapper = wishListWrapper.ListItemsWrapper.Where(n => n.Name.Equals(listName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); if (listItemsWrapper == null) { listItemsWrapper = new ListItemsWrapper(); } } else { Console.WriteLine("GetList Null/Empty"); } wishListWrapper.ListItemsWrapper = new List <ListItemsWrapper> { listItemsWrapper }; return(wishListWrapper); }
public async Task <WishListWrapper> GetList(string shopperId, string listName) { ListItemsWrapper listItemsWrapper = new ListItemsWrapper(); WishListWrapper wishListWrapper = await _wishListRepository.GetWishList(shopperId); if (wishListWrapper != null && wishListWrapper.ListItemsWrapper != null) { listItemsWrapper = wishListWrapper.ListItemsWrapper.Where(n => n.Name.Equals(listName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); if (listItemsWrapper == null) { listItemsWrapper = new ListItemsWrapper(); } } else { wishListWrapper = await _wishListRepository.GetWishList(shopperId); if (wishListWrapper != null && wishListWrapper.ListItemsWrapper != null) { listItemsWrapper = wishListWrapper.ListItemsWrapper.Where(n => n.Name.Equals(listName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); if (listItemsWrapper == null) { listItemsWrapper = new ListItemsWrapper(); } } } wishListWrapper.ListItemsWrapper = new List <ListItemsWrapper> { listItemsWrapper }; return(wishListWrapper); }
public async Task <bool> SaveWishList(IList <ListItem> listItems, string shopperId, string listName, bool?isPublic, string documentId) { // PATCH https://{{accountName}}.vtexcommercestable.com.br/api/dataentities/{{data_entity_name}}/documents if (listItems == null) { listItems = new List <ListItem>(); } ListItemsWrapper listItemsWrapper = new ListItemsWrapper { ListItems = listItems, IsPublic = isPublic, Name = listName }; WishListWrapper wishListWrapper = new WishListWrapper { Id = documentId, Email = shopperId, ListItemsWrapper = new List <ListItemsWrapper> { listItemsWrapper } }; var jsonSerializedListItems = JsonConvert.SerializeObject(wishListWrapper); var request = new HttpRequestMessage { Method = HttpMethod.Patch, RequestUri = new Uri($"http://{this._httpContextAccessor.HttpContext.Request.Headers[WishListConstants.VTEX_ACCOUNT_HEADER_NAME]}.vtexcommercestable.com.br/api/dataentities/{WishListConstants.DATA_ENTITY}/documents"), Content = new StringContent(jsonSerializedListItems, Encoding.UTF8, WishListConstants.APPLICATION_JSON) }; 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($"Save:{response.StatusCode} Id:{documentId}"); return(response.IsSuccessStatusCode); }
public async Task <int?> SaveItem(ListItem listItem, string shopperId, string listName, bool?isPublic) { IList <ListItem> listItemsToSave = null; WishListWrapper wishListWrapper = await this.GetList(shopperId, listName); ListItemsWrapper listItemsWrapper = wishListWrapper.ListItemsWrapper.FirstOrDefault(); if (listItemsWrapper != null && listItemsWrapper.ListItems != null) { listItemsToSave = listItemsWrapper.ListItems; if (listItem.Id == null) { int maxId = 0; if (listItemsToSave.Count > 0) { maxId = listItemsToSave.Max(t => t.Id ?? 0); } listItem.Id = ++maxId; } else { // If an Id has been specified, remove existing item ListItem itemToRemove = listItemsToSave.Where(r => r.Id == listItem.Id).FirstOrDefault(); if (itemToRemove != null && listItemsToSave.Remove(itemToRemove)) { listItemsToSave.Remove(itemToRemove); } } listItemsToSave.Add(listItem); } else { listItem.Id = listItem.Id ?? 0; listItemsToSave = new List <ListItem> { listItem }; } if (await _wishListRepository.SaveWishList(listItemsToSave, shopperId, listName, isPublic, wishListWrapper.Id)) { } return(listItem.Id); }
public async Task <bool> RemoveItem(int itemId, string shopperId, string listName) { bool wasRemoved = false; IList <ListItem> listItemsToSave = null; WishListWrapper wishListWrapper = await this.GetList(shopperId, listName); ListItemsWrapper listItemsWrapper = wishListWrapper.ListItemsWrapper.FirstOrDefault(); if (listItemsWrapper != null && listItemsWrapper.ListItems != null) { listItemsToSave = listItemsWrapper.ListItems; ListItem itemToRemove = listItemsToSave.FirstOrDefault(r => r.Id == itemId); if (itemToRemove != null && listItemsToSave.Remove(itemToRemove)) { wasRemoved = await _wishListRepository.SaveWishList(listItemsToSave, shopperId, listName, listItemsWrapper.IsPublic, wishListWrapper.Id); } } return(wasRemoved); }
public async Task <bool> SaveList(IList <ListItem> listItems, string shopperId, string listName, bool?isPublic, string documentId) { IList <ListItem> listItemsToSave = null; WishListWrapper wishListWrapper = await this.GetList(shopperId, listName); ListItemsWrapper listItemsWrapper = wishListWrapper.ListItemsWrapper.FirstOrDefault(); if (listItemsWrapper != null && listItemsWrapper.ListItems != null) { listItemsToSave = listItemsWrapper.ListItems; foreach (ListItem listItem in listItems) { listItemsToSave.Add(listItem); } } else { listItemsToSave = listItems; } return(await _wishListRepository.SaveWishList(listItemsToSave, shopperId, listName, isPublic, documentId)); }