add Logging to DirectionController

This commit is contained in:
aslan 2024-03-13 11:41:33 +03:00
parent ec57a7bd02
commit 54901c7ed5
1 changed files with 59 additions and 2 deletions

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers\admin\Catalog; namespace App\Http\Controllers\admin\Catalog;
use App\Enums\ExaminationTypeEnum; use App\Enums\ExaminationTypeEnum;
use App\Helpers\SlugHelper;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Requests\admin\Catalog\StoreDirectionRequest; use App\Http\Requests\admin\Catalog\StoreDirectionRequest;
use App\Http\Requests\admin\Catalog\UpdateDirectionRequest; use App\Http\Requests\admin\Catalog\UpdateDirectionRequest;
@ -20,6 +21,8 @@ use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View; use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Application; use Illuminate\Foundation\Application;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
class DirectionController extends Controller class DirectionController extends Controller
{ {
@ -56,12 +59,14 @@ class DirectionController extends Controller
{ {
$validated = $request->validated(); $validated = $request->validated();
$slug = SlugHelper::get($validated);
$direction = new Direction(); $direction = new Direction();
$direction->name = $validated['name']; $direction->name = $validated['name'];
$direction->full_name = "{$validated['code']} {$validated['name']}"; $direction->full_name = "{$validated['code']} {$validated['name']}";
$direction->description = $validated['description']; $direction->description = $validated['description'];
$direction->position = $validated['position']; $direction->position = $validated['position'];
$direction->slug = $validated['slug']; $direction->slug = $slug;
$direction->code = $validated['code']; $direction->code = $validated['code'];
$direction->education_level_id = $validated['education_level_id']; $direction->education_level_id = $validated['education_level_id'];
$direction->education_form_id = $validated['education_form_id']; $direction->education_form_id = $validated['education_form_id'];
@ -92,6 +97,15 @@ class DirectionController extends Controller
$direction->directionProfiles()->attach($validated['direction_profiles']); $direction->directionProfiles()->attach($validated['direction_profiles']);
} }
Log::channel('app')
->info(
'CREATE напр. подготовки {direction} - user {user}',
[
'user' => Auth::user()->name,
'direction' => $direction->name,
'data' => $validated
]
);
return redirect()->route('directions.index'); return redirect()->route('directions.index');
} }
@ -158,7 +172,9 @@ class DirectionController extends Controller
public function update(UpdateDirectionRequest $request, Direction $direction): RedirectResponse public function update(UpdateDirectionRequest $request, Direction $direction): RedirectResponse
{ {
$oldData = $direction->toArray();
$validated = $request->validated(); $validated = $request->validated();
$direction->name = $validated['name']; $direction->name = $validated['name'];
$direction->full_name = "{$validated['code']} {$validated['name']}"; $direction->full_name = "{$validated['code']} {$validated['name']}";
$direction->description = $validated['description']; $direction->description = $validated['description'];
@ -173,6 +189,18 @@ class DirectionController extends Controller
$direction->paid_places = $validated['paid_places']; $direction->paid_places = $validated['paid_places'];
$direction->cost_paid_place = $validated['cost_paid_place']; $direction->cost_paid_place = $validated['cost_paid_place'];
$direction->period = $validated['period']; $direction->period = $validated['period'];
Log::channel('app')
->warning(
'UPDATE напр. подготовки {direction} - user {user}',
[
'user' => Auth::user()->name,
'direction' => $direction->name,
'oldData' => $oldData,
'newData' => $validated
]
);
$direction->save(); $direction->save();
if (array_key_exists('entrance-examination', $validated)) { if (array_key_exists('entrance-examination', $validated)) {
@ -188,7 +216,7 @@ class DirectionController extends Controller
} }
} }
// dd($validated);
if (array_key_exists('delete', $validated)) { if (array_key_exists('delete', $validated)) {
foreach ($validated['delete'] as $id => $value) { foreach ($validated['delete'] as $id => $value) {
$entranceExamination = EntranceExamination::firstOrNew(['id' => $id]); $entranceExamination = EntranceExamination::firstOrNew(['id' => $id]);
@ -207,8 +235,27 @@ class DirectionController extends Controller
public function destroy(Direction $direction): RedirectResponse public function destroy(Direction $direction): RedirectResponse
{ {
if ($direction->entranceExaminations()->exists()) { if ($direction->entranceExaminations()->exists()) {
Log::channel('app')
->error(
'NOT DELETE напр. подготовки {direction} - user {user}',
[
'user' => Auth::user()->name,
'direction' => $direction->name,
'data' => $direction->toArray(),
]
);
return back(); return back();
} }
Log::channel('app')
->critical(
'DELETE напр. подготовки {direction} - user {user}',
[
'user' => Auth::user()->name,
'direction' => $direction->name,
'data' => $direction->toArray(),
]
);
$direction->directionProfiles()->detach(); $direction->directionProfiles()->detach();
$direction->delete(); $direction->delete();
return redirect()->route('directions.index'); return redirect()->route('directions.index');
@ -248,6 +295,16 @@ class DirectionController extends Controller
$newDirection->directionProfiles()->attach($direction->directionProfiles); $newDirection->directionProfiles()->attach($direction->directionProfiles);
Log::channel('app')
->info(
'DUPLICATE напр. подготовки {direction} - user {user}',
[
'user' => Auth::user()->name,
'direction' => $direction->name,
'data' => $direction->toArray(),
]
);
return redirect()->route('directions.index'); return redirect()->route('directions.index');
} }
} }