<p>Hello.</p>
<p>I'm using <a href="https://github.com/julienschmidt/httprouter">httprouter</a> and i do have problem regarding resource categories.</p>
<p>Let's say i do have movie resource.
So, there's some standard REST path:</p>
<pre><code>DELETE("/movies/:id", deleteMovie)
</code></pre>
<p>But now i want to make routes for movie categories, and this approach will produce route conflict:</p>
<pre><code>DELETE("/movies/categories/:id", deleteMovieCategory)
</code></pre>
<p>So, now available routes are:</p>
<pre><code>DELETE("/categories/movies/:id", deleteMovieCategory) // movies as categories subresource (not so good, because movies aren't exactly categories subresource)
DELETE("/movies/:id/categories/:id", deleteMovieCategory) // categories as a movie subresource (not so good i guess, because categories aren't exactly single movie subresource)
</code></pre>
<p>But neither satifies me, so what is standard way to do this?</p>
<hr/>**评论:**<br/><br/>mgutz: <pre><p>That's a problem with httprouter. It's the main reason I stopped using it. Other routers allow you to create those routes without conflict. It doesn't make sense to do a workaround for a common use case a router should handle.</p></pre>EZYCYKA: <pre><p>What's the categories for? Accessing a subset of all movies? Make it only a GET endpoint then.</p></pre>kerakk19: <pre><p>Yes, but these categories are managed by user. And he need option to create and delete categories.</p></pre>EZYCYKA: <pre><p>I see. Then you can just change the paths to /movies/categories/:a and movies/titles/:b, right? </p></pre>kerakk19: <pre><p>No, because there are conflicts in routes.</p>
<p>For example</p>
<pre><code>DELETE("/movies/:id", deleteMovie)
DELETE("/movies/categories/:id", deleteMoviesCategory)
</code></pre>
<p>Will produce conflict, because {:id} can be anything, even "/categories/:id".</p></pre>tv64738: <pre><p>You didn't read what he wrote..</p></pre>nagai: <pre><p>Well what does your model look like exactly? Maybe have another endpoint <code>/categories/:id</code>, returning a category with a set of movie ID:s, which you then query for on <code>/movies/:ids</code>? You want to delete a category, and not the movies, correct?</p>
<pre><code>type Category struct {
ID int
Name string
Movies []int
...
}
</code></pre></pre>kerakk19: <pre><p>Hey, main problem is, i will have multiple resources with categories managed by user, that's why i cant have something so generic like <code>/categories/:id</code>.</p>
<p>For example, let's say i will have movies, series, books and comics and every of there resources will have categories.</p>
<p>So for now i will probably stick to this model: </p>
<p><code>/categories/movies/:id</code></p>
<p><code>/categories/series/:id</code></p>
<p>etc...</p></pre>rwbcxrz: <pre><p>I think a fairly typical structure would look something like this:</p>
<pre><code> GET("/movies", listMovies)
GET("/categories", listCategories)
POST("/categories", createCategory)
DELETE("/categories/:categoryId", deleteCategory)
POST("/categories/:categoryId/movies", addMovieToCategory)
DELETE("/categories/:categoryId/movies/:movieId", deleteMovieFromCategory)
</code></pre>
<p>If your API needs to be able to list all of the categories that a movie belongs to, then I'd add categories as a sub-resource of movies:</p>
<pre><code> GET("/movies/:movieId/categories", listCategoriesForMovie)
</code></pre></pre>kerakk19: <pre><p>Thanks for comment.</p>
<p>Currently i'm using this approach:</p>
<pre><code>POST("/categories/movies", createMoviesCategory)
DELETE("/categories/movies/:id, deleteMoviesCategory)
POST("/categories/series", createSeriesCategory)
DELETE("/categories/series/:id, deleteSeriesCategory)
POST("/categories/comics", createComicsCategory)
DELETE("/categories/comics/:id, deleteComicsCategory)
</code></pre>
<p>I see your approach uses one generic Categories resource for all other resources. It can work, because i guess some categories can be shared among movies/series/comics etc...</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传