added posts and groups api

master
A1Gard 2 weeks ago
parent ca310273b8
commit 9bc2cde901

@ -0,0 +1,31 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Resources\GroupCollection;
use App\Http\Resources\GroupsCollection;
use App\Models\Group;
use Illuminate\Http\Request;
class GroupController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
return GroupsCollection::collection(Group::orderBy('sort', 'asc')->get());
}
/**
* Display the specified resource.
*/
public function show(Group $group)
{
//
return GroupCollection::make($group);
}
}

@ -0,0 +1,66 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
/**
* 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(Post $post)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Post $post)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Post $post)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Post $post)
{
//
}
}

@ -39,7 +39,9 @@ class CategoryResource extends JsonResource
'bg_original' => $this->bgOriginalUrl(),
'svg' => $this->svgUrl(),
'icons' => $this->icon,
'products' => $this->when($request->input('loadProduct', true), ProductResource::collection($this->products()->paginate($request->input('per_page', 20)))->additional(['request' => $request['loadCategory']])),
'products' => $this->when($request->input('loadProduct', true),
ProductResource::collection($this->products()->paginate($request->input('per_page', 20)))
->additional(['request' => $request['loadCategory']])),
'products_pages_count' => ceil($this->products()->count() / $request->input('per_page', 20) ),
];
}

@ -0,0 +1,44 @@
<?php
namespace App\Http\Resources;
use App\Models\Group;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;
class GroupCollection extends JsonResource
{
/**
* Transform the resource collection into an array.
*
* @return array<int|string, mixed>
*/
public function toArray(Request $request): array
{
/**
* @var $this Group
*/
if (!$request['loadGroup'])
$request->merge([
'loadGroup' => false
]);
return [
'id' => $this->id,
'name' => $this->name,
'slug' => $this->slug,
'subtitle' => $this->subtitle,
'description' => $this->description,
'sort' => $this->sort,
'image' => $this->imgUrl(),
'image_original' => $this->imgOriginalUrl(),
'bg' => $this->bgUrl(),
'bg_original' => $this->bgOriginalUrl(),
'posts' => $this->when($request->input('loadPost', true),
PostResource::collection($this->posts()->paginate($request->input('per_page', 20)))
->additional(['request' => $request['loadGroup']])),
'posts_pages_count' => ceil($this->posts()->count() / $request->input('per_page', 20) ),
];
}
}

@ -0,0 +1,35 @@
<?php
namespace App\Http\Resources;
use App\Models\Group;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;
class GroupsCollection extends JsonResource
{
/**
* Transform the resource collection into an array.
*
* @return array<int|string, mixed>
*/
public function toArray(Request $request): array
{
/**
* @var $this Group
*/
return[
'id' => $this->id,
'name' => $this->name,
'slug' => $this->slug,
'subtitle' => $this->subtitle,
'description' => $this->description,
'sort' => $this->sort,
'image' => $this->imgUrl(),
'image_original' => $this->imgOriginalUrl(),
'bg' => $this->bgUrl(),
'bg_original' => $this->bgOriginalUrl(),
];
}
}

@ -23,7 +23,9 @@ class PostResource extends JsonResource
'slug' => $this->slug,
'subtitle' => $this->subtitle,
'body' => $this->body,
'group' => $this->load('groups'),
'group' => GroupsCollection::make($this->mainGroup),
'groups'=> GroupsCollection::collection($this->groups),
// 'group' => $this->load('groups'),
'author' => $this->load('author'),
'view' => $this->view,
'is_pinned' => $this->is_pinned,
@ -31,8 +33,9 @@ class PostResource extends JsonResource
'like' => $this->like,
'dislike' => $this->dislike,
'icon' => $this->icon,
'created_at' => $this->created_at,
'image' => $this->imgUrl(),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}

@ -36,7 +36,9 @@ Route::prefix('v1')->name('v1.')->group(
Route::get('states', [\App\Http\Controllers\Api\StateController::class, 'index'])->name('state.index');
Route::get('state/{state}', [\App\Http\Controllers\Api\StateController::class, 'show'])->name('state.show');
Route::get('categories', [\App\Http\Controllers\Api\CategoryController::class, 'index'])->name('category.index');
Route::get('groups', [\App\Http\Controllers\Api\GroupController::class, 'index'])->name('group.index');
Route::get('category/{category}', [\App\Http\Controllers\Api\CategoryController::class, 'show'])->name('category.show');
Route::get('group/{group}', [\App\Http\Controllers\Api\GroupController::class, 'show'])->name('group.show');
Route::get('products', [\App\Http\Controllers\Api\ProductController::class, 'index'])->name('product.index');
Route::get('category/props/{category}', [\App\Http\Controllers\Api\CategoryController::class, 'props'])->name('category.prop');
Route::post('morph/search', [\App\Http\Controllers\Api\MorphController::class, 'search'])->name('morph.search');

Loading…
Cancel
Save