Skip to content

Commit 47e68cc

Browse files
committed
feat: event CRUD
- Add model, migrations, controller, seeder, routes and policy for calendar events feature
1 parent 7afb7ff commit 47e68cc

File tree

8 files changed

+207
-0
lines changed

8 files changed

+207
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Models\Event;
6+
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
7+
use Illuminate\Http\Request;
8+
9+
class EventController extends Controller
10+
{
11+
use AuthorizesRequests;
12+
/**
13+
* Display a listing of the resource.
14+
*/
15+
public function index()
16+
{
17+
return response()->json(auth()->user()->events);
18+
}
19+
20+
/**
21+
* Store a newly created resource in storage.
22+
*/
23+
public function store(Request $request)
24+
{
25+
$validated = $request->validate([
26+
'title' => 'required|string|max:255',
27+
'start' => 'required|date',
28+
'end' => 'required|date',
29+
]);
30+
31+
$event = auth()->user()->events()->create($validated);
32+
33+
return response()->json($event, 201);
34+
}
35+
36+
/**
37+
* Update the specified resource in storage.
38+
*/
39+
public function update(Request $request, Event $event)
40+
{
41+
$this->authorize('update', $event);
42+
43+
$validated = $request->validate([
44+
'title' => 'required|string|max:255',
45+
'start' => 'required|date',
46+
'end' => 'required|date',
47+
]);
48+
49+
$event->update($validated);
50+
return response()->json($event);
51+
}
52+
53+
/**
54+
* Remove the specified resource from storage.
55+
*/
56+
public function destroy(Event $event)
57+
{
58+
$this->authorize('delete', $event);
59+
60+
$event->delete();
61+
62+
return response()->noContent();
63+
}
64+
}

app/Models/Event.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Event extends Model
8+
{
9+
10+
protected $fillable = ['title', 'start', 'end', 'user_id'];
11+
12+
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
13+
{
14+
return $this->belongsTo(User::class);
15+
}
16+
}

app/Models/User.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// use Illuminate\Contracts\Auth\MustVerifyEmail;
66
use Database\Factories\UserFactory;
77
use Illuminate\Database\Eloquent\Factories\HasFactory;
8+
use Illuminate\Database\Eloquent\Relations\HasMany;
89
use Illuminate\Database\Eloquent\Relations\HasOne;
910
use Illuminate\Foundation\Auth\User as Authenticatable;
1011
use Illuminate\Notifications\Notifiable;
@@ -61,4 +62,9 @@ public function creditCard(): HasOne
6162
{
6263
return $this->hasOne(CreditCard::class);
6364
}
65+
66+
public function events(): HasMany
67+
{
68+
return $this->hasMany(Event::class);
69+
}
6470
}

app/Policies/EventPolicy.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Policies;
4+
5+
use App\Models\Event;
6+
use App\Models\User;
7+
8+
class EventPolicy
9+
{
10+
public function update(User $user, Event $event): bool
11+
{
12+
return $event->user_id === $user->id;
13+
}
14+
15+
public function delete(User $user, Event $event): bool
16+
{
17+
return $event->user_id === $user->id;
18+
}
19+
}

app/Providers/AuthServiceProvider.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use App\Models\Event;
6+
use App\Policies\EventPolicy;
7+
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
8+
9+
class AuthServiceProvider extends ServiceProvider
10+
{
11+
protected $policies = [
12+
Event::class => EventPolicy::class,
13+
];
14+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('events', function (Blueprint $table) {
15+
$table->id();
16+
$table->string('title');
17+
$table->datetime('start');
18+
$table->datetime('end');
19+
$table->foreignId('user_id')->constrained()->onDelete('cascade');
20+
$table->timestamps();
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*/
27+
public function down(): void
28+
{
29+
Schema::dropIfExists('events');
30+
}
31+
};

database/seeders/EventSeeder.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
namespace Database\Seeders;
3+
4+
use Illuminate\Database\Seeder;
5+
use App\Models\Event;
6+
use App\Models\User;
7+
8+
class EventSeeder extends Seeder
9+
{
10+
public function run()
11+
{
12+
// Fetch all users
13+
$users = User::all();
14+
15+
// Sample events
16+
$events = [
17+
[
18+
'title' => 'Morning Meeting',
19+
'start' => '2025-02-07 09:00:00',
20+
'end' => '2025-02-07 10:00:00',
21+
],
22+
[
23+
'title' => 'Lunch Break',
24+
'start' => '2025-03-13 12:00:00',
25+
'end' => '2025-03-13 13:00:00',
26+
],
27+
[
28+
'title' => 'Afternoon Workshop',
29+
'start' => '2025-04-06 14:00:00',
30+
'end' => '2025-04-06 16:00:00',
31+
],
32+
[
33+
'title' => 'All-Day Event',
34+
'start' => '2025-03-09 00:00:00',
35+
'end' => '2025-03-09 23:59:59',
36+
],
37+
[
38+
'title' => 'Evening Networking',
39+
'start' => '2025-04-11 18:30:00',
40+
'end' => '2025-04-11 20:00:00',
41+
],
42+
];
43+
44+
// Loop through each user and assign the events
45+
foreach ($users as $user) {
46+
foreach ($events as $event) {
47+
Event::create(array_merge($event, ['user_id' => $user->id]));
48+
}
49+
}
50+
}
51+
}
52+

routes/api.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use App\Http\Controllers\AuthController;
44
use App\Http\Controllers\CourseController;
55
use App\Http\Controllers\CreditCardController;
6+
use App\Http\Controllers\EventController;
67
use App\Http\Controllers\GoogleAuthController;
78
use Illuminate\Http\Request;
89
use Illuminate\Support\Facades\Route;
@@ -21,6 +22,10 @@
2122
});
2223
});
2324

25+
Route::middleware('auth:sanctum')->group(function () {
26+
Route::apiResource('/events', EventController::class);
27+
});
28+
2429
Route::get('/user', function (Request $request) {
2530
return $request->user();
2631
})->middleware('auth:sanctum');

0 commit comments

Comments
 (0)