I'm currently building a backend API for my web app in laravel. This app should enable the user to save contents to a watchlist. I have already built a crud API for the contents and all necessary endpoints for user authentication.
The endpoint I've got already:
Route::post('user/register', [UserController::class, 'register']);
Route::post('user/login', [UserController::class, 'login']) >name('login');
Route::middleware('auth:sanctum')->group(function (){
Route::get('user/login', [UserController::class, 'loggedInUser']);
Route::post('user/logout', [UserController::class, 'logout']);
Route::apiResource('content', ContentController::class);
});
Now I want to add an endpoint that would take a user id and a content-id to add/deletes this content to/from the specified user's watch list.
My question now is how would I name this endpoint, watchlist/user/{id}/content/{id}/add
?
It would be be good to make the request a POST method with route like watchlist/user/{id}
and have POST data as {'content': id}
. Similarly to remove the content from watchlist you can reuse the same end point but change the method to DELETE.
Even better you can remove the user id from the endpoint and use your JWT token data to retrieve user data. Using which you can insert/delete corresponding watchlist content.