Skip to content

Commit

Permalink
Added restore functionalty to asset API
Browse files Browse the repository at this point in the history
Signed-off-by: snipe <snipe@snipe.net>
  • Loading branch information
snipe committed Aug 14, 2021
1 parent b1e2f86 commit 45caa8a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
34 changes: 34 additions & 0 deletions app/Http/Controllers/Api/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Http\Transformers\AssetsTransformer;
use App\Http\Transformers\LicensesTransformer;
use App\Http\Transformers\SelectlistTransformer;
use App\Models\Actionlog;
use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Company;
Expand Down Expand Up @@ -640,6 +641,39 @@ public function destroy($id)
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')), 200);
}



/**
* Retore a soft-deleted asset.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @param int $assetId
* @since [v5.1.18]
* @return JsonResponse
*/
public function restore($assetId = null)
{
// Get asset information
$asset = Asset::withTrashed()->find($assetId);
$this->authorize('delete', $asset);
if (isset($asset->id)) {
// Restore the asset
Asset::withTrashed()->where('id', $assetId)->restore();

$logaction = new Actionlog();
$logaction->item_type = Asset::class;
$logaction->item_id = $asset->id;
$logaction->created_at = date("Y-m-d H:i:s");
$logaction->user_id = Auth::user()->id;
$logaction->logaction('restored');

return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/hardware/message.restore.success')));


}
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')), 200);
}



/**
Expand Down
14 changes: 14 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,20 @@
]
);

Route::post('{asset_id}/restore',
[
'as' => 'api.assets.restore',
'uses' => 'AssetsController@restore'
]
);

Route::post('{asset_id}/destroy',
[
'as' => 'api.assets.destroy',
'uses' => 'AssetsController@destroy'
]
);

});

/*--- Asset Maintenances API ---*/
Expand Down

0 comments on commit 45caa8a

Please sign in to comment.