1

I'm looking for some guidance here.

Scenario:

I have a Post model with a polymorphic relation to a Comment model. Whenever I want to create a new comment for a given post I have the following endpoint:

$router->post('/posts/{post}/comments', 'PostsCommentsController@store');

So far so good. Now I want to add an Attachment model which will also be a polymorphic relation since I may need to add attachments to more things other than comments (ex: messages, etc).

My first idea was doing something in the lines of:

$router->post('/posts/{post}/comments/attachments', 'PostsCommentsAttachmentsController@store');

So the comment will belong to a post and will have an attachment.

This feels a bit "dirty" to me (especially the controller name) and the need of having 3 nested resources (maybe I'm just thinking too much).

Hope I was clear enough explaining my problem :)


Have anyone faced something like this before? How did you guys solve it?

Other approaches? Am I thinking completely wrong?

Open to ideas and suggestions :D

Thank you all.

Helder Lucas
  • 3,273
  • 2
  • 21
  • 26

2 Answers2

1

I'd prefer Single Responsibility for each Controllers or route. So it's pretty clear what their actually do and handle. Let me give you example:

- Post
/posts               -> list all post
/posts/{id}          -> get specific post
/posts/{id}/comments -> get comments of the post

- Comment
/comments/{id}            -> get specific comment
/comments/{id}/attacments -> get attacments of a comment

- Attachment
/attachment/{id} -> get specific attachment

For Controller name, just keep it simple. Just usePostController, CommentController and AttachmentController. It's quite clear I think.

Dharma Saputra
  • 1,524
  • 12
  • 17
  • Thanks. My `comments` table has these two fields: `commentable_id`(post_id, tasks_id, etc) and the `commentable_type`( a string that can be 'posts', 'tasks', etc) so when I create a comment I need to dynamically know its type and the same happens to the attachment (it can be in the context of a comment or a chat message). So should I first make a request to `/posts/{id}/comments` to create and get the comment resource in the response and then do another request to `/comments/{id}/attachments' so I can specify the attachment is in the context of a comment? – Helder Lucas Oct 06 '17 at 10:00
  • Do you mean submiting a comment of a post and the comment has an attachment with it? – Dharma Saputra Oct 06 '17 at 10:41
  • Yes, it is exactly that. – Helder Lucas Oct 06 '17 at 10:51
  • Actually you doesn't need put not every process in `controller`. Because when you put it in `controller`, it's mean you should call it via `http` request. I suggest, you create a `helper` class or `helper` function. So you can call it in every place you need it. Even in many `controller`. I hope you can understand. I am sorry, if my explanation is not very clear. – Dharma Saputra Oct 06 '17 at 11:03
0

After more researching, I actually realized that what I wanted to do was basically a file upload with metadata.

Came across this excellent post: HTTP/REST API File Uploads that explains it

PS: Thank you Dharma for the help and time.

Helder Lucas
  • 3,273
  • 2
  • 21
  • 26