Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Crud example #26

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions app/Api/Controllers/PostController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace App\Api\Controllers;

use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use App\Contracts\Repository\PostRepositoryContract;
use Illuminate\Contracts\Routing\ResponseFactory as Response;

/**
* Usually I would make a service for each of the actions defined in this controller
* to keep the controller thin, and I find single action services to be much more testable
* (for an example of a single action service check out SignUpService.php)
*/
class PostController
{
private $response;
private $repository;

public function __construct(
Response $response,
PostRepositoryContract $repository
)
{
$this->response = $response;
$this->repository = $repository;
}

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
/**
* We set skip presenter by default to work directly with the
* repository instead of the transformed result in the code, then
* when it comes to displaying an API result to the user, we apply
* the presenter. More on presenters and transformers here:
* https://github.com/andersao/l5-repository#presenters
*/
try {
$posts = $this->repository->skipPresenter(false)->all();

return $this->response->success($posts);
} catch (ValidationException $e) {
return $this->response->validateError($e->errors());
}
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$postData = $request->only(['title', 'body']);

$createdPost = $this->repository
->skipPresenter(false)
->create($postData);

return $this->response->success($createdPost);
}

/**
* Display the specified resource.
*
* @param string $slug
* @return \Illuminate\Http\Response
*/
public function show($slug)
{
$id = $this->repository->decodeSlug($slug);
$post = $this->repository->skipPresenter(false)->find($id);

return $this->response->success($post);
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param string $slug
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $slug)
{
try {
$data = $request->only(['title', 'body']);
$id = $this->repository->decodeSlug($slug);

$updatedPost = $this->repository->skipPresenter(false)->update($data, $id);

return $this->response->success($updatedPost);
} catch (ValidationException $e) {
return $this->response->validateError($e->errors());
}
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($slug)
{
$id = $this->repository->decodeSlug($slug);

$deletedPost = $this->repository->skipPresenter(false)->delete($id);

return $this->response->success(['message' => 'post deleted']);
}
}
9 changes: 9 additions & 0 deletions app/Contracts/Repository/PostRepositoryContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Contracts\Repository;

use Prettus\Repository\Contracts\RepositoryInterface;

interface PostRepositoryContract extends RepositoryInterface
{
}
13 changes: 13 additions & 0 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Models;

use Balping\HashSlug\HasHashSlug;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
use HasHashSlug;

protected $fillable = ['title', 'body'];
}
24 changes: 24 additions & 0 deletions app/Presenters/PostPresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Presenters;

use App\Transformers\PostTransformer;
use Prettus\Repository\Presenter\FractalPresenter;

/**
* Class PostPresenter.
*
* @package namespace App\Presenters;
*/
class PostPresenter extends FractalPresenter
{
/**
* Transformer
*
* @return \League\Fractal\TransformerAbstract
*/
public function getTransformer()
{
return new PostTransformer();
}
}
5 changes: 5 additions & 0 deletions app/Providers/RepositoryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@ public function register()
'App\Contracts\Repository\UserRepositoryContract',
'App\Repositories\Eloquent\UserRepository'
);

$this->app->bind(
'App\Contracts\Repository\PostRepositoryContract',
'App\Repositories\Eloquent\PostRepository'
);
}
}
29 changes: 29 additions & 0 deletions app/Repositories/Eloquent/PostRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Repositories\Eloquent;

use App\Models\Post;
use App\Presenters\PostPresenter;
use App\Validators\PostValidator;
use App\Repositories\Eloquent\BaseRepoWithSlugs;
use App\Contracts\Repository\PostRepositoryContract;

class PostRepository extends BaseRepoWithSlugs implements PostRepositoryContract
{
protected $skipPresenter = true;

public function model()
{
return Post::class;
}

public function presenter()
{
return PostPresenter::class;
}

public function validator()
{
return PostValidator::class;
}
}
29 changes: 29 additions & 0 deletions app/Transformers/PostTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace App\Transformers;

use App\Models\Post;
use League\Fractal\TransformerAbstract;

/**
* Class PostTransformer.
*
* @package namespace App\Transformers;
*/
class PostTransformer extends TransformerAbstract
{
/**
* Transform the Post entity.
*
* @param \App\Models\Post $model
*
* @return array
*/
public function transform(Post $model)
{
return [
'slug' => $model->slug(),
'title' => $model->title,
'body' => $model->body
];
}
}
12 changes: 12 additions & 0 deletions app/Validators/PostValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Validators;

use Prettus\Validator\LaravelValidator;

class PostValidator extends LaravelValidator {
protected $rules = [
'title' => 'required',
'body' => 'required',
];
}
33 changes: 33 additions & 0 deletions database/migrations/2019_08_18_093140_create_posts_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->string('body');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
Loading