I have a simple WebApi app managing Gym visits - workouts, exercises, weights etc. Two entities of interest here are User (visitor) and Workout. UsersController and WorkoutsController are doing their typical CRUD operations and adding HATEOAS links to each resource in the response. On top of that UsersController can provide list of workouts for individual User via route "api/v1/Users/{id}/Workouts".
But now I am unable to generate links for Workouts because UsersController doesn't work with this entity and can generate links for Users only. I know that instantiating WorkoutsController directly is a bad practice, so I didn't try that. So my question is how to make it work without breaking REST? Is there an out-of-box solution or do I need my own implementation/workaround for this one?
Thanks.
[EDIT] Code I'm using to generate links for resources:
public static class ResourceLinksGeneration
{
public static List<Link> CreateLinksForResource(this LinkGenerator linkGenerator,
HttpContext context,
int resourceID,
Dictionary<string, string> methods)
{
return new List<Link>
{
new Link
{
HttpMethod = "GET",
Description = "self",
Url = linkGenerator.GetUriByAction(context, methods[nameof(DefaultApiConventions.Get)], values: new { id = resourceID } )
},
new Link
{
HttpMethod = "POST",
Description = "add",
Url = linkGenerator.GetUriByAction(context, methods[nameof(DefaultApiConventions.Post)])
},
new Link
{
HttpMethod = "PUT",
Description = "update",
Url = linkGenerator.GetUriByAction(context, methods[nameof(DefaultApiConventions.Put)])
},
new Link
{
HttpMethod = "DELETE",
Description = "delete",
Url = linkGenerator.GetUriByAction(context, methods[nameof(DefaultApiConventions.Delete)], values: new { id = resourceID })
}
};
}
}