Created
September 27, 2017 18:34
-
-
Save chriscalip/9244a0ef2784f597cab9e705bf2b24be to your computer and use it in GitHub Desktop.
laravel-api-json Post Controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Api; | |
use App\JsonApi\Posts; | |
use App\Post; | |
use CloudCreativity\LaravelJsonApi\Http\Controllers\EloquentController; | |
use CloudCreativity\LaravelJsonApi\Http\Controllers\CreatesResponses; | |
use CloudCreativity\JsonApi\Contracts\Object\ResourceObjectInterface; | |
class PostsController extends EloquentController | |
{ | |
use CreatesResponses; | |
/** | |
* PostsController constructor. | |
* | |
* @param Posts\Hydrator $hydrator | |
*/ | |
public function __construct(Posts\Hydrator $hydrator) | |
{ | |
parent::__construct(new Post(), $hydrator); | |
} | |
/** | |
* @param Hydrator $hydrator | |
* @param ResourceObjectInterface $resource | |
* @return mixed | |
*/ | |
public function create(Hydrator $hydrator, ResourceObjectInterface $resource) | |
{ | |
$record = new Site($resource->getId()); // client generated id. | |
$hydrator->hydrate($resource, $record); | |
$record->save(); | |
return $this->reply()->created($record); | |
} | |
/** | |
* @param Site $record | |
* @return mixed | |
*/ | |
public function read(Post $record) | |
{ | |
return $this->reply()->content($record); | |
} | |
/** | |
* @param Hydrator $hydrator | |
* @param ResourceObjectInterface $resource | |
* @param Site $record | |
* @return mixed | |
*/ | |
public function update(Hydrator $hydrator, ResourceObjectInterface $resource, Post $record) | |
{ | |
return [ | |
'resource' => $resource, | |
'record' => $record, | |
]; | |
$hydrator->hydrate($resource, $record); | |
$record->save(); | |
return $this->reply()->content($record); | |
} | |
/** | |
* @param Site $record | |
* @return mixed | |
*/ | |
public function delete(Post $record) | |
{ | |
$record->delete(); | |
return $this->reply()->noContent(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment