Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
snipe committed Jan 20, 2025
2 parents 48ff7cd + 752d89d commit d4ef0f8
Showing 3 changed files with 59 additions and 37 deletions.
33 changes: 6 additions & 27 deletions app/Http/Controllers/Api/AssetModelFilesController.php
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
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 @@ public function store(UploadFileRequest $request, $assetModelId = null) : JsonRe
/**
* 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());
}

/**
35 changes: 35 additions & 0 deletions app/Http/Transformers/AssetModelsTransformer.php
Original file line number Diff line number Diff line change
@@ -87,6 +87,41 @@ public function transformAssetModel(AssetModel $assetmodel)
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);
28 changes: 18 additions & 10 deletions tests/Feature/AssetModels/Api/AssetModelFilesTest.php
Original file line number Diff line number Diff line change
@@ -43,11 +43,10 @@ public function testAssetModelApiListsFiles()
->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 @@ public function testAssetModelApiDownloadsFile()
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 @@ public function testAssetModelApiDeletesFile()
->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',
]);
}
}

0 comments on commit d4ef0f8

Please sign in to comment.