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";
case Lecture = "lecture";
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"),
];
}
}
30 changes: 30 additions & 0 deletions app/Enums/StudyForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace App\Enums;

enum StudyForm: string
{
case Stationary = "stationary";
case PartTime = "part-time";
case StudyAndWork = "study-and-work";

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

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

declare(strict_types=1);

namespace App\Http\Requests;

use App\Enums\ClassType;
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", "numeric", "min:1", "max:10"],
"type" => ["required", new Enum(ClassType::class)],
];
}
}
28 changes: 28 additions & 0 deletions app/Http/Resources/CourseResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace App\Http\Resources;

use App\Enums\ClassType;
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->getRomanizedSemester(),
"type" => ClassType::labels()[$course->type],
];
}
}
61 changes: 61 additions & 0 deletions app/Models/Course.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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 int $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",
];

public function getRomanizedSemester(): string
{
return match ($this->semester) {
1 => __("I"),
2 => __("II"),
3 => __("III"),
4 => __("IV"),
5 => __("V"),
6 => __("VI"),
7 => __("VII"),
8 => __("VIII"),
9 => __("IX"),
default => __("X"),
};
}

protected function description(): Attribute
{
return Attribute::make(
set: fn(?string $value): string => Purify::clean($value),
);
}
}
22 changes: 22 additions & 0 deletions database/factories/CourseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Enums\ClassType;
use Illuminate\Database\Eloquent\Factories\Factory;

class CourseFactory extends Factory
{
public function definition(): array
{
return [
"name" => fake()->asciify("******"),
"abbreviation" => fake()->asciify("*"),
"description" => fake()->text,
"semester" => fake()->numberBetween(1, 10),
"type" => ClassType::Laboratory->value,
];
}
}
27 changes: 27 additions & 0 deletions database/migrations/2023_10_03_193906_create_courses_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
public function up(): void
{
Schema::create("courses", function (Blueprint $table): void {
$table->ulid("id")->primary();
$table->string("abbreviation");
$table->string("name");
$table->longText("description");
$table->integer("semester");
$table->string("type");
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists("courses");
}
};
11 changes: 11 additions & 0 deletions lang/pl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"lecture": "wykład",
"laboratory": "laboratorium",
"seminar": "seminarium",
"workshop": "warsztat",
"exercises": "ćwiczenia",
"project": "projekt",
"stationary": "stacjonarny",
"part-time": "niestacjonarny",
"study and work": "ucz się i pracuj"
}
2 changes: 2 additions & 0 deletions lang/pl/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,7 @@
"surname" => "nazwisko",
"index_number" => "numer indeksu",
"abbreviation" => "skrótowiec",
"form" => "tryb studiów",
"type" => "typ zajęć",
],
];
2 changes: 1 addition & 1 deletion resources/js/Layouts/DashboardLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const navigation = [
{
title: 'Słowniki',
elements: [
{ name: 'Kursy', href: '#', icon: BriefcaseIcon, current: false },
{ name: 'Kursy', href: '/dashboard/courses', icon: BriefcaseIcon, current: false },
{ name: 'Kierunki i specjalności', href: '/dashboard/fields', icon: MagnifyingGlassIcon, current: false },
{ name: 'Semestry', href: '/dashboard/semesters', icon: ClipboardIcon, current: false },
],
Expand Down
Loading
Loading