|
4 | 4 |
|
5 | 5 | use Illuminate\Http\Request;
|
6 | 6 |
|
| 7 | +/* |
| 8 | +ChatGPT: |
| 9 | +Here is a list of methods that the UserController class should have: |
| 10 | +
|
| 11 | +index(): This method should handle the GET /users endpoint, it should be responsible for fetching all the records from the "users" table and returning them to the client. |
| 12 | +
|
| 13 | +filter(): This method should handle the GET /users/filter?country=&from=&to= endpoint, it should be responsible for fetching all the records from the "users" table that match the provided country and date_of_birth range and returning them to the client. |
| 14 | +Copilot: All parameters should be optional, if the client doesn't provide a country or date_of_birth range, the method should return all the users from the database. |
| 15 | +
|
| 16 | +show(): This method should handle the GET /users/{id} endpoint, it should be responsible for fetching a specific user from the "users" table and returning it to the client. |
| 17 | +
|
| 18 | +update(): This method should handle the PATCH /users/{id} endpoint, it should be responsible for handling the form submission, updating a specific user in the database. |
| 19 | +
|
| 20 | +destroy(): This method should handle the DELETE /users/{id} endpoint, it should be responsible for deleting a specific user from the "users" table. |
| 21 | +*/ |
| 22 | + |
7 | 23 | class UserController extends Controller
|
8 | 24 | {
|
9 |
| - /** |
10 |
| - * Display a listing of the resource. |
11 |
| - * |
12 |
| - * @return \Illuminate\Http\Response |
13 |
| - */ |
| 25 | + // GET /users - This endpoint is used to retrieve a list of all users from the database. |
14 | 26 | public function index()
|
15 | 27 | {
|
16 |
| - // |
| 28 | + // Get all users from the database |
| 29 | + $users = \App\Models\User::all(); |
| 30 | + // Return the users as a JSON |
| 31 | + return response()->json($users, 200); |
17 | 32 | }
|
18 | 33 |
|
19 |
| - /** |
20 |
| - * Show the form for creating a new resource. |
21 |
| - * |
22 |
| - * @return \Illuminate\Http\Response |
23 |
| - */ |
24 |
| - public function create() |
| 34 | + // GET /users/filter?country=&from=&to= - This endpoint is used to retrieve a list of all users from the database. |
| 35 | + public function filter(Request $request) |
25 | 36 | {
|
26 |
| - // |
| 37 | + // Validate the request |
| 38 | + $request->validate([ |
| 39 | + 'country' => 'exists:countries,id', |
| 40 | + 'from' => 'date:Y-m-d|before_or_equal:to', |
| 41 | + 'to' => 'date:Y-m-d|after_or_equal:from', |
| 42 | + ]); |
| 43 | + // Get the country and date_of_birth range from the request |
| 44 | + $country = $request->input('country'); |
| 45 | + $from = $request->input('from'); |
| 46 | + $to = $request->input('to'); |
| 47 | + // Filter the users based on the provided country and date_of_birth range |
| 48 | + $users = \App\Models\User::when($country, function ($query, $country) { |
| 49 | + return $query->where('country_id', $country); |
| 50 | + })->when($from, function ($query, $from) { |
| 51 | + return $query->where('date_of_birth', '>=', $from); |
| 52 | + })->when($to, function ($query, $to) { |
| 53 | + return $query->where('date_of_birth', '<=', $to); |
| 54 | + })->get(); |
| 55 | + // Return the users as a JSON |
| 56 | + return response()->json($users, 200); |
27 | 57 | }
|
28 | 58 |
|
29 |
| - /** |
30 |
| - * Store a newly created resource in storage. |
31 |
| - * |
32 |
| - * @param \Illuminate\Http\Request $request |
33 |
| - * @return \Illuminate\Http\Response |
34 |
| - */ |
35 |
| - public function store(Request $request) |
36 |
| - { |
37 |
| - // |
38 |
| - } |
39 |
| - |
40 |
| - /** |
41 |
| - * Display the specified resource. |
42 |
| - * |
43 |
| - * @param int $id |
44 |
| - * @return \Illuminate\Http\Response |
45 |
| - */ |
| 59 | + // GET /users/{id} - This endpoint is used to retrieve a specific user from the database. The {id} path parameter should be replaced with the id of the user you want to retrieve. |
46 | 60 | public function show($id)
|
47 | 61 | {
|
48 |
| - // |
| 62 | + // Get the user from the database |
| 63 | + $user = \App\Models\User::find($id); |
| 64 | + // If the user doesn't exist, return a 404 response |
| 65 | + if (!$user) { |
| 66 | + return response()->json([ |
| 67 | + 'message' => 'User not found', |
| 68 | + ], 404); |
| 69 | + } |
| 70 | + // Return the user as a JSON |
| 71 | + return response()->json($user, 200); |
49 | 72 | }
|
50 | 73 |
|
51 |
| - /** |
52 |
| - * Show the form for editing the specified resource. |
53 |
| - * |
54 |
| - * @param int $id |
55 |
| - * @return \Illuminate\Http\Response |
56 |
| - */ |
57 |
| - public function edit($id) |
| 74 | + // PATCH /users/{id} - This endpoint is used to update a specific user in the database via a country. The {id} path parameter should be replaced with the id of the user you want to update and the client should send the updated information in the request body. |
| 75 | + public function update(Request $request, $id) |
58 | 76 | {
|
59 |
| - // |
| 77 | + // Validate the request |
| 78 | + $request->validate([ |
| 79 | + 'first_name' => 'required|string', |
| 80 | + 'last_name' => 'required|string', |
| 81 | + 'date_of_birth' => 'required|date', |
| 82 | + 'country_id' => 'required|exists:countries,id', |
| 83 | + ]); |
| 84 | + // Get the user from the database |
| 85 | + $user = \App\Models\User::find($id); |
| 86 | + // If the user doesn't exist, return a 404 response |
| 87 | + if (!$user) { |
| 88 | + return response()->json([ |
| 89 | + 'message' => 'User not found', |
| 90 | + ], 404); |
| 91 | + } |
| 92 | + // Update the user |
| 93 | + $user->first_name = $request->input('first_name'); |
| 94 | + $user->last_name = $request->input('last_name'); |
| 95 | + $user->date_of_birth = $request->input('date_of_birth'); |
| 96 | + $user->country_id = $request->input('country_id'); |
| 97 | + $user->save(); |
| 98 | + // Return the user as a JSON |
| 99 | + return response()->json($user, 200); |
60 | 100 | }
|
61 | 101 |
|
62 |
| - /** |
63 |
| - * Update the specified resource in storage. |
64 |
| - * |
65 |
| - * @param \Illuminate\Http\Request $request |
66 |
| - * @param int $id |
67 |
| - * @return \Illuminate\Http\Response |
68 |
| - */ |
69 |
| - public function update(Request $request, $id) |
| 102 | + // DELETE /users/{id} - This endpoint is used to delete a specific user from the database. The {id} path parameter should be replaced with the id of the user you want to delete. |
| 103 | + public function destroy($id) |
70 | 104 | {
|
71 |
| - // |
| 105 | + // Get the user from the database |
| 106 | + $user = \App\Models\User::find($id); |
| 107 | + // If the user doesn't exist, return a 404 response |
| 108 | + if (!$user) { |
| 109 | + return response()->json([ |
| 110 | + 'message' => 'User not found', |
| 111 | + ], 404); |
| 112 | + } |
| 113 | + // Delete the user |
| 114 | + $user->delete(); |
| 115 | + // Return a 204 response |
| 116 | + return response()->json(null, 204); |
72 | 117 | }
|
73 | 118 |
|
74 |
| - /** |
75 |
| - * Remove the specified resource from storage. |
76 |
| - * |
77 |
| - * @param int $id |
78 |
| - * @return \Illuminate\Http\Response |
79 |
| - */ |
80 |
| - public function destroy($id) |
| 119 | + // POST /users - This endpoint is used to create a new user in the database. The client should send the user information in the request body. |
| 120 | + public function store(Request $request) |
81 | 121 | {
|
82 |
| - // |
| 122 | + // Validate the request |
| 123 | + $request->validate([ |
| 124 | + 'first_name' => 'required|string', |
| 125 | + 'last_name' => 'required|string', |
| 126 | + 'date_of_birth' => 'required|date', |
| 127 | + 'country_id' => 'required|exists:countries,id', |
| 128 | + ]); |
| 129 | + // Create the user |
| 130 | + $user = \App\Models\User::create([ |
| 131 | + 'first_name' => $request->input('first_name'), |
| 132 | + 'last_name' => $request->input('last_name'), |
| 133 | + 'date_of_birth' => $request->input('date_of_birth'), |
| 134 | + 'country_id' => $request->input('country_id'), |
| 135 | + ]); |
| 136 | + $user->save(); |
| 137 | + // Return the user as a JSON |
| 138 | + return response()->json($user, 201); |
83 | 139 | }
|
84 | 140 | }
|
0 commit comments