Skip to content

Commit

Permalink
Finish CategoryAppResource.php file and app/category api route
Browse files Browse the repository at this point in the history
  • Loading branch information
perisicnikola37 committed Sep 1, 2023
1 parent 5895d31 commit be66586
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app/Http/Controllers/Api/HomeAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Resources\HomeResource;
use App\Http\Resources\CategoryAppResource;
use App\Models\User;
use App\Models\Lesson;
use App\Models\Category;
use App\Models\Course;
use App\Models\Comment;

class HomeAPIController extends Controller
Expand Down Expand Up @@ -40,6 +42,21 @@ function home() {
return new HomeResource(((object) $homeArray));
}

function category($id) {
$courses = Course::where('category_id', $id)->get();
$minPrice = Course::min('price');
$maxPrice = Course::max('price');
$categories = Category::all();

$categoryArray = [
'courses' => $courses,
'minPrice' => $minPrice,
'maxPrice' => $maxPrice,
];

return new CategoryAppResource(((object) $categoryArray));
}

/**
* Show the form for creating a new resource.
*/
Expand Down
24 changes: 24 additions & 0 deletions app/Http/Resources/CategoryAppResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use App\Http\Resources\CourseResource;

class CategoryAppResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'minPrice' => $this->minPrice,
'maxPrice' => $this->maxPrice,
'courses' => CourseResource::collection($this->courses),
];
}
}
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

Route::apiResource('/users', UserAPIController::class);
Route::get('/app/home', [HomeAPIController::class, 'home']);
Route::get('/app/category/{id}', [HomeAPIController::class, 'category']);
Route::apiResource('/courses', CourseAPIController::class);
Route::apiResource('/categories', CategoryAPIController::class);
Route::get('/categories-search', [CategoryAPIController::class, 'search']);
Expand Down

0 comments on commit be66586

Please sign in to comment.