8
8
use App \Models \Comment ;
9
9
use App \Models \Tag ;
10
10
use App \Models \User ;
11
+ use Illuminate \Support \Facades \URL ;
11
12
use Illuminate \Http \Request ;
12
- use Illuminate \Support \Facades \Auth ;
13
-
13
+ use Jorenvh \Share \ShareFacade ;
14
14
class BlogController extends Controller
15
15
{
16
+ public function __construct ()
17
+ {
18
+ $ this ->middleware ('auth ' )->except (['index ' , 'show ' ]);
19
+ }
16
20
/**
17
21
* Display a listing of the resource.
18
22
*
@@ -30,82 +34,8 @@ public function index(Request $request)
30
34
*/
31
35
public function create ()
32
36
{
33
- if (Auth::check ()) {
34
- $ last_draft = Blog::where ([['user_id ' , auth ()->user ()->id ], ['status ' , "drafted " ]])->OrderBy ('updated_at ' , 'desc ' )->first ();
35
- $ tagTitles = [];
36
- if ($ last_draft ) {
37
- foreach ($ last_draft ->tags as $ tag ) {
38
- $ tagTitles [] = $ tag ->title ;
39
- }
40
- }
41
- $ isDraftNull = 0 ;
42
- if ($ last_draft ) {
43
- $ isDraftNull = 1 ;
44
- }
45
- return view ("blogs.create " )
46
- ->with (["draft " => $ last_draft , "tagTitles " => json_encode ($ tagTitles ), "isDraftNull " => $ isDraftNull ]);
47
- } else {
48
- return view ("auth.login " )->with (["warning " => "You must be logged in to create Blog. " ]);
49
- }
50
- }
51
-
52
- /**
53
- * Store a newly created resource in storage.
54
- *
55
- * @param \App\Http\Requests\StoreBlogRequest $request
56
- * @return \Illuminate\Http\Response
57
- */
58
- public function draft (Request $ request )
59
- {
60
-
61
- $ blogId = $ request ->get ('blogId ' );
62
- $ blogTitle = $ request ->get ('blogTitle ' );
63
- $ blogDescription = $ request ->get ('blogDescription ' );
64
- $ tagNames = json_decode ($ request ->get ('tags ' ));
65
-
66
- if ($ blogId != '' ) {
67
-
68
- $ blog = Blog::find ($ blogId );
69
- $ blog ->title = $ blogTitle ;
70
- $ blog ->description = $ blogDescription ;
71
- $ tagIds = [];
72
- $ tagTitles = [];
73
- foreach ($ tagNames as $ tagName ) {
74
- $ tag = Tag::firstOrCreate (['title ' => $ tagName ]);
75
- if ($ tag ) {
76
- $ tagIds [] = $ tag ->id ;
77
- $ tagTitles [] = $ tag ->title ;
78
- }
79
- };
80
- $ blog ->save ();
81
- $ blog ->tags ()->sync ($ tagIds );
82
- } else {
83
- $ blog = new Blog ();
84
- $ blog ->title = $ blogTitle ;
85
- $ blog ->description = $ blogDescription ;
86
- $ blog ->status = "drafted " ;
87
- $ blog ->user_id = auth ()->user ()->id ;
88
- $ tagIds = [];
89
- $ tagTitles = [];
90
- foreach ($ tagNames as $ tagName ) {
91
-
92
- $ tag = Tag::firstOrCreate (['title ' => $ tagName ]);
93
- if ($ tag ) {
94
- $ tagIds [] = $ tag ->id ;
95
- $ tagTitles [] = $ tag ->title ;
96
- }
97
- };
98
- $ blog ->save ();
99
- $ blog ->tags ()->sync ($ tagIds );
100
-
101
- $ blogId = $ blog ->id ;
102
- }
103
-
104
- return response ()->json ([
105
- "success " => 'post created successfully ' ,
106
- "blogId " => $ blogId ,
107
- "tagTitles " => $ request ->get ('tags ' )
108
- ]);
37
+ $ this ->authorize ('create ' ,Blog::class);
38
+ return view ("blogs.create " );
109
39
}
110
40
111
41
/**
@@ -117,50 +47,46 @@ public function draft(Request $request)
117
47
public function show (Request $ request , $ slug )
118
48
{
119
49
$ blog = Blog::where ("slug " , $ slug )->first ();
50
+ $ this ->authorize ('view ' , $ blog );
120
51
if ($ blog ) {
121
- if ($ blog ->status == "posted " ) {
122
- // $shareBlog = ShareFacade::page(
123
- // URL::current(),
124
- // $blog->title,
125
- // )
126
- // ->facebook()
127
- // ->twitter()
128
- // ->linkedin()
129
- // ->telegram()
130
- // ->whatsapp()
131
- // ->reddit()
132
- // ->getRawLinks();
133
- // dd($blog->body());
134
-
135
-
136
- $ existView = BlogView::where ([['ip_address ' , "= " , $ request ->ip ()], ["blog_id " , "= " , $ blog ->id ]])->count ();
137
- if ($ existView < 1 ) {
138
- $ newView = new BlogView ();
139
- $ newView ->ip_address = $ request ->ip ();
140
- $ newView ->blog_id = $ blog ->id ;
141
- $ newView ->save ();
142
- }
143
- $ related = Blog::where ("status " , "= " , "posted " )->with (['user ' , 'tags ' , 'bloglikes ' , 'blogviews ' ])->whereHas ('tags ' , function ($ query ) use ($ blog ) {
144
- $ query ->whereIn ('title ' , $ blog ->tags ->pluck ('title ' ));
145
- }, '>= ' , count ($ blog ->tags ->pluck ('title ' )))->where ("id " , "!= " , $ blog ->id )->limit (5 )->withCount ('tags ' )
146
- ->get ();
147
- return view ("blogs.show " )->with ([
148
- "blog " => $ blog ,
149
- "related " => $ related ,
150
- ]);
52
+ $ shareBlog = ShareFacade::page (
53
+ URL ::current (),
54
+ $ blog ->title ,
55
+ )
56
+ ->facebook ()
57
+ ->twitter ()
58
+ ->linkedin ()
59
+ ->telegram ()
60
+ ->whatsapp ()
61
+ ->reddit ()
62
+ ->getRawLinks ();
63
+ $ existView = BlogView::where ([['ip_address ' , "= " , $ request ->ip ()], ["blog_id " , "= " , $ blog ->id ]])->count ();
64
+ if ($ existView < 1 ) {
65
+ $ newView = new BlogView ();
66
+ $ newView ->ip_address = $ request ->ip ();
67
+ $ newView ->blog_id = $ blog ->id ;
68
+ $ newView ->save ();
151
69
}
70
+ $ related = Blog::published ()->with (['user ' , 'tags ' , 'bloglikes ' , 'blogviews ' ])->whereHas ('tags ' , function ($ query ) use ($ blog ) {
71
+ $ query ->whereIn ('title ' , $ blog ->tags ->pluck ('title ' ));
72
+ }, '>= ' , count ($ blog ->tags ->pluck ('title ' )))->where ("id " , "!= " , $ blog ->id )->limit (5 )->withCount ('tags ' )
73
+ ->get ();
74
+ return view ("blogs.show " )->with ([
75
+ "blog " => $ blog ,
76
+ "related " => $ related ,
77
+ "shareBlog " =>$ shareBlog ,
78
+ ]);
152
79
}
153
80
return abort (404 );;
154
81
}
155
82
public function edit ($ slug )
156
83
{
157
84
$ blog = Blog::where ("slug " , $ slug )->first ();
158
- $ this ->Authorize ( ' view ' , $ blog );
85
+ $ this ->authorize ( ' update ' , $ blog );
159
86
if ($ blog ) {
160
87
return view ("blogs.update " )->with (["blog " => $ blog ]);
161
88
}
162
89
}
163
-
164
90
public function manage ($ slug )
165
91
{
166
92
$ blog = Blog::where ("slug " , $ slug )->first ();
@@ -232,13 +158,10 @@ public function tagSearch(Request $request, $slug)
232
158
* @param \App\Models\Blog $blog
233
159
* @return \Illuminate\Http\Response
234
160
*/
235
- public function destroy (Request $ request , $ id )
161
+ public function destroy (Blog $ blog )
236
162
{
237
- if (auth ()->user ()->id == $ request ->get ('user_id ' )) {
238
- $ blog = Blog::findOrFail ($ id );
239
- $ blog ->delete ();
240
- return redirect ('/blogs ' )->with (["deleteSuccess " => "blog deleted successfully. " ]);
241
- }
242
- return view ('error ' );
163
+ $ this ->authorize ('delete ' , $ blog );
164
+ $ blog ->delete ();
165
+ return redirect ('/blogs ' )->with (["deleteSuccess " => "blog deleted successfully. " ]);
243
166
}
244
167
}
0 commit comments