added controller and service

This commit is contained in:
m.zargarov 2024-05-08 12:59:29 +04:00
parent 08f5909a9f
commit 85d7aacb02
5 changed files with 167 additions and 0 deletions

View File

@ -0,0 +1,88 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\LessonPostRequest;
use App\Models\Lesson;
use App\Services\ServiceInterface;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
class LessonController extends Controller
{
public function __construct
(
protected ServiceInterface $service,
){
}
/**
* Display a listing of the resource.
*/
public function index(): View
{
return view('lessons.index', [
'lessons' => $this->service->getAll(),
]);
}
/**
* Show the form for creating a new resource.
*/
public function create(): View
{
return view('lessons.create');
}
/**
* Store a newly created resource in storage.
*/
public function store(LessonPostRequest $request): RedirectResponse
{
return redirect()->route(
'lessons.show',
$this->service->create($request->validated())
);
}
/**
* Display the specified resource.
*/
public function show(Lesson $lesson): View
{
return view('lessons.show', [
'lesson' => $lesson,
]);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Lesson $lesson): View
{
return view('lessons.edit', [
'lesson' => $lesson,
]);
}
/**
* Update the specified resource in storage.
*/
public function update(LessonPostRequest $request, Lesson $lesson): RedirectResponse
{
return redirect()->route(
'lessons.show',
$this->service->update($lesson, $request->validated())
);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Lesson $lesson): RedirectResponse
{
$this->service->delete($lesson);
return redirect()->route('lessons.index');
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Http\Requests;
use App\Enums\TypeLesson;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class LessonPostRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'name' => 'required|string|max:255',
'type' => ['required', Rule::in(TypeLesson::cases())],
'lesson_date' => 'required|date',
'grade_id' => 'required|exists:grades,id',
'teacher_id' => 'required|exists:teachers,id',
'subject_id' => 'required|exists:subjects,id',
];
}
}

View File

@ -15,6 +15,9 @@ class Lesson extends Model
'name',
'type',
'lesson_date',
'grade_id',
'teacher_id',
'subject_id',
];
public function scores(): HasMany

View File

@ -5,6 +5,7 @@ namespace App\Providers;
use App\Http\Controllers\GradeController;
use App\Http\Controllers\GradeSubjectController;
use App\Http\Controllers\GradeTeacherController;
use App\Http\Controllers\LessonController;
use App\Http\Controllers\StudentController;
use App\Http\Controllers\SubjectController;
use App\Http\Controllers\SubjectTeacherController;
@ -12,6 +13,7 @@ use App\Http\Controllers\TeacherController;
use App\Services\GradeService;
use App\Services\GradeSubjectService;
use App\Services\GradeTeacherService;
use App\Services\LessonService;
use App\Services\ServiceInterface;
use App\Services\StudentService;
use App\Services\SubjectService;
@ -67,6 +69,12 @@ class ModelServiceProvider extends ServiceProvider
->give(function () {
return new GradeTeacherService();
});
$this->app->when(LessonController::class)
->needs(ServiceInterface::class)
->give(function () {
return new LessonService();
});
}
/**

View File

@ -0,0 +1,33 @@
<?php
namespace App\Services;
use App\Models\Lesson;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
class LessonService implements ServiceInterface
{
public function getAll(): Collection
{
return Lesson::all();
}
public function create(array $data): Lesson
{
return Lesson::create($data);
}
public function update(Model $model, array $data): Lesson
{
$model->update($data);
return $model;
}
public function delete($model): void
{
$model->delete();
}
}