source: app/Http/Controllers/Blog/BlogController.php@ d25ba66

Last change on this file since d25ba66 was 0924b6c, checked in by Özkan İliyaz <iliyaz_96@…>, 4 years ago

initial commit

  • Property mode set to 100644
File size: 6.1 KB
Line 
1<?php
2
3namespace App\Http\Controllers\Blog;
4
5use App\Http\Requests\Blog\CommentRequest;
6use App\Http\Requests\Blog\PostLikeRequest;
7use App\Http\Requests\Blog\PostUnlikeRequest;
8use App\Models\Tag;
9use App\Models\Like;
10use App\Models\Post;
11use App\Models\Comment;
12use App\Models\Category;
13use App\Models\UserProfile;
14use Illuminate\Http\Request;
15use App\Http\Controllers\Controller;
16use App\Notifications\NewCommentAdded;
17
18class BlogController extends Controller
19{
20 public function __construct()
21 {
22 $this->middleware("web");
23 }
24
25 public function index()
26 {
27 $posts = collect();
28
29 $categories = collect();
30 $allCategories = Category::all();
31
32 foreach ($allCategories as $c) {
33 if (Post::where([
34 "category_id" => $c->id,
35 "is_confirmed" => true,
36 "is_active" => true
37 ])->count() > 0) {
38 $categories->push($c);
39 }
40 }
41
42 foreach ($categories as $category) {
43 $tempPosts = Post::where([
44 "category_id" => $category->id,
45 "is_confirmed" => true,
46 "is_active" => true
47 ])
48 ->orderBy("created_at", "desc")
49 ->take(3)
50 ->get();
51
52 foreach ($tempPosts as $post) {
53 $posts->push($post);
54 }
55 }
56
57 $posts = $posts->flatten(1);
58
59 return view("blog.index")->with([
60 "latestPosts" => Post::where([
61 "is_confirmed" => true,
62 "is_active" => true
63 ])->orderBy("created_at", "desc")->take(5)->get(),
64 "posts" => $posts
65 ]);
66 }
67
68 public function about()
69 {
70 return view("blog.about");
71 }
72
73 public function showCategory($category)
74 {
75 if (Category::whereName(ucfirst($category))->count() == 0) {
76 return abort(404);
77 }
78
79 if (Post::where([
80 "category_id" => Category::whereName(ucfirst($category))->first()->id,
81 "is_confirmed" => true,
82 "is_active" => true
83 ])->count() == 0) {
84 return abort(404);
85 }
86
87 $posts = Post::where([
88 "category_id" => Category::whereName(ucfirst($category))->first()->id,
89 "is_confirmed" => true,
90 "is_active" => true
91 ])
92 ->orderBy("created_at", "desc")
93 ->paginate(8);
94
95 return view("blog.category")->with([
96 "category" => Category::whereName(ucfirst($category))->first(),
97 "posts" => $posts
98 ]);
99 }
100
101 public function showPost(Request $request, $category, $slug)
102 {
103 $post = Post::where([
104 "slug" => $slug,
105 "is_confirmed" => true,
106 "is_active" => true
107 ])->firstOrFail();
108
109 if ($post->isLiked($request->ip())) {
110 $likedId = Like::where([
111 "post_id" => $post->id,
112 "ip_address" => $request->ip()
113 ])->firstOrFail()->id;
114 } else $likedId = null;
115
116 if (auth()->check()) $comments = $post->comment;
117 else $comments = $post->comment()->where([
118 "post_id" => $post->id,
119 "is_active" => true,
120 ])->get();
121
122 return view("blog.post")->with([
123 "comments" => $comments,
124 "category" => $category,
125 "likeCounter" => $post->total_likes,
126 "post" => $post,
127 "ip" => $request->ip(),
128 "likedId" => $likedId
129 ]);
130 }
131
132 public function showTagPosts($tag)
133 {
134 return view("blog.tag")->with([
135 "tag" => $tag,
136 "posts" => Tag::whereName($tag)
137 ->firstOrFail()
138 ->post()
139 ->where([
140 "is_confirmed" => true,
141 "is_active" => true,
142 ])
143 ->orderBy("created_at", "desc")
144 ->paginate(5)
145 ]);
146 }
147
148 public function showProfile($profileLink)
149 {
150 $userProfile = UserProfile::where("profile_link", $profileLink)->firstOrFail();
151
152 $posts = Post::where([
153 "user_id" => $userProfile->user_id,
154 "is_confirmed" => true,
155 "is_active" => true,
156 ])
157 ->orderBy("created_at", "desc")
158 ->paginate(5);
159
160 if ($posts->count() == 0) {
161 return abort(404);
162 }
163
164 return view("blog.profile")->with("posts", $posts);
165 }
166
167 public function comment(CommentRequest $request)
168 {
169 $comment = new Comment();
170
171 $comment->post_id = $request->post_id;
172 $comment->name = $request->name;
173 $comment->email = $request->email;
174 $comment->comment = $request->comment;
175
176 $comment->save();
177
178 $commentedPost = Post::find($request->post_id);
179
180 $commentedPost->user->notify(new NewCommentAdded("Have new comment for review"));
181
182 return response()->json([
183 "type" => "alert-success",
184 "message" => "Comment submited successfully and will be published after review."
185 ]);
186 }
187
188 public function like(PostLikeRequest $request)
189 {
190 $like = new Like();
191 $post = Post::find($request->post_id);
192 $likes = $post->like->all();
193
194 foreach ($likes as $l) {
195 if ($l->post_id == $request->post_id && $l->ip_address == $request->ip()) {
196 return false;
197 }
198 }
199
200 $like->post_id = $request->post_id;
201 $like->ip_address = $request->ip();
202
203 $like->save();
204
205 $post->increment("total_likes");
206 $post->save();
207
208 return response()->json([
209 "unlike_id" => $like->id,
210 "likeCounter" => $post->total_likes
211 ]);
212 }
213
214 public function unlike(PostUnlikeRequest $request)
215 {
216 $like = Like::find($request->unlike_id);
217 $post = Post::find($like->post_id);
218 $like->delete();
219
220 $post->decrement("total_likes");
221 $post->save();
222
223 return response()->json([
224 "post_id" => $post->id,
225 "likeCounter" => $post->total_likes
226 ]);
227 }
228}
Note: See TracBrowser for help on using the repository browser.