all posts

WhatIWantMost: Adding Sidebar Flair II and more Product Search

Published to Blog on 26 Jan 2007

In my last post, a status update, I wrote that the next thing I wanted to add to the site was a list of the most popular items in wishlists and that I was planning on using the Amazon ID to count them.

Again, building the domain layer functionality to present the most popular items was as simple as adding a view that I added to my .netTiers template and then I regenerated my code. I added the list to the page using the same datagrid that I have used before along with the 2-3 lines of code necessary to return the list of objects and bind them to the datagrid. This process was described in a previous post so I won't rehash it here.  

 

There was one interesting point to displaying the most popular items, though. I wanted to query Amazon to get the "proper" title of the product rather than depend on user entered data. To do that I just returned the Amazon ID, or ASIN, from the view and then did a little web service call, a search, using that ASIN and then grabbed the product title from the returned data, displaying that instead. My code for doing so ended up looking something like this:

//TODO: make use of some caching here
MostPopularItemsService itemService = new MostPopularItemsService();
VList<MostPopularItems> itms = itemService.GetAll();
foreach (MostPopularItems itm in itms)
{
    string itemname = "";
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.AssociateTag = "MyAmazonAssociateID";
    searchRequest.Keywords = itm.AmazonID;
    searchRequest.SubscriptionId =  "MyAmazonSubscriptionID";
    Items items = searchRequest.Search(true);
    if (items != null) {
        itemname = items.Item[0].ItemAttributes.Title;
        url = items.Item[0].DetailPageURL;
    }
    itm.Itemname = "<a href=\"" + url + "\">" + itemname + "</a>";
}
dg.DataSource = itms;
dg.DataBind();

Though I am using the Amazon API library that encapsulates a lot of the details of working with the Amazon web service, there is still a lot of Amazon specific stuff here. I had said before that I should think about abstracting all the product search calls (last paragraph) by building my own API to handle all of them. By doing so I could remove the web service specific logic from my UI layer (approx 12 lines of code above) and end up with something like this intead:

Product product = MyProductService.Item.GetProductByASIN(asin, Amazon);

or even:

string productName = MyProductService.Item.GetProductByASIN(asin, Amazon).Title;

That would be so much nicer. I could live with that in my UI layer. Additionally, by grouping all the searches (Amazon, Yahoo, Live Search, Google, Ebay) under one library I would be able to work with the results from each of the searches using the same strongly typed objects and I could even combine them in some really interesting ways. One final and even more grand benefit is that if I build this thing properly it will be reusable in other projects and I might even be able to pass it on for others to use. That final benefit may end up being a noble side-effect, but for now my goal is to do some refactoring to make the product search API work for this project.

Based on these thoughts my next steps will involve some multi-tasking. I'm going to start prototyping the combined product search API while also continuing to build out more functionality for WIWM.

UPDATED: I've started working on the ProductSearch project. Currently it only implements one search provider, Amazon, and it only has one search method, GetItemByASIN, because those are all that I needed to implement the above code. Following is what the above code snippet now looks like by making use of the DanHounshell.ProductSearch component:

//TODO: make use of some caching here
MostPopularItemsService itemService = new MostPopularItemsService();
VList<MostPopularItems> itms = itemService.GetAll();
foreach (MostPopularItems itm in itms)
{
    ProductSearch ps = new ProductSearch(SearchProviderType.Amazon);
    Item searchItem = ps.GetItemByASIN(itm.AmazonID);
    itm.Itemname = "<a href=\"" + searchItem.Url + "\">" + searchItem.Title + "</a>";
}
dg.DataSource = itms;
dg.DataBind();

Much nicer. And ProductSearch will be reusable and sharable, well at least it will be once it has some more functionality.
 


Dan Hounshell
Web geek, nerd, amateur maker. Likes: apis, node, mobile, motorcycles, watches, food, Nashville, Savannah, Cincinnati and family. Dislikes: mean people
Dan Hounshell on Twitter


  • On 27 Jan 2007 Dave Burke said:

    I said it before and I'll say it again, I love to see that .netTiers code in action!  And yeah, you're right about the ASIN search being restrictive.

  • On 27 Jan 2007 Anonymous said:

    Dave, I'm amazed by it still, too. I'm actually saddened that I have grown so casual in explaining its usage... Ho hum add a datagrid and then add 2 lines of code, just another day at the office. It's actually pretty freakin' amazing!

  • On 6 Jul 2007 Digging My Blog - Dan Hounshell said:

    Following is a list of all the posts of the WhatIWantMost series. I'm listing them all here mostly for