Skip to content

Commit a9de640

Browse files
added new api for notification
1 parent 3c81811 commit a9de640

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use App\Models\User;
7+
use App\Models\Apartment;
8+
use App\Models\Message;
9+
class NotificationController extends Controller
10+
{
11+
public function index()
12+
{
13+
$user = auth()->user();
14+
$apartments = Apartment::where('user_id', $user->id)->with('messages')->get();
15+
$newMessages = [];
16+
17+
foreach ($apartments as $apartment) {
18+
foreach ($apartment->messages as $message) {
19+
if ($message['created_at'] > now()->subMinutes(5)) {
20+
$newMessages[] = $message;
21+
}
22+
}
23+
}
24+
25+
return response()->json($newMessages);
26+
}
27+
}

routes/api.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Http\Controllers\Api\ApartmentApi;
66
use App\Http\Controllers\ImageUploadController;
77
use App\Http\Controllers\Api\ReceiverController as EmailReceiverController;
8+
use App\Http\Controllers\NotificationController;
89
/*
910
|--------------------------------------------------------------------------
1011
| API Routes
@@ -20,6 +21,11 @@
2021
return $request->user();
2122
});
2223

24+
// Notification con Auth
25+
Route::middleware('auth:sanctum')->get('/notifications', [NotificationController::class, 'index']);
26+
// debugging
27+
// Route::get('/notifications/{id}', [NotificationController::class, 'index']);
28+
2329
Route::get('geocode', function (Request $request) {
2430
$apiKey = 'SooRbYbji9V5qUxAh3i2ijnD8m9ZWVZ7';
2531
// utilizzo bounding box per la zona di ricerca lombarda

0 commit comments

Comments
 (0)