mirror of https://github.com/4xmen/xshop.git
added posts and groups api
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)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
@ -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(),
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue