-
Notifications
You must be signed in to change notification settings - Fork 2
#8- Crud For Course #19
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
base: main
Are you sure you want to change the base?
Changes from all commits
ebe835b
c35da99
fac91c1
1204773
e5f5225
b83bb98
7fb90cb
10462c7
a51c3ed
afb41eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Interns2024c\Http\Controllers; | ||
|
|
||
| use Illuminate\Foundation\Auth\Access\AuthorizesRequests; | ||
| use Illuminate\Http\Request; | ||
| use Inertia\Inertia; | ||
| use Interns2024c\Models\Course; | ||
| use Illuminate\Validation\Rule; | ||
|
|
||
| class CourseController extends Controller | ||
| { | ||
| use AuthorizesRequests; | ||
|
|
||
| public function index(Request $request) | ||
| { | ||
| $query = Course::query(); | ||
|
|
||
| if ($request->filled('skill_level')) { | ||
| $query->where('skill_level', $request->skill_level); | ||
| } | ||
|
|
||
| $sortBy = $request->get('sort_by', 'id'); | ||
| $order = $request->get('order', 'asc'); | ||
| $allowedSortBy = ['title', 'created_at', 'id']; | ||
|
|
||
| if (in_array($sortBy, $allowedSortBy)) { | ||
| $query->orderBy($sortBy, $order); | ||
| } | ||
|
|
||
| $courses = $query->paginate(6); | ||
|
|
||
| return inertia('Courses/Index', [ | ||
| 'courses' => $courses, | ||
| 'filters' => $request->only('skill_level', 'sort_by', 'order'), | ||
| ]); | ||
| } | ||
|
|
||
| public function create() | ||
| { | ||
| return Inertia::render("Courses/Create"); | ||
| } | ||
|
|
||
| public function show($id) | ||
| { | ||
| $course = Course::findOrFail($id); | ||
| return Inertia::render("Courses/Show", ["course" => $course]); | ||
| } | ||
|
|
||
| public function store(Request $request) | ||
| { | ||
| $validated = $request->validate([ | ||
| "title" => [ | ||
| "required", | ||
| "string", | ||
| "max:255", | ||
| Rule::unique('courses', 'title'), | ||
| ], | ||
| "description" => "required|string", | ||
| "language" => "required|string", | ||
| "skill_level" => "required|string", | ||
| ]); | ||
|
|
||
| $request->user()->courses()->create($validated); | ||
| return redirect()->route("courses.index")->with("success", "Course created successfully."); | ||
| } | ||
|
|
||
| public function edit(Course $course) | ||
| { | ||
| return Inertia::render("Courses/Edit", ["course" => $course]); | ||
| } | ||
|
|
||
| public function update(Request $request, Course $course) | ||
| { | ||
| $this->authorize("update", $course); | ||
|
|
||
| $validated = $request->validate([ | ||
| "title" => [ | ||
| "required", | ||
| "string", | ||
| "max:255", | ||
| Rule::unique('courses', 'title')->ignore($course->id), | ||
| ], | ||
| "description" => "required|string", | ||
| "language" => "required|string", | ||
| "skill_level" => "required|string", | ||
| ]); | ||
|
|
||
| $course->update($validated); | ||
| return redirect()->route("courses.index")->with("success", "Course updated successfully."); | ||
| } | ||
|
|
||
| public function destroy(Course $course) | ||
| { | ||
| $this->authorize("delete", $course); | ||
| $course->delete(); | ||
| return redirect()->route("courses.index")->with("success", "Course deleted successfully."); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Interns2024c\Models; | ||
|
|
||
| use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
| use Illuminate\Database\Eloquent\Model; | ||
|
|
||
| class Course extends Model | ||
| { | ||
| use HasFactory; | ||
|
|
||
| protected $fillable = ["title", "description", "language", "skill_level", "user_id"]; | ||
|
|
||
| public function teacher() | ||
| { | ||
| return $this->belongsTo(User::class, "user_id"); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| <?php | ||
|
|
||
| namespace Interns2024c\Policies; | ||
|
|
||
| use Illuminate\Auth\Access\Response; | ||
| use Interns2024c\Models\Course; | ||
| use Interns2024c\Models\User; | ||
|
|
||
| class CoursePolicy | ||
| { | ||
| /** | ||
| * Determine whether the user can view any models. | ||
| */ | ||
| public function viewAny(User $user): bool | ||
| { | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Determine whether the user can view the model. | ||
| */ | ||
| public function view(User $user, Course $course): bool | ||
| { | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Determine whether the user can create models. | ||
| */ | ||
| public function create(User $user): bool | ||
| { | ||
|
|
||
| return $user->role === 'teacher' || $user->role === 'admin'; | ||
| } | ||
|
|
||
| /** | ||
| * Determine whether the user can update the model. | ||
| */ | ||
| public function update(User $user, Course $course): bool | ||
| { | ||
|
|
||
| return $user->id === $course->user_id || $user->role === 'admin'; | ||
| } | ||
|
|
||
| /** | ||
| * Determine whether the user can delete the model. | ||
| */ | ||
| public function delete(User $user, Course $course): bool | ||
| { | ||
|
|
||
| return $user->id === $course->user_id || $user->role === 'admin'; | ||
| } | ||
|
|
||
| /** | ||
| * Determine whether the user can restore the model. | ||
| */ | ||
| public function restore(User $user, Course $course): bool | ||
| { | ||
|
|
||
| return $user->role === 'admin' || $user->id === $course->user_id; | ||
| } | ||
|
|
||
| /** | ||
| * Determine whether the user can permanently delete the model. | ||
| */ | ||
| public function forceDelete(User $user, Course $course): bool | ||
| { | ||
|
|
||
| return $user->role === 'admin'; | ||
| } | ||
| } | ||
| 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->id(); | ||
| $table->string("title"); | ||
| $table->text("description"); | ||
| $table->string("language"); | ||
| $table->string("skill_level"); | ||
| $table->foreignId("user_id")->constrained()->onDelete("cascade"); | ||
| $table->timestamps(); | ||
| }); | ||
| } | ||
|
|
||
| public function down(): void | ||
| { | ||
| Schema::dropIfExists("courses"); | ||
| } | ||
| }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need comments here?