Skip to content

Commit

Permalink
#10 - cr fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilpiech97 committed Oct 3, 2023
1 parent 8c10026 commit 9df27fe
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 32 deletions.
35 changes: 35 additions & 0 deletions app/Actions/ActivateSemesterAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace App\Actions;

use App\Models\Semester;
use Exception;
use Illuminate\Database\ConnectionInterface;

class ActivateSemesterAction
{
public function __construct(
protected ConnectionInterface $db,
) {}

/**
* @throws Exception
*/
public function execute(Semester $semester): void
{
try {
$this->db->beginTransaction();

Semester::getActive()?->update(["active" => 0]);
$semester->update(["active" => 1]);

$this->db->commit();
} catch (Exception $exception) {
$this->db->rollBack();

throw $exception;
}
}
}
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
{
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"),
];
}
}
1 change: 1 addition & 0 deletions app/Enums/StudyForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static function labels(): array
public static function abbreviationLabels(): array
{
return [
"stationary" => __("S"),
"part-time" => __("N"),
];
}
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/Dashboard/CourseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
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 App\Models\Semester;
use Illuminate\Http\RedirectResponse;
use Inertia\Response;
use Spatie\LaravelOptions\Options;
Expand All @@ -33,7 +33,7 @@ public function index(): Response
public function create(): Response
{
return inertia("Dashboard/Course/Create", [
"semesters" => Semester::all(["id", "name"]),
"semesters" => Options::forEnum(Semester::class)->toArray(),
"classTypes" => Options::forEnum(ClassType::class)->toArray(),
"studyForms" => Options::forEnum(StudyForm::class)->toArray(),
]);
Expand All @@ -52,7 +52,7 @@ public function edit(Course $course): Response
{
return inertia("Dashboard/Course/Edit", [
"course" => $course,
"semesters" => Semester::all(["id", "name"]),
"semesters" => Options::forEnum(Semester::class)->toArray(),
"classTypes" => Options::forEnum(ClassType::class)->toArray(),
"studyForms" => Options::forEnum(StudyForm::class)->toArray(),
]);
Expand Down
15 changes: 11 additions & 4 deletions app/Http/Controllers/Dashboard/SemesterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

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 @@ -63,11 +65,16 @@ public function destroy(Semester $semester): RedirectResponse
->with("success", "Usunięto semestr");
}

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

return redirect()->back()
->with("success", "Semestr aktywny");
return redirect()->back()
->with("success", "Semestr aktywny");
} catch (Exception $e) {
return redirect()->back()
->with("error", "Wystąpił nieoczekiwany problem");
}
}
}
3 changes: 2 additions & 1 deletion app/Http/Requests/CourseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand All @@ -17,7 +18,7 @@ public function rules(): array
"name" => ["required", "max:255"],
"abbreviation" => ["required", "max:255"],
"description" => ["nullable", "max:65000"],
"semester" => ["required", "exists:semesters,name"],
"semester" => ["required", new Enum(Semester::class)],
"type" => ["required", new Enum(ClassType::class)],
"form" => ["required", new Enum(StudyForm::class)],
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public function up(): void
$table->string("name");
$table->longText("description");
$table->string("semester");
$table->enum("type", ["lecture", "laboratory", "seminar", "workshop", "exercises", "project"]);
$table->enum("form", ["stationary", "part-time"]);
$table->string("type");
$table->string("form");
$table->timestamps();
});
}
Expand Down
2 changes: 1 addition & 1 deletion resources/js/Pages/Dashboard/Course/Create.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function createCourse() {
<FormLabel for="semester">
Semestr
</FormLabel>
<Select id="semester" v-model="form.semester" :error="form.errors.semester" :options="semesters" item-value="name" />
<Select id="semester" v-model="form.semester" :error="form.errors.semester" :options="semesters" label="label" item-value="value" />
<FormError :error="form.errors.semester" />
</FormGroup>
<FormGroup>
Expand Down
2 changes: 1 addition & 1 deletion resources/js/Pages/Dashboard/Course/Edit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function updateCourse() {
<FormLabel for="semester">
Semestr
</FormLabel>
<Select id="semester" v-model="form.semester" :error="form.errors.semester" :options="semesters" :item-value="'name'" />
<Select id="semester" v-model="form.semester" :error="form.errors.semester" :options="semesters" label="label" item-value="value" />
<FormError :error="form.errors.semester" />
</FormGroup>
<FormGroup>
Expand Down
5 changes: 2 additions & 3 deletions resources/js/Pages/Dashboard/Semester/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ref } from 'vue'
import { Method } from '@inertiajs/inertia'
import ManagementHeader from '@/Shared/Components/ManagementHeader.vue'
import ManagementHeaderItem from '@/Shared/Components/ManagementHeaderItem.vue'
import { Cog6ToothIcon, XCircleIcon, CheckIcon, XMarkIcon } from '@heroicons/vue/24/outline'
import { Cog6ToothIcon, XCircleIcon, CheckIcon } from '@heroicons/vue/24/outline'
defineProps({
semesters: Object,
Expand Down Expand Up @@ -76,8 +76,7 @@ const semesterToDeleteId = ref(0)
<Button v-if="semester.status !== true" :method="Method.POST"
:href="`/dashboard/semesters/${semester.id}/activate`"
>
<XMarkIcon v-if="semester.active" class="w-5" />
<CheckIcon v-else class="w-5" />
<CheckIcon class="w-5" />
</Button>
<Button :href="`/dashboard/semesters/${semester.id}/edit`">
<Cog6ToothIcon class="w-5" />
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
Route::get("/semesters/{semester}/edit", "edit")->name("semesters.edit");
Route::patch("/semesters/{semester}", "update")->name("semesters.update");
Route::delete("/semesters/{semester}", "destroy")->name("semesters.destroy");
Route::post("/semesters/{semester}/toggle-active", "toggleActive")->name("semesters.toggle.active");
Route::post("/semesters/{semester}/activate", "toggleActive")->name("semesters.toggle.active");
});
Route::controller(FieldController::class)->group(function (): void {
Route::get("/fields", "index")->name("fields.index");
Expand Down
10 changes: 4 additions & 6 deletions tests/Feature/CourseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Feature;

use App\Models\Course;
use App\Models\Semester;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Str;
Expand All @@ -20,7 +19,6 @@ protected function setUp(): void
parent::setUp();

$this->user = User::factory()->create();
$this->semester = Semester::factory()->create();
$this->actingAs($this->user);
}

Expand All @@ -29,7 +27,7 @@ public function testCourseCanBeCreated(): void
$this->post("/dashboard/courses", [
"name" => "Course",
"abbreviation" => "C",
"semester" => $this->semester->name,
"semester" => "II",
"type" => "laboratory",
"form" => "stationary",
])->assertSessionHasNoErrors();
Expand All @@ -42,23 +40,23 @@ public function testCourseCanBeUpdated(): void
$this->assertDatabaseMissing("courses", [
"name" => "Course",
"abbreviation" => "C",
"semester" => $this->semester->name,
"semester" => "II",
"type" => "laboratory",
"form" => "stationary",
]);

$this->patch("/dashboard/courses/{$semester->id}", [
"name" => "Course",
"abbreviation" => "C",
"semester" => $this->semester->name,
"semester" => "II",
"type" => "laboratory",
"form" => "stationary",
])->assertSessionHasNoErrors();

$this->assertDatabaseHas("courses", [
"name" => "Course",
"abbreviation" => "C",
"semester" => $this->semester->name,
"semester" => "II",
"type" => "laboratory",
"form" => "stationary",
]);
Expand Down
13 changes: 3 additions & 10 deletions tests/Feature/SemesterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,20 @@ public function testSemesterCanBeSetToActiveStatus(): void
$semester = Semester::factory()->create(["active" => 0]);
$this->assertFalse($semester->active);

$this->post("/dashboard/semesters/{$semester->id}/toggle-active");
$this->post("/dashboard/semesters/{$semester->id}/activate");

$semester->refresh();
$this->assertTrue($semester->active);
}

public function testMoreThanOneSemesterCanBeActive(): void
public function testOnlyOneSemesterCanBeActive(): void
{
$inactiveSemester = Semester::factory()->create(["active" => 0]);
$activeSemester = Semester::factory()->create(["active" => 1]);
$this->assertFalse($inactiveSemester->active);
$this->assertTrue($activeSemester->active);

$this->post("/dashboard/semesters/{$inactiveSemester->id}/toggle-active");

$inactiveSemester->refresh();
$activeSemester->refresh();
$this->assertTrue($inactiveSemester->active);
$this->assertTrue($activeSemester->active);

$this->post("/dashboard/semesters/{$activeSemester->id}/toggle-active");
$this->post("/dashboard/semesters/{$inactiveSemester->id}/activate");

$inactiveSemester->refresh();
$activeSemester->refresh();
Expand Down

0 comments on commit 9df27fe

Please sign in to comment.