From 9a29453fd73ffca721515c51c742bf41fb2ef2b3 Mon Sep 17 00:00:00 2001 From: aihara005 Date: Sun, 28 Oct 2018 18:26:19 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=E3=80=90WIP=E3=80=91Category=E3=81=AEQuery?= =?UTF-8?q?Param=E6=A4=9C=E7=B4=A2=E8=BF=BD=E5=8A=A0(Nuxt=E5=81=B4?= =?UTF-8?q?=E6=9C=AA=E5=AF=BE=E5=BF=9C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/CategoryController.php | 25 ++++++++ app/Http/Controllers/PostController.php | 6 +- app/Http/Resources/Category.php | 5 +- app/Repositories/Category.php | 25 ++++++++ app/Repositories/Post.php | 27 +++++++- app/Sevices/Category.php | 25 ++++++++ app/Sevices/Post.php | 11 ++-- nuxtjs/pages/index.vue | 16 ++++- nuxtjs/pages/posts/_id.vue | 2 +- nuxtjs/pages/posts/index.vue | 69 +++++++++++++++++++++ routes/web.php | 5 +- 11 files changed, 202 insertions(+), 14 deletions(-) create mode 100644 app/Http/Controllers/CategoryController.php create mode 100644 app/Repositories/Category.php create mode 100644 app/Sevices/Category.php create mode 100644 nuxtjs/pages/posts/index.vue diff --git a/app/Http/Controllers/CategoryController.php b/app/Http/Controllers/CategoryController.php new file mode 100644 index 0000000..b7dd5bb --- /dev/null +++ b/app/Http/Controllers/CategoryController.php @@ -0,0 +1,25 @@ +category_service = $category_service; + } + + /** + * Category一覧を表示 + * + * @return mixed + */ + public function index() + { + return $this->category_service->getAll(); + } +} diff --git a/app/Http/Controllers/PostController.php b/app/Http/Controllers/PostController.php index 81f698b..88558c3 100644 --- a/app/Http/Controllers/PostController.php +++ b/app/Http/Controllers/PostController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Services\Post as PostService; +use Illuminate\Auth\Access\Response; use Illuminate\Http\Request; class PostController extends BaseController @@ -20,9 +21,9 @@ public function __construct(PostService $post_service) * * @return mixed */ - public function index() + public function index(Request $request) { - return $this->post_service->getAllPosts(); + return $this->post_service->getAllPosts($request->query()); } /** @@ -40,6 +41,7 @@ public function show(int $id) * Postを作成する * * @param Request $request + * @return mixed */ public function create(Request $request) { diff --git a/app/Http/Resources/Category.php b/app/Http/Resources/Category.php index 445a6f4..209b0fc 100644 --- a/app/Http/Resources/Category.php +++ b/app/Http/Resources/Category.php @@ -13,6 +13,9 @@ class Category extends Resource */ public function toArray($request) { - return $this->name; + return [ + 'id' => $this->id, + 'name' => $this->name, + ]; } } diff --git a/app/Repositories/Category.php b/app/Repositories/Category.php new file mode 100644 index 0000000..9bb9de0 --- /dev/null +++ b/app/Repositories/Category.php @@ -0,0 +1,25 @@ +category_model = $category_model; + } + + /** + * Categoryをすべて取得する + * + * @return mixed + */ + public function getAll() + { + return $this->category_model + ->all(); + } +} diff --git a/app/Repositories/Post.php b/app/Repositories/Post.php index 52de841..200c747 100644 --- a/app/Repositories/Post.php +++ b/app/Repositories/Post.php @@ -17,20 +17,41 @@ public function __construct(PostModel $post_model) * * @return mixed */ - public function getAllPosts() + public function getAll() { return $this->post_model ->where('show_flag', FLAG_ON) ->get(); } + /** + * Queryが一致し かつ フラグがonのPostをすべて取得する + * + * @return mixed + */ + public function getAllByQuery(array $query) + { + $base_query = $this->post_model->where('show_flag', FLAG_ON); + + if (array_has($query, 'category')) { + $base_query->join('category_post', 'category_post.post_id', '=', 'posts.id') + ->join('categories', 'categories.id', '=', 'category_post.category_id') + ->where('categories.id', $query['category']); + } + +// foreach($query as $key => $value) { +// $base_query->where($key, $value); +// } + return $base_query->get(); + } + /** * 指定IDのPostを取得する * * @param int $id * @return mixed */ - public function getPostById(int $id) + public function getById(int $id) { return $this->post_model->findOrFail($id); } @@ -41,7 +62,7 @@ public function getPostById(int $id) * @param $input * @return mixed */ - public function createPost($input) + public function create($input) { return $this->post_model->create($input); } diff --git a/app/Sevices/Category.php b/app/Sevices/Category.php new file mode 100644 index 0000000..4d3777d --- /dev/null +++ b/app/Sevices/Category.php @@ -0,0 +1,25 @@ +category_repository = $category_repository; + } + + /** + * 全てのCategoryを取得し成型してJsonで返す + * + * @return mixed + */ + public function getAll() + { + return CategoryResource::collection($this->category_repository->getAll()); + } +} diff --git a/app/Sevices/Post.php b/app/Sevices/Post.php index 8ccbc47..e120bba 100644 --- a/app/Sevices/Post.php +++ b/app/Sevices/Post.php @@ -18,9 +18,12 @@ public function __construct(PostRepository $post_repository) * * @return mixed */ - public function getAllPosts() + public function getAllPosts(?array $query) { - return PostResource::collection($this->post_repository->getAllPosts()); + if (is_null($query)) { + return PostResource::collection($this->post_repository->getAll()); + } + return PostResource::collection($this->post_repository->getAllByQuery($query)); } /** @@ -31,7 +34,7 @@ public function getAllPosts() */ public function getPostById(int $id) { - return new PostResource($this->post_repository->getPostById($id)); + return new PostResource($this->post_repository->getById($id)); } /** @@ -41,6 +44,6 @@ public function getPostById(int $id) */ public function createPost($input) { - return $this->post_repository->createPost($input); + return $this->post_repository->create($input); } } diff --git a/nuxtjs/pages/index.vue b/nuxtjs/pages/index.vue index be07a6d..9da6804 100644 --- a/nuxtjs/pages/index.vue +++ b/nuxtjs/pages/index.vue @@ -8,14 +8,26 @@ + +
+

Category

+ +
diff --git a/nuxtjs/pages/posts/_id.vue b/nuxtjs/pages/posts/_id.vue index 353cf37..724733d 100644 --- a/nuxtjs/pages/posts/_id.vue +++ b/nuxtjs/pages/posts/_id.vue @@ -9,7 +9,7 @@

category

diff --git a/nuxtjs/pages/posts/index.vue b/nuxtjs/pages/posts/index.vue new file mode 100644 index 0000000..4573514 --- /dev/null +++ b/nuxtjs/pages/posts/index.vue @@ -0,0 +1,69 @@ + + + + + + diff --git a/routes/web.php b/routes/web.php index 5d643eb..c5c880a 100644 --- a/routes/web.php +++ b/routes/web.php @@ -12,13 +12,16 @@ */ // post -Route::get('/', 'PostController@index'); +Route::get('/posts', 'PostController@index'); Route::get('/posts/{id}', 'PostController@show'); Route::post('/posts', 'PostController@create'); // comment Route::post('/comments', 'CommentController@create'); +// category +Route::get('/categories', 'CategoryController@index'); + // vue Route::get('/{any}', function () { return view('app'); From be2553ad45877ba045acf8d4120c5f4b8d88091e Mon Sep 17 00:00:00 2001 From: aihara005 Date: Sat, 3 Nov 2018 19:28:37 +0900 Subject: [PATCH 2/3] =?UTF-8?q?CORS=E5=AF=BE=E5=BF=9C=E3=80=81Nuxt?= =?UTF-8?q?=E5=81=B4=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Kernel.php | 1 + app/Http/Middleware/Cors.php | 22 ++++++++++++++++++++++ nuxtjs/pages/index.vue | 25 +++++++++++++++---------- nuxtjs/pages/posts/index.vue | 17 ++--------------- routes/web.php | 20 +++++++++++--------- 5 files changed, 51 insertions(+), 34 deletions(-) create mode 100644 app/Http/Middleware/Cors.php diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 93bf68b..f05585c 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -57,5 +57,6 @@ class Kernel extends HttpKernel 'can' => \Illuminate\Auth\Middleware\Authorize::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, + 'cors' => \App\Http\Middleware\Cors::class, ]; } diff --git a/app/Http/Middleware/Cors.php b/app/Http/Middleware/Cors.php new file mode 100644 index 0000000..50f8787 --- /dev/null +++ b/app/Http/Middleware/Cors.php @@ -0,0 +1,22 @@ +header('Access-Control-Allow-Origin', '*') + ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); + } +} diff --git a/nuxtjs/pages/index.vue b/nuxtjs/pages/index.vue index 9da6804..6ee603b 100644 --- a/nuxtjs/pages/index.vue +++ b/nuxtjs/pages/index.vue @@ -12,11 +12,16 @@

Category

@@ -24,11 +29,11 @@ diff --git a/nuxtjs/pages/posts/index.vue b/nuxtjs/pages/posts/index.vue index 4573514..f9af23a 100644 --- a/nuxtjs/pages/posts/index.vue +++ b/nuxtjs/pages/posts/index.vue @@ -1,29 +1,16 @@