|
6 | 6 | use App\Http\Requests\NoteDeleteRequest; |
7 | 7 | use App\Http\Requests\NoteStoreRequest; |
8 | 8 | use App\Http\Requests\NoteUpdateRequest; |
| 9 | +use App\Models\Link; |
| 10 | +use App\Models\Note; |
9 | 11 |
|
10 | 12 | class NoteController extends Controller |
11 | 13 | { |
12 | | - /** |
13 | | - * Display a listing of the resource. |
14 | | - * |
15 | | - * @return void |
16 | | - */ |
17 | | - public function index() |
18 | | - { |
19 | | - // |
20 | | - } |
21 | | - |
22 | | - /** |
23 | | - * Show the form for creating a new resource. |
24 | | - * |
25 | | - * @return void |
26 | | - */ |
27 | | - public function create() |
28 | | - { |
29 | | - // |
30 | | - } |
31 | | - |
32 | 14 | /** |
33 | 15 | * Store a newly created resource in storage. |
34 | 16 | * |
35 | 17 | * @param NoteStoreRequest $request |
36 | | - * @return void |
| 18 | + * @return \Illuminate\Http\RedirectResponse |
37 | 19 | */ |
38 | 20 | public function store(NoteStoreRequest $request) |
39 | 21 | { |
40 | | - // |
41 | | - } |
| 22 | + $link = Link::find($request->get('link_id')); |
42 | 23 |
|
43 | | - /** |
44 | | - * Display the specified resource. |
45 | | - * |
46 | | - * @param int $id |
47 | | - * @return void |
48 | | - */ |
49 | | - public function show($id) |
50 | | - { |
51 | | - // |
| 24 | + if ($link->user_id !== auth()->id()) { |
| 25 | + abort(403); |
| 26 | + } |
| 27 | + |
| 28 | + $data = $request->except(['_token']); |
| 29 | + $data['user_id'] = auth()->id(); |
| 30 | + |
| 31 | + $note = Note::create($data); |
| 32 | + |
| 33 | + Note::flushCache(); |
| 34 | + Link::flushCache(); |
| 35 | + |
| 36 | + alert(trans('note.added_successfully'), 'success'); |
| 37 | + |
| 38 | + return redirect()->route('links.show', [$link->id]); |
52 | 39 | } |
53 | 40 |
|
54 | 41 | /** |
55 | 42 | * Show the form for editing the specified resource. |
56 | 43 | * |
57 | 44 | * @param int $id |
58 | | - * @return void |
| 45 | + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
59 | 46 | */ |
60 | 47 | public function edit($id) |
61 | 48 | { |
62 | | - // |
| 49 | + $note = Note::find($id); |
| 50 | + |
| 51 | + if (empty($note)) { |
| 52 | + abort(404); |
| 53 | + } |
| 54 | + |
| 55 | + if ($note->user_id !== auth()->id()) { |
| 56 | + abort(404); |
| 57 | + } |
| 58 | + |
| 59 | + return view('models.notes.edit')->with('note', $note); |
63 | 60 | } |
64 | 61 |
|
65 | 62 | /** |
66 | 63 | * Update the specified resource in storage. |
67 | 64 | * |
68 | 65 | * @param NoteUpdateRequest $request |
69 | 66 | * @param int $id |
70 | | - * @return void |
| 67 | + * @return \Illuminate\Http\RedirectResponse |
71 | 68 | */ |
72 | 69 | public function update(NoteUpdateRequest $request, $id) |
73 | 70 | { |
74 | | - // |
| 71 | + $note = Note::find($request->get('note_id')); |
| 72 | + |
| 73 | + if (empty($note)) { |
| 74 | + abort(404); |
| 75 | + } |
| 76 | + |
| 77 | + $data = $request->except(['_token', 'note_id']); |
| 78 | + $data['is_private'] = $data['is_private'] ?? false; |
| 79 | + |
| 80 | + $note->update($data); |
| 81 | + |
| 82 | + Note::flushCache(); |
| 83 | + Link::flushCache(); |
| 84 | + |
| 85 | + alert(trans('note.updated_successfully'), 'success'); |
| 86 | + |
| 87 | + return redirect()->route('links.show', [$note->link->id]); |
75 | 88 | } |
76 | 89 |
|
77 | 90 | /** |
78 | 91 | * Remove the specified resource from storage. |
79 | 92 | * |
80 | 93 | * @param NoteDeleteRequest $request |
81 | 94 | * @param int $id |
82 | | - * @return void |
| 95 | + * @return \Illuminate\Http\RedirectResponse |
| 96 | + * @throws \Exception |
83 | 97 | */ |
84 | 98 | public function destroy(NoteDeleteRequest $request, $id) |
85 | 99 | { |
86 | | - // |
| 100 | + $note = Note::find($id); |
| 101 | + |
| 102 | + if (empty($note)) { |
| 103 | + abort(404); |
| 104 | + } |
| 105 | + |
| 106 | + if ($note->link->user_id !== auth()->id()) { |
| 107 | + abort(404); |
| 108 | + } |
| 109 | + |
| 110 | + $link = $note->link->id; |
| 111 | + $note->delete(); |
| 112 | + |
| 113 | + Note::flushCache(); |
| 114 | + Link::flushCache(); |
| 115 | + |
| 116 | + alert(trans('note.deleted_successfully'), 'success'); |
| 117 | + |
| 118 | + return redirect()->route('links.show', [$link]); |
87 | 119 | } |
88 | 120 | } |
0 commit comments