mirror of
https://github.com/snipe/snipe-it.git
synced 2025-01-26 13:10:55 -08:00
Use transformer for model files
Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
parent
5afcd8ddb3
commit
0d7304eb8b
|
@ -9,6 +9,7 @@ use App\Http\Controllers\Controller;
|
|||
use App\Models\AssetModel;
|
||||
use App\Models\Actionlog;
|
||||
use App\Http\Requests\UploadFileRequest;
|
||||
use App\Http\Transformers\AssetModelsTransformer;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
@ -68,37 +69,15 @@ class AssetModelFilesController extends Controller
|
|||
/**
|
||||
* List the files for an asset.
|
||||
*
|
||||
* @param int $assetModelId
|
||||
* @param int $assetmodel
|
||||
* @since [v7.0.12]
|
||||
* @author [r-xyz]
|
||||
*/
|
||||
public function list($assetModelId = null) : JsonResponse
|
||||
public function list($assetmodel_id) : JsonResponse | array
|
||||
{
|
||||
// Start by checking if the asset being acted upon exists
|
||||
if (! $assetModel = AssetModel::find($assetModelId)) {
|
||||
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/models/message.does_not_exist')), 404);
|
||||
}
|
||||
|
||||
// the asset is valid
|
||||
if (isset($assetModel->id)) {
|
||||
$this->authorize('view', $assetModel);
|
||||
|
||||
// Check that there are some uploads on this asset that can be listed
|
||||
if ($assetModel->uploads->count() > 0) {
|
||||
$files = array();
|
||||
foreach ($assetModel->uploads as $upload) {
|
||||
array_push($files, $upload);
|
||||
}
|
||||
// Give the list of files back to the user
|
||||
return response()->json(Helper::formatStandardApiResponse('success', $files, trans('admin/models/message.upload.success')));
|
||||
}
|
||||
|
||||
// There are no files.
|
||||
return response()->json(Helper::formatStandardApiResponse('success', array(), trans('admin/models/message.upload.success')));
|
||||
}
|
||||
|
||||
// Send back an error message
|
||||
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/models/message.download.error')), 500);
|
||||
$assetmodel = AssetModel::with('uploads')->find($assetmodel_id);
|
||||
$this->authorize('view', $assetmodel);
|
||||
return (new AssetModelsTransformer)->transformAssetModelFiles($assetmodel, $assetmodel->uploads()->count());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -87,6 +87,41 @@ class AssetModelsTransformer
|
|||
return $array;
|
||||
}
|
||||
|
||||
public function transformAssetModelFiles($assetmodel, $total)
|
||||
{
|
||||
|
||||
$array = [];
|
||||
foreach ($assetmodel->uploads as $file) {
|
||||
$array[] = self::transformAssetModelFile($file, $assetmodel);
|
||||
}
|
||||
|
||||
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
||||
}
|
||||
|
||||
public function transformAssetModelFile($file, $assetmodel)
|
||||
{
|
||||
|
||||
$array = [
|
||||
'id' => (int) $file->id,
|
||||
'filename' => e($file->filename),
|
||||
'url' => route('show/modelfile', [$assetmodel->id, $file->id]),
|
||||
'created_by' => ($file->adminuser) ? [
|
||||
'id' => (int) $file->adminuser->id,
|
||||
'name'=> e($file->adminuser->present()->fullName),
|
||||
] : null,
|
||||
'created_at' => Helper::getFormattedDateObject($file->created_at, 'datetime'),
|
||||
'updated_at' => Helper::getFormattedDateObject($file->updated_at, 'datetime'),
|
||||
'deleted_at' => Helper::getFormattedDateObject($file->deleted_at, 'datetime'),
|
||||
];
|
||||
|
||||
$permissions_array['available_actions'] = [
|
||||
'delete' => (Gate::allows('update', AssetModel::class) && ($assetmodel->deleted_at == '')),
|
||||
];
|
||||
|
||||
$array += $permissions_array;
|
||||
return $array;
|
||||
}
|
||||
|
||||
public function transformAssetModelsDatatable($assetmodels)
|
||||
{
|
||||
return (new DatatablesTransformer)->transformDatatables($assetmodels);
|
||||
|
|
|
@ -43,11 +43,10 @@ class AssetModelFilesTest extends TestCase
|
|||
->getJson(
|
||||
route('api.models.files.index', ['model_id' => $model[0]["id"]]))
|
||||
->assertOk()
|
||||
->assertJsonStructure([
|
||||
'status',
|
||||
'messages',
|
||||
'payload',
|
||||
]);
|
||||
->assertJsonStructure([
|
||||
'rows',
|
||||
'total',
|
||||
]);
|
||||
}
|
||||
|
||||
public function testAssetModelApiDownloadsFile()
|
||||
|
@ -66,20 +65,25 @@ class AssetModelFilesTest extends TestCase
|
|||
route('api.models.files.store', ['model_id' => $model[0]["id"]]), [
|
||||
'file' => [UploadedFile::fake()->create("test.jpg", 100)]
|
||||
])
|
||||
->assertOk();
|
||||
->assertOk()
|
||||
->assertJsonStructure([
|
||||
'status',
|
||||
'messages',
|
||||
]);
|
||||
|
||||
// List the files to get the file ID
|
||||
$result = $this->actingAsForApi($user)
|
||||
->getJson(
|
||||
route('api.models.files.index', ['model_id' => $model[0]["id"]]))
|
||||
->assertOk();
|
||||
->assertOk();
|
||||
|
||||
|
||||
// Get the file
|
||||
$this->actingAsForApi($user)
|
||||
->get(
|
||||
route('api.models.files.show', [
|
||||
'model_id' => $model[0]["id"],
|
||||
'file_id' => $result->decodeResponseJson()->json()["payload"][0]["id"],
|
||||
'file_id' => $result->decodeResponseJson()->json()["rows"][0]["id"],
|
||||
]))
|
||||
->assertOk();
|
||||
}
|
||||
|
@ -113,8 +117,12 @@ class AssetModelFilesTest extends TestCase
|
|||
->delete(
|
||||
route('api.models.files.destroy', [
|
||||
'model_id' => $model[0]["id"],
|
||||
'file_id' => $result->decodeResponseJson()->json()["payload"][0]["id"],
|
||||
'file_id' => $result->decodeResponseJson()->json()["rows"][0]["id"],
|
||||
]))
|
||||
->assertOk();
|
||||
->assertOk()
|
||||
->assertJsonStructure([
|
||||
'status',
|
||||
'messages',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue