Skip to content

Commit b6a830f

Browse files
authored
Merge pull request #557 from Muhammad235/feat/user-notification
refactor: user notification endpoint
2 parents 6769adf + 871480f commit b6a830f

File tree

5 files changed

+92
-10
lines changed

5 files changed

+92
-10
lines changed

app/Console/Commands/StoreApiStatus.php

+17-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use DateTime;
66
use DateTimeZone;
77
use Illuminate\Console\Command;
8+
use Illuminate\Support\Facades\Http;
89

910
class StoreApiStatus extends Command
1011
{
@@ -29,7 +30,7 @@ public function handle()
2930
{
3031

3132
// Get test result from json file
32-
$data = file_get_contents(base_path('../result.json'));
33+
$data = file_get_contents(base_path('result.json'));
3334

3435
$data = json_decode($data, true);
3536

@@ -58,19 +59,29 @@ public function handle()
5859
$date = new DateTime("now", new DateTimeZone('Africa/Lagos'));
5960
$last_checked = $date->format('Y-m-d h:i A');
6061

61-
// Update the api status record
62-
\App\Models\ApiStatus::updateOrCreate(['api_group' => $item['name']], [
62+
63+
$data = [
6364
'api_group' => $item['name'],
6465
'method' => $item['request']['method'],
6566
'status' => $status,
6667
'response_time' => $response_time,
6768
'last_checked' => $last_checked,
68-
'details' => $this->getDetails($execution),
69-
]);
69+
'details' => $this->getDetails($execution)
70+
];
71+
72+
$url = "https://staging.api-php.boilerplate.hng.tech/api/v1/api-status";
73+
74+
$response = Http::post($url, $data);
75+
76+
if ($response->failed()) {
77+
78+
return $this->info('Failed to send API status data: '. $response->body());
79+
}
80+
7081
}
7182
}
7283

73-
$this->info('Api status stored successfully');
84+
$this->info('API status data stored successfully: ' . $response->body());
7485
}
7586

7687

app/Http/Controllers/Api/V1/ApiStatusCheckerController.php

+37
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace App\Http\Controllers\Api\V1;
44

5+
use DateTime;
6+
use DateTimeZone;
57
use App\Models\ApiStatus;
8+
use Illuminate\Http\Request;
69
use App\Http\Controllers\Controller;
10+
use App\Http\Requests\StoreApiStatusDataRequest;
711

812

913
class ApiStatusCheckerController extends Controller
@@ -20,4 +24,37 @@ public function index()
2024
]);
2125
}
2226

27+
public function store(StoreApiStatusDataRequest $request)
28+
{
29+
30+
$request->validated();
31+
32+
try {
33+
$date = new DateTime("now", new DateTimeZone('Africa/Lagos'));
34+
$last_checked = $date->format('Y-m-d h:i A');
35+
36+
// Update the api status record
37+
ApiStatus::updateOrCreate(['api_group' => $request->api_group], [
38+
'api_group' => $request->api_group,
39+
'method' => $request->method,
40+
'status' => $request->status,
41+
'response_time' => $request->response_time,
42+
'last_checked' => $request->last_checked ?? $last_checked,
43+
'details' => $request->details,
44+
]);
45+
46+
return response()->json([
47+
'status_code' => 200,
48+
'message' => "Data stored successfully"
49+
], 200);
50+
51+
} catch (\Exception $e) {
52+
return response()->json([
53+
'status_code' => 200,
54+
'message' => "Server error" . $e->getMessage()
55+
], 500);
56+
}
57+
58+
}
59+
2360
}

app/Http/Controllers/UserNotificationController.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public function create(CreateNotificationRequest $request)
5959

6060
// Return 201
6161
return response()->json([
62-
'status' => 'success',
6362
'message' => 'Notification created successfully',
6463
'status_code' => 201,
6564
'data' => [
@@ -115,18 +114,19 @@ public function getByUser(Request $request)
115114
$isRead = $notification->pivot->status === 'read';
116115
return [
117116
'id' => $notification->id,
118-
'message' => $notification->message,
117+
'user_id' => auth()->id(),
119118
'is_read' => $isRead,
119+
'message' => $notification->message,
120120
'created_at' => $notification->created_at,
121+
'updated_at' => $notification->updated_at,
121122
];
122123
});
123124

124125
//
125126
$status = $isRead === 'false' ? 'Unread ' : '';
126127

127128
return response()->json([
128-
'status' => 'success',
129-
'message' => "{$status}Notifications retrieved successfully",
129+
'message' => "{$status}Notification retrieved successfully",
130130
'status_code' => Response::HTTP_OK,
131131
'data' => [
132132
'total_notification_count' => $totalNotificationsCount,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class StoreApiStatusDataRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*/
12+
public function authorize(): bool
13+
{
14+
return true;
15+
}
16+
17+
/**
18+
* Get the validation rules that apply to the request.
19+
*
20+
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
21+
*/
22+
public function rules(): array
23+
{
24+
return [
25+
'api_group' => ['nullable', 'string'],
26+
'method' => ['nullable', 'string'],
27+
'status' => ['nullable', 'string'],
28+
'response_time' => ['nullable'],
29+
'last_checked' => ['nullable'],
30+
'details' => ['nullable', 'string'],
31+
];
32+
}
33+
}

routes/api.php

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
});
7575

7676
Route::get('/api-status', [ApiStatusCheckerController::class, 'index']);
77+
Route::post('/api-status', [ApiStatusCheckerController::class, 'store']);
7778

7879
Route::post('/auth/register', [AuthController::class, 'store']);
7980
Route::post('/auth/login', [LoginController::class, 'login']);

0 commit comments

Comments
 (0)