|
2 | 2 |
|
3 | 3 | namespace App\Http\Controllers; |
4 | 4 |
|
5 | | -use Illuminate\Http\Request; |
6 | 5 | use App\Models\Note; |
7 | | -use Illuminate\Pagination\CursorPaginator; |
8 | | - |
| 6 | +use App\Models\UserNote; |
| 7 | +use Illuminate\Http\Request; |
| 8 | +use Illuminate\Support\Facades\Auth; |
9 | 9 |
|
10 | 10 | class NoteController extends Controller |
11 | 11 | { |
| 12 | + /** |
| 13 | + * Return a paginated list of the authenticated user's notes |
| 14 | + * in a shape compatible with the frontend InfiniteQuery: |
| 15 | + * { |
| 16 | + * results: UserNote[], |
| 17 | + * nextPage: int|null, |
| 18 | + * hasNextPage: bool |
| 19 | + * } |
| 20 | + */ |
12 | 21 | public function index(Request $request) |
13 | | -{ |
14 | | - $query = Note::query(); |
| 22 | + { |
| 23 | + $userId = Auth::id(); |
15 | 24 |
|
16 | | - if ($request->has('tag')) { |
17 | | - $query->where('tag_id', $request->get('tag')); |
18 | | - } |
| 25 | + $query = UserNote::with(['note']) |
| 26 | + ->where('user_id', $userId) |
| 27 | + ->orderByDesc('updated_at'); |
| 28 | + |
| 29 | + // Optional search against the related note title/content |
| 30 | + if ($request->filled('search')) { |
| 31 | + $search = '%' . $request->string('search')->toString() . '%'; |
| 32 | + $query->whereHas('note', function ($q) use ($search) { |
| 33 | + $q->where('title', 'like', $search) |
| 34 | + ->orWhere('content', 'like', $search); |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + // Optional sorting; allow only a limited set of columns |
| 39 | + $allowedSortColumns = ['created_at', 'updated_at']; |
| 40 | + $sortBy = $request->string('sort_by')->toString(); |
| 41 | + $sortDir = $request->string('sort_direction')->toString() ?: 'desc'; |
| 42 | + if ($sortBy && in_array($sortBy, $allowedSortColumns)) { |
| 43 | + $query->orderBy($sortBy, in_array(strtolower($sortDir), ['asc', 'desc']) ? $sortDir : 'desc'); |
| 44 | + } |
| 45 | + |
| 46 | + $perPage = (int) ($request->integer('per_page') ?: 10); |
| 47 | + $page = (int) ($request->integer('page') ?: 1); |
| 48 | + $paginator = $query->paginate($perPage, ['*'], 'page', $page); |
19 | 49 |
|
20 | | - if ($request->has('notebook')) { |
21 | | - $query->where('notebook_id', '<=', $request->get('notebook')); |
| 50 | + return response()->json([ |
| 51 | + 'results' => $paginator->items(), |
| 52 | + 'nextPage' => $paginator->currentPage() < $paginator->lastPage() |
| 53 | + ? $paginator->currentPage() + 1 |
| 54 | + : null, |
| 55 | + 'hasNextPage' => $paginator->hasMorePages(), |
| 56 | + ]); |
22 | 57 | } |
23 | 58 |
|
| 59 | + /** Create a new note and attach to current user */ |
| 60 | + public function store(Request $request) |
| 61 | + { |
| 62 | + $validated = $request->validate([ |
| 63 | + 'title' => ['required', 'string', 'max:255'], |
| 64 | + 'content' => ['nullable', 'string'], |
| 65 | + ]); |
24 | 66 |
|
25 | | - if ($request->has('sort_by')) { |
26 | | - $sortBy = $request->get('sort_by'); |
27 | | - $sortDirection = $request->get('sort_direction', 'asc'); |
| 67 | + $note = Note::create([ |
| 68 | + 'title' => $validated['title'], |
| 69 | + 'content' => $validated['content'] ?? '', |
| 70 | + ]); |
28 | 71 |
|
29 | | - // Validate sort_by to prevent SQL injection |
30 | | - $allowedSortColumns = ['title', 'updated_at', 'created_at']; |
31 | | - if (in_array($sortBy, $allowedSortColumns)) { |
32 | | - $query->orderBy($sortBy, $sortDirection); |
33 | | - } |
| 72 | + $userNote = UserNote::create([ |
| 73 | + 'note_id' => $note->id, |
| 74 | + 'user_id' => Auth::id(), |
| 75 | + 'is_favorited' => false, |
| 76 | + 'is_pinned' => false, |
| 77 | + 'is_trashed' => false, |
| 78 | + ]); |
| 79 | + |
| 80 | + $userNote->load('note'); |
| 81 | + return response()->json($userNote, 201); |
34 | 82 | } |
35 | 83 |
|
| 84 | + /** Update an existing note (only if it belongs to the user) */ |
| 85 | + public function update(Request $request, string $id) |
| 86 | + { |
| 87 | + $userNote = UserNote::with('note') |
| 88 | + ->where('id', $id) |
| 89 | + ->where('user_id', Auth::id()) |
| 90 | + ->firstOrFail(); |
| 91 | + |
| 92 | + $validated = $request->validate([ |
| 93 | + 'title' => ['sometimes', 'string', 'max:255'], |
| 94 | + 'content' => ['sometimes', 'string', 'nullable'], |
| 95 | + 'is_favorited' => ['sometimes', 'boolean'], |
| 96 | + 'is_pinned' => ['sometimes', 'boolean'], |
| 97 | + 'is_trashed' => ['sometimes', 'boolean'], |
| 98 | + ]); |
| 99 | + |
| 100 | + if (array_key_exists('title', $validated) || array_key_exists('content', $validated)) { |
| 101 | + $userNote->note->fill([ |
| 102 | + 'title' => $validated['title'] ?? $userNote->note->title, |
| 103 | + 'content' => $validated['content'] ?? $userNote->note->content, |
| 104 | + ])->save(); |
| 105 | + } |
36 | 106 |
|
| 107 | + $userNote->fill($validated)->save(); |
| 108 | + $userNote->load('note'); |
37 | 109 |
|
38 | | - if ($request->has('search')) { |
39 | | - $searchTerm = '%' . $request->get('search') . '%'; |
40 | | - $query->where('title', 'like', $searchTerm) |
41 | | - ->orWhere('content', 'like', $searchTerm); |
| 110 | + return response()->json($userNote); |
42 | 111 | } |
43 | 112 |
|
| 113 | + /** Soft delete the link and the note (if this is the last owner) */ |
| 114 | + public function destroy(string $id) |
| 115 | + { |
| 116 | + $userNote = UserNote::with('note') |
| 117 | + ->where('id', $id) |
| 118 | + ->where('user_id', Auth::id()) |
| 119 | + ->firstOrFail(); |
44 | 120 |
|
45 | | - $notes = $query->cursorPaginate(10); |
46 | | - return response()->json($notes); |
47 | | -} |
| 121 | + $note = $userNote->note; |
| 122 | + $userNote->delete(); |
| 123 | + |
| 124 | + // If no other users are linked to this note, delete the note itself |
| 125 | + $remaining = UserNote::where('note_id', $note->id)->exists(); |
| 126 | + if (!$remaining) { |
| 127 | + $note->delete(); |
| 128 | + } |
| 129 | + |
| 130 | + return response()->json(['status' => 'deleted']); |
| 131 | + } |
48 | 132 | } |
0 commit comments