Riccardo Mariani
Riccardo Mariani

Reputation: 83

Different resource for the same URI REST

I have a problem to represent correctly and efficiently the resources in a rest api. I have in my database two tables products and categories, the categories are stored in hierarchy way: id_category and parent_id and the "leafs" categories "contain" the products. The clients navigates through the categories by .../api/categories/sub/{parentid}.

In the server I do a query like this SELECT id_cat, name_cat FROM categories WHERE parent_id = {parentid} and this returns the sub categories to the clients and it is ok, but when the clients click on a leaf category I must show the products that are in relation with this category and the client request are the same .../api/categories/sub/{parentid} and in this case this request produces a list of products and not a list of sub categories!!!

The example are in php with Slim

$app->get(
'/categories/sub/:parentid',
function ($parentid) {
    $sql = "select idcat, nome_categoria FROM categorie WHERE parentid =".$parentid;
    try {
        $db = getConnection();
        $stmt = $db->query($sql);
        $cat = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $db = null;
        if (count($cat)>0) {

            deliver_response("sub categories", $cat);

        } else {
            $sql = "select p.nome_prodotto FROM categorie AS c JOIN cat_to_prod AS k ON c.idcat = k.idcat JOIN prodotti AS p ON k.codprod = p.codprod WHERE c.idcat = ".$parentid;
            $db = getConnection();
            $stmt = $db->query($sql);
            $prod = $stmt->fetchAll(PDO::FETCH_ASSOC);
            $db = null;
            if (count($prod)>0) {

                deliver_response("products", $prod);

            } else {

                deliver_response("no products found", null);

            }
        }
    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    }
}   

);

I think that is wrong in a rest solution but how can solve it? My idea is to return a "not found message" when the clients make a request ../api/categories/sub/{parentid} on a leaf category and send another request to show the products of a category with the URI .../products/category/{id_leaf_category} but this need two request by the client and it could be expansive.

What do you think?

thanks for replies and sorry for my bad english

Upvotes: 0

Views: 132

Answers (1)

Jørn Wildt
Jørn Wildt

Reputation: 4482

Use hypermedia and include a link to the sub-collections of either more categories or products.

  • If the response contains a "categories" link then the client knows it will be descending into the next level of categories by following that link.

  • If the response contains a "products" link then the client knows it can get all the products for the current category by following that link.

It would be natural to let these links refer to different resources (as you suggest yourself).

Hypermedia is of course not a requirement - you can do the same with a boolean flag and hard coded client knowledge of the possible URLs.

Another approach would be to return both categories and products in the same response:

{
  "products": [ array of products, possibly empty ],
  "categories": [ array of sub-categories (including links to them), possibly empty ],
}

This solution would allow you to expose products in non-leaf categories.

Whether or not to return "404 Not found" is an old debate with no authoritative answer. Some people prefer to return 200 OK with an empty list (no sub-categories) - others prefer 404 Not found (there exists no collection of sub-categories).

Upvotes: 1

Related Questions