Skip to content

Commit

Permalink
admin panel added
Browse files Browse the repository at this point in the history
  • Loading branch information
kkumar-gcc committed Oct 13, 2022
1 parent fc9ab90 commit 55b5d37
Show file tree
Hide file tree
Showing 141 changed files with 7,986 additions and 4,794 deletions.
30 changes: 30 additions & 0 deletions app/Http/Controllers/Admin/IndexController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class IndexController extends Controller
{
public function index()
{
return view('admin.index');
}
public function users()
{
if (!auth()->user()->can('access users'))
{
abort(403);
}
return view('admin.users.index');
}
public function tags()
{
if (!auth()->user()->can('access tags'))
{
abort(403);
}
return view('admin.tags.index');
}
}
19 changes: 19 additions & 0 deletions app/Http/Controllers/Admin/PermissionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Spatie\Permission\Models\Permission;

class PermissionController extends Controller
{
public function index()
{
if (!auth()->user()->can('access permissions'))
{
abort(403);
}
return view('admin.permissions.index');
}
}
20 changes: 20 additions & 0 deletions app/Http/Controllers/Admin/RoleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Spatie\Permission\Models\Role;

class RoleController extends Controller
{
public function index()
{
if (!auth()->user()->can('access roles'))
{
abort(403);
}

return view('admin.roles.index');
}
}
157 changes: 40 additions & 117 deletions app/Http/Controllers/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
use App\Models\Comment;
use App\Models\Tag;
use App\Models\User;
use Illuminate\Support\Facades\URL;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

use Jorenvh\Share\ShareFacade;
class BlogController extends Controller
{
public function __construct()
{
$this->middleware('auth')->except(['index', 'show']);
}
/**
* Display a listing of the resource.
*
Expand All @@ -30,82 +34,8 @@ public function index(Request $request)
*/
public function create()
{
if (Auth::check()) {
$last_draft = Blog::where([['user_id', auth()->user()->id], ['status', "drafted"]])->OrderBy('updated_at', 'desc')->first();
$tagTitles = [];
if ($last_draft) {
foreach ($last_draft->tags as $tag) {
$tagTitles[] = $tag->title;
}
}
$isDraftNull = 0;
if ($last_draft) {
$isDraftNull = 1;
}
return view("blogs.create")
->with(["draft" => $last_draft, "tagTitles" => json_encode($tagTitles), "isDraftNull" => $isDraftNull]);
} else {
return view("auth.login")->with(["warning" => "You must be logged in to create Blog."]);
}
}

/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\StoreBlogRequest $request
* @return \Illuminate\Http\Response
*/
public function draft(Request $request)
{

$blogId = $request->get('blogId');
$blogTitle = $request->get('blogTitle');
$blogDescription = $request->get('blogDescription');
$tagNames = json_decode($request->get('tags'));

if ($blogId != '') {

$blog = Blog::find($blogId);
$blog->title = $blogTitle;
$blog->description = $blogDescription;
$tagIds = [];
$tagTitles = [];
foreach ($tagNames as $tagName) {
$tag = Tag::firstOrCreate(['title' => $tagName]);
if ($tag) {
$tagIds[] = $tag->id;
$tagTitles[] = $tag->title;
}
};
$blog->save();
$blog->tags()->sync($tagIds);
} else {
$blog = new Blog();
$blog->title = $blogTitle;
$blog->description = $blogDescription;
$blog->status = "drafted";
$blog->user_id = auth()->user()->id;
$tagIds = [];
$tagTitles = [];
foreach ($tagNames as $tagName) {

$tag = Tag::firstOrCreate(['title' => $tagName]);
if ($tag) {
$tagIds[] = $tag->id;
$tagTitles[] = $tag->title;
}
};
$blog->save();
$blog->tags()->sync($tagIds);

$blogId = $blog->id;
}

return response()->json([
"success" => 'post created successfully',
"blogId" => $blogId,
"tagTitles" => $request->get('tags')
]);
$this->authorize('create',Blog::class);
return view("blogs.create");
}

/**
Expand All @@ -117,50 +47,46 @@ public function draft(Request $request)
public function show(Request $request, $slug)
{
$blog = Blog::where("slug", $slug)->first();
$this->authorize('view', $blog);
if ($blog) {
if ($blog->status == "posted") {
// $shareBlog = ShareFacade::page(
// URL::current(),
// $blog->title,
// )
// ->facebook()
// ->twitter()
// ->linkedin()
// ->telegram()
// ->whatsapp()
// ->reddit()
// ->getRawLinks();
// dd($blog->body());


$existView = BlogView::where([['ip_address', "=", $request->ip()], ["blog_id", "=", $blog->id]])->count();
if ($existView < 1) {
$newView = new BlogView();
$newView->ip_address = $request->ip();
$newView->blog_id = $blog->id;
$newView->save();
}
$related = Blog::where("status", "=", "posted")->with(['user', 'tags', 'bloglikes', 'blogviews'])->whereHas('tags', function ($query) use ($blog) {
$query->whereIn('title', $blog->tags->pluck('title'));
}, '>=', count($blog->tags->pluck('title')))->where("id", "!=", $blog->id)->limit(5)->withCount('tags')
->get();
return view("blogs.show")->with([
"blog" => $blog,
"related" => $related,
]);
$shareBlog = ShareFacade::page(
URL::current(),
$blog->title,
)
->facebook()
->twitter()
->linkedin()
->telegram()
->whatsapp()
->reddit()
->getRawLinks();
$existView = BlogView::where([['ip_address', "=", $request->ip()], ["blog_id", "=", $blog->id]])->count();
if ($existView < 1) {
$newView = new BlogView();
$newView->ip_address = $request->ip();
$newView->blog_id = $blog->id;
$newView->save();
}
$related = Blog::published()->with(['user', 'tags', 'bloglikes', 'blogviews'])->whereHas('tags', function ($query) use ($blog) {
$query->whereIn('title', $blog->tags->pluck('title'));
}, '>=', count($blog->tags->pluck('title')))->where("id", "!=", $blog->id)->limit(5)->withCount('tags')
->get();
return view("blogs.show")->with([
"blog" => $blog,
"related" => $related,
"shareBlog"=>$shareBlog,
]);
}
return abort(404);;
}
public function edit($slug)
{
$blog = Blog::where("slug", $slug)->first();
$this->Authorize('view', $blog);
$this->authorize('update', $blog);
if ($blog) {
return view("blogs.update")->with(["blog" => $blog]);
}
}

public function manage($slug)
{
$blog = Blog::where("slug", $slug)->first();
Expand Down Expand Up @@ -232,13 +158,10 @@ public function tagSearch(Request $request, $slug)
* @param \App\Models\Blog $blog
* @return \Illuminate\Http\Response
*/
public function destroy(Request $request, $id)
public function destroy(Blog $blog)
{
if (auth()->user()->id == $request->get('user_id')) {
$blog = Blog::findOrFail($id);
$blog->delete();
return redirect('/blogs')->with(["deleteSuccess" => "blog deleted successfully."]);
}
return view('error');
$this->authorize('delete', $blog);
$blog->delete();
return redirect('/blogs')->with(["deleteSuccess" => "blog deleted successfully."]);
}
}
6 changes: 3 additions & 3 deletions app/Http/Controllers/PrivateProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ public function index(Request $request)
]);
} else if ($request->tab == "blogs") {
$tab = 'blogs';
$blogs = Blog::where("user_id", "=", auth()->user()->id)->where('status', '=', 'posted')->paginate(5);
$blogs = Blog::where("user_id", "=", auth()->user()->id)->published()->paginate(5);
return view("profile.private.index")->with([
"user" => $user,
"blogs" => $blogs,
"tab" => $tab
]);
} else if ($request->tab == "drafts") {
$tab = 'drafts';
$drafts = Blog::where("user_id", "=", auth()->user()->id)->where('status', '=', 'drafted')->paginate(5);
$drafts = Blog::where("user_id", "=", auth()->user()->id)->unpublished()->paginate(5);
return view("profile.private.index")->with([
"user" => $user,
"drafts" => $drafts,
Expand Down Expand Up @@ -90,7 +90,7 @@ public function index(Request $request)
]);
} else if ($request->tab == 'comments') {
$tab = 'comments';
$comments = Comment::where('user_id', '=', Auth()->user()->id)->get();
$comments = Comment::where('user_id', '=', Auth()->user()->id)->orderBy('created_at','DESC')->paginate(20);
// ->paginate(5);
return view("profile.private.index")->with([
"user" => $user,
Expand Down
23 changes: 1 addition & 22 deletions app/Http/Controllers/PublicProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function index(Request $request, $username)
if ($request->tab == 'blogs') {
$tab = 'blogs';
$pins = BlogPin::where("user_id", "=", $id)->get();
$blogs = Blog::with('user','tags')->where("user_id", "=", $id)->where([['status', '=', 'posted'], ["is_pinned", "=", false]])->paginate(5);
$blogs = Blog::with('user','tags')->where([["user_id", "=", $id],["is_pinned", "=", false]])->published()->paginate(5);
return view("profile.public.index")->with([
"user" => $user,
"pins" => $pins,
Expand Down Expand Up @@ -62,27 +62,6 @@ public function index(Request $request, $username)
}
}
}
// public function detailCard(Request $request)
// {
// $userId = $request->get('userId');

// $user = User::query()->where('id', '=', $userId)
// ->withCount('friendships')->first();
// $friendship = NULL;
// if (Auth::check()) {
// $friendship = Friendship::where([
// ["user_id", "=", $userId],
// ["follower_id", "=", auth()->user()->id]
// ])->count();
// }

// $html = view("profile.public.popover")
// ->with([
// "user" => $user,
// "friendship" => $friendship,
// ])->render();
// return response()->json($html);
// }
/**
* Show the form for creating a new resource.
*
Expand Down
Loading

0 comments on commit 55b5d37

Please sign in to comment.