in api.go [99:129]
func (h apiHandlers) getProductDetails(c *gin.Context) {
idString := c.Param("id")
if idString == "top" {
products, err := getTopProducts(c.Request.Context(), h.db)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.JSON(http.StatusOK, products)
return
}
// Product by ID.
id, err := strconv.Atoi(idString)
if err != nil {
err := errors.Wrap(err, "failed to parse product ID")
c.AbortWithError(http.StatusInternalServerError, err)
return
}
product, err := getProduct(c.Request.Context(), h.db, id)
if err != nil {
err := errors.Wrap(err, "failed to get product")
c.AbortWithError(http.StatusInternalServerError, err)
return
}
if product == nil {
c.AbortWithStatus(http.StatusNotFound)
return
}
c.JSON(http.StatusOK, product)
}