From 735d1b663d0be2ce9894c367bed5f58d2bb73fd3 Mon Sep 17 00:00:00 2001 From: agongon Date: Thu, 7 Apr 2022 11:50:27 -0300 Subject: [PATCH 1/4] adding the endpoint to filter by month and week --- app/Http/Controllers/PetitionController.php | 20 ++++++++++++++++++++ routes/api.php | 2 ++ 2 files changed, 22 insertions(+) diff --git a/app/Http/Controllers/PetitionController.php b/app/Http/Controllers/PetitionController.php index 0596538..91ae7d1 100644 --- a/app/Http/Controllers/PetitionController.php +++ b/app/Http/Controllers/PetitionController.php @@ -8,6 +8,7 @@ use App\Mail\PetitionCreated; use App\Mail\MailerAuth; use Illuminate\Support\Facades\Mail; +use Carbon\Carbon; //Validation use App\Http\Requests\PetitionStoreFormRequest; @@ -94,6 +95,25 @@ public function update(Request $request, $id) return $petition; } + /** + * Filter the specified resource in storage. + * + * @param \Illuminate\Http\Request $request + * @param \App\Models\Petition $petition + * @return \Illuminate\Http\Response + */ + public function filterByMonth() + { + $byMonth = Petition::whereMonth('created_at', Carbon::now()->month)->get(); + return $byMonth; + } + + public function filterByWeek() + { + $byweek = Petition::whereBetween('created_at', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])->get(); + return $byweek; + } + /** * Remove the specified resource from storage. * diff --git a/routes/api.php b/routes/api.php index 3826dee..cd6ad48 100644 --- a/routes/api.php +++ b/routes/api.php @@ -34,6 +34,8 @@ Route::delete('/donations/{id}', [DonationController::class, 'destroy']); //petitions routes Route::get('/petitions', [PetitionController::class, 'index']); + Route::get('/petitions/filter-month', [PetitionController::class, 'filterByMonth']); + Route::get('/petitions/filter-week', [PetitionController::class, 'filterByWeek']); Route::get('/petition/{id}', [PetitionController::class, 'show']); Route::post('/petitions', [PetitionController::class, 'store']); Route::delete('/petitions/{id}', [PetitionController::class, 'destroy']); From 94363a9c907eb8acc6d137b9559bb684cf72c439 Mon Sep 17 00:00:00 2001 From: agongon Date: Thu, 7 Apr 2022 22:38:51 -0300 Subject: [PATCH 2/4] fixing as comment said --- app/Http/Controllers/PetitionController.php | 17 ++++++++++++----- routes/api.php | 3 +-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/PetitionController.php b/app/Http/Controllers/PetitionController.php index 91ae7d1..406f3ec 100644 --- a/app/Http/Controllers/PetitionController.php +++ b/app/Http/Controllers/PetitionController.php @@ -102,16 +102,23 @@ public function update(Request $request, $id) * @param \App\Models\Petition $petition * @return \Illuminate\Http\Response */ - public function filterByMonth() + public function filter(Request $request) { - $byMonth = Petition::whereMonth('created_at', Carbon::now()->month)->get(); - return $byMonth; + $by = $request->query("by"); + if ($by === "week") { + $byweek = Petition::whereBetween('created_at', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])->get(); + return $byweek; + } else if ($by === "month") { + $byMonth = Petition::whereMonth('created_at', Carbon::now()->month)->get(); + return $byMonth; + }else{ + return "Something went wrong filtering"; + } } public function filterByWeek() { - $byweek = Petition::whereBetween('created_at', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])->get(); - return $byweek; + } /** diff --git a/routes/api.php b/routes/api.php index cd6ad48..b68b209 100644 --- a/routes/api.php +++ b/routes/api.php @@ -34,8 +34,7 @@ Route::delete('/donations/{id}', [DonationController::class, 'destroy']); //petitions routes Route::get('/petitions', [PetitionController::class, 'index']); - Route::get('/petitions/filter-month', [PetitionController::class, 'filterByMonth']); - Route::get('/petitions/filter-week', [PetitionController::class, 'filterByWeek']); + Route::get('/petitions/filter', [PetitionController::class, 'filter']); Route::get('/petition/{id}', [PetitionController::class, 'show']); Route::post('/petitions', [PetitionController::class, 'store']); Route::delete('/petitions/{id}', [PetitionController::class, 'destroy']); From b8b4aed9879ad909d47bf23f9b68fd168a8621e8 Mon Sep 17 00:00:00 2001 From: agongon Date: Fri, 8 Apr 2022 15:54:27 -0300 Subject: [PATCH 3/4] fix from comment --- app/Http/Controllers/PetitionController.php | 40 +++++++-------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/app/Http/Controllers/PetitionController.php b/app/Http/Controllers/PetitionController.php index 406f3ec..dfc9cd2 100644 --- a/app/Http/Controllers/PetitionController.php +++ b/app/Http/Controllers/PetitionController.php @@ -19,8 +19,20 @@ class PetitionController extends Controller * * @return \Illuminate\Http\Response */ - public function index() + public function index(Request $request = null) { + if (!is_null($request)) { + $time_period = $request->query("time_period"); + if ($time_period === "week") { + $byweek = Petition::whereBetween('created_at', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])->get(); + return $byweek; + } else if ($time_period === "month") { + $byMonth = Petition::whereMonth('created_at', Carbon::now()->month)->get(); + return $byMonth; + }else{ + return back()->withErrors('We could not manage your filter'); + } + } return Petition::where('state', 'PUBLISHED')->paginate(10); } @@ -93,32 +105,6 @@ public function update(Request $request, $id) $petition = Petition::find($id); $petition->update($request->all()); return $petition; - } - - /** - * Filter the specified resource in storage. - * - * @param \Illuminate\Http\Request $request - * @param \App\Models\Petition $petition - * @return \Illuminate\Http\Response - */ - public function filter(Request $request) - { - $by = $request->query("by"); - if ($by === "week") { - $byweek = Petition::whereBetween('created_at', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])->get(); - return $byweek; - } else if ($by === "month") { - $byMonth = Petition::whereMonth('created_at', Carbon::now()->month)->get(); - return $byMonth; - }else{ - return "Something went wrong filtering"; - } - } - - public function filterByWeek() - { - } /** From f460c488f1cb50a0d0dd0569c164f19ea5834240 Mon Sep 17 00:00:00 2001 From: agongon Date: Fri, 8 Apr 2022 16:28:52 -0300 Subject: [PATCH 4/4] fix from comment --- app/Http/Controllers/PetitionController.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/PetitionController.php b/app/Http/Controllers/PetitionController.php index dfc9cd2..5685c35 100644 --- a/app/Http/Controllers/PetitionController.php +++ b/app/Http/Controllers/PetitionController.php @@ -24,10 +24,14 @@ public function index(Request $request = null) if (!is_null($request)) { $time_period = $request->query("time_period"); if ($time_period === "week") { - $byweek = Petition::whereBetween('created_at', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])->get(); + $byweek = Petition::whereBetween('created_at', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()]) + ->get() + ->paginate(10); return $byweek; } else if ($time_period === "month") { - $byMonth = Petition::whereMonth('created_at', Carbon::now()->month)->get(); + $byMonth = Petition::whereMonth('created_at', Carbon::now()->month) + ->get() + ->paginate(10); return $byMonth; }else{ return back()->withErrors('We could not manage your filter');