Skip to content

Commit

Permalink
Refactor Admin Logic
Browse files Browse the repository at this point in the history
  • Loading branch information
YasserElgammal committed Oct 15, 2023
1 parent 0b6a103 commit 05322c3
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 85 deletions.
29 changes: 13 additions & 16 deletions app/Http/Controllers/Admin/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

namespace App\Http\Controllers\Admin;

use App\Traits\SlugCreater;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\CategoryRequest;
use App\Models\Category;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class CategoryController extends Controller
{
use SlugCreater;
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$categories = Category::with('user')->get();
$categories = Category::with('user:id,name')->get();

return view('admin.category.index', compact('categories'));
}

Expand All @@ -39,10 +41,9 @@ public function create()
*/
public function store(CategoryRequest $request)
{
$validate = $request->validated();
Category::create($validate);
Category::create($request->validated());

return to_route('admin.category.index')->with('message', 'Category Created');
return to_route('admin.category.index')->with('message', trans('admin.category_created'));
}

/**
Expand Down Expand Up @@ -76,10 +77,9 @@ public function edit(Category $category)
*/
public function update(CategoryRequest $request, Category $category)
{
$vaildated = $request->validated();
$category->update($vaildated);
$category->update($request->validated());

return to_route('admin.category.index')->with('message', 'Category Updated');
return to_route('admin.category.index')->with('message', trans('admin.category_updated'));
}

/**
Expand All @@ -91,17 +91,14 @@ public function update(CategoryRequest $request, Category $category)
public function destroy(Category $category)
{
$category->delete();
return to_route('admin.category.index')->with('message', 'Category Deleted !');

return to_route('admin.category.index')->with('message', trans('admin.category_deleted'));
}

public function getSlug(Request $request)
{
$slug = str($request->name)->slug();
if (Category::where('slug', $slug)->exists()) {
$slug = $slug . '-' . Str::random(2);
return response()->json(['slug' => $slug]);
} else {
return response()->json(['slug' => $slug]);
}
$slug = $this->createSlug($request, Category::class);

return response()->json(['slug' => $slug]);
}
}
52 changes: 23 additions & 29 deletions app/Http/Controllers/Admin/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Admin;

use App\Traits\SlugCreater;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\PageRequest;
use Illuminate\Http\Request;
Expand All @@ -10,59 +11,52 @@

class PageController extends Controller
{
public function index(){
use SlugCreater;

public function index()
{
$pages = Page::with('user:id,name')->orderBy('id', 'desc')->paginate(15);

$pages = Page::with('user')->orderBy('id', 'desc')->paginate(15);
return view('admin.page.index', compact('pages'));
}

public function create(){
public function create()
{

return view('admin.page.create');
}

public function store(PageRequest $request){

$page_data = $request->validated();
$page_data['user_id'] = auth()->user()->id;

// dd($page_data);
Page::create($page_data);
public function store(PageRequest $request)
{
Page::create($request->validated());

return to_route('admin.page.index')->with('message', 'Page Created');
return to_route('admin.page.index')->with('message', trans('admin.page_created'));
}

public function update(PageRequest $request, Page $page){

$page_data = $request->validated();
$page_data['user_id'] = auth()->user()->id;

$page->update($page_data);
public function update(PageRequest $request, Page $page)
{
$page->update($request->validated());

return to_route('admin.page.index')->with('message', 'Page Updated');
return to_route('admin.page.index')->with('message', trans('admin.page_updated'));
}

public function edit(Page $page){
public function edit(Page $page)
{

return view('admin.page.edit', compact('page'));
}

public function destroy(Page $page)
{
$page->delete();
return back()->with('message', 'Page Deleted');

return back()->with('message', trans('admin.page_deleted'));
}

public function getSlug(Request $request)
{
$slug = str($request->name)->slug();
if (Page::where('slug', $slug)->exists()) {
$slug = $slug . '-' . Str::random(2);
return response()->json(['slug' => $slug]);
} else {
return response()->json(['slug' => $slug]);
}
}

$slug = $this->createSlug($request, Page::class);

return response()->json(['slug' => $slug]);
}
}
43 changes: 19 additions & 24 deletions app/Http/Controllers/Admin/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
use App\Models\Category;
use App\Models\Post;
use App\Models\Tag;
use App\Traits\SlugCreater;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class PostController extends Controller
{
use SlugCreater;

public function __construct()
{
$this->authorizeResource(Post::class, 'post');
Expand All @@ -24,7 +26,8 @@ public function __construct()
*/
public function index()
{
$posts = Post::with(['category', 'user', 'tags'])->orderByDesc('id')->paginate(15);
$posts = Post::with(['category:id,name', 'user:id,name', 'tags:id,name'])->latest()->paginate(15);

return view('admin.post.index', compact('posts'));
}

Expand All @@ -37,6 +40,7 @@ public function create()
{
$categories = Category::all();
$tags = Tag::all();

return view('admin.post.create', compact('categories', 'tags'));
}

Expand All @@ -55,18 +59,13 @@ public function store(PostRequest $request)
$post_data['image'] = $get_file;
}

$post_data['user_id'] = auth()->user()->id;

// dd($request->tags);
$post = Post::create($post_data);

if ($request->has('tags')) {
// $validate_tags = $request->validate(['tags'=> ['exists:tags,id']]);
$post->tags()->attach($request->tags);
// dd($post);
}

return to_route('admin.post.index')->with('message', 'Post Created');
return to_route('admin.post.index')->with('message', trans('admin.post_created'));
}

/**
Expand All @@ -79,6 +78,7 @@ public function edit(Post $post)
{
$categories = Category::all();
$tags = Tag::all();

return view('admin.post.edit', compact('post', 'categories', 'tags'));
}

Expand All @@ -99,11 +99,10 @@ public function update(PostRequest $request, Post $post)
$post_data['image'] = $get_file;
}

$post_data['user_id'] = auth()->user()->id;
$post->update($post_data);
$post->tags()->sync($request->tags);

return to_route('admin.post.index')->with('message', 'Post Updated');
return to_route('admin.post.index')->with('message', trans('admin.post_updated'));
}

/**
Expand All @@ -118,31 +117,27 @@ public function destroy(Post $post)
Storage::delete($post->image);
}
$post->delete();
return back()->with('message', 'Post Deleted');

return back()->with('message', trans('admin.post_deleted'));
}

public function getSlug(Request $request)
{
$slug = str($request->title)->slug();
if (Post::where('slug', $slug)->exists()) {
$slug = $slug . '-' . Str::random(2);
return response()->json(['slug' => $slug]);
} else {
return response()->json(['slug' => $slug]);
}
$slug = $this->createSlug($request, Post::class);

return response()->json(['slug' => $slug]);
}

public function search(Request $request)
{
$searched_text = $request->input('search');

$posts = Post::query()->with(['category', 'user', 'tags'])
->where('title', 'LIKE', "%{$searched_text}%")
->orWhere('content', 'LIKE', "%{$searched_text}%")
->paginate(10);

// Return the search view with the resluts
return view('admin.post.search', compact('posts'));
->where('title', 'LIKE', "%{$searched_text}%")
->orWhere('content', 'LIKE', "%{$searched_text}%")
->paginate(10);

// Return the search view with the resluts
return view('admin.post.search', compact('posts'));
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Admin/RoleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class RoleController extends Controller
public function index()
{
$roles = Role::all();

return view('admin.role.index', compact('roles'));
}

}
9 changes: 4 additions & 5 deletions app/Http/Controllers/Admin/SettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ public function index()
return view('admin.setting.index', compact('setting'));
}

public function update(UpdateSettingRequest $request, Setting $setting){
public function update(UpdateSettingRequest $request, Setting $setting)
{
$setting->update($request->validated());

$validated = $request->validated();
$setting->update($validated);
// dd($validated);
return back()->with('message','Data Updated !');
return back()->with('message', trans('admin.data_updated'));
}
}
11 changes: 5 additions & 6 deletions app/Http/Controllers/Admin/TagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ public function create()
*/
public function store(TagRequest $request)
{
$validate = $request->validated();
Tag::create($validate);
Tag::create($request->validated());

return to_route('admin.tag.index')->with('message', 'Tag Created');
return to_route('admin.tag.index')->with('message', trans('admin.tag_created'));
}

/**
Expand All @@ -64,10 +63,9 @@ public function edit(Tag $tag)
*/
public function update(TagRequest $request, Tag $tag)
{
$vaildated = $request->validated();
$tag->update($vaildated);
$tag->update($request->validated());

return to_route('admin.tag.index')->with('message', 'Tag Updated');
return to_route('admin.tag.index')->with('message', trans('admin.tag_updated'));
}

/**
Expand All @@ -79,6 +77,7 @@ public function update(TagRequest $request, Tag $tag)
public function destroy(Tag $tag)
{
$tag->delete();

return to_route('admin.tag.index')->with('message', 'Tag Deleted !');
}
}
7 changes: 5 additions & 2 deletions app/Http/Controllers/Admin/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class UserController extends Controller
public function index()
{
$users = User::with('role')->orderBy('id', 'desc')->paginate(15);

return view('admin.user.index', compact('users'));
}

Expand All @@ -31,6 +32,7 @@ public function index()
public function edit(User $user)
{
$roles = Role::all();

return view('admin.user.edit', compact('user', 'roles'));
}

Expand All @@ -47,7 +49,7 @@ public function update(Request $request, User $user)
$user->role_id = $validated['role_id'];
$user->update();

return to_route('admin.user.index')->with('message','Role to user Updated');
return to_route('admin.user.index')->with('message', trans('admin.role_updated'));
}

/**
Expand All @@ -59,6 +61,7 @@ public function update(Request $request, User $user)
public function destroy(User $user)
{
$user->delete();
return to_route('admin.user.index')->with('message', 'User Deleted !');

return to_route('admin.user.index')->with('message', trans('admin.user_deleted'));
}
}
19 changes: 19 additions & 0 deletions app/Observers/PageObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Observers;

use App\Models\Page;
use Illuminate\Support\Facades\Auth;

class PageObserver
{
public function creating(Page $page)
{
$page->user_id = Auth::id();
}

public function updating(Page $page)
{
$page->user_id = Auth::id();
}
}
Loading

0 comments on commit 05322c3

Please sign in to comment.