I'm struggle with path for my tags collection.
I got products that may have tags and posts that may also have same tags.
What is better approach with returning all tags associated to specific collection??
List of all tags associated to products or posts
[GET] /products/tags
[GET] /posts/tags
or
[GET] /tags?type=products
[GET] /tags?type=posts
Collection of tags will be returned.
Related
Given a resource called pets, there should be two ways of retrieving the resource with the pet endpoint:
GET .../pets/{petId} - retrieve a pet by its id
GET .../pets/ - retrieves all pets
How should the API respond when the database doesn't find anything at the resource path?
My assumption is that:
GET .../pets/{petId} -> results in a HTTP 404 (Not found / error). This is because the API has been requested to find a specific resource (petId), it is an error as that pet id doesn't exist.
GET .../pets/ -> returns an empty array [] with HTTP 200 (ok). This is because the API has been requested to find all pet resources, the database just happens to not contain any.
Is this the right approach?
Is there any documentation / resources that explains what is meant to happen in this scnario? (I couldn't find anything using search)
Is there a way (a better way) to get the latest created record from database using loopback REST API?
I can get the latest created record by sorting the created property in descending order, and get the first object from the result. This is what I have now:
[GET] http://localhost:3335/v0/contracts?filter[order]=created DESC
but this returns the whole contracts modal records, is there a way to return only one single latest record?
[GET] http://localhost:3335/v0/contracts?filter[order]=created DESC&filter[limit]=1
We have a resource collection for products: /products.
We want to filter this collection to only return the members which have one of a list of specific class id's. For example:
GET /products?classes=100,101,102
This should return a collection of product members which have any of the classes listed.
The issue we have, is that we're working with thousands of products and classes, so the class list of id's could be thousands long - too long for a query string.
I'm keen to stick to RESTful principles whenever we can, so I like the fact that the resource /products?classes=100,101,102 when called with GET returns a filtered products collection.
Obviously, we could include the id's list in the body in JSON format, but that would mean that the call GET /products won't return a representation of the state of the resource (the resource being the URL), because the body is being used to provide filter options.
What's the best way to request a collection which is filtered, but the filter options are too long to use the query string..?
Interesting comment from #C. Smith who suggests making a POST call using a X-HTTP-Method-Override header set to GET and passing the id's in the body. This would work.
After thinking about it we're probably going to limit the number of class id's allowed in the query string, and suggest making multiple calls, breaking up the id's list into say, groups of 200. Submitting more than 200 would return an error.
GET /products?classes=1004,2342,8753... (limited to 200 id's)
GET /products?classes=2326,3343,6981... (limited to 200 id's)
Then the results can easily be stitched together after.
This method would let you use 5,000 id's for example, by doing 25 calls, which while not ideal, is ok for our use case.
I am creating a MongoDB database with a users collection (with UserFiles in it) and a posts collection. Each post has tags and sharedFrom fields in it. I eventually plan to have users' search results influenced by what tags they normally post about and from which other users they often share posts. Would it be better to:
make a field in the UserFile document of each user that lists the post IDs made by the user?
make a field in the UserFile that documents that lists all the tags they have used and other users that they have sharedFrom?
make the search function look up the searchers activity that then influences the search results?
something I haven't thought of?
I'm creating a REST API in Laravel and there is one thing I can't figure out. The situation is as follows; a user can log in, browse articles and make them a favorite.
The favorite table consists out of article_id and user_id
The requests are designed in the following way:
[GET] /article/24 view article 24
[GET] /article/24/favorite indicates a favorite, if not return 404
[POST] /article/24/favorite make favorite
[DELETE] /article/24/favorite remove favorite
Seems fine, but the nested resources by default expect the following format:
/controller/[id]/sub_controller/[sub_id]
What's the best way to avoid this sub id requirement? I don't need the ID of the favorite because the combination of article id and user id are enough to locate the item in my database.
These are my current routes for the requests:
Route::resource('articles', 'ArticlesController',
array('only' => array('index','store','show','destroy')));
Route::resource('articles.favorite', 'ArticlesFavoriteController',
array('only' => array('show','store','destroy')));
Currently, I require the API user to append /0 to the url in order to trigger the right route, but there must be a better way.
I think you should consider not using Laravel's ResourceController here. Maybe you should go with RestfullControllers for your specific needs.