firstcommit
This commit is contained in:
0
Modules/Testimonial/app/Http/Controllers/.gitkeep
Normal file
0
Modules/Testimonial/app/Http/Controllers/.gitkeep
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Testimonial\app\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Modules\Testimonial\app\Http\Requests\CreateTestimonialRequest;
|
||||
use Modules\Testimonial\app\Repositories\TestimonialRepository;
|
||||
|
||||
class TestimonialController extends Controller
|
||||
{
|
||||
protected $testimonialRepository;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->testimonialRepository = new TestimonialRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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') : [];
|
||||
$testimonials = $this->testimonialRepository->allTestimonials($perPage, $filter);
|
||||
|
||||
return view('testimonial::index', compact('testimonials'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
return view('testimonial::create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(CreateTestimonialRequest $request): RedirectResponse
|
||||
{
|
||||
|
||||
try {
|
||||
$validated = $request->validated();
|
||||
|
||||
$this->testimonialRepository->storeTestimonial($validated);
|
||||
toastr()->success('Testimonial created successfully!');
|
||||
|
||||
return redirect()->route('cms.testimonials.index');
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
toastr()->error('Something went wrong.');
|
||||
|
||||
return back();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
return view('testimonial::show');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($uuid)
|
||||
{
|
||||
$testimonial = $this->testimonialRepository->findTestimonialByUuid($uuid);
|
||||
if (!$testimonial) {
|
||||
toastr()->error('Testimonial not found');
|
||||
return back();
|
||||
}
|
||||
|
||||
return view('testimonial::edit', compact('testimonial'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(CreateTestimonialRequest $request, $uuid): RedirectResponse
|
||||
{
|
||||
|
||||
try {
|
||||
$validated = $request->validated();
|
||||
|
||||
$testimonial = $this->testimonialRepository->updateTestimonial($validated, $uuid);
|
||||
|
||||
if (!$testimonial) {
|
||||
toastr()->error('Testimonial not found !');
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
toastr()->success('Testimonial updated successfully.');
|
||||
|
||||
return redirect()->route('cms.testimonials.index');
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
toastr()->error('Something went wrong.');
|
||||
|
||||
return redirect()->back();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($uuid)
|
||||
{
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$testimonial = $this->testimonialRepository->deleteTestimonial($uuid);
|
||||
if (!$testimonial) {
|
||||
toastr()->error('Testimonial not found');
|
||||
return back();
|
||||
}
|
||||
DB::commit();
|
||||
toastr()->success('Testimonial deleted successfully.');
|
||||
|
||||
return redirect()->route('cms.testimonials.index');
|
||||
} catch (\Throwable $th) {
|
||||
DB::rollback();
|
||||
report($th);
|
||||
|
||||
return back()->error('Something went wrong');
|
||||
}
|
||||
}
|
||||
}
|
0
Modules/Testimonial/app/Http/Middleware/.gitkeep
Normal file
0
Modules/Testimonial/app/Http/Middleware/.gitkeep
Normal file
0
Modules/Testimonial/app/Http/Requests/.gitkeep
Normal file
0
Modules/Testimonial/app/Http/Requests/.gitkeep
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Testimonial\app\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class CreateTestimonialRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'required|string|max:255',
|
||||
'designation' => 'sometimes|nullable',
|
||||
'ordering' => 'required|integer|min:1',
|
||||
'statement' => 'required|string',
|
||||
'link' => 'sometimes|nullable|string',
|
||||
'status' => 'required|in:active,inactive',
|
||||
'image' => 'sometimes|nullable|image|mimes:jpeg,png,jpg,gif',
|
||||
];
|
||||
}
|
||||
|
||||
public function messages()
|
||||
{
|
||||
return [
|
||||
'name.required' => 'The name field is required.',
|
||||
'name.string' => 'The name field must be a string.',
|
||||
'name.max' => 'The name may not be greater than 255 characters.',
|
||||
'ordering.required' => 'The ordering field is required.',
|
||||
'ordering.integer' => 'The ordering field must be an integer.',
|
||||
'ordering.min' => 'The ordering field must be a positive number.',
|
||||
'statement.required' => 'The statement field is required.',
|
||||
'statement.string' => 'The statement field must be a string.',
|
||||
'link.string' => 'The link field must be a string.',
|
||||
'status.required' => 'The status field is required.',
|
||||
'status.in' => 'The status field must be either "active" or "inactive".',
|
||||
'image.image' => 'The image must be an image file.',
|
||||
'image.mimes' => 'The image must be a file of type: jpeg, png, jpg, gif.',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
// return auth()->user()->can('users.create');
|
||||
}
|
||||
}
|
0
Modules/Testimonial/app/Models/.gitkeep
Normal file
0
Modules/Testimonial/app/Models/.gitkeep
Normal file
43
Modules/Testimonial/app/Models/Testimonial.php
Normal file
43
Modules/Testimonial/app/Models/Testimonial.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Testimonial\app\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Modules\Testimonial\Database\factories\TestimonialFactory;
|
||||
|
||||
class Testimonial extends Model
|
||||
{
|
||||
const FILE_PATH = 'uploads/testimonials/';
|
||||
|
||||
// use HasFactory;
|
||||
use SoftDeletes;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected $fillable = [
|
||||
'uuid',
|
||||
'name',
|
||||
'designation',
|
||||
'ordering',
|
||||
'statement',
|
||||
'image',
|
||||
'image_path',
|
||||
'status',
|
||||
'link',
|
||||
];
|
||||
|
||||
|
||||
protected static function newFactory(): TestimonialFactory
|
||||
{
|
||||
//return TestimonialFactory::new();
|
||||
}
|
||||
|
||||
public function getFullImageAttribute()
|
||||
{
|
||||
return $this->image ? asset('storage/' . Self::FILE_PATH . $this->image) : null;
|
||||
}
|
||||
|
||||
}
|
0
Modules/Testimonial/app/Providers/.gitkeep
Normal file
0
Modules/Testimonial/app/Providers/.gitkeep
Normal file
59
Modules/Testimonial/app/Providers/RouteServiceProvider.php
Normal file
59
Modules/Testimonial/app/Providers/RouteServiceProvider.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Testimonial\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\Testimonial\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('Testimonial', '/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('Testimonial', '/routes/api.php'));
|
||||
}
|
||||
}
|
114
Modules/Testimonial/app/Providers/TestimonialServiceProvider.php
Normal file
114
Modules/Testimonial/app/Providers/TestimonialServiceProvider.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Testimonial\app\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Blade;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class TestimonialServiceProvider extends ServiceProvider
|
||||
{
|
||||
protected string $moduleName = 'Testimonial';
|
||||
|
||||
protected string $moduleNameLower = 'testimonial';
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
130
Modules/Testimonial/app/Repositories/TestimonialRepository.php
Normal file
130
Modules/Testimonial/app/Repositories/TestimonialRepository.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Testimonial\app\Repositories;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\Banner\app\Services\FileManagementService;
|
||||
use Modules\Testimonial\app\Models\Testimonial;
|
||||
|
||||
class TestimonialRepository
|
||||
{
|
||||
//-- Retrieve all Services
|
||||
public function allTestimonials($perPage = null, $filter = [], $sort = ['by' => 'id', 'sort' => 'DESC'])
|
||||
{
|
||||
return Testimonial::when(array_keys($filter, true), function ($query) use ($filter) {
|
||||
if (! empty($filter['name'])) {
|
||||
$query->where('name', $filter['name']);
|
||||
}
|
||||
if (! empty($filter['designation'])) {
|
||||
$query->where('designation', 'like', '%'.$filter['designation'].'%');
|
||||
}
|
||||
})
|
||||
->orderBy($sort['by'], $sort['sort'])
|
||||
->paginate($perPage ?: env('PAGE_LIMIT', 999));
|
||||
}
|
||||
|
||||
//-- Find Service by uuid
|
||||
public function findTestimonialByUuid($uuid)
|
||||
{
|
||||
return Testimonial::where('uuid', $uuid)->first();
|
||||
}
|
||||
|
||||
public function storeTestimonial(array $validated)
|
||||
{
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$testimonial = new Testimonial();
|
||||
$testimonial->uuid = Str::uuid();
|
||||
$testimonial->name = $validated['name'];
|
||||
$testimonial->designation = $validated['designation'];
|
||||
$testimonial->ordering = $validated['ordering'];
|
||||
$testimonial->statement = $validated['statement'];
|
||||
$testimonial->statement = $validated['statement'];
|
||||
$testimonial->link = $validated['link'];
|
||||
$testimonial->save();
|
||||
|
||||
if (isset($validated['image']) && $validated['image']->isValid()) {
|
||||
FileManagementService::storeFile(
|
||||
file: $validated['image'],
|
||||
uploadedFolderName: 'testimonials',
|
||||
model: $testimonial
|
||||
);
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
return $testimonial;
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
DB::rollback();
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function updateTestimonial($validated, $uuid)
|
||||
{
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$testimonial = $this->findTestimonialByUuid($uuid);
|
||||
|
||||
if (! $testimonial) {
|
||||
|
||||
return null;
|
||||
}
|
||||
$testimonial->name = $validated['name'];
|
||||
$testimonial->designation = $validated['designation'];
|
||||
$testimonial->ordering = $validated['ordering'];
|
||||
$testimonial->statement = $validated['statement'];
|
||||
$testimonial->link = $validated['link'];
|
||||
$testimonial->status = $validated['status'];
|
||||
$testimonial->save();
|
||||
|
||||
if (isset($validated['image']) && $validated['image']->isValid()) {
|
||||
FileManagementService::uploadFile(
|
||||
file: $validated['image'],
|
||||
uploadedFolderName: 'testimonials',
|
||||
filePath: $testimonial->image_path,
|
||||
model: $testimonial
|
||||
);
|
||||
}
|
||||
|
||||
return $testimonial;
|
||||
DB::commit();
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
DB::rollBack();
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
//-- Delete Testimonial
|
||||
public function deleteTestimonial(string $uuid)
|
||||
{
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$testimonial = $this->findTestimonialByUuid($uuid);
|
||||
if (! $testimonial) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Delete the image file associated with the activity
|
||||
if ($testimonial->image_path !== null) {
|
||||
FileManagementService::deleteFile($testimonial->image_path);
|
||||
}
|
||||
|
||||
$testimonial->delete();
|
||||
|
||||
DB::commit();
|
||||
|
||||
return $testimonial;
|
||||
} catch (\Throwable $th) {
|
||||
DB::rollBack();
|
||||
report($th);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
66
Modules/Testimonial/app/Services/FileManagementService.php
Normal file
66
Modules/Testimonial/app/Services/FileManagementService.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Testimonial\app\Services;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class FileManagementService
|
||||
{
|
||||
//-- store file
|
||||
public static function storeFile($file, $uploadedFolderName, $model)
|
||||
{
|
||||
try {
|
||||
$originalFileName = $file->getClientOriginalName();
|
||||
$modifiedFileName = date('YmdHis') . "_" . uniqid() . "." . $originalFileName;
|
||||
|
||||
$file->storeAs($uploadedFolderName, $modifiedFileName, 'public_uploads'); // This line uses 'public_uploads' disk
|
||||
|
||||
$model->image = $modifiedFileName;
|
||||
$model->image_path = $uploadedFolderName . '/' . $modifiedFileName;
|
||||
$model->save();
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
toastr()->error('Something went wrong.');
|
||||
return redirect()->back();
|
||||
}
|
||||
}
|
||||
|
||||
//-- update file
|
||||
public static function uploadFile($file, $uploadedFolderName ,$filePath, $model)
|
||||
{
|
||||
try {
|
||||
if ($filePath && Storage::disk('public_uploads')->exists($filePath)) {
|
||||
Storage::disk('public_uploads')->delete($filePath);
|
||||
}
|
||||
|
||||
$originalFileName = $file->getClientOriginalName();
|
||||
$modifiedFileName = date('YmdHis') . "_" . uniqid() . "." . $originalFileName;
|
||||
|
||||
$file->storeAs($uploadedFolderName, $modifiedFileName, 'public_uploads'); // This line uses 'public_uploads' disk
|
||||
|
||||
$model->image = $modifiedFileName;
|
||||
$model->image_path = $uploadedFolderName . '/' . $modifiedFileName;
|
||||
|
||||
$model->save();
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
toastr()->error('Something went wrong.');
|
||||
return redirect()->back();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//-- delete file
|
||||
public static function deleteFile($filePath)
|
||||
{
|
||||
try {
|
||||
if ($filePath && Storage::disk('public_uploads')->exists($filePath)) {
|
||||
Storage::disk('public_uploads')->delete($filePath);
|
||||
} else {
|
||||
toastr()->error('File Not wrong.');
|
||||
}
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
toastr()->error('Something went wrong while deleting the file.');
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user