tomhartley97
2 months ago

Question regarding Best Practice for Controller Methods

Posted 2 months ago by tomhartley97

Say for example I am creating a GitHub style discussion thread in my application that gives the user to post messages into a thread, open an issue and close an issue.

Therefore I would create a DiscussionController and use the store method to add a new message in the database that the user enters using a form within the app with the correct data for issue id, user id etc.

But if I wanted to add a an option for the user to "Close" an issue, the store method wouldn't be appropriate, because I don't need the user to enter a message in order to close the issue.

What would be the best approach for this? Should I create a new method in my DiscussionController called storeCloseIssue and storeOpenIssue or is this something I can do through the my Discussion model?

Or should I create a new method in my IssueController and use the following in a updateStatus method? Or is this messy and disorganised?

use App\Discussion;
use App\Issue;

class IssueController extends Controller
{
  public function updateStatus(Issue $issue)
  {
    Issue::where('id', $issue->id)->update(['status' => $status]);

    /**
     *
     *  Type
     *  1 = A custom message
     *  2 = "Username opened this issue"
     *  3 = "Username closed this issue"
     *
     */
  
    $attributes = [
      'issue_id' => $issue->id,
      'type' => 1
    ]
  
    Discussion::create($attributes);
  }
}

Or should I create a StatusController and organise the status' like that?

Also, my application is using Vue.js for the view so therefore is it correct in saying that I don't have any other choice but to create a new endpoint?

Example:

Route::resource('/api/v1/issues', 'IssueController');
Route::post('/api/v1/issues/{issue}/discussion', '[email protected]');
Route::put('/api/v1/issues/{issue}/status', '[email protected]')

Thank you for any help in advance.

Please sign in or create an account to participate in this conversation.