mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-25 13:44:06 -08:00
Merge pull request #15162 from snipe/fixes/added_location_assets_endpoint
Added assets endpoint for locations
This commit is contained in:
commit
962ae5231d
|
@ -5,8 +5,10 @@ namespace App\Http\Controllers\Api;
|
|||
use App\Helpers\Helper;
|
||||
use App\Http\Requests\ImageUploadRequest;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Transformers\AssetsTransformer;
|
||||
use App\Http\Transformers\LocationsTransformer;
|
||||
use App\Http\Transformers\SelectlistTransformer;
|
||||
use App\Models\Asset;
|
||||
use App\Models\Location;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
|
@ -222,6 +224,15 @@ class LocationsController extends Controller
|
|||
return response()->json(Helper::formatStandardApiResponse('error', null, $location->getErrors()));
|
||||
}
|
||||
|
||||
public function assets(Request $request, Location $location) : JsonResponse | array
|
||||
{
|
||||
$this->authorize('view', Asset::class);
|
||||
$this->authorize('view', $location);
|
||||
$assets = Asset::where('assigned_to', '=', $location->id)->where('assigned_type', '=', Location::class)->with('model', 'model.category', 'assetstatus', 'location', 'company', 'defaultLoc');
|
||||
$assets = $assets->get();
|
||||
return (new AssetsTransformer)->transformAssets($assets, $assets->count(), $request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
|
|
|
@ -712,7 +712,7 @@ Route::group(['prefix' => 'v1', 'middleware' => ['api', 'throttle:api']], functi
|
|||
Route::get('{location}/assets',
|
||||
[
|
||||
Api\LocationsController::class,
|
||||
'getDataViewAssets'
|
||||
'assets'
|
||||
]
|
||||
)->name('api.locations.viewassets');
|
||||
|
||||
|
|
44
tests/Feature/Locations/Api/LocationsViewTest.php
Normal file
44
tests/Feature/Locations/Api/LocationsViewTest.php
Normal file
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\Consumables\Api;
|
||||
|
||||
use App\Models\Location;
|
||||
use App\Models\Asset;
|
||||
use App\Models\User;
|
||||
use Tests\TestCase;
|
||||
|
||||
class LocationsViewTest extends TestCase
|
||||
{
|
||||
public function testViewingLocationRequiresPermission()
|
||||
{
|
||||
$location = Location::factory()->create();
|
||||
$this->actingAsForApi(User::factory()->create())
|
||||
->getJson(route('api.locations.show', $location->id))
|
||||
->assertForbidden();
|
||||
}
|
||||
|
||||
public function testViewingLocationAssetIndexRequiresPermission()
|
||||
{
|
||||
$location = Location::factory()->create();
|
||||
$this->actingAsForApi(User::factory()->create())
|
||||
->getJson(route('api.locations.viewassets', $location->id))
|
||||
->assertForbidden();
|
||||
}
|
||||
|
||||
public function testViewingLocationAssetIndex()
|
||||
{
|
||||
$location = Location::factory()->create();
|
||||
Asset::factory()->count(3)->assignedToLocation($location)->create();
|
||||
|
||||
$this->actingAsForApi(User::factory()->superuser()->create())
|
||||
->getJson(route('api.locations.viewassets', $location->id))
|
||||
->assertOk()
|
||||
->assertJsonStructure([
|
||||
'total',
|
||||
'rows',
|
||||
])
|
||||
->assertJson([
|
||||
'total' => 3,
|
||||
]);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue