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
35 changes: 0 additions & 35 deletions app/Actions/ActivateSemesterAction.php

This file was deleted.

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" => __("ĆW"),
kamilpiech97 marked this conversation as resolved.
Show resolved Hide resolved
"project" => __("P"),
];
}
}
26 changes: 26 additions & 0 deletions app/Enums/StudyForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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 [
"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\StudyForm;
use App\Http\Controllers\Controller;
use App\Http\Requests\CourseRequest;
use App\Http\Resources\CourseResource;
use App\Models\Course;
use App\Models\Semester;
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" => Semester::all(["id", "name"]),
krzysztofrewak marked this conversation as resolved.
Show resolved Hide resolved
"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" => Semester::all(["id", "name"]),
"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");
}
}
15 changes: 4 additions & 11 deletions app/Http/Controllers/Dashboard/SemesterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

namespace App\Http\Controllers\Dashboard;

use App\Actions\ActivateSemesterAction;
use App\Http\Controllers\Controller;
use App\Http\Requests\SemesterRequest;
use App\Models\Semester;
use Exception;
use Illuminate\Http\RedirectResponse;
use Inertia\Response;

Expand Down Expand Up @@ -65,16 +63,11 @@ public function destroy(Semester $semester): RedirectResponse
->with("success", "Usunięto semestr");
}

public function toggleActive(Semester $semester, ActivateSemesterAction $activateSemesterAction): RedirectResponse
public function toggleActive(Semester $semester): RedirectResponse
{
try {
$activateSemesterAction->execute($semester);
$semester->update(["active" => !$semester->active]);

return redirect()->back()
->with("success", "Semestr aktywny");
} catch (Exception $e) {
return redirect()->back()
->with("error", "Wystąpił nieoczekiwany problem");
}
return redirect()->back()
->with("success", "Semestr aktywny");
}
}
25 changes: 25 additions & 0 deletions app/Http/Requests/CourseRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use App\Enums\ClassType;
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", "exists:semesters,name"],
"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
Loading