Skip to content

Commit

Permalink
livewire added
Browse files Browse the repository at this point in the history
  • Loading branch information
kkumar-gcc committed Oct 1, 2022
1 parent 3b5844f commit f6d933b
Show file tree
Hide file tree
Showing 108 changed files with 5,058 additions and 1,664 deletions.
37 changes: 37 additions & 0 deletions app/Events/BlogWasCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Events;

use App\Models\Blog;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class BlogWasCreated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct(public Blog $blog)
{
//
}

/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
87 changes: 21 additions & 66 deletions app/Http/Controllers/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@
namespace App\Http\Controllers;

use App\Http\Requests\StoreBlogRequest;
use App\Http\Requests\UpdateBlogRequest;
use App\Models\Blog;
use App\Models\BlogLike;
use App\Models\BlogView;
use App\Models\Comment;
use App\Models\Friendship;
use App\Models\Subscriber;
use App\Models\Tag;
use App\Models\User;
use Illuminate\Auth\Middleware\Authorize;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


class BlogController extends Controller
{
/**
Expand Down Expand Up @@ -126,7 +120,6 @@ public function show(Request $request, $slug)
$titleArray = explode('-', $slug);
$id = end($titleArray);
$blog = Blog::find($id);
$like = NULL;
if ($blog) {
if ($blog->status == "posted") {
// $shareBlog = ShareFacade::page(
Expand Down Expand Up @@ -154,30 +147,13 @@ public function show(Request $request, $slug)
$query->whereIn('title', $blog->tags->pluck('title'));
}, '>=', count($blog->tags->pluck('title')))->where("id", "!=", $blog->id)->limit(5)->withCount('tags')
->get();
if ($request->tab == 'likes') {
$comments = Comment::where("blog_id", "=", $id)->withCount(['commentlikes' => function ($q) {
$q->where('status', '=', 1);
}])->orderByDesc('commentlikes_count')->paginate(5)->fragment('comments');
} else if ($request->tab == 'newest') {
$comments = Comment::where("blog_id", "=", $id)->orderByDesc("updated_at")->paginate(5)->fragment('comments');
} else if ($request->tab == 'dislikes') {
$comments = Comment::where("blog_id", "=", $id)->withCount(['commentlikes' => function ($q) {
$q->where('status', '=', 0);
}])->orderByDesc('commentlikes_count')->paginate(5)->fragment('comments');
} else {
$comments = Comment::where("blog_id", "=", $id)->with(['replies', 'user', 'commentlikes'])->orderByDesc("created_at")->paginate(5)->fragment('comments');
}
return view("blogs.show")->with([
"blog" => $blog,
"comments" => $comments,
"like" => $like,
// "tagTitles" => json_encode($tagTitles),
"related" => $related,
// "shareBlog" => $shareBlog,
]);
}
}
return view("error");
return abort(404);;
}
/**
* Show the form for editing the specified resource.
Expand Down Expand Up @@ -371,48 +347,27 @@ public function detailCard(Request $request)
public function tagSearch(Request $request, $title)
{
$searchTag = Tag::where("title", "=", $title)->first();

$blogCount = Blog::where("status", "=", "posted")->whereHas('tags', function ($q) use ($title) {
$q->where('title', $title);
})->count();
$tab = 'newest';
if ($request->tab == 'likes') {
$blogs = Blog::where("status", "=", "posted")->whereHas('tags', function ($q) use ($title) {
$q->where('title', $title);
})->withCount(['bloglikes' => function ($q) {
$q->where('status', '=', 1);
}])->orderByDesc('bloglikes_count')->paginate(10);
} else if ($request->tab == 'newest') {
$blogs = Blog::where("status", "=", "posted")->whereHas('tags', function ($q) use ($title) {
$q->where('title', $title);
})->orderByDesc('created_at')->paginate(10);
} else if ($request->tab == 'views') {
$blogs = Blog::where("status", "=", "posted")->whereHas('tags', function ($q) use ($title) {
$q->where('title', $title);
})->withCount('blogviews')->orderByDesc('blogviews_count')->paginate(10);
} else {
$blogs = Blog::where("status", "=", "posted")->whereHas('tags', function ($q) use ($title) {
$q->where('title', $title);
})->orderByDesc('created_at')->paginate(10);
}

if ($request->tab) {
$tab = $request->tab;
}
$topBlogs = Blog::where("status", "=", "posted")->withCount('blogviews')->orderByDesc('blogviews_count')->limit(5)->get();

$topUsers = User::limit(5)->get();
$topTags = Tag::withCount(['blogs' => function ($q) {
$q->where('status', '=', "posted");
}])->orderByDesc('blogs_count')->limit(10)->get();
// if ($request->tab == 'likes') {
// $blogs = Blog::where("status", "=", "posted")->whereHas('tags', function ($q) use ($title) {
// $q->where('title', $title);
// })->withCount(['bloglikes' => function ($q) {
// $q->where('status', '=', 1);
// }])->orderByDesc('bloglikes_count')->paginate(10);
// } else if ($request->tab == 'newest') {
// $blogs = Blog::where("status", "=", "posted")->whereHas('tags', function ($q) use ($title) {
// $q->where('title', $title);
// })->orderByDesc('created_at')->paginate(10);
// } else if ($request->tab == 'views') {
// $blogs = Blog::where("status", "=", "posted")->whereHas('tags', function ($q) use ($title) {
// $q->where('title', $title);
// })->withCount('blogviews')->orderByDesc('blogviews_count')->paginate(10);
// } else {
// $blogs = Blog::where("status", "=", "posted")->whereHas('tags', function ($q) use ($title) {
// $q->where('title', $title);
// })->orderByDesc('created_at')->paginate(10);
// }
return view("blogs.tagged")->with([
"blogs" => $blogs,
"blogCount" => $blogCount,
"searchTag" => $searchTag,
"tab" => $tab,
"topUsers" => $topUsers,
"topTags" => $topTags,
"topBlogs" => $topBlogs
"searchTag"=>$searchTag
]);
}

Expand Down
5 changes: 3 additions & 2 deletions app/Http/Controllers/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ public function store(StoreCommentRequest $request)
* @param \App\Models\Comment $comment
* @return \Illuminate\Http\Response
*/
public function show(Comment $comment)
public function show($slug)
{
//
$comment =Comment::find($slug);
return view("comments.show")->with(["comment"=>$comment]);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions app/Http/Controllers/NotificationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class NotificationController extends Controller
{
public function index()
{
return view("notifications.index");
}
}
10 changes: 3 additions & 7 deletions app/Http/Controllers/PrivateProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,14 @@ public function index(Request $request)
"tab" => $tab
]);
} else if ($request->tab == 'pins') {
$tab = 'pins';
$pins = BlogPin::where("user_id", "=", auth()->user()->id)->get();
$blogs = Blog::where("user_id", "=", auth()->user()->id)->where([['status', '=', 'posted'], ["pinned", "=", false]])->paginate(5);
return view("profile.private.index")->with([
$tab = 'pins';return view("profile.private.index")->with([
"user" => $user,
"tab" => $tab,
"pins" => $pins,
"blogs" => $blogs
]);
} else if ($request->tab == 'comments') {
$tab = 'comments';
$comments = Comment::where('user_id', '=', Auth()->user()->id)->paginate(5);
$comments = Comment::where('user_id', '=', Auth()->user()->id)->get();
// ->paginate(5);
return view("profile.private.index")->with([
"user" => $user,
"comments" => $comments,
Expand Down
22 changes: 20 additions & 2 deletions app/Http/Controllers/TagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Http\Requests\StoreTagRequest;
use App\Http\Requests\UpdateTagRequest;
use App\Models\Tag;
use Illuminate\Http\Request;

class TagController extends Controller
{
Expand All @@ -13,9 +14,26 @@ class TagController extends Controller
*
* @return \Illuminate\Http\Response
*/
public function index()
public function index(Request $request)
{
//
if ($request->tab == 'name') {
$tags = Tag::withCount(['blogs' => function ($q) {
$q->where('status', '=', "posted");
}])->orderBy('title')->paginate(18);
} else if ($request->tab == 'newest') {
$tags = Tag::withCount(['blogs' => function ($q) {
$q->where('status', '=', "posted");
}])->orderByDesc('created_at')->paginate(18);
} else if ($request->tab == 'popular') {
$tags = Tag::withCount(['blogs' => function ($q) {
$q->where('status', '=', "posted");
}])->orderByDesc('blogs_count')->paginate(18);
} else {
$tags = Tag::withCount(['blogs' => function ($q) {
$q->where('status', '=', "posted");
}])->paginate(18);
}
return view('tags.index')->with(["tags" => $tags]);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
public function index()
{
return view('users.index');
}
}
53 changes: 0 additions & 53 deletions app/Http/Livewire/Blog/Create.php

This file was deleted.

67 changes: 67 additions & 0 deletions app/Http/Livewire/Blogs/Create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace App\Http\Livewire\Blogs;

use App\Events\BlogWasCreated;
use App\Models\Blog;
use Livewire\Component;
use Livewire\WithFileUploads;
use Illuminate\Support\Str;

class Create extends Component
{
use WithFileUploads;

public $title;

public $body = "# hello peoples";

public $message;

public $photo;
public $tags = [];

protected $rules = [
// 'cover_image' => ['required', 'mimes:png,jpg,svg,gif', 'max:2048'],
'title' => ['required', 'max:200', 'min:20'],
'body' => ['required', 'min:20'],
];

public function render()
{
return view('livewire.blogs.create');
}

public function updatedPhoto()

{
$this->validate([
'photo' => 'image|max:1024', // 1MB Max
]);
}
public function submit()
{
$this->validate();
$blog = Blog::create([
'title' => $this->title,
'body' => $this->body,
'user_id' => auth()->id()
]);
BlogWasCreated::dispatch($blog);
return redirect()->to('/blogs/' . Str::slug($this->title, '-') . '-' . $blog->id);
}
public function draft()
{
$this->validate();
$blog = Blog::create([
'title' => $this->title,
'body' => $this->body,
'status' => "drafted",
'user_id' => auth()->id(),

]);
$this->emit('changed');
session()->flash('message', 'Social info updated successfully');
return redirect()->to('/settings?tab=drafts');
}
}
Loading

0 comments on commit f6d933b

Please sign in to comment.