diff --git a/app/Http/Controllers/PostController.php b/app/Http/Controllers/PostController.php index a4f0f93..25bbdc7 100755 --- a/app/Http/Controllers/PostController.php +++ b/app/Http/Controllers/PostController.php @@ -62,19 +62,34 @@ public function __construct(Request $request, PostRepository $repo, ActivityLogR $this->activity = $activity; $this->user = $user; $this->category = $category; - $this->middleware('permission:access-post')->except(['index']); + $this->middleware('permission:access-post')->except(['getPublicPosts', 'getPublicPost']); } /** - * Display all posts + * Display all public posts * * @return Post[]|Collection */ - public function index() + public function getPublicPosts() { return $this->repo->getPosts($this->request->all()); } + /** + * Display a public post + * + * @param string $category + * @param string $slug + * + * @return JsonResponse + */ + public function getPublicPost($category, $slug) + { + $post = $this->repo->getByCategoryAndSlug($category, $slug); + + return $this->success(compact('post')); + } + /** * Get pre-requisites for post module * diff --git a/app/Post.php b/app/Post.php index b9295e8..749e583 100755 --- a/app/Post.php +++ b/app/Post.php @@ -161,6 +161,20 @@ public function scopeFilterBySlug($query, $slug = null) return $query->where('slug', '=', $slug); } + /** + * Scope a query to only include post with the given category and slug. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param Category $category + * @param string $slug + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeFilterByCategoryAndSlug($query, $category, $slug) + { + return $query->where('slug', '=', $slug)->where('category_id', '=', $category->id); + } + /** * Scope a query to only include post with the given id. * diff --git a/app/Repositories/PostRepository.php b/app/Repositories/PostRepository.php index d472396..3886ef6 100755 --- a/app/Repositories/PostRepository.php +++ b/app/Repositories/PostRepository.php @@ -2,6 +2,7 @@ namespace App\Repositories; +use App\Category; use App\Post; use Carbon\Carbon; use Illuminate\Support\Collection; @@ -77,7 +78,7 @@ public function countDateBetween($start_date, $end_date) } /** - * Get valid post. + * Get valid post by slug. * * @param string $slug * @@ -98,6 +99,33 @@ public function getBySlug($slug) return $post; } + /** + * Get valid post by category and slug. + * + * @param string $category + * @param string $slug + * + * @return Post + */ + public function getByCategoryAndSlug($category, $slug) + { + $category = Category::where('name', $category)->first(); + + if (!$category) { + return null; + } + + $post = $this->post->with('user', 'user.profile', 'category') + ->filterByCategoryAndSlug($category, $slug) + ->first(); + + if (!$post) { + return null; + } + + return $post; + } + /** * Find post by Id * diff --git a/database/migrations/2018_07_13_124411_create_posts_table.php b/database/migrations/2018_07_13_124411_create_posts_table.php index 0eba8a0..56b2980 100755 --- a/database/migrations/2018_07_13_124411_create_posts_table.php +++ b/database/migrations/2018_07_13_124411_create_posts_table.php @@ -19,7 +19,7 @@ public function up() $table->boolean('is_draft')->default(0); $table->string('title')->nullable(); $table->string('slug')->nullable(); - $table->text('body')->nullable(); + $table->longText('body')->nullable(); $table->string('cover')->nullable(); $table->timestamps(); }); diff --git a/resources/assets/js/routes.js b/resources/assets/js/routes.js index fc14ed5..228546c 100755 --- a/resources/assets/js/routes.js +++ b/resources/assets/js/routes.js @@ -8,7 +8,7 @@ let routes = [ { path: '/', component: require('./views/auth/Login.vue'), - meta: {title: appName + ' | ' + i18n.auth.login} + meta: {title: appName} }, { path: '/', @@ -212,6 +212,11 @@ let routes = [ } ] }, + { + path: '/:category/:slug', + component: require('./views/post/View.vue'), + meta: {title: appName + ' | ' + i18n.post.view} + }, { path: '*', component: require('./layouts/ErrorPage.vue'), diff --git a/resources/assets/js/views/auth/Login.vue b/resources/assets/js/views/auth/Login.vue index 9601bd7..0aee74f 100755 --- a/resources/assets/js/views/auth/Login.vue +++ b/resources/assets/js/views/auth/Login.vue @@ -48,21 +48,7 @@
@@ -80,6 +66,7 @@ diff --git a/resources/assets/js/views/post/View.vue b/resources/assets/js/views/post/View.vue new file mode 100755 index 0000000..c194d8f --- /dev/null +++ b/resources/assets/js/views/post/View.vue @@ -0,0 +1,75 @@ + +