add all controllers logic
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ClassSections;
|
||||
|
||||
use App\Models\ClassSection;
|
||||
use App\Models\Configuration;
|
||||
use App\Models\Student;
|
||||
use App\Models\User;
|
||||
|
||||
class ClassAttendanceService
|
||||
{
|
||||
public function getAttendancePayload(int $classSectionId, int $teacherId, ?string $schoolYear = null, ?string $semester = null): array
|
||||
{
|
||||
$schoolYear = $schoolYear ?: (string) (Configuration::getConfig('school_year') ?? '');
|
||||
$semester = $semester ?: (string) (Configuration::getConfig('semester') ?? '');
|
||||
|
||||
$teacher = User::query()->find($teacherId);
|
||||
$teacherName = trim((string) ($teacher?->firstname ?? '') . ' ' . (string) ($teacher?->lastname ?? ''));
|
||||
|
||||
$className = ClassSection::getClassSectionNameBySectionId($classSectionId) ?? (string) $classSectionId;
|
||||
$students = Student::getStudentInfoByClassSectionId($classSectionId, $semester, $schoolYear, $teacherId);
|
||||
|
||||
return [
|
||||
'teacher_name' => $teacherName,
|
||||
'class_name' => $className,
|
||||
'class_section_id' => $classSectionId,
|
||||
'semester' => $semester,
|
||||
'school_year' => $schoolYear,
|
||||
'students' => $students,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ClassSections;
|
||||
|
||||
use App\Models\ClassSection;
|
||||
|
||||
class ClassSectionCommandService
|
||||
{
|
||||
public function create(array $data): ClassSection
|
||||
{
|
||||
return ClassSection::query()->create($data);
|
||||
}
|
||||
|
||||
public function update(ClassSection $section, array $data): ClassSection
|
||||
{
|
||||
$section->fill($data);
|
||||
$section->save();
|
||||
|
||||
return $section->refresh();
|
||||
}
|
||||
|
||||
public function delete(ClassSection $section): bool
|
||||
{
|
||||
return (bool) $section->delete();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ClassSections;
|
||||
|
||||
use App\Models\ClassSection;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
|
||||
class ClassSectionQueryService
|
||||
{
|
||||
public function list(array $filters, int $page = 1, int $perPage = 20): LengthAwarePaginator
|
||||
{
|
||||
$query = ClassSection::query()
|
||||
->leftJoin('classes', 'classSection.class_id', '=', 'classes.id')
|
||||
->select('classSection.*', 'classes.class_name');
|
||||
|
||||
if (!empty($filters['search'])) {
|
||||
$term = '%' . strtolower((string) $filters['search']) . '%';
|
||||
$query->where(function ($q) use ($term) {
|
||||
$q->whereRaw('LOWER(classSection.class_section_name) LIKE ?', [$term])
|
||||
->orWhereRaw('LOWER(classes.class_name) LIKE ?', [$term]);
|
||||
});
|
||||
}
|
||||
|
||||
if (!empty($filters['class_id'])) {
|
||||
$query->where('classSection.class_id', (int) $filters['class_id']);
|
||||
}
|
||||
|
||||
if (!empty($filters['school_year'])) {
|
||||
$query->where('classSection.school_year', (string) $filters['school_year']);
|
||||
}
|
||||
|
||||
if (!empty($filters['semester'])) {
|
||||
$query->where('classSection.semester', (string) $filters['semester']);
|
||||
}
|
||||
|
||||
$sortBy = (string) ($filters['sort_by'] ?? 'class_section_name');
|
||||
$sortDir = strtolower((string) ($filters['sort_dir'] ?? 'asc')) === 'desc' ? 'desc' : 'asc';
|
||||
|
||||
$allowedSorts = [
|
||||
'class_section_name' => 'classSection.class_section_name',
|
||||
'class_section_id' => 'classSection.class_section_id',
|
||||
'class_id' => 'classSection.class_id',
|
||||
'school_year' => 'classSection.school_year',
|
||||
'semester' => 'classSection.semester',
|
||||
'class_name' => 'classes.class_name',
|
||||
];
|
||||
|
||||
$sortColumn = $allowedSorts[$sortBy] ?? $allowedSorts['class_section_name'];
|
||||
$query->orderBy($sortColumn, $sortDir);
|
||||
|
||||
return $query->paginate($perPage, ['*'], 'page', $page);
|
||||
}
|
||||
|
||||
public function find(int $id): ?ClassSection
|
||||
{
|
||||
return ClassSection::query()
|
||||
->leftJoin('classes', 'classSection.class_id', '=', 'classes.id')
|
||||
->select('classSection.*', 'classes.class_name')
|
||||
->where('classSection.id', $id)
|
||||
->first();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ClassSections;
|
||||
|
||||
use App\Models\Configuration;
|
||||
use App\Models\SchoolClass;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ClassSectionSeedService
|
||||
{
|
||||
public function seedDefaults(): int
|
||||
{
|
||||
$semester = (string) (Configuration::getConfig('semester') ?? '');
|
||||
$schoolYear = (string) (Configuration::getConfig('school_year') ?? '');
|
||||
|
||||
$classes = [
|
||||
['class_name' => 'Class 1', 'schedule' => 'Monday 9:00 AM - 10:00 AM', 'capacity' => 30],
|
||||
['class_name' => 'Class 2', 'schedule' => 'Tuesday 9:00 AM - 10:00 AM', 'capacity' => 30],
|
||||
['class_name' => 'Class 3', 'schedule' => 'Wednesday 9:00 AM - 10:00 AM', 'capacity' => 30],
|
||||
['class_name' => 'Class 4', 'schedule' => 'Thursday 9:00 AM - 10:00 AM', 'capacity' => 30],
|
||||
['class_name' => 'Class 5', 'schedule' => 'Friday 9:00 AM - 10:00 AM', 'capacity' => 30],
|
||||
['class_name' => 'Class 6', 'schedule' => 'Monday 10:00 AM - 11:00 AM', 'capacity' => 30],
|
||||
['class_name' => 'Class 7', 'schedule' => 'Tuesday 10:00 AM - 11:00 AM', 'capacity' => 30],
|
||||
['class_name' => 'Class 8', 'schedule' => 'Wednesday 10:00 AM - 11:00 AM', 'capacity' => 30],
|
||||
['class_name' => 'Class 9', 'schedule' => 'Thursday 10:00 AM - 11:00 AM', 'capacity' => 30],
|
||||
['class_name' => 'Youth', 'schedule' => 'Friday 10:00 AM - 11:00 AM', 'capacity' => 30],
|
||||
];
|
||||
|
||||
return DB::transaction(function () use ($classes, $semester, $schoolYear) {
|
||||
$created = 0;
|
||||
foreach ($classes as $class) {
|
||||
$row = SchoolClass::query()->firstOrCreate(
|
||||
[
|
||||
'class_name' => $class['class_name'],
|
||||
'semester' => $semester,
|
||||
'school_year' => $schoolYear,
|
||||
],
|
||||
[
|
||||
'schedule' => $class['schedule'],
|
||||
'capacity' => $class['capacity'],
|
||||
]
|
||||
);
|
||||
|
||||
if ($row->wasRecentlyCreated) {
|
||||
$created++;
|
||||
}
|
||||
}
|
||||
|
||||
return $created;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user