(REST api) What's your approach for resource categories ?

agolangf · · 342 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello.</p> <p>I&#39;m using <a href="https://github.com/julienschmidt/httprouter">httprouter</a> and i do have problem regarding resource categories.</p> <p>Let&#39;s say i do have movie resource. So, there&#39;s some standard REST path:</p> <pre><code>DELETE(&#34;/movies/:id&#34;, 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(&#34;/movies/categories/:id&#34;, deleteMovieCategory) </code></pre> <p>So, now available routes are:</p> <pre><code>DELETE(&#34;/categories/movies/:id&#34;, deleteMovieCategory) // movies as categories subresource (not so good, because movies aren&#39;t exactly categories subresource) DELETE(&#34;/movies/:id/categories/:id&#34;, deleteMovieCategory) // categories as a movie subresource (not so good i guess, because categories aren&#39;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&#39;s a problem with httprouter. It&#39;s the main reason I stopped using it. Other routers allow you to create those routes without conflict. It doesn&#39;t make sense to do a workaround for a common use case a router should handle.</p></pre>EZYCYKA: <pre><p>What&#39;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(&#34;/movies/:id&#34;, deleteMovie) DELETE(&#34;/movies/categories/:id&#34;, deleteMoviesCategory) </code></pre> <p>Will produce conflict, because {:id} can be anything, even &#34;/categories/:id&#34;.</p></pre>tv64738: <pre><p>You didn&#39;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&#39;s why i cant have something so generic like <code>/categories/:id</code>.</p> <p>For example, let&#39;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(&#34;/movies&#34;, listMovies) GET(&#34;/categories&#34;, listCategories) POST(&#34;/categories&#34;, createCategory) DELETE(&#34;/categories/:categoryId&#34;, deleteCategory) POST(&#34;/categories/:categoryId/movies&#34;, addMovieToCategory) DELETE(&#34;/categories/:categoryId/movies/:movieId&#34;, deleteMovieFromCategory) </code></pre> <p>If your API needs to be able to list all of the categories that a movie belongs to, then I&#39;d add categories as a sub-resource of movies:</p> <pre><code> GET(&#34;/movies/:movieId/categories&#34;, listCategoriesForMovie) </code></pre></pre>kerakk19: <pre><p>Thanks for comment.</p> <p>Currently i&#39;m using this approach:</p> <pre><code>POST(&#34;/categories/movies&#34;, createMoviesCategory) DELETE(&#34;/categories/movies/:id, deleteMoviesCategory) POST(&#34;/categories/series&#34;, createSeriesCategory) DELETE(&#34;/categories/series/:id, deleteSeriesCategory) POST(&#34;/categories/comics&#34;, createComicsCategory) DELETE(&#34;/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

342 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传