-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* #10 - crud for courses * #10 - wip: added some enums and edit/create view for courses * #10 - added tests * Update app/Enums/ClassType.php Co-authored-by: Krzysztof Rewak <krzysztof.rewak@blumilk.pl> * #10 - cr fixes * #10 - cr fix * #10 - cr fix * #10 - fix * #10 - linter fix * #10 - removed form of study from course * #10 - fixed tests --------- Co-authored-by: Krzysztof Rewak <krzysztof.rewak@blumilk.pl>
- Loading branch information
1 parent
e926d78
commit 65a28f9
Showing
17 changed files
with
720 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
"study-and-work" => __("W"), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
database/migrations/2023_10_03_193906_create_courses_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.