Skip to content

Commit

Permalink
#8 - semester crud
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilpiech97 committed Sep 23, 2023
1 parent ddbb902 commit 967cad3
Show file tree
Hide file tree
Showing 17 changed files with 448 additions and 3 deletions.
1 change: 0 additions & 1 deletion .env.ci
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ DB_PORT=5432
DB_DATABASE=keating
DB_USERNAME=keating
DB_PASSWORD=password
DB_ROOT_PASSWORD=example
19 changes: 19 additions & 0 deletions app/Enums/SemesterStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace App\Enums;

enum SemesterStatus: string
{
case ACTIVE = "active";
case INACTIVE = "inactive";

public function getLabel(): string
{
return match ($this->value) {
"active" => "aktywny",
"inactive" => "nieaktywny",
};
}
}
62 changes: 62 additions & 0 deletions app/Http/Controllers/Dashboard/SemesterController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Http\Requests\SemesterRequest;
use App\Http\Resources\SemesterResource;
use App\Models\Semester;
use Illuminate\Http\RedirectResponse;
use Inertia\Response;

class SemesterController extends Controller
{
public function index(): Response
{
$semesters = Semester::all();

return inertia("Dashboard/Semester/Index", [
"semesters" => SemesterResource::collection($semesters),
]);
}

public function create(): Response
{
return inertia("Dashboard/Semester/Create");
}

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

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

public function edit(Semester $semester): Response
{
return inertia("Dashboard/Semester/Edit", [
"semester" => $semester,
]);
}

public function update(SemesterRequest $request, Semester $semester): RedirectResponse
{
$semester->update($request->validated());

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

public function destroy(Semester $semester): RedirectResponse
{
$semester->delete();

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

declare(strict_types=1);

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class SemesterRequest extends FormRequest
{
public function rules(): array
{
return [
"name" => ["required", "max:255"],
];
}
}
24 changes: 24 additions & 0 deletions app/Http/Resources/SemesterResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace App\Http\Resources;

use App\Models\Semester;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class SemesterResource extends JsonResource
{
public function toArray(Request $request): array
{
/** @var Semester $semester */
$semester = $this;

return [
"name" => $semester->name,
"status" => $semester->status,
"status_label" => $semester->status->getLabel(),
];
}
}
32 changes: 32 additions & 0 deletions app/Models/Semester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Models;

use App\Enums\SemesterStatus;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
* @property string $id
* @property string $name
* @property SemesterStatus $status
* @property Carbon $created_at
* @property Carbon $updated_at
*/
class Semester extends Model
{
use HasFactory;
use HasUlids;

protected $fillable = [
"name",
"status",
];
protected $casts = [
"status" => SemesterStatus::class,
];
}
9 changes: 9 additions & 0 deletions app/Models/Student.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
* @property string $id
* @property string $name
* @property string $surname
* @property string $index_number
* @property Carbon $created_at
* @property Carbon $updated_at
*/
class Student extends Model
{
use HasFactory;
Expand Down
6 changes: 6 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

/**
* @property string $id
* @property string $name
* @property string $email
* @property string $password
*/
class User extends Authenticatable
{
use HasApiTokens;
Expand Down
17 changes: 17 additions & 0 deletions database/factories/SemesterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

class SemesterFactory extends Factory
{
public function definition(): array
{
return [
"name" => "Semestr" . fake()->numberBetween(1, 7),
];
}
}
24 changes: 24 additions & 0 deletions database/migrations/2023_09_23_075139_create_semesters_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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("semesters", function (Blueprint $table): void {
$table->ulid("id")->primary();
$table->string("name");
$table->enum("status", ["active", "inactive"])->default("inactive");
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists("semesters");
}
};
2 changes: 1 addition & 1 deletion resources/js/Layouts/DashboardLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const navigation = [
elements: [
{ name: 'Kursy', href: '#', icon: BriefcaseIcon, current: false },
{ name: 'Kierunki i specjalności', href: '#', icon: MagnifyingGlassIcon, current: false },
{ name: 'Semestry', href: '#', icon: ClipboardIcon, current: false },
{ name: 'Semestry', href: '/dashboard/semesters', icon: ClipboardIcon, current: false },
{ name: 'Formy zajęć', href: '#', icon: CodeBracketSquareIcon, current: false },
{ name: 'Tryby studiów', href: '#', icon: ClockIcon, current: false },
],
Expand Down
48 changes: 48 additions & 0 deletions resources/js/Pages/Dashboard/Semester/Create.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<script setup>
import DashboardLayout from '@/Layouts/DashboardLayout.vue'
import Section from '@/Shared/Components/Section.vue'
import SubmitButton from '@/Shared/Components/Buttons/SubmitButton.vue'
import FormGroup from '@/Shared/Forms/FormGroup.vue'
import FormLabel from '@/Shared/Forms/FormLabel.vue'
import TextInput from '@/Shared/Forms/TextInput.vue'
import SecondaryButton from '@/Shared/Components/Buttons/SecondaryButton.vue'
import { useForm } from '@inertiajs/inertia-vue3'
import FormError from '@/Shared/Forms/FormError.vue'
const form = useForm({
name: '',
})
function createSemester() {
form.post('/dashboard/semesters')
}
</script>

<template>
<DashboardLayout>
<h3 class="text-base font-semibold leading-6 text-gray-900">
Dodawanie semestru
</h3>
<form @submit.prevent="createSemester">
<Section class="mt-3">
<div class="flex justify-between">
<FormGroup>
<FormLabel for="name">
Nazwa
</FormLabel>
<TextInput id="name" v-model="form.name" :error="form.errors.name" />
<FormError :error="form.errors.name" class="mt-2" />
</FormGroup>
</div>
<div class="flex justify-end space-x-3 py-3">
<SecondaryButton href="/dashboard/semesters">
Cofnij
</SecondaryButton>
<SubmitButton>
Utwórz
</SubmitButton>
</div>
</Section>
</form>
</DashboardLayout>
</template>
52 changes: 52 additions & 0 deletions resources/js/Pages/Dashboard/Semester/Edit.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<script setup>
import DashboardLayout from '@/Layouts/DashboardLayout.vue'
import Section from '@/Shared/Components/Section.vue'
import SubmitButton from '@/Shared/Components/Buttons/SubmitButton.vue'
import FormGroup from '@/Shared/Forms/FormGroup.vue'
import FormLabel from '@/Shared/Forms/FormLabel.vue'
import TextInput from '@/Shared/Forms/TextInput.vue'
import SecondaryButton from '@/Shared/Components/Buttons/SecondaryButton.vue'
import { useForm } from '@inertiajs/inertia-vue3'
import FormError from '@/Shared/Forms/FormError.vue'
const props = defineProps({
semester: Object,
})
const form = useForm({
name: props.semester.name,
})
function updateSemester() {
form.patch(`/dashboard/semesters/${props.semester.id}`)
}
</script>

<template>
<DashboardLayout>
<h3 class="text-base font-semibold leading-6 text-gray-900">
Edycja semestru
</h3>
<form @submit.prevent="updateSemester">
<Section class="mt-3">
<div class="flex justify-between">
<FormGroup>
<FormLabel for="name">
Nazwa
</FormLabel>
<TextInput id="name" v-model="form.name" :error="form.errors.name" />
<FormError :error="form.errors.name" class="mt-2" />
</FormGroup>
</div>
<div class="flex justify-end space-x-3 py-3">
<SecondaryButton href="/dashboard/semesters">
Cofnij
</SecondaryButton>
<SubmitButton>
Zapisz
</SubmitButton>
</div>
</Section>
</form>
</DashboardLayout>
</template>
Loading

0 comments on commit 967cad3

Please sign in to comment.