Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the endpoint to filter by month and week #31

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion app/Http/Controllers/PetitionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Mail\PetitionManagmentCreated;
use App\Mail\MailerAuth;
use Illuminate\Support\Facades\Mail;
use Carbon\Carbon;
//Validation
use App\Http\Requests\PetitionStoreFormRequest;

Expand All @@ -19,8 +20,24 @@ 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()
->paginate(10);
return $byweek;
} else if ($time_period === "month") {
$byMonth = Petition::whereMonth('created_at', Carbon::now()->month)
->get()
->paginate(10);
return $byMonth;
}else{
return back()->withErrors('We could not manage your filter');
}
}
return Petition::where('state', 'PUBLISHED')->paginate(10);
}

Expand Down
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
Route::delete('/donations/{id}', [DonationController::class, 'destroy']);
//petitions routes
Route::get('/petitions', [PetitionController::class, 'index']);
Route::get('/petitions/filter', [PetitionController::class, 'filter']);
Comment on lines 36 to +37
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This filter stuff should be done at the endpoint /petitions and the params should be time_period

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Route::get('/petition/{id}', [PetitionController::class, 'show']);
Route::post('/petitions', [PetitionController::class, 'store']);
Route::delete('/petitions/{id}', [PetitionController::class, 'destroy']);
Expand Down