1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace BristolSU\Module\UploadFile\Http\Controllers\ParticipantApi; |
4
|
|
|
|
5
|
|
|
use BristolSU\Module\UploadFile\Events\CommentCreated; |
6
|
|
|
use BristolSU\Module\UploadFile\Events\CommentDeleted; |
7
|
|
|
use BristolSU\Module\UploadFile\Events\CommentUpdated; |
8
|
|
|
use BristolSU\Module\UploadFile\Http\Controllers\Controller; |
9
|
|
|
use BristolSU\Module\UploadFile\Models\Comment; |
10
|
|
|
use BristolSU\Module\UploadFile\Models\File; |
11
|
|
|
use BristolSU\Support\Activity\Activity; |
12
|
|
|
use BristolSU\Support\ActivityInstance\Contracts\ActivityInstanceResolver; |
13
|
|
|
use BristolSU\Support\Authentication\Contracts\Authentication; |
14
|
|
|
use BristolSU\Support\ModuleInstance\ModuleInstance; |
15
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
16
|
|
|
use Illuminate\Http\Request; |
17
|
|
|
|
18
|
|
|
class CommentController extends Controller |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
|
21
|
4 |
|
public function index(Request $request, Activity $activity, ModuleInstance $moduleInstance, File $file) |
|
|
|
|
22
|
|
|
{ |
23
|
4 |
|
$this->authorize('comment.index'); |
24
|
3 |
|
if ((int)$file->activity_instance_id !== (int)app(ActivityInstanceResolver::class)->getActivityInstance()->id) { |
25
|
1 |
|
throw new AuthorizationException(); |
26
|
|
|
} |
27
|
2 |
|
return $file->comments; |
28
|
|
|
} |
29
|
|
|
|
30
|
5 |
|
public function store(Request $request, Activity $activity, ModuleInstance $moduleInstance, File $file) |
|
|
|
|
31
|
|
|
{ |
32
|
5 |
|
$this->authorize('comment.store'); |
33
|
|
|
|
34
|
4 |
|
$comment = $file->comments()->create([ |
|
|
|
|
35
|
4 |
|
'comment' => $request->input('comment'), |
36
|
4 |
|
'posted_by' => app(Authentication::class)->getUser()->id |
|
|
|
|
37
|
|
|
]); |
|
|
|
|
38
|
|
|
|
39
|
4 |
|
event(new CommentCreated($comment)); |
40
|
|
|
|
41
|
4 |
|
return $comment; |
42
|
|
|
} |
43
|
|
|
|
44
|
6 |
|
public function destroy(Request $request, Activity $activity, ModuleInstance $moduleInstance, Comment $comment) |
|
|
|
|
45
|
|
|
{ |
46
|
6 |
|
$this->authorize('comment.destroy'); |
47
|
|
|
|
48
|
5 |
|
if ((int)$comment->file->activity_instance_id !== (int)app(ActivityInstanceResolver::class)->getActivityInstance()->id) { |
49
|
1 |
|
throw new AuthorizationException(); |
50
|
|
|
} |
51
|
|
|
|
52
|
4 |
|
$comment->delete(); |
53
|
|
|
|
54
|
4 |
|
event(new CommentDeleted($comment)); |
55
|
|
|
|
56
|
4 |
|
return $comment; |
57
|
|
|
} |
58
|
|
|
|
59
|
6 |
|
public function update(Request $request, Activity $activity, ModuleInstance $moduleInstance, Comment $comment) |
|
|
|
|
60
|
|
|
{ |
61
|
6 |
|
$this->authorize('comment.update'); |
62
|
|
|
|
63
|
5 |
|
if ((int)$comment->file->activity_instance_id !== (int)app(ActivityInstanceResolver::class)->getActivityInstance()->id) { |
64
|
1 |
|
throw new AuthorizationException(); |
65
|
|
|
} |
66
|
|
|
|
67
|
4 |
|
$comment->comment = $request->input('comment', $comment->comment); |
68
|
|
|
|
69
|
4 |
|
$comment->save(); |
70
|
|
|
|
71
|
4 |
|
event(new CommentUpdated($comment)); |
72
|
|
|
|
73
|
4 |
|
return $comment; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
} |