firstcommit
This commit is contained in:
0
Modules/Post/app/Http/Controllers/.gitkeep
Normal file
0
Modules/Post/app/Http/Controllers/.gitkeep
Normal file
131
Modules/Post/app/Http/Controllers/PostController.php
Normal file
131
Modules/Post/app/Http/Controllers/PostController.php
Normal file
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Post\app\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Modules\Page\app\Models\Page;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Modules\Post\app\Http\Requests\PostRequest;
|
||||
use Modules\Post\app\Repositories\PostRepository;
|
||||
|
||||
class PostController extends Controller
|
||||
{
|
||||
protected $postRepo;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->postRepo = new PostRepository();
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$perPage = $request->has('per-page') ? $request->input('per-page') : null;
|
||||
$filter = $request->has('filter') ? $request->input('filter') : [];
|
||||
$data['posts'] = $this->postRepo->findAll($perPage, $filter);
|
||||
|
||||
return view('post::index', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$data['pageList'] = Page::pluck('title', 'id');
|
||||
|
||||
return view('post::create', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(PostRequest $request): RedirectResponse
|
||||
{
|
||||
try {
|
||||
$validated = $request->validated();
|
||||
|
||||
$this->postRepo->create($validated);
|
||||
toastr()->success('Data created successfully.');
|
||||
|
||||
return redirect()->route('post.index');
|
||||
} catch (\Throwable $th) {
|
||||
throw $th;
|
||||
report($th);
|
||||
toastr()->error('Something went wrong.');
|
||||
|
||||
return back();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
return view('post::show');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
$data['pageList'] = Page::pluck('title', 'id');
|
||||
$data['post'] = $this->postRepo->findById($id);
|
||||
if (!$data['post']) {
|
||||
toastr()->success('post not found');
|
||||
return back();
|
||||
}
|
||||
|
||||
return view('post::edit', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update($id, PostRequest $request): RedirectResponse
|
||||
{
|
||||
try {
|
||||
$validated = $request->validated();
|
||||
$post = $this->postRepo->update($id, $validated);
|
||||
if (! $post) {
|
||||
toastr()->success('post not found');
|
||||
return back();
|
||||
}
|
||||
toastr()->success('Data updated successfully.');
|
||||
|
||||
return redirect()->route('post.index');
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
toastr()->error('Oops! Something went wrong.');
|
||||
|
||||
return redirect()->back();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
try {
|
||||
$post = $this->postRepo->delete($id);
|
||||
if (! $post) {
|
||||
toastr()->error('post not found');
|
||||
|
||||
return back();
|
||||
}
|
||||
toastr()->success('Data deleted successfully.');
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
toastr()->error('Oops! Something went wrong.');
|
||||
}
|
||||
|
||||
return redirect()->back();
|
||||
}
|
||||
}
|
0
Modules/Post/app/Http/Middleware/.gitkeep
Normal file
0
Modules/Post/app/Http/Middleware/.gitkeep
Normal file
0
Modules/Post/app/Http/Requests/.gitkeep
Normal file
0
Modules/Post/app/Http/Requests/.gitkeep
Normal file
59
Modules/Post/app/Http/Requests/PostRequest.php
Normal file
59
Modules/Post/app/Http/Requests/PostRequest.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Post\app\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class PostRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'slug' => 'nullable|string',
|
||||
'image' => 'nullable|image|mimes:jpeg,png,jpg,gif',
|
||||
'title' => 'nullable|string|max:255',
|
||||
'short_detail' => 'nullable|string',
|
||||
'full_detail' => 'nullable|string',
|
||||
'page_id' => 'nullable|integer',
|
||||
'order' => 'nullable|integer',
|
||||
'sidebar_flag' => 'nullable|integer',
|
||||
'navbar_flag' => 'nullable|integer',
|
||||
'meta_title' => 'nullable',
|
||||
'meta_description' => 'nullable',
|
||||
'meta_keywords' => 'nullable'
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
public function messages()
|
||||
{
|
||||
return [
|
||||
'slug.string' => 'The slug field must be a string.',
|
||||
|
||||
'image.image' => 'The image must be an image file.',
|
||||
'image.mimes' => 'The image must be a file of type: jpeg, png, jpg, gif.',
|
||||
|
||||
'title.string' => 'The title field must be a string.',
|
||||
'title.max' => 'The title may not be greater than 255 characters.',
|
||||
|
||||
'short_detail.string' => 'The short detail field must be a string.',
|
||||
|
||||
'full_detail.string' => 'The full detail field must be a string.',
|
||||
|
||||
'page_id.integer' => 'The page ID field must be an integer.',
|
||||
|
||||
'order.integer' => 'The order field must be an integer.',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
0
Modules/Post/app/Models/.gitkeep
Normal file
0
Modules/Post/app/Models/.gitkeep
Normal file
51
Modules/Post/app/Models/Post.php
Normal file
51
Modules/Post/app/Models/Post.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Post\app\Models;
|
||||
|
||||
use Modules\Page\app\Models\Page;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Post extends Model
|
||||
{
|
||||
const FILE_PATH = 'uploads/posts/';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected $fillable = [
|
||||
'slug',
|
||||
'image',
|
||||
'title',
|
||||
'short_detail',
|
||||
'full_detail',
|
||||
'page_id',
|
||||
'sidebar_flag',
|
||||
'navbar_flag',
|
||||
'order',
|
||||
'meta_title',
|
||||
'meta_description',
|
||||
'meta_keywords'
|
||||
];
|
||||
|
||||
/**
|
||||
* Relation with page
|
||||
*/
|
||||
public function page()
|
||||
{
|
||||
return $this->belongsTo(Page::class, 'page_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to get full image path
|
||||
*/
|
||||
public function getFullImageAttribute()
|
||||
{
|
||||
$result = null;
|
||||
|
||||
if($this->image) {
|
||||
$result = asset('storage/' . Self::FILE_PATH . $this->image);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
0
Modules/Post/app/Providers/.gitkeep
Normal file
0
Modules/Post/app/Providers/.gitkeep
Normal file
114
Modules/Post/app/Providers/PostServiceProvider.php
Normal file
114
Modules/Post/app/Providers/PostServiceProvider.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Post\app\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Blade;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class PostServiceProvider extends ServiceProvider
|
||||
{
|
||||
protected string $moduleName = 'Post';
|
||||
|
||||
protected string $moduleNameLower = 'post';
|
||||
|
||||
/**
|
||||
* Boot the application events.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
$this->registerCommands();
|
||||
$this->registerCommandSchedules();
|
||||
$this->registerTranslations();
|
||||
$this->registerConfig();
|
||||
$this->registerViews();
|
||||
$this->loadMigrationsFrom(module_path($this->moduleName, 'database/migrations'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the service provider.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
$this->app->register(RouteServiceProvider::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register commands in the format of Command::class
|
||||
*/
|
||||
protected function registerCommands(): void
|
||||
{
|
||||
// $this->commands([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register command Schedules.
|
||||
*/
|
||||
protected function registerCommandSchedules(): void
|
||||
{
|
||||
// $this->app->booted(function () {
|
||||
// $schedule = $this->app->make(Schedule::class);
|
||||
// $schedule->command('inspire')->hourly();
|
||||
// });
|
||||
}
|
||||
|
||||
/**
|
||||
* Register translations.
|
||||
*/
|
||||
public function registerTranslations(): void
|
||||
{
|
||||
$langPath = resource_path('lang/modules/'.$this->moduleNameLower);
|
||||
|
||||
if (is_dir($langPath)) {
|
||||
$this->loadTranslationsFrom($langPath, $this->moduleNameLower);
|
||||
$this->loadJsonTranslationsFrom($langPath);
|
||||
} else {
|
||||
$this->loadTranslationsFrom(module_path($this->moduleName, 'lang'), $this->moduleNameLower);
|
||||
$this->loadJsonTranslationsFrom(module_path($this->moduleName, 'lang'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register config.
|
||||
*/
|
||||
protected function registerConfig(): void
|
||||
{
|
||||
$this->publishes([module_path($this->moduleName, 'config/config.php') => config_path($this->moduleNameLower.'.php')], 'config');
|
||||
$this->mergeConfigFrom(module_path($this->moduleName, 'config/config.php'), $this->moduleNameLower);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register views.
|
||||
*/
|
||||
public function registerViews(): void
|
||||
{
|
||||
$viewPath = resource_path('views/modules/'.$this->moduleNameLower);
|
||||
$sourcePath = module_path($this->moduleName, 'resources/views');
|
||||
|
||||
$this->publishes([$sourcePath => $viewPath], ['views', $this->moduleNameLower.'-module-views']);
|
||||
|
||||
$this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), $this->moduleNameLower);
|
||||
|
||||
$componentNamespace = str_replace('/', '\\', config('modules.namespace').'\\'.$this->moduleName.'\\'.config('modules.paths.generator.component-class.path'));
|
||||
Blade::componentNamespace($componentNamespace, $this->moduleNameLower);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the services provided by the provider.
|
||||
*/
|
||||
public function provides(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
private function getPublishableViewPaths(): array
|
||||
{
|
||||
$paths = [];
|
||||
foreach (config('view.paths') as $path) {
|
||||
if (is_dir($path.'/modules/'.$this->moduleNameLower)) {
|
||||
$paths[] = $path.'/modules/'.$this->moduleNameLower;
|
||||
}
|
||||
}
|
||||
|
||||
return $paths;
|
||||
}
|
||||
}
|
59
Modules/Post/app/Providers/RouteServiceProvider.php
Normal file
59
Modules/Post/app/Providers/RouteServiceProvider.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Post\app\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
|
||||
|
||||
class RouteServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* The module namespace to assume when generating URLs to actions.
|
||||
*/
|
||||
protected string $moduleNamespace = 'Modules\Post\app\Http\Controllers';
|
||||
|
||||
/**
|
||||
* Called before routes are registered.
|
||||
*
|
||||
* Register any model bindings or pattern based filters.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the routes for the application.
|
||||
*/
|
||||
public function map(): void
|
||||
{
|
||||
$this->mapApiRoutes();
|
||||
|
||||
$this->mapWebRoutes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the "web" routes for the application.
|
||||
*
|
||||
* These routes all receive session state, CSRF protection, etc.
|
||||
*/
|
||||
protected function mapWebRoutes(): void
|
||||
{
|
||||
Route::middleware('web')
|
||||
->namespace($this->moduleNamespace)
|
||||
->group(module_path('Post', '/routes/web.php'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the "api" routes for the application.
|
||||
*
|
||||
* These routes are typically stateless.
|
||||
*/
|
||||
protected function mapApiRoutes(): void
|
||||
{
|
||||
Route::prefix('api')
|
||||
->middleware('api')
|
||||
->namespace($this->moduleNamespace)
|
||||
->group(module_path('Post', '/routes/api.php'));
|
||||
}
|
||||
}
|
150
Modules/Post/app/Repositories/PostRepository.php
Normal file
150
Modules/Post/app/Repositories/PostRepository.php
Normal file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Post\app\Repositories;
|
||||
|
||||
use Modules\Post\app\Models\Post;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class PostRepository
|
||||
{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function findAll($perPage=null, $filter=[], $sort=['by'=>'id', 'sort'=>'DESC'])
|
||||
{
|
||||
return Post::when(array_keys($filter, true), function ($query) use ($filter) {
|
||||
if (!empty($filter['page_id'])) {
|
||||
$query->where('page_id', $filter['page_id']);
|
||||
}
|
||||
})
|
||||
->orderBy($sort['by'], $sort['sort'])
|
||||
->paginate($perPage ?: env('PAGE_LIMIT', 999));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function findBySlug($slug)
|
||||
{
|
||||
return Post::where('slug', $slug)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function findById($id)
|
||||
{
|
||||
return Post::where('id', $id)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function create(array $data)
|
||||
{
|
||||
$result = false;
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
if(isset($data['image'])) {
|
||||
$data['image'] = $this->upload($data['image']);
|
||||
}
|
||||
|
||||
$result = Post::create($data);
|
||||
if($result) {
|
||||
DB::commit();
|
||||
}
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
DB::rollback();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function update($id, array $data)
|
||||
{
|
||||
$result = false;
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
$model = $this->findById($id);
|
||||
$oldImage = $model->image;
|
||||
|
||||
if(isset($data['image'])) {
|
||||
if (!is_null($oldImage)) {
|
||||
$this->remove($oldImage);
|
||||
}
|
||||
$data['image'] = $this->upload($data['image']);
|
||||
}
|
||||
|
||||
$result = $model->update($data);
|
||||
if($result) {
|
||||
DB::commit();
|
||||
}
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
DB::rollback();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
$result = false;
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
$model = $this->findById($id);
|
||||
$oldImage = $model->image;
|
||||
|
||||
$result = $model->delete();
|
||||
if($result) {
|
||||
$this->remove($oldImage);
|
||||
DB::commit();
|
||||
}
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
DB::rollback();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function upload($file)
|
||||
{
|
||||
$fileExtension = $file->getClientOriginalExtension();
|
||||
$fileName = 'IMG' . time() . '.' . $fileExtension;
|
||||
$file->move(storage_path() . '/app/public/' . Post::FILE_PATH, $fileName);
|
||||
|
||||
return $fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function remove($fileName)
|
||||
{
|
||||
$fullFilePath = storage_path() . '/app/public/' . Post::FILE_PATH . $fileName;
|
||||
if (file_exists($fullFilePath)) {
|
||||
unlink($fullFilePath);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user