Skip to content

Commit 752d89d

Browse files
authored
Merge pull request grokability#16101 from snipe/standarize_modelfile_api
2 parents 5afcd8d + 0d7304e commit 752d89d

File tree

3 files changed

+59
-37
lines changed

3 files changed

+59
-37
lines changed

app/Http/Controllers/Api/AssetModelFilesController.php

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use App\Models\AssetModel;
1010
use App\Models\Actionlog;
1111
use App\Http\Requests\UploadFileRequest;
12+
use App\Http\Transformers\AssetModelsTransformer;
1213
use Illuminate\Http\JsonResponse;
1314
use Illuminate\Support\Facades\Log;
1415
use Symfony\Component\HttpFoundation\StreamedResponse;
@@ -68,37 +69,15 @@ public function store(UploadFileRequest $request, $assetModelId = null) : JsonRe
6869
/**
6970
* List the files for an asset.
7071
*
71-
* @param int $assetModelId
72+
* @param int $assetmodel
7273
* @since [v7.0.12]
7374
* @author [r-xyz]
7475
*/
75-
public function list($assetModelId = null) : JsonResponse
76+
public function list($assetmodel_id) : JsonResponse | array
7677
{
77-
// Start by checking if the asset being acted upon exists
78-
if (! $assetModel = AssetModel::find($assetModelId)) {
79-
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/models/message.does_not_exist')), 404);
80-
}
81-
82-
// the asset is valid
83-
if (isset($assetModel->id)) {
84-
$this->authorize('view', $assetModel);
85-
86-
// Check that there are some uploads on this asset that can be listed
87-
if ($assetModel->uploads->count() > 0) {
88-
$files = array();
89-
foreach ($assetModel->uploads as $upload) {
90-
array_push($files, $upload);
91-
}
92-
// Give the list of files back to the user
93-
return response()->json(Helper::formatStandardApiResponse('success', $files, trans('admin/models/message.upload.success')));
94-
}
95-
96-
// There are no files.
97-
return response()->json(Helper::formatStandardApiResponse('success', array(), trans('admin/models/message.upload.success')));
98-
}
99-
100-
// Send back an error message
101-
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/models/message.download.error')), 500);
78+
$assetmodel = AssetModel::with('uploads')->find($assetmodel_id);
79+
$this->authorize('view', $assetmodel);
80+
return (new AssetModelsTransformer)->transformAssetModelFiles($assetmodel, $assetmodel->uploads()->count());
10281
}
10382

10483
/**

app/Http/Transformers/AssetModelsTransformer.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,41 @@ public function transformAssetModel(AssetModel $assetmodel)
8787
return $array;
8888
}
8989

90+
public function transformAssetModelFiles($assetmodel, $total)
91+
{
92+
93+
$array = [];
94+
foreach ($assetmodel->uploads as $file) {
95+
$array[] = self::transformAssetModelFile($file, $assetmodel);
96+
}
97+
98+
return (new DatatablesTransformer)->transformDatatables($array, $total);
99+
}
100+
101+
public function transformAssetModelFile($file, $assetmodel)
102+
{
103+
104+
$array = [
105+
'id' => (int) $file->id,
106+
'filename' => e($file->filename),
107+
'url' => route('show/modelfile', [$assetmodel->id, $file->id]),
108+
'created_by' => ($file->adminuser) ? [
109+
'id' => (int) $file->adminuser->id,
110+
'name'=> e($file->adminuser->present()->fullName),
111+
] : null,
112+
'created_at' => Helper::getFormattedDateObject($file->created_at, 'datetime'),
113+
'updated_at' => Helper::getFormattedDateObject($file->updated_at, 'datetime'),
114+
'deleted_at' => Helper::getFormattedDateObject($file->deleted_at, 'datetime'),
115+
];
116+
117+
$permissions_array['available_actions'] = [
118+
'delete' => (Gate::allows('update', AssetModel::class) && ($assetmodel->deleted_at == '')),
119+
];
120+
121+
$array += $permissions_array;
122+
return $array;
123+
}
124+
90125
public function transformAssetModelsDatatable($assetmodels)
91126
{
92127
return (new DatatablesTransformer)->transformDatatables($assetmodels);

tests/Feature/AssetModels/Api/AssetModelFilesTest.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ public function testAssetModelApiListsFiles()
4343
->getJson(
4444
route('api.models.files.index', ['model_id' => $model[0]["id"]]))
4545
->assertOk()
46-
->assertJsonStructure([
47-
'status',
48-
'messages',
49-
'payload',
50-
]);
46+
->assertJsonStructure([
47+
'rows',
48+
'total',
49+
]);
5150
}
5251

5352
public function testAssetModelApiDownloadsFile()
@@ -66,20 +65,25 @@ public function testAssetModelApiDownloadsFile()
6665
route('api.models.files.store', ['model_id' => $model[0]["id"]]), [
6766
'file' => [UploadedFile::fake()->create("test.jpg", 100)]
6867
])
69-
->assertOk();
68+
->assertOk()
69+
->assertJsonStructure([
70+
'status',
71+
'messages',
72+
]);
7073

7174
// List the files to get the file ID
7275
$result = $this->actingAsForApi($user)
7376
->getJson(
7477
route('api.models.files.index', ['model_id' => $model[0]["id"]]))
75-
->assertOk();
78+
->assertOk();
79+
7680

7781
// Get the file
7882
$this->actingAsForApi($user)
7983
->get(
8084
route('api.models.files.show', [
8185
'model_id' => $model[0]["id"],
82-
'file_id' => $result->decodeResponseJson()->json()["payload"][0]["id"],
86+
'file_id' => $result->decodeResponseJson()->json()["rows"][0]["id"],
8387
]))
8488
->assertOk();
8589
}
@@ -113,8 +117,12 @@ public function testAssetModelApiDeletesFile()
113117
->delete(
114118
route('api.models.files.destroy', [
115119
'model_id' => $model[0]["id"],
116-
'file_id' => $result->decodeResponseJson()->json()["payload"][0]["id"],
120+
'file_id' => $result->decodeResponseJson()->json()["rows"][0]["id"],
117121
]))
118-
->assertOk();
122+
->assertOk()
123+
->assertJsonStructure([
124+
'status',
125+
'messages',
126+
]);
119127
}
120128
}

0 commit comments

Comments
 (0)