Skip to content
Merged
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
15 changes: 14 additions & 1 deletion app/Http/Controllers/BaseController.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Response;


class BaseController extends Controller
{
protected function __construct()
{
}

/**
* レスポンスステータス追加
*
* @param array $result
* @return mixed
*/
protected function getSuccessResponse(array $result = [])
{
return response()->json($result, Response::HTTP_OK);
}
}
16 changes: 14 additions & 2 deletions app/Http/Controllers/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@

namespace App\Http\Controllers;

use App\Services\Comment as CommentService;
use Illuminate\Http\Request;

class CommentController extends BaseController
{
private $comment_repository;
private $comment_service;

public function __construct()
public function __construct(CommentService $comment_service)
{
$this->comment_service = $comment_service;
}

/**
* 入力情報からCommentを作成する
*
* @param Request $request
*/
public function create(Request $request)
{
return $this->comment_service->createComment($request->data);
}
}
12 changes: 11 additions & 1 deletion app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function index()
return $this->post_service->getAllPosts();
}


/**
* 特定IDのPostを取得
*
Expand All @@ -36,4 +35,15 @@ public function show(int $id)
{
return $this->post_service->getPostById($id);
}

/**
* Postを作成する
*
* @param Request $request
*/
public function create(Request $request)
{
$this->post_service->createPost($request->data);
return $this->getSuccessResponse();
}
}
5 changes: 5 additions & 0 deletions app/Models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

class Comment extends Model
{
public $fillable =[
'post_id',
'body'
];

public function post()
{
return $this->belongsTo(Post::class);
Expand Down
6 changes: 6 additions & 0 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

class Post extends Model
{
public $fillable = [
'title',
'body',
'show_flag'
];

public function comments()
{
return $this->hasMany(Comment::class);
Expand Down
18 changes: 18 additions & 0 deletions app/Repositories/Comment.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
<?php
namespace App\Repositories;

use App\Models\Comment as CommentModel;

class Comment extends BaseRepository
{
private $comment_model;

public function __construct(CommentModel $comment_model)
{
$this->comment_model = $comment_model;
}

/**
* 入力情報からCommentを作成する
*
* @param $input
*/
public function createComment($input)
{
$this->comment_model->create($input);
}
}
26 changes: 25 additions & 1 deletion app/Repositories/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,37 @@ public function __construct(PostModel $post_model)
$this->post_model = $post_model;
}

/**
* フラグがonのPostをすべて取得する
*
* @return mixed
*/
public function getAllPosts()
{
return $this->post_model->all();
return $this->post_model
->where('show_flag', FLAG_ON)
->get();
}

/**
* 指定IDのPostを取得する
*
* @param int $id
* @return mixed
*/
public function getPostById(int $id)
{
return $this->post_model->findOrFail($id);
}

/**
* 入力からPostを作成する
*
* @param $input
* @return mixed
*/
public function createPost($input)
{
return $this->post_model->create($input);
}
}
18 changes: 18 additions & 0 deletions app/Sevices/Comment.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
<?php
namespace App\Services;

use App\Repositories\Comment as CommentRepository;

class Comment extends BaseService
{
private $comment_repository;

public function __construct(CommentRepository $comment_repository)
{
$this->comment_repository = $comment_repository;
}

/**
* 入力情報からCommentを作成する
*
* @param $input
*/
public function createComment($input)
{
$this->comment_repository->createComment($input);
}
}
10 changes: 10 additions & 0 deletions app/Sevices/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ public function getPostById(int $id)
{
return new PostResource($this->post_repository->getPostById($id));
}

/**
* Postを作成
*
* @param $input
*/
public function createPost($input)
{
return $this->post_repository->createPost($input);
}
}
35 changes: 35 additions & 0 deletions bootstrap/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
|--------------------------------------------------------------------------
| Register The Composer Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/
require __DIR__.'/../vendor/autoload.php';

$filelist = glob(__DIR__ . '/constants/*.php');
foreach ($filelist as $file) {
if (is_file($file)) {
require_once $file;
}
}
/*
|--------------------------------------------------------------------------
| Include The Compiled Class File
|--------------------------------------------------------------------------
|
| To dramatically increase your application's performance, you may use a
| compiled class file which contains all of the classes commonly used
| by a request. The Artisan "optimize" is used to create this file.
|
*/
$compiledPath = __DIR__.'/cache/compiled.php';
if (file_exists($compiledPath)) {
require $compiledPath;
}
3 changes: 3 additions & 0 deletions bootstrap/constants/common.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
const FLAG_ON = 1;
const FLAG_OFF = 0;
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"filp/whoops": "~2.0",
"fzaninotto/faker": "~1.4",
"mockery/mockery": "~1.0",
"phpunit/phpunit": "~6.0",
"phpunit/phpunit": "7.*",
"symfony/thanks": "^1.0"
},
"autoload": {
Expand Down
Loading