Skip to content

Commit

Permalink
Finish HomeAPIController.php file and app/home api route
Browse files Browse the repository at this point in the history
  • Loading branch information
perisicnikola37 committed Sep 1, 2023
1 parent fdc3ee8 commit 5895d31
Show file tree
Hide file tree
Showing 7 changed files with 414 additions and 244 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ MEMCACHED_HOST=127.0.0.1
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
REDIS_CLIENT=predis

RAPIDAPI_HOST=
RAPIDAPI_KEY=
Expand Down
90 changes: 90 additions & 0 deletions app/Http/Controllers/Api/HomeAPIController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Resources\HomeResource;
use App\Models\User;
use App\Models\Lesson;
use App\Models\Category;
use App\Models\Comment;

class HomeAPIController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}

function home() {
$userCount = User::count();
$instructorCount = User::where('type_id', 2)->count();
$lessons = Lesson::count();
$hours = Lesson::count();
$featuredCategories = Category::take(5)->get();
$topComments = Comment::take(5)->orderBy('id', 'desc')->get();

$homeArray = [
'userCount' => $userCount,
'instructorCount' => $instructorCount,
'lessons' => $lessons,
'hours' => $hours,
'featuredCategories' => $featuredCategories,
'topComments' => $topComments
];

return new HomeResource(((object) $homeArray));
}

/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}

/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}

/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}

/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}

/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}

/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}
26 changes: 26 additions & 0 deletions app/Http/Resources/HomeResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Http\Resources;

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

class HomeResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'usersCount' => $this->userCount,
'instructorCount' => $this->instructorCount,
'lessons' => $this->lessons,
'hours' => $this->hours,
'featuredCategories' => $this->featuredCategories,
'topComments' => $this->topComments,
];
}
}
Loading

0 comments on commit 5895d31

Please sign in to comment.