Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#10 - courses crud #55

Merged
merged 13 commits into from
Oct 4, 2023
39 changes: 39 additions & 0 deletions app/Enums/ClassType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace App\Enums;

enum ClassType: string
{
case LABORATORY = "laboratory";
krzysztofrewak marked this conversation as resolved.
Show resolved Hide resolved
case LECTURE = "lecture";
krzysztofrewak marked this conversation as resolved.
Show resolved Hide resolved
case SEMINAR = "seminar";
case WORKSHOP = "workshop";
case EXERCISES = "exercises";
case PROJECT = "project";

public static function labels(): array
{
return [
"laboratory" => __("laboratory"),
"lecture" => __("lecture"),
"seminar" => __("seminar"),
"workshop" => __("workshop"),
"exercises" => __("exercises"),
"project" => __("project"),
];
}

public static function abbreviationLabels(): array
{
return [
"laboratory" => __("L"),
"lecture" => __("W"),
"seminar" => __("S"),
"workshop" => __("WT"),
"exercises" => __("C"),
"project" => __("P"),
];
}
}
35 changes: 35 additions & 0 deletions app/Enums/Semester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace App\Enums;

enum Semester: string
krzysztofrewak marked this conversation as resolved.
Show resolved Hide resolved
{
case FIRST = "I";
case SECOND = "II";
case THIRD = "III";
case FOURTH = "IV";
case FIFTH = "V";
case SIXTH = "VI";
case SEVENTH = "VII";
case EIGHTH = "VIII";
case NINTH = "IX";
case TENTH = "X";

public static function labels(): array
{
return [
"I" => __("I"),
"II" => __("II"),
"III" => __("III"),
"IV" => __("IV"),
"V" => __("V"),
"VI" => __("VI"),
"VII" => __("VII"),
"VIII" => __("VIII"),
"IX" => __("IX"),
"X" => __("X"),
];
}
}
27 changes: 27 additions & 0 deletions app/Enums/StudyForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace App\Enums;

enum StudyForm: string
{
case STATIONARY = "stationary";
case PART_TIME = "part-time";

public static function labels(): array
{
return [
"stationary" => __("stationary"),
"part-time" => __("part-time"),
];
}

public static function abbreviationLabels(): array
{
return [
"stationary" => __("S"),
"part-time" => __("N"),
krzysztofrewak marked this conversation as resolved.
Show resolved Hide resolved
];
}
}
77 changes: 77 additions & 0 deletions app/Http/Controllers/Dashboard/CourseController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Dashboard;

use App\Enums\ClassType;
use App\Enums\Semester;
use App\Enums\StudyForm;
use App\Http\Controllers\Controller;
use App\Http\Requests\CourseRequest;
use App\Http\Resources\CourseResource;
use App\Models\Course;
use Illuminate\Http\RedirectResponse;
use Inertia\Response;
use Spatie\LaravelOptions\Options;

class CourseController extends Controller
{
public function index(): Response
{
$courses = Course::query()
->orderBy("semester")
->get();

return inertia("Dashboard/Course/Index", [
"courses" => CourseResource::collection($courses),
"total" => Course::query()->count(),
"lastUpdate" => Course::query()->orderByDesc("updated_at")->first()?->updated_at->diffForHumans(),
]);
}

public function create(): Response
{
return inertia("Dashboard/Course/Create", [
"semesters" => Options::forEnum(Semester::class)->toArray(),
"classTypes" => Options::forEnum(ClassType::class)->toArray(),
"studyForms" => Options::forEnum(StudyForm::class)->toArray(),
]);
}

public function store(CourseRequest $request): RedirectResponse
{
Course::query()->create($request->validated());

return redirect()
->route("courses.index")
->with("success", "Dodano kurs");
}

public function edit(Course $course): Response
{
return inertia("Dashboard/Course/Edit", [
"course" => $course,
"semesters" => Options::forEnum(Semester::class)->toArray(),
"classTypes" => Options::forEnum(ClassType::class)->toArray(),
"studyForms" => Options::forEnum(StudyForm::class)->toArray(),
]);
}

public function update(CourseRequest $request, Course $course): RedirectResponse
{
$course->update($request->validated());

return redirect()
->route("courses.index")
->with("success", "Zaktualizowano kurs");
}

public function destroy(Course $course): RedirectResponse
{
$course->delete();

return redirect()->back()
->with("success", "Usunięto kurs");
}
}
26 changes: 26 additions & 0 deletions app/Http/Requests/CourseRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use App\Enums\ClassType;
use App\Enums\Semester;
use App\Enums\StudyForm;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rules\Enum;

class CourseRequest extends FormRequest
{
public function rules(): array
{
return [
"name" => ["required", "max:255"],
"abbreviation" => ["required", "max:255"],
"description" => ["nullable", "max:65000"],
"semester" => ["required", new Enum(Semester::class)],
"type" => ["required", new Enum(ClassType::class)],
"form" => ["required", new Enum(StudyForm::class)],
];
}
}
30 changes: 30 additions & 0 deletions app/Http/Resources/CourseResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace App\Http\Resources;

use App\Enums\ClassType;
use App\Enums\StudyForm;
use App\Models\Course;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class CourseResource extends JsonResource
{
public function toArray(Request $request): array
{
/** @var Course $course */
$course = $this;

return [
"id" => $course->id,
"name" => $course->name,
"abbreviation" => $course->abbreviation,
"description" => $course->description,
"semester" => $course->semester,
"type" => ClassType::labels()[$course->type],
"form" => StudyForm::labels()[$course->form],
];
}
}
45 changes: 45 additions & 0 deletions app/Models/Course.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Stevebauman\Purify\Facades\Purify;

/**
* @property string $id
* @property string $name
* @property string $abbreviation
* @property string $description
* @property string $semester
* @property string $type
* @property string $form
* @property Carbon $created_at
* @property Carbon $updated_at
*/
class Course extends Model
{
use HasFactory;
use HasUlids;

protected $fillable = [
"name",
"abbreviation",
"description",
"semester",
"type",
"form",
];

protected function description(): Attribute
{
return Attribute::make(
set: fn(?string $value): string => Purify::clean($value),
);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"laravel/framework": "^10.13.0",
"laravel/sanctum": "^3.2.5",
"laravel/tinker": "^2.8.1",
"spatie/laravel-options": "^1.1",
"stevebauman/purify": "^6.0"
},
"require-dev": {
Expand Down
Loading