Retrieving the product detail from the datastore

The product registry data store in the Redis datastore is JSON data representing a slice of strings. We use the Unmarshal function found in the json package to unmarshal the JSON encoded data into the productKeys variable. Now, that we have all the product keys that should be displayed on the products listing page, it's time to create a product instance for each key. We do so, by first declaring the products variable that will be a slice of products. We iterate through the product keys and derive the productTitle value, which is the SEO friendly name of the product. We supply the productTitle variable to the GetProductDetail method of the Redis datastore to fetch a product for the given product title. We assign the fetched product to the product variable and append it to the products slice. Once the for loop ends, we will have collected all the products that should be displayed on the product listing page. Finally, we return the products slice.

Let's examine the GetProductDetail method defined in the common/datastore/redis.go source file:

func (r *RedisDatastore) GetProductDetail(productTitle string) *models.Product {

productKey := "/product-detail/" + productTitle
exists, err := r.Cmd("EXISTS", productKey).Int()

if err != nil {
log.Println("Encountered error: ", err)
return nil
} else if exists == 0 {
return nil
}

var p models.Product
jsonData, err := r.Cmd("GET", productKey).Str()

if err != nil {
log.Print("Encountered error when attempting to fetch product data from Redis instance: ", err)
return nil
}

if err := json.Unmarshal([]byte(jsonData), &p); err != nil {
log.Print("Encountered error when attempting to unmarshal JSON product data: ", err)
return nil
}

return &p

}

We assign the productKey variable of the string type with the value of the route to the product detail page. This involves concatenating the "/product-detail" string with the productTitle variable for the given product. We check to see whether the product key exists in the Redis datastore. If it doesn't exist, we return from the method, and if it does exist, we continue on and declare the p variable of the Product type. This will be the variable that the function will return. The product data stored in the Redis datastore is the JSON representation of a Product object. We unmarshal the JSON encoded data into the p variable. If we didn't encounter any errors, we return p, which represents the Product object for the requested productTitle variable that was specified as the input argument to the GetProductDetail method.

At this point, we have satisfied the data needs to display a list of products for the /products route and to display a product's profile page for the /product-detail/{productTitle} route. Now it's time to register the server-side routes for the product-related pages.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.173.199