firstcommit
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Transformation\app\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Modules\Transformation\app\Http\Requests\TransformationRequest;
|
||||
use Modules\Transformation\app\Repositories\TransformationRepository;
|
||||
|
||||
class TransformationController extends Controller
|
||||
{
|
||||
protected $transformationRepo;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->transformationRepo = new TransformationRepository();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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['transformations'] = $this->transformationRepo->findAll($perPage, $filter);
|
||||
|
||||
return view('transformation::index', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$data = [];
|
||||
|
||||
return view('transformation::create', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(TransformationRequest $request): RedirectResponse
|
||||
{
|
||||
try {
|
||||
$validated = $request->validated();
|
||||
|
||||
$this->transformationRepo->create($validated);
|
||||
toastr()->success('Data created successfully.');
|
||||
|
||||
return redirect()->route('transformation.index');
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
toastr()->error('Something went wrong.');
|
||||
|
||||
return back();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
return view('transformation::show');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
$data['transformation'] = $this->transformationRepo->findById($id);
|
||||
if (!$data['transformation']) {
|
||||
toastr()->error('transformation not found');
|
||||
return back();
|
||||
}
|
||||
|
||||
return view('transformation::edit', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update($id, TransformationRequest $request): RedirectResponse
|
||||
{
|
||||
try {
|
||||
$validated = $request->validated();
|
||||
$transformation = $this->transformationRepo->update($id, $validated);
|
||||
if (!$transformation) {
|
||||
toastr()->error('transformation not found');
|
||||
return back();
|
||||
}
|
||||
toastr()->success('Data updated successfully.');
|
||||
|
||||
return redirect()->route('transformation.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 {
|
||||
$transformation = $this->transformationRepo->delete($id);
|
||||
if (!$transformation) {
|
||||
toastr()->error('transformation 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/Transformation/app/Http/Middleware/.gitkeep
Normal file
0
Modules/Transformation/app/Http/Middleware/.gitkeep
Normal file
0
Modules/Transformation/app/Http/Requests/.gitkeep
Normal file
0
Modules/Transformation/app/Http/Requests/.gitkeep
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Transformation\app\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class TransformationRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'before' => 'sometimes|nullable|image|mimes:jpeg,png,jpg,gif',
|
||||
'after' => 'sometimes|nullable|image|mimes:jpeg,png,jpg,gif',
|
||||
// 'name' => 'required|nullable|string|max:255|regex:/^(?![ .]+$)(?!\.)(?!.*[. ])[a-zA-Z. ]+$/',
|
||||
'grade' => 'sometimes|nullable|string',
|
||||
'graft' => 'sometimes|nullable|string'
|
||||
];
|
||||
}
|
||||
|
||||
public function messages()
|
||||
{
|
||||
return [
|
||||
'before.image' => 'The before image must be an image file.',
|
||||
'before.mimes' => 'The before image must be a file of type: jpeg, png, jpg, gif.',
|
||||
|
||||
'after.image' => 'The after image must be an image file.',
|
||||
'after.mimes' => 'The after image must be a file of type: jpeg, png, jpg, gif.',
|
||||
|
||||
// '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.',
|
||||
// 'name.regex' => 'The name field only accepts letters, spaces, and dots.',
|
||||
|
||||
'grade.string' => 'The grade field must be a string.',
|
||||
|
||||
'graft.string' => 'The graft field must be a string.',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
0
Modules/Transformation/app/Models/.gitkeep
Normal file
0
Modules/Transformation/app/Models/.gitkeep
Normal file
46
Modules/Transformation/app/Models/Transformation.php
Normal file
46
Modules/Transformation/app/Models/Transformation.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Transformation\app\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Transformation extends Model
|
||||
{
|
||||
const FILE_PATH = 'uploads/transformations/';
|
||||
|
||||
protected $fillable = [
|
||||
'before',
|
||||
'after',
|
||||
'name',
|
||||
'grade',
|
||||
'graft'
|
||||
];
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function getBeforeImageAttribute()
|
||||
{
|
||||
$result = null;
|
||||
|
||||
if($this->before) {
|
||||
$result = asset('storage/' . Self::FILE_PATH . $this->before);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function getAfterImageAttribute()
|
||||
{
|
||||
$result = null;
|
||||
|
||||
if($this->after) {
|
||||
$result = asset('storage/' . Self::FILE_PATH . $this->after);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
0
Modules/Transformation/app/Providers/.gitkeep
Normal file
0
Modules/Transformation/app/Providers/.gitkeep
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Transformation\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\Transformation\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('Transformation', '/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('Transformation', '/routes/api.php'));
|
||||
}
|
||||
}
|
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Transformation\app\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Blade;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class TransformationServiceProvider extends ServiceProvider
|
||||
{
|
||||
protected string $moduleName = 'Transformation';
|
||||
|
||||
protected string $moduleNameLower = 'transformation';
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Transformation\app\Repositories;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Modules\Transformation\app\Models\Transformation;
|
||||
|
||||
class TransformationRepository
|
||||
{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function findAll($perPage = null, $filter = [], $sort = ['by' => 'id', 'sort' => 'DESC'])
|
||||
{
|
||||
return Transformation::when(array_keys($filter, true), function ($query) use ($filter) {
|
||||
if (!empty($filter['name'])) {
|
||||
$query->where('name', 'like', '%' . $filter['name'] . '%');
|
||||
}
|
||||
})
|
||||
->orderBy($sort['by'], $sort['sort'])
|
||||
->paginate($perPage ?: env('PAGE_LIMIT', 999));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function findById($id)
|
||||
{
|
||||
return Transformation::where('id', $id)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function create(array $data)
|
||||
{
|
||||
$result = false;
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
if (isset($data['before'])) {
|
||||
$data['before'] = $this->upload($data['before']);
|
||||
}
|
||||
if (isset($data['after'])) {
|
||||
$data['after'] = $this->upload($data['after']);
|
||||
}
|
||||
|
||||
$result = Transformation::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);
|
||||
|
||||
$oldBefore = $model->before;
|
||||
$oldAfter = $model->after;
|
||||
|
||||
if (isset($data['before'])) {
|
||||
if (!is_null($oldBefore)) {
|
||||
$this->remove($oldBefore);
|
||||
}
|
||||
$data['before'] = $this->upload($data['before']);
|
||||
}
|
||||
|
||||
if (isset($data['after'])) {
|
||||
if (!is_null($oldAfter)) {
|
||||
$this->remove($oldAfter);
|
||||
}
|
||||
$data['after'] = $this->upload($data['after']);
|
||||
}
|
||||
|
||||
$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);
|
||||
$oldBefore = $model->before;
|
||||
$oldAfter = $model->after;
|
||||
|
||||
$result = $model->delete();
|
||||
if ($result) {
|
||||
$this->remove($oldBefore);
|
||||
$this->remove($oldAfter);
|
||||
DB::commit();
|
||||
}
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
DB::rollback();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function upload($file)
|
||||
{
|
||||
$fileExtension = $file->getClientOriginalExtension();
|
||||
$fileName = 'IMG' . time() . '_' . uniqid() . '.' . $fileExtension;
|
||||
$file->move(storage_path() . '/app/public/' . Transformation::FILE_PATH, $fileName);
|
||||
|
||||
return $fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function remove($fileName)
|
||||
{
|
||||
$fullFilePath = storage_path() . '/app/public/' . Transformation::FILE_PATH . $fileName;
|
||||
if (file_exists($fullFilePath)) {
|
||||
unlink($fullFilePath);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user