1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\TechTips; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use App\Http\Controllers\Controller; |
7
|
|
|
|
8
|
|
|
use App\Models\TechTip; |
9
|
|
|
use App\Models\TechTipComment; |
10
|
|
|
use App\Events\TechTips\TechTipCommentCreatedEvent; |
11
|
|
|
use App\Events\TechTips\TechTipCommentDeletedEvent; |
12
|
|
|
use App\Events\TechTips\TechTipCommentFlaggedEvent; |
13
|
|
|
use App\Events\TechTips\TechTipCommentUpdatedEvent; |
14
|
|
|
use App\Http\Requests\TechTips\TechTipCommentRequest; |
15
|
|
|
use App\Http\Requests\TechTips\UpdateCommentRequest; |
16
|
|
|
use Inertia\Inertia; |
17
|
|
|
|
18
|
|
|
class TechTipCommentsController extends Controller |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Index method will list all comments that have been flagged |
22
|
|
|
*/ |
23
|
|
|
public function index() |
24
|
|
|
{ |
25
|
|
|
return Inertia::render('TechTips/Comments/Index', [ |
26
|
|
|
'flagged' => TechTipComment::where('flagged', true)->with('User')->get(), |
27
|
|
|
]); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Store a newly created Tech Tip Comment |
32
|
|
|
*/ |
33
|
|
|
public function store(TechTipCommentRequest $request) |
34
|
|
|
{ |
35
|
|
|
$comment = TechTipComment::create([ |
36
|
|
|
'tip_id' => $request->tip_id, |
37
|
|
|
'user_id' => $request->user()->user_id, |
38
|
|
|
'comment' => $request->comment, |
39
|
|
|
]); |
40
|
|
|
|
41
|
|
|
event(new TechTipCommentCreatedEvent($comment)); |
42
|
|
|
return back()->with([ |
43
|
|
|
'message' => 'Comment Created', |
44
|
|
|
'type' => 'success', |
45
|
|
|
]); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Show Method will un-flag a comment |
50
|
|
|
*/ |
51
|
|
|
public function show($id) |
52
|
|
|
{ |
53
|
|
|
$comment = TechTipComment::findOrFail($id); |
54
|
|
|
$this->authorize('manage', TechTip::class); |
55
|
|
|
$comment->update(['flagged' => false]); |
56
|
|
|
|
57
|
|
|
return back()->with([ |
58
|
|
|
'message' => 'Comment has been unflagged', |
59
|
|
|
'type' => 'warning', |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Edit function will flag a Tech Tip as innapropriate |
65
|
|
|
*/ |
66
|
|
|
public function edit($id) |
67
|
|
|
{ |
68
|
|
|
$comment = TechTipComment::find($id); |
69
|
|
|
$comment->update(['flagged' => true]); |
70
|
|
|
|
71
|
|
|
event(new TechTipCommentFlaggedEvent($comment)); |
|
|
|
|
72
|
|
|
return back(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Update a Tech Tip Comment |
77
|
|
|
*/ |
78
|
|
|
public function update(UpdateCommentRequest $request, $id) |
79
|
|
|
{ |
80
|
|
|
$comment = TechTipComment::find($id); |
81
|
|
|
$comment->update($request->only(['comment'])); |
82
|
|
|
$comment->save(); |
83
|
|
|
|
84
|
|
|
event(new TechTipCommentUpdatedEvent($comment)); |
|
|
|
|
85
|
|
|
return back(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Delete the Tech Tip Comment |
90
|
|
|
*/ |
91
|
|
|
public function destroy($id) |
92
|
|
|
{ |
93
|
|
|
$comment = TechTipComment::find($id); |
94
|
|
|
$this->authorize('delete', $comment); |
95
|
|
|
$comment->delete(); |
96
|
|
|
|
97
|
|
|
event(new TechTipCommentDeletedEvent($comment)); |
|
|
|
|
98
|
|
|
return back()->with([ |
99
|
|
|
'message' => 'Comment Deleted', |
100
|
|
|
'type' => 'success', |
101
|
|
|
]); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|