prod #7

Merged
klllst merged 40 commits from feature/task-7 into master 2024-06-16 12:25:52 +04:00
8 changed files with 35 additions and 21 deletions
Showing only changes of commit 92efb4498d - Show all commits

View File

@ -4,6 +4,7 @@ namespace App\Enums;
enum ScoreEnum: string enum ScoreEnum: string
{ {
case WithoutScore = 'Без оценки';
case One = '1'; case One = '1';
case Two = '2'; case Two = '2';
case Three = '3'; case Three = '3';

View File

@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use App\Enums\ScoreEnum; use App\Enums\ScoreEnum;
use App\Models\Lesson; use App\Models\Lesson;
use App\Services\ServiceInterface; use App\Services\ServiceInterface;
use Illuminate\Http\Request;
class ScoreController extends Controller class ScoreController extends Controller
{ {
@ -22,4 +23,11 @@ class ScoreController extends Controller
'scores' => ScoreEnum::cases(), 'scores' => ScoreEnum::cases(),
]); ]);
} }
public function update(Request $request, Lesson $lesson)
{
$this->service->update($lesson, $request->toArray());
return redirect()->route('grades.lessons.show', [$lesson->grade, $lesson]);
}
} }

View File

@ -23,11 +23,6 @@ class Lesson extends Model
'subject_id', 'subject_id',
]; ];
public function scores(): HasMany
{
return $this->hasMany(Score::class);
}
public function grade(): BelongsTo public function grade(): BelongsTo
{ {
return $this->belongsTo(Grade::class); return $this->belongsTo(Grade::class);

View File

@ -2,6 +2,7 @@
namespace App\Services; namespace App\Services;
use App\Enums\ScoreEnum;
use App\Models\Grade; use App\Models\Grade;
use App\Models\Lesson; use App\Models\Lesson;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
@ -22,7 +23,12 @@ class LessonService implements ServiceInterface
public function create(array $data): Lesson public function create(array $data): Lesson
{ {
return Lesson::create($data); $lesson = Lesson::create($data);
$lesson
->students()
->syncWithPivotValues($lesson->grade->students->pluck('id')->all(), ['score' => ScoreEnum::WithoutScore]);
return $lesson;
} }
public function update(Model $model, array $data): Lesson public function update(Model $model, array $data): Lesson

View File

@ -10,21 +10,27 @@ class ScoreService implements ServiceInterface
public function getAll(?Lesson $lesson = null) public function getAll(?Lesson $lesson = null)
{ {
return $lesson->grade->students; return $lesson->students;
} }
public function create(array $data): Model public function update(Model $model, array $data)
{ {
$model->students->each(function ($item, $key) use ($data, $model) {
if ($data['score' . $item->id]) {
$model->students()->syncWithoutDetaching([$item->id => ['score' => $data['score' . $item->id]]]);
} }
});
public function update(Model $model, array $data): Model return $model;
{
// TODO: Implement update() method.
} }
public function delete(Model $model) public function delete(Model $model)
{ {
// TODO: Implement delete() method. // TODO: Implement delete() method.
} }
public function create(array $data)
{
// TODO: Implement create() method.
}
} }

View File

@ -8,9 +8,9 @@ interface ServiceInterface
{ {
public function getAll(); public function getAll();
public function create(array $data): Model; public function create(array $data);
public function update(Model $model, array $data): Model; public function update(Model $model, array $data);
public function delete(Model $model); public function delete(Model $model);
} }

View File

@ -12,10 +12,9 @@ return new class extends Migration
public function up(): void public function up(): void
{ {
Schema::create('lesson_student', function (Blueprint $table) { Schema::create('lesson_student', function (Blueprint $table) {
$table->id();
$table->foreignId('lesson_id')->constrained('lessons')->onDelete('cascade'); $table->foreignId('lesson_id')->constrained('lessons')->onDelete('cascade');
$table->foreignId('student_id')->constrained('lessons')->onDelete('cascade'); $table->foreignId('student_id')->constrained('students')->onDelete('cascade');
$table->unsignedInteger('score'); $table->string('score');
$table->timestamps(); $table->timestamps();
}); });
} }

View File

@ -27,10 +27,9 @@
</td> </td>
<td> <td>
<div> <div>
<select id="score" name="score" class="form-select form-select-sm" required> <select id="score" name="score{{ $student->id }}" class="form-select form-select-sm">
<option value="">Нет оценки</option>
@foreach($scores as $score) @foreach($scores as $score)
<option value="{{ $score }}" {{ isset($student->pivot->score) && $student->pivot->score == $score ? 'selected' : '' }}> <option value="{{ $score }}" {{ isset($student->pivot->score) && $student->pivot->score == $score->value ? 'selected' : '' }}>
{{ $score }} {{ $score }}
</option> </option>
@endforeach @endforeach