From 7381c2046c078c6fed35d8f11770412dc7846da9 Mon Sep 17 00:00:00 2001 From: VTS <5238065+VirTechSystems@users.noreply.github.com> Date: Thu, 3 Aug 2023 20:35:20 -0400 Subject: [PATCH] API get_location (#14779) * api endpoint to get specific location * missing nl * fix missed styleci error * make requested change --- doc/API/Locations.md | 27 +++++++++++++++++++++++++++ includes/html/api_functions.inc.php | 15 +++++++++++++++ routes/api.php | 1 + 3 files changed, 43 insertions(+) diff --git a/doc/API/Locations.md b/doc/API/Locations.md index 69524d02c5..894297b400 100644 --- a/doc/API/Locations.md +++ b/doc/API/Locations.md @@ -112,3 +112,30 @@ Output: "message": "Location updated successfully" } ``` + +###`get_location` + +Gets a specific location + +Route: `/api/v0/location/:location` + +- location: name or id of the location to get + +Output: + +```json +{ + "status": "ok", + "get_location": [ + { + "id": 1, + "location": "TEST", + "lat": 00.000000, + "lng": 00.000000, + "timestamp": "2023-01-01 00:00:00", + "fixed_coordinates": 1 + } + ], + "count": 1 +} +``` diff --git a/includes/html/api_functions.inc.php b/includes/html/api_functions.inc.php index f1ad4e316b..0f32b22647 100644 --- a/includes/html/api_functions.inc.php +++ b/includes/html/api_functions.inc.php @@ -17,6 +17,7 @@ use App\Models\Availability; use App\Models\Device; use App\Models\DeviceGroup; use App\Models\DeviceOutage; +use App\Models\Location; use App\Models\MplsSap; use App\Models\MplsService; use App\Models\OspfPort; @@ -2900,6 +2901,20 @@ function edit_location(Illuminate\Http\Request $request) return api_error(500, 'Failed to update location'); } +function get_location(Illuminate\Http\Request $request) +{ + $location = $request->route('location_id_or_name'); + if (empty($location)) { + return api_error(400, 'No location has been provided to get'); + } + $data = ctype_digit($location) ? Location::find($location_id) : Location::where('location', $location)->first(); + if (empty($data)) { + return api_error(404, 'Location does not exist'); + } + + return api_success($data, 'get_location'); +} + function get_location_id_by_name($location) { return dbFetchCell('SELECT id FROM locations WHERE location = ?', $location); diff --git a/routes/api.php b/routes/api.php index ce1bfed91a..611fda6022 100644 --- a/routes/api.php +++ b/routes/api.php @@ -89,6 +89,7 @@ Route::prefix('v0')->namespace('\App\Api\Controllers')->group(function () { Route::post('devices/{id}/parents', 'LegacyApiController@add_parents_to_host')->name('add_parents_to_host'); Route::delete('/devices/{id}/parents', 'LegacyApiController@del_parents_from_host')->name('del_parents_from_host'); Route::post('locations', 'LegacyApiController@add_location')->name('add_location'); + Route::get('location/{location_id_or_name}', 'LegacyApiController@get_location')->name('get_location'); Route::patch('locations/{location_id_or_name}', 'LegacyApiController@edit_location')->name('edit_location'); Route::delete('locations/{location}', 'LegacyApiController@del_location')->name('del_location'); Route::delete('services/{id}', 'LegacyApiController@del_service_from_host')->name('del_service_from_host');