Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/Http/Controllers/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions app/Http/Controllers/.idea/Controllers.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions app/Http/Controllers/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions app/Http/Controllers/.idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions app/Http/Controllers/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 101 additions & 0 deletions app/Http/Controllers/CourseController.php
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.");
}
}
20 changes: 20 additions & 0 deletions app/Models/Course.php
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");
}
}
5 changes: 5 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class User extends Authenticatable
"remember_token",
];

public function courses()
{
return $this->hasMany(Course::class);
}

protected function casts(): array
{
return [
Expand Down
73 changes: 73 additions & 0 deletions app/Policies/CoursePolicy.php
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.
Copy link
Member

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?

*/
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';
}
}
9 changes: 9 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
namespace Interns2024c\Providers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Vite;
use Illuminate\Support\ServiceProvider;
use Inertia\Inertia;
use Interns2024c\Models\Course;
use Interns2024c\Policies\CoursePolicy;

class AppServiceProvider extends ServiceProvider
{
Expand All @@ -23,13 +26,19 @@ public function register(): void
*/
public function boot(): void
{
// Vite prefetch
Vite::prefetch(concurrency: 3);

// Inertia auth
Inertia::share([
"auth" => function () {
return [
"user" => Auth::user(),
];
},
]);


Gate::policy(Course::class, CoursePolicy::class);
}
}
Empty file modified bootstrap/cache/.gitignore
100644 → 100755
Empty file.
27 changes: 27 additions & 0 deletions database/migrations/2024_12_17_124351_create_courses_table.php
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");
}
};
22 changes: 21 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"@tailwindcss/typography": "^0.5.15",
"laravel-vite-plugin": "^1.0.5",
"lodash": "^4.17.21",
"vue": "^3.5.10"
"vue": "^3.5.10",
"vue-router": "^4.5.0"
},
"devDependencies": {
"@blumilksoftware/eslint-config": "^2.0.0",
Expand Down
Loading
Loading