Skip to content

Commit

Permalink
feat: teacher
Browse files Browse the repository at this point in the history
  • Loading branch information
iqbaleff214 committed Jun 1, 2024
1 parent d938794 commit 985d8cb
Show file tree
Hide file tree
Showing 13 changed files with 462 additions and 23 deletions.
129 changes: 129 additions & 0 deletions app/Http/Controllers/TeacherController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

namespace App\Http\Controllers;

use App\Enum\Role;
use App\Http\Requests\StoreTeacherRequest;
use App\Http\Requests\UpdateTeacherRequest;
use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\View\View;
use Yajra\DataTables\DataTables;

class TeacherController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request): View|JsonResponse
{
if ($request->ajax()) {
return DataTables::of(User::role(Role::TEACHER))
->addColumn('action', function ($row) {
return '<div class="dropdown">
<button type="button"
class="btn p-0 dropdown-toggle hide-arrow"
data-bs-toggle="dropdown">
<i class="bx bx-dots-vertical-rounded"></i>
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="'.route('teacher.edit', $row).'"><i class="bx bx-edit-alt me-1"></i>'.__('label.edit').'</a>
<form action="'.route('teacher.destroy', $row).'" method="post" onsubmit="confirmSubmit(event, this)">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="'.csrf_token().'" />
<button class="dropdown-item" type="submit"><i class="bx bx-trash me-1"></i>'.__('label.delete').'</button>
</form>
</div>
</div>';
})
->editColumn('gender', fn ($row) => __('label.'.$row->gender))
->rawColumns(['action'])
->make();
}

return view('pages.teacher.index');
}

/**
* Show the form for creating a new resource.
*/
public function create(): View
{
return view('pages.teacher.create');
}

/**
* Store a newly created resource in storage.
*/
public function store(StoreTeacherRequest $request): RedirectResponse
{
try {
$data = $request->validated();
$data['role'] = Role::TEACHER;
$data['password'] = bcrypt('password');

User::create($data);

return redirect()->route('teacher.index')->with('notification', $this->successNotification('notification.success_create', 'menu.teacher'));
} catch (\Throwable $throwable) {
Log::error($throwable->getMessage());

return back()->with('notification', $this->successNotification('notification.fail_create', 'menu.teacher'));
}
}

/**
* Display the specified resource.
*/
public function show(User $teacher): View
{
return view('pages.teacher.show', [
'teacher' => $teacher,
]);
}

/**
* Show the form for editing the specified resource.
*/
public function edit(User $teacher): View
{
return view('pages.teacher.edit', [
'teacher' => $teacher,
]);
}

/**
* Update the specified resource in storage.
*/
public function update(UpdateTeacherRequest $request, User $teacher): RedirectResponse
{
try {
$teacher->update($request->validated());

return back()->with('notification', $this->successNotification('notification.success_update', 'menu.teacher'));
} catch (\Throwable $throwable) {
Log::error($throwable->getMessage());

return back()->with('notification', $this->successNotification('notification.fail_update', 'menu.teacher'));
}
}

/**
* Remove the specified resource from storage.
*/
public function destroy(User $teacher): RedirectResponse
{
try {
$teacher->delete();

return back()->with('notification', $this->successNotification('notification.success_delete', 'menu.teacher'));
} catch (\Throwable $throwable) {
Log::error($throwable->getMessage());

return back()->with('notification', $this->successNotification('notification.fail_delete', 'menu.teacher'));
}
}
}
50 changes: 50 additions & 0 deletions app/Http/Requests/StoreTeacherRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Http\Requests;

use App\Enum\Role;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreTeacherRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return auth()->user()->isRole(Role::OWNER) || auth()->user()->isRole(Role::HEADMASTER) || auth()->user()->isRole(Role::ADMINISTRATOR);
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array|string>
*/
public function rules(): array
{
return [
'name' => ['required'],
'email' => ['required', 'email', Rule::unique('users', 'email')],
'image' => ['nullable', 'image', 'max:2048'],
'phone' => ['required'],
'address' => ['nullable'],
'marital_status' => ['nullable'],
'gender' => ['required'],
];
}

public function attributes(): array
{
return [
'name' => __('field.name'),
'email' => __('field.email'),
'image' => __('field.image'),
'phone' => __('field.phone'),
'address' => __('field.address'),
'marital_status' => __('field.marital_status'),
'gender' => __('field.gender'),
];
}
}
50 changes: 50 additions & 0 deletions app/Http/Requests/UpdateTeacherRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Http\Requests;

use App\Enum\Role;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpdateTeacherRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return auth()->user()->isRole(Role::OWNER) || auth()->user()->isRole(Role::HEADMASTER) || auth()->user()->isRole(Role::ADMINISTRATOR);
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array|string>
*/
public function rules(): array
{
return [
'name' => ['required'],
'email' => ['required', 'email', Rule::unique('users', 'email')->ignore($this->id)],
'image' => ['nullable', 'image', 'max:2048'],
'phone' => ['required'],
'address' => ['nullable'],
'marital_status' => ['nullable'],
'gender' => ['required'],
];
}

public function attributes(): array
{
return [
'name' => __('field.name'),
'email' => __('field.email'),
'image' => __('field.image'),
'phone' => __('field.phone'),
'address' => __('field.address'),
'marital_status' => __('field.marital_status'),
'gender' => __('field.gender'),
];
}
}
14 changes: 12 additions & 2 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;

use App\Enum\Role;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
Expand Down Expand Up @@ -64,4 +64,14 @@ public function imageUrl(): Attribute
get: fn () => 'https://ui-avatars.com/api/?name='.$this->name,
);
}

public function isRole(Role $role): bool
{
return Role::tryFrom($this->role) === $role;
}

public function scopeRole(Builder $query, Role $role)
{
return $query->where('role', $role);
}
}
6 changes: 6 additions & 0 deletions lang/en/field.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@
'password' => 'Password',
'password_confirmation' => 'Password Confirmation',
'current_password' => 'Current Password',
'role' => 'Role',
'image' => 'Image',
'phone' => 'Phone',
'address' => 'Address',
'marital_status' => 'Marital Status',
'gender' => 'Gender',
];
1 change: 1 addition & 0 deletions lang/en/menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
'profile' => 'Profile',
'change_password' => 'Change Password',
'locale' => 'Language',
'teacher' => 'Teacher',
];
6 changes: 6 additions & 0 deletions lang/id/field.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@
'password' => 'Password',
'password_confirmation' => 'Konfirmasi Password',
'current_password' => 'Password Sekarang',
'role' => 'Peran',
'image' => 'Gambar',
'phone' => 'Nomor Telepon',
'address' => 'Alamat',
'marital_status' => 'Status Perkawinan',
'gender' => 'Jenis Kelamin',
];
1 change: 1 addition & 0 deletions lang/id/menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
'profile' => 'Profil',
'change_password' => 'Ganti Password',
'locale' => 'Bahasa',
'teacher' => 'Pengajar',
];
78 changes: 57 additions & 21 deletions resources/menu/verticalMenu.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,63 @@
{
"menu": [
{
"url": "dashboard",
"name": "menu.dashboard",
"icon": "menu-icon tf-icons bx bx-home-circle",
"slug": "dashboard"
},
{
"name": "menu.account",
"icon": "menu-icon tf-icons bx bx bx-user",
"slug": "account.*",
"submenu": [
"menu": [
{
"url": "account.profile.edit",
"name": "menu.profile",
"slug": "account.profile.edit"
"url": "dashboard",
"name": "menu.dashboard",
"icon": "menu-icon tf-icons bx bx-home-circle",
"slug": "dashboard",
"role": [
"owner",
"headmaster",
"admin",
"student_guardian"
]
},
{
"url": "account.password.edit",
"name": "menu.change_password",
"slug": "account.password.edit"
"url": "teacher.index",
"name": "menu.teacher",
"icon": "menu-icon tf-icons bx bx-user-voice",
"slug": "teacher.*",
"role": [
"owner",
"headmaster",
"admin",
"student_guardian"
]
},
{
"name": "menu.account",
"icon": "menu-icon tf-icons bx bx bx-user",
"slug": "account.*",
"role": [
"owner",
"headmaster",
"admin",
"student_guardian"
],
"submenu": [
{
"url": "account.profile.edit",
"name": "menu.profile",
"slug": "account.profile.edit",
"role": [
"owner",
"headmaster",
"admin",
"student_guardian"
]
},
{
"url": "account.password.edit",
"name": "menu.change_password",
"slug": "account.password.edit",
"role": [
"owner",
"headmaster",
"admin",
"student_guardian"
]
}
]
}
]
}
]
]
}
Loading

0 comments on commit 985d8cb

Please sign in to comment.