Added services

This commit moves most controller logic onto Services. Services are part of the Service-Repository pattern. The models act as repositories.

Services are easily testable and are needed for the upcoming API, in order to avoid duplicated code and to maintain a single source of "truth".

 The User, Vacancy and Vote controllers still need their logic moved onto services.
This commit is contained in:
2021-07-25 22:54:15 +01:00
parent c739933668
commit 8942623bde
44 changed files with 1308 additions and 691 deletions

View File

@@ -24,21 +24,22 @@ namespace App\Http\Controllers;
use App\Application;
use App\Comment;
use App\Http\Requests\NewCommentRequest;
use App\Services\CommentService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class CommentController extends Controller
{
private $commentService;
public function __construct(CommentService $commentService) {
$this->commentService = $commentService;
}
public function insert(NewCommentRequest $request, Application $application)
{
$this->authorize('create', Comment::class);
$comment = Comment::create([
'authorID' => Auth::user()->id,
'applicationID' => $application->id,
'text' => $request->comment,
]);
$comment = $this->commentService->addComment($application, $request->comment);
if ($comment) {
$request->session()->flash('success', __('Comment posted!'));
@@ -52,10 +53,10 @@ class CommentController extends Controller
public function delete(Request $request, Comment $comment)
{
$this->authorize('delete', $comment);
$this->commentService->deleteComment($comment);
$comment->delete();
$request->session()->flash('success', __('Comment deleted!'));
return redirect()->back();
return redirect()
->back()
->with('success', __('Comment deleted!'));
}
}