Add Notes API

This commit is contained in:
Peter Šurda 2024-06-20 15:01:09 +08:00
parent c66312c6f2
commit 6b50978097
Signed by untrusted user: PeterSurda
GPG Key ID: 3E47497CF67ABB95
2 changed files with 62 additions and 0 deletions

View File

@ -7,6 +7,7 @@ use App\Models\IPs;
use App\Models\Labels;
use App\Models\Misc;
use App\Models\NetworkSpeed;
use App\Models\Note;
use App\Models\OS;
use App\Models\Pricing;
use App\Models\Providers;
@ -501,4 +502,59 @@ class ApiController extends Controller
return response($yabs, 200);
}
protected function getAllNotes()
{
$notes = Note::allNotes()->toJson(JSON_PRETTY_PRINT);
return response($notes, 200);
}
protected function getNote($id)
{
$note = Note::note($id)->toJson(JSON_PRETTY_PRINT);
return response($note, 200);
}
protected function storeNote(Request $request)
{
$request->validate([
'service_id' => 'required|string|size:8',
'note' => 'required|string'
]);
try {
$note_id = Str::random(8);
$note = Note::create([
'id' => $note_id,
'service_id' => $request->service_id,
'note' => $request->note
]);
} catch (\Exception $e) {
if ($e->getCode() === "23000") {
return response("A note already exists for this service", 409);
} else {
return response("Error inserting note", 500);
}
}
Cache::forget('all_notes');
return response()->json(array('result' => 'success', 'note_id' => $note_id), 201);
}
public function updateNote(Request $request)
{
$request->validate([
'service_id' => 'required|string|size:8',
'note' => 'required|string'
]);
$note = Note::note($request->id);
$note->update([
'service_id' => $request->service_id,
'note' => $request->note
]);
Cache::forget('all_notes');
Cache::forget("note.$note->service_id");
return response()->json(array('result' => 'success', 'note_id' => $note->id), 200);
}
}

View File

@ -77,3 +77,9 @@ Route::middleware('auth:api')->get('dns/{domainName}/{type}', 'App\Http\Controll
Route::middleware('throttle:4')->post('yabs/{server}/{key}', 'App\Http\Controllers\ApiController@storeYabs')->name('api.store-yabs');
Route::middleware('auth:api')->get('yabs/', 'App\Http\Controllers\ApiController@getAllYabs');
Route::middleware('auth:api')->get('yabs/{id}', 'App\Http\Controllers\ApiController@getYabs');
Route::middleware('auth:api')->get('notes', 'App\Http\Controllers\ApiController@getAllNotes');
Route::middleware('auth:api')->get('notes/{id}', 'App\Http\Controllers\ApiController@getNote');
Route::middleware('auth:api')->post('notes', 'App\Http\Controllers\ApiController@storeNote');
Route::middleware('auth:api')->put('notes/{id}', 'App\Http\Controllers\ApiController@updateNote');