1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Faithgen\AppBuild\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Faithgen\AppBuild\Http\Resources\Template as TemplateResource; |
6
|
|
|
use Faithgen\AppBuild\Models\Template; |
7
|
|
|
use Faithgen\AppBuild\Services\TemplateService; |
8
|
|
|
use FaithGen\SDK\Helpers\CommentHelper; |
9
|
|
|
use FaithGen\SDK\Http\Requests\CommentRequest; |
10
|
|
|
use Illuminate\Http\Request; |
11
|
|
|
use Illuminate\Routing\Controller; |
12
|
|
|
use InnoFlash\LaraStart\Helper; |
13
|
|
|
use InnoFlash\LaraStart\Http\Requests\IndexRequest; |
14
|
|
|
|
15
|
|
|
class TemplateController extends Controller |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var TemplateService |
19
|
|
|
*/ |
20
|
|
|
private TemplateService $templateService; |
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* TemplateController constructor. |
24
|
|
|
* |
25
|
|
|
* @param TemplateService $templateService |
26
|
|
|
*/ |
27
|
|
|
public function __construct(TemplateService $templateService) |
28
|
|
|
{ |
29
|
|
|
$this->templateService = $templateService; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Fetches the templates. |
34
|
|
|
* |
35
|
|
|
* @param IndexRequest $request |
36
|
|
|
* |
37
|
|
|
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection |
38
|
|
|
*/ |
39
|
|
|
public function index(IndexRequest $request) |
40
|
|
|
{ |
41
|
|
|
$templates = Template::latest() |
42
|
|
|
->withCount('comments') |
43
|
|
|
->active() |
44
|
|
|
->search(['name', 'branch', 'repository', 'description'], $request->filter_text) |
45
|
|
|
->paginate(Helper::getLimit($request)); |
46
|
|
|
|
47
|
|
|
TemplateResource::wrap('templates'); |
48
|
|
|
|
49
|
|
|
return TemplateResource::collection($templates); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Fetches the comments of a template. |
54
|
|
|
* |
55
|
|
|
* @param Request $request |
56
|
|
|
* @param Template $template |
57
|
|
|
* |
58
|
|
|
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection |
59
|
|
|
*/ |
60
|
|
|
public function comments(Request $request, Template $template) |
61
|
|
|
{ |
62
|
|
|
return CommentHelper::getComments($template, $request); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Sends a comment for the given template. |
67
|
|
|
* |
68
|
|
|
* @param \Faithgen\AppBuild\Models\Template $template |
69
|
|
|
* @param CommentRequest $request |
70
|
|
|
* |
71
|
|
|
* @return \Illuminate\Http\JsonResponse |
72
|
|
|
*/ |
73
|
|
|
public function comment(Template $template, CommentRequest $request) |
74
|
|
|
{ |
75
|
|
|
return CommentHelper::createComment($template, $request); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function show(Template $template) |
79
|
|
|
{ |
80
|
|
|
$template->load('images'); |
81
|
|
|
|
82
|
|
|
TemplateResource::withoutWrapping(); |
83
|
|
|
|
84
|
|
|
return new TemplateResource($template); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|