Skip to content

Commit f8f8d33

Browse files
committed
WIP: PostController actions and public assets
1 parent b3f08b2 commit f8f8d33

File tree

10 files changed

+292
-26
lines changed

10 files changed

+292
-26
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ All notable changes to `blogger` will be documented in this file
88

99
## 1.0.1 - XX-XX-2020
1010

11-
Updates to comments and tags
11+
- Updates to comments and tags
12+
- Add facades

routes/web.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Route::group(['middleware' => config('blog.middleware')], function () {
44
Route::resource('posts', 'PostController');
5+
Route::resource('tags', 'TagController');
6+
Route::resource('categories', 'CategoryController');
57
});
68

79
Route::get('posts/{post}', 'PostController@show')->name('posts.show');

src/BlogServiceProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ private function handleMigrations()
105105
private function handlePublishing()
106106
{
107107
if ($this->app->runningInConsole()) {
108+
$this->publishes([
109+
__DIR__.'/../public' => public_path('vendor/blog'),
110+
], 'blog-assets');
111+
108112
$this->publishes([
109113
__DIR__.'/../database/factories/' => database_path('factories'),
110114
], 'blog-factories');
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Chriscreates\Blog\Controllers;
4+
5+
use Chriscreates\Blog\Category;
6+
use Illuminate\Http\Request;
7+
8+
class CategoryController extends Controller
9+
{
10+
/**
11+
* Instantiate a new controller instance.
12+
*
13+
* @return void
14+
*/
15+
public function __construct()
16+
{
17+
$this->middleware('auth');
18+
}
19+
20+
/**
21+
* Display a listing of the resource.
22+
*
23+
* @return \Illuminate\Http\Response
24+
*/
25+
public function index() : JsonResponse
26+
{
27+
}
28+
29+
/**
30+
* Store a newly created resource in storage.
31+
*
32+
* @param \Chriscreates\Blog\Requests\ValidateCategoryRequest $request
33+
* @return \Illuminate\Http\Response
34+
*/
35+
public function store(ValidateCategoryRequest $request) : JsonResponse
36+
{
37+
}
38+
39+
/**
40+
* Show the form for editing the specified resource.
41+
*
42+
* @param \Chriscreates\Blog\Category $category
43+
* @return \Illuminate\Http\Response
44+
*/
45+
public function edit(Category $category) : JsonResponse
46+
{
47+
}
48+
49+
/**
50+
* Update the specified resource in storage.
51+
*
52+
* @param \Chriscreates\Blog\Requests\ValidateCategoryRequest $request
53+
* @param \Chriscreates\Blog\Category $category
54+
* @return \Illuminate\Http\Response
55+
*/
56+
public function update(ValidateCategoryRequest $request, Category $category) : JsonResponse
57+
{
58+
}
59+
60+
/**
61+
* Remove the specified resource from storage.
62+
*
63+
* @param \Chriscreates\Blog\Category $category
64+
* @return \Illuminate\Http\Response
65+
*/
66+
public function destroy(Category $category) : JsonResponse
67+
{
68+
}
69+
}

src/Controllers/PostController.php

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
use Artesaos\SEOTools\Facades\OpenGraph;
66
use Artesaos\SEOTools\Facades\SEOMeta;
7+
use Chriscreates\Blog\Category;
78
use Chriscreates\Blog\Post;
89
use Chriscreates\Blog\Requests\ValidatePostRequest;
10+
use Chriscreates\Blog\Tag;
11+
use Illuminate\Http\JsonResponse;
912
use Illuminate\Http\Request;
1013
use Illuminate\Support\Facades\Storage;
1114

@@ -42,16 +45,22 @@ public function index()
4245
*/
4346
public function create()
4447
{
45-
return view('admin.posts.create-post');
48+
$post = new Post;
49+
50+
return view('admin.posts.edit-post', [
51+
'post' => $post,
52+
'categories' => Category::all(),
53+
'tags' => Tag::all(),
54+
]);
4655
}
4756

4857
/**
4958
* Store a newly created resource in storage.
5059
*
51-
* @param \App\Http\Requests\ValidatePostRequest $request
52-
* @return \Illuminate\Http\Response
60+
* @param \Chriscreates\Blog\Requests\ValidatePostRequest $request
61+
* @return \Illuminate\Http\JsonResponse
5362
*/
54-
public function store(ValidatePostRequest $request)
63+
public function store(ValidatePostRequest $request) : JsonResponse
5564
{
5665
$post = Post::create($request->only([
5766
'title',
@@ -64,9 +73,7 @@ public function store(ValidatePostRequest $request)
6473
'allow_guest_comments',
6574
]));
6675

67-
return redirect()
68-
->route('posts.edit', ['post' => $post->id])
69-
->with('success', 'Post created.');
76+
return response()->json($post);
7077
}
7178

7279
/**
@@ -98,17 +105,23 @@ public function show(Post $post)
98105
*/
99106
public function edit(Post $post)
100107
{
101-
return view('admin.posts.edit-post', compact('post'));
108+
$post->load('category', 'tags');
109+
110+
return view('admin.posts.edit-post', [
111+
'post' => $post,
112+
'categories' => Category::all(),
113+
'tags' => Tag::all(),
114+
]);
102115
}
103116

104117
/**
105118
* Update the specified resource in storage.
106119
*
107-
* @param \App\Http\Requests\ValidatePostRequest $request
120+
* @param \Chriscreates\Blog\Requests\ValidatePostRequest $request
108121
* @param \Chriscreates\Blog\Post $post
109-
* @return \Illuminate\Http\Response
122+
* @return \Illuminate\Http\JsonResponse
110123
*/
111-
public function update(ValidatePostRequest $request, Post $post)
124+
public function update(ValidatePostRequest $request, Post $post) : JsonResponse
112125
{
113126
$post->update($request->only([
114127
'title',
@@ -121,23 +134,23 @@ public function update(ValidatePostRequest $request, Post $post)
121134
'allow_guest_comments',
122135
]));
123136

124-
return redirect()
125-
->route('posts.edit', ['post' => $post->id])
126-
->with('success', 'Post updated.');
137+
$post->refresh();
138+
139+
return response()->json($post);
127140
}
128141

129142
/**
130143
* Remove the specified resource from storage.
131144
*
132145
* @param \Chriscreates\Blog\Post $post
133-
* @return \Illuminate\Http\Response
146+
* @return \Illuminate\Http\JsonResponse
134147
*/
135-
public function destroy(Post $post)
148+
public function destroy(Post $post) : JsonResponse
136149
{
137150
$post->delete();
138151

139-
return redirect()
140-
->route('posts.index')
141-
->with('success', 'Post deleted.');
152+
$post = new Post;
153+
154+
return response()->json($post);
142155
}
143156
}

src/Controllers/TagController.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Chriscreates\Blog\Controllers;
4+
5+
use Chriscreates\Blog\Tag;
6+
use Illuminate\Http\Request;
7+
8+
class TagController extends Controller
9+
{
10+
/**
11+
* Instantiate a new controller instance.
12+
*
13+
* @return void
14+
*/
15+
public function __construct()
16+
{
17+
$this->middleware('auth');
18+
}
19+
20+
/**
21+
* Display a listing of the resource.
22+
*
23+
* @return \Illuminate\Http\Response
24+
*/
25+
public function index() : JsonResponse
26+
{
27+
}
28+
29+
/**
30+
* Store a newly created resource in storage.
31+
*
32+
* @param \Chriscreates\Blog\Requests\ValidateTagRequest $request
33+
* @return \Illuminate\Http\Response
34+
*/
35+
public function store(ValidateTagRequest $request) : JsonResponse
36+
{
37+
}
38+
39+
/**
40+
* Show the form for editing the specified resource.
41+
*
42+
* @param \Chriscreates\Blog\Tag $tag
43+
* @return \Illuminate\Http\Response
44+
*/
45+
public function edit(Tag $tag) : JsonResponse
46+
{
47+
}
48+
49+
/**
50+
* Update the specified resource in storage.
51+
*
52+
* @param \Chriscreates\Blog\Requests\ValidateTagRequest $request
53+
* @param \Chriscreates\Blog\Tag $tag
54+
* @return \Illuminate\Http\Response
55+
*/
56+
public function update(ValidateTagRequest $request, Tag $tag) : JsonResponse
57+
{
58+
}
59+
60+
/**
61+
* Remove the specified resource from storage.
62+
*
63+
* @param \Chriscreates\Blog\Tag $tag
64+
* @return \Illuminate\Http\Response
65+
*/
66+
public function destroy(Tag $tag) : JsonResponse
67+
{
68+
}
69+
}

src/Observers/PostObserver.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Chriscreates\Blog\Observers;
44

55
use Chriscreates\Blog\Post;
6+
use Illuminate\Support\Facades\Auth;
67

78
class PostObserver
89
{
@@ -23,7 +24,7 @@ public function saving(Post $post)
2324
}
2425

2526
if ( ! request()->filled('user_id')) {
26-
$post->user_id = auth()->id();
27+
$post->user_id = Auth::id();
2728
}
2829

2930
if ( ! request()->filled('slug')) {
@@ -45,6 +46,8 @@ public function deleting(Post $post)
4546
{
4647
$post->tags()->detach();
4748

48-
$post->comments()->delete();
49+
if ($post->comments()) {
50+
$post->comments()->delete();
51+
}
4952
}
5053
}

src/Post.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ class Post extends Model
1818
PostsHaveComments,
1919
PostsHaveACategory;
2020

21-
const PUBLISHED = 'published';
22-
const DRAFT = 'draft';
23-
const SCHEDULED = 'scheduled';
21+
public const PUBLISHED = 'published';
22+
public const DRAFT = 'draft';
23+
public const SCHEDULED = 'scheduled';
2424

2525
protected $table = 'posts';
2626

@@ -30,7 +30,13 @@ class Post extends Model
3030

3131
public $timestamps = true;
3232

33-
protected $appends = ['tagsCount'];
33+
protected $appends = [
34+
'tags_count',
35+
'statuses',
36+
'friendly_status',
37+
'view_path',
38+
'delete_path',
39+
];
3440

3541
protected $dates = ['published_at'];
3642

@@ -154,4 +160,40 @@ public function statuses()
154160
self::SCHEDULED,
155161
]);
156162
}
163+
164+
public function getStatusesAttribute()
165+
{
166+
return $this->statuses()->toArray();
167+
}
168+
169+
public function getFriendlyStatusAttribute()
170+
{
171+
if ($this->isDraft()) {
172+
return ucfirst(self::DRAFT);
173+
}
174+
175+
if ($this->isScheduled()) {
176+
return ucfirst(self::SCHEDULED)." for: {$this->published_at->format('d-m-Y')}";
177+
}
178+
179+
return ucfirst(self::PUBLISHED);
180+
}
181+
182+
public function getViewPathAttribute()
183+
{
184+
if ( ! $this->id) {
185+
return '';
186+
}
187+
188+
return route('posts.show', ['post' => $this->id]);
189+
}
190+
191+
public function getDeletePathAttribute()
192+
{
193+
if ( ! $this->id) {
194+
return '';
195+
}
196+
197+
return route('posts.destroy', ['post' => $this->id]);
198+
}
157199
}

0 commit comments

Comments
 (0)