Skip to content
This repository was archived by the owner on Mar 13, 2023. It is now read-only.

Commit bbd961b

Browse files
committed
Add blog frontend fonctionnalities
1 parent f1ee813 commit bbd961b

File tree

10 files changed

+382
-100
lines changed

10 files changed

+382
-100
lines changed

app/Http/Controllers/BlogController.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,31 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Repositories\CategoryRepository;
6+
use App\Repositories\PostRepository;
57
use Illuminate\Http\Request;
68

79
class BlogController extends Controller
810
{
9-
public function __construct()
11+
/**
12+
* @var PostRepository
13+
*/
14+
private $postRepository;
15+
16+
/**
17+
* @var CategoryRepository
18+
*/
19+
private $categoryRepository;
20+
21+
/**
22+
* BlogController constructor.
23+
* @param PostRepository $postRepository
24+
* @param CategoryRepository $categoryRepository
25+
*/
26+
public function __construct(PostRepository $postRepository, CategoryRepository $categoryRepository)
1027
{
28+
$this->postRepository = $postRepository;
29+
$this->categoryRepository = $categoryRepository;
1130
}
1231

1332
/**
@@ -17,19 +36,28 @@ public function __construct()
1736
*/
1837
public function index()
1938
{
20-
$posts = [];
39+
$posts = $this->postRepository->paginate(6);
40+
$total = $this->postRepository->online()->count();
41+
$categories = $this->categoryRepository->categoryType('POST');
42+
$category = null;
2143

22-
return view('frontend.blog.home', compact('posts'));
44+
return view('frontend.blog.home', compact('posts', 'total', 'categories', 'category'));
2345
}
2446

2547
/**
2648
* Display Blog single Category
2749
*
28-
* @param string $category
50+
* @param string $slug
51+
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2952
*/
30-
public function category(string $category)
53+
public function category(string $slug)
3154
{
55+
$total = $this->postRepository->online()->count();
56+
$categories = $this->categoryRepository->categoryType('POST');
57+
$category = $this->categoryRepository->findBySlug($slug);
58+
$posts = $category->posts()->where('status', 'PUBLISHED')->paginate('6');
3259

60+
return view('frontend.blog.home', compact('posts', 'total', 'categories', 'category'));
3361
}
3462

3563
/**
@@ -40,6 +68,8 @@ public function category(string $category)
4068
*/
4169
public function post(string $slug)
4270
{
43-
return view('frontend.blog.post');
71+
$post = $this->postRepository->findBySlug($slug);
72+
73+
return view('frontend.blog.post', compact('post'));
4474
}
4575
}

app/Http/Controllers/SiteController.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Repositories\PostRepository;
56
use Illuminate\Http\Request;
67

78
class SiteController extends Controller
89
{
10+
/**
11+
* @var PostRepository
12+
*/
13+
private $postRepository;
14+
915
/**
1016
* Create a new controller instance.
1117
*
12-
* @return void
18+
* @param PostRepository $postRepository
1319
*/
14-
public function __construct()
20+
public function __construct(PostRepository $postRepository)
1521
{
16-
22+
$this->postRepository = $postRepository;
1723
}
1824

1925
/**
@@ -23,6 +29,10 @@ public function __construct()
2329
*/
2430
public function index()
2531
{
26-
return view('frontend.home');
32+
$posts = $this->postRepository->last(6);
33+
$event = [];
34+
$events = [];
35+
36+
return view('frontend.home', compact('posts'));
2737
}
2838
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2018. MckenzieArts
4+
* @author Mckenziearts <[email protected]>
5+
* @link https://github.com/Mckenziearts/laravel-command
6+
*/
7+
8+
namespace App\Repositories;
9+
10+
use TCG\Voyager\Models\Category;
11+
12+
class CategoryRepository
13+
{
14+
/**
15+
* @var Category
16+
*/
17+
private $model;
18+
19+
/**
20+
* CategoryRepository constructor.
21+
* @param Category $model
22+
*/
23+
public function __construct(Category $model)
24+
{
25+
$this->model = $model;
26+
}
27+
28+
/**
29+
* Return a new instance of Category Model
30+
*
31+
* @return Category
32+
*/
33+
public function newInstance()
34+
{
35+
return $this->model->newInstance();
36+
}
37+
38+
/**
39+
* Get all book categories from the database
40+
*
41+
* @return \Illuminate\Database\Eloquent\Collection|static[]
42+
*/
43+
public function all()
44+
{
45+
return $this->model->newQuery()->get();
46+
}
47+
48+
/**
49+
* Get all categories by type give in param
50+
*
51+
* @param string $type
52+
* @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
53+
*/
54+
public function categoryType(string $type)
55+
{
56+
return $this->model
57+
->newQuery()
58+
->where('type', $type)
59+
->orderBy('created_at', 'DESC')
60+
->get();
61+
}
62+
63+
64+
65+
/**
66+
* Return a model with the id set in parameter
67+
*
68+
* @param int $id
69+
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
70+
*/
71+
public function find(int $id)
72+
{
73+
return $this->model->newQuery()
74+
->findOrFail($id);
75+
}
76+
77+
/**
78+
* Return a model with the slug set in parameter
79+
*
80+
* @param string $slug
81+
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
82+
*/
83+
public function findBySlug(string $slug)
84+
{
85+
return $this->model->newQuery()
86+
->where('slug', $slug)
87+
->firstOrFail();
88+
}
89+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2018. MckenzieArts
4+
* @author Mckenziearts <[email protected]>
5+
* @link https://github.com/Mckenziearts/laravel-command
6+
*/
7+
8+
namespace App\Repositories;
9+
10+
use TCG\Voyager\Models\Post;
11+
12+
class PostRepository
13+
{
14+
/**
15+
* @var Post
16+
*/
17+
private $model;
18+
19+
/**
20+
* PostRepository constructor.
21+
* @param Post $model
22+
*/
23+
public function __construct(Post $model)
24+
{
25+
$this->model = $model;
26+
}
27+
28+
/**
29+
* Return a new instance of Post Model
30+
*
31+
* @return Post
32+
*/
33+
public function newInstance()
34+
{
35+
return $this->model->newInstance();
36+
}
37+
38+
/**
39+
* Get all book categories from the database
40+
*
41+
* @return \Illuminate\Database\Eloquent\Collection|static[]
42+
*/
43+
public function all()
44+
{
45+
return $this->model->newQuery()->get();
46+
}
47+
48+
/**
49+
* Get all book categories from the database
50+
*
51+
* @return \Illuminate\Database\Eloquent\Collection|static[]
52+
*/
53+
public function online()
54+
{
55+
return $this->model
56+
->newQuery()
57+
->where('status', '=', Post::PUBLISHED)
58+
->get();
59+
}
60+
61+
/**
62+
* Get last post from the database
63+
*
64+
* @param int $limit
65+
* @return \Illuminate\Database\Eloquent\Collection|static[]
66+
*/
67+
public function last(int $limit = 3)
68+
{
69+
return $this->model->newQuery()
70+
->with('authorId')
71+
->where('status', '=', Post::PUBLISHED)
72+
->orderBy('created_at', 'DESC')
73+
->limit($limit)
74+
->get();
75+
}
76+
77+
/**
78+
* Get all books categories by count pagination
79+
*
80+
* @param int $results
81+
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
82+
*/
83+
public function paginate(int $results = 10)
84+
{
85+
return $this->model->newQuery()
86+
->with('authorId')
87+
->where('status', '=', Post::PUBLISHED)
88+
->orderBy('created_at', 'DESC')
89+
->paginate($results);
90+
}
91+
92+
/**
93+
* Return a model with the id set in parameter
94+
*
95+
* @param int $id
96+
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
97+
*/
98+
public function find(int $id)
99+
{
100+
return $this->model->newQuery()
101+
->findOrFail($id);
102+
}
103+
104+
/**
105+
* Return a model with the slug set in parameter
106+
*
107+
* @param string $slug
108+
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
109+
*/
110+
public function findBySlug(string $slug)
111+
{
112+
return $this->model->newQuery()
113+
->with('category')
114+
->where('slug', $slug)
115+
->firstOrFail();
116+
}
117+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2018. MckenzieArts
4+
* @author Mckenziearts <[email protected]>
5+
* @link https://github.com/Mckenziearts/laravel-command
6+
*/
7+
8+
namespace App\Repositories;
9+
10+
use App\User;
11+
12+
class UserRepository
13+
{
14+
/**
15+
* @var User
16+
*/
17+
private $model;
18+
19+
/**
20+
* UserRepository constructor.
21+
* @param User $model
22+
*/
23+
public function __construct(User $model)
24+
{
25+
$this->model = $model;
26+
}
27+
28+
/**
29+
* Return a new instance of User Model
30+
*
31+
* @return User
32+
*/
33+
public function newInstance()
34+
{
35+
return $this->model->newInstance();
36+
}
37+
}

app/User.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Notifications\Notifiable;
66
use Illuminate\Foundation\Auth\User as Authenticatable;
7+
use TCG\Voyager\Models\Post;
78

89
class User extends \TCG\Voyager\Models\User
910
{
@@ -26,4 +27,12 @@ class User extends \TCG\Voyager\Models\User
2627
protected $hidden = [
2728
'password', 'remember_token',
2829
];
30+
31+
/**
32+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
33+
*/
34+
public function posts()
35+
{
36+
return $this->hasMany(Post::class);
37+
}
2938
}

0 commit comments

Comments
 (0)