Skip to content

Commit

Permalink
Merge pull request #7 from skni-kod/feat/courses-filtering
Browse files Browse the repository at this point in the history
Feat/courses filtering
  • Loading branch information
marioooo0o authored Dec 29, 2024
2 parents e962be0 + d22b6bd commit 7afb7ff
Show file tree
Hide file tree
Showing 8 changed files with 777 additions and 386 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

abstract class Controller
{
//
public const PER_PAGE = 10;
}
26 changes: 21 additions & 5 deletions app/Http/Controllers/CourseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,25 @@
use App\Http\Resources\CourseResource;
use App\Models\Course;
use App\Services\CourseService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;

class CourseController extends Controller
{
public function __construct(protected CourseService $courseService) {}

public function index(): CourseCollection
{
return new CourseCollection(
Course::query()
->with(['school', 'category'])
->paginate()
);
return new CourseCollection(QueryBuilder::for(Course::class)
->allowedFilters(AllowedFilter::partial('school.name'),
AllowedFilter::exact('school.city'),
AllowedFilter::exact('category.name')
)
->allowedSorts(['price', 'start_date'])
->with(['school', 'category'])
->paginate(self::PER_PAGE));
}

public function store(StoreCourseRequest $request): CourseResource
Expand All @@ -44,4 +50,14 @@ public function destroy(Course $course): Response

return response()->noContent();
}

public function locations(): JsonResponse
{
$cities = Course::query()
->join('schools', 'courses.school_id', '=', 'schools.id')
->distinct()
->pluck('schools.city');

return response()->json($cities);
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"laravel/framework": "^11.9",
"laravel/sanctum": "^4.0",
"laravel/socialite": "^5.16",
"laravel/tinker": "^2.9"
"laravel/tinker": "^2.9",
"spatie/laravel-query-builder": "^6.3"
},
"require-dev": {
"fakerphp/faker": "^1.23",
Expand Down
1,125 changes: 749 additions & 376 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion database/factories/CourseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function definition(): array
return [
'start_date' => fake()->dateTimeThisYear(),
'school_id' => School::factory(),
'category_id' => Category::factory(),
'category_id' => Category::inRandomOrder()->first()->id,
'price' => fake()->randomNumber(4),
'currency' => 'PLN',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function up(): void
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name', 32);
$table->string('name', 32)->unique();
});
}

Expand Down
2 changes: 1 addition & 1 deletion database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class DatabaseSeeder extends Seeder
public function run(): void
{
$this->call(CategorySeeder::class);
Course::factory(1)->create();
Course::factory(5)->create();
// User::factory()->create([
// 'name' => 'Test User',
// 'email' => 'test@example.com',
Expand Down
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
return $request->user();
})->middleware('auth:sanctum');

Route::get('/course-locations', [CourseController::class, 'locations']);
Route::apiResource('/courses', CourseController::class)->except('update');

Route::post('login', [AuthController::class, 'login']);
Expand Down

0 comments on commit 7afb7ff

Please sign in to comment.