mirror of
https://github.com/snipe/snipe-it.git
synced 2026-02-04 14:05:41 +00:00
Changed NotesController::getList() to NotesController::index() & reordered methods for consistency
This commit is contained in:
@ -18,6 +18,46 @@ use Illuminate\Support\Facades\Log;
|
|||||||
*/
|
*/
|
||||||
class NotesController extends Controller
|
class NotesController extends Controller
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Retrieve a list of manual notes (action logs) for a given asset.
|
||||||
|
*
|
||||||
|
* Checks authorization to view assets, attempts to find the asset by ID,
|
||||||
|
* and fetches related action log entries of type 'note added', including
|
||||||
|
* user information for each note. Returns a JSON response with the notes or errors.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request The incoming HTTP request.
|
||||||
|
* @param Asset $asset The ID of the asset whose notes to retrieve.
|
||||||
|
* @return \Illuminate\Http\JsonResponse
|
||||||
|
*/
|
||||||
|
public function index(Asset $asset): JsonResponse
|
||||||
|
{
|
||||||
|
$this->authorize('view', $asset);
|
||||||
|
|
||||||
|
// Get the manual notes for the asset
|
||||||
|
$notes = ActionLog::with('user:id,username')
|
||||||
|
->where('item_type', Asset::class)
|
||||||
|
->where('item_id', $asset->id)
|
||||||
|
->where('action_type', 'note added')
|
||||||
|
->orderBy('created_at', 'desc')
|
||||||
|
->get(['id', 'created_at', 'note', 'created_by', 'item_id', 'item_type', 'action_type', 'target_id', 'target_type']);
|
||||||
|
|
||||||
|
$notesArray = $notes->map(function ($note) {
|
||||||
|
return [
|
||||||
|
'id' => $note->id,
|
||||||
|
'created_at' => $note->created_at,
|
||||||
|
'note' => $note->note,
|
||||||
|
'created_by' => $note->created_by,
|
||||||
|
'username' => $note->user?->username, // adding the username
|
||||||
|
'item_id' => $note->item_id,
|
||||||
|
'item_type' => $note->item_type,
|
||||||
|
'action_type' => $note->action_type,
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
// Return a success response
|
||||||
|
return response()->json(Helper::formatStandardApiResponse('success', ['notes' => $notesArray, 'asset_id' => $asset->id]));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Store a manual note on a specified asset and log the action.
|
* Store a manual note on a specified asset and log the action.
|
||||||
*
|
*
|
||||||
@ -52,44 +92,4 @@ class NotesController extends Controller
|
|||||||
// Return an error response if something went wrong
|
// Return an error response if something went wrong
|
||||||
return response()->json(Helper::formatStandardApiResponse('error', null, 'Something went wrong'), 500);
|
return response()->json(Helper::formatStandardApiResponse('error', null, 'Something went wrong'), 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve a list of manual notes (action logs) for a given asset.
|
|
||||||
*
|
|
||||||
* Checks authorization to view assets, attempts to find the asset by ID,
|
|
||||||
* and fetches related action log entries of type 'note added', including
|
|
||||||
* user information for each note. Returns a JSON response with the notes or errors.
|
|
||||||
*
|
|
||||||
* @param \Illuminate\Http\Request $request The incoming HTTP request.
|
|
||||||
* @param Asset $asset The ID of the asset whose notes to retrieve.
|
|
||||||
* @return \Illuminate\Http\JsonResponse
|
|
||||||
*/
|
|
||||||
public function getList(Asset $asset): JsonResponse
|
|
||||||
{
|
|
||||||
$this->authorize('view', $asset);
|
|
||||||
|
|
||||||
// Get the manual notes for the asset
|
|
||||||
$notes = ActionLog::with('user:id,username')
|
|
||||||
->where('item_type', Asset::class)
|
|
||||||
->where('item_id', $asset->id)
|
|
||||||
->where('action_type', 'note added')
|
|
||||||
->orderBy('created_at', 'desc')
|
|
||||||
->get(['id', 'created_at', 'note', 'created_by', 'item_id', 'item_type', 'action_type', 'target_id', 'target_type']);
|
|
||||||
|
|
||||||
$notesArray = $notes->map(function ($note) {
|
|
||||||
return [
|
|
||||||
'id' => $note->id,
|
|
||||||
'created_at' => $note->created_at,
|
|
||||||
'note' => $note->note,
|
|
||||||
'created_by' => $note->created_by,
|
|
||||||
'username' => $note->user?->username, // adding the username
|
|
||||||
'item_id' => $note->item_id,
|
|
||||||
'item_type' => $note->item_type,
|
|
||||||
'action_type' => $note->action_type,
|
|
||||||
];
|
|
||||||
});
|
|
||||||
|
|
||||||
// Return a success response
|
|
||||||
return response()->json(Helper::formatStandardApiResponse('success', ['notes' => $notesArray, 'asset_id' => $asset->id]));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -855,12 +855,12 @@ Route::group(['prefix' => 'v1', 'middleware' => ['api', 'api-throttle:api']], fu
|
|||||||
)->name('api.notes.store');
|
)->name('api.notes.store');
|
||||||
|
|
||||||
Route::get(
|
Route::get(
|
||||||
'{asset}/getList',
|
'{asset}/index',
|
||||||
[
|
[
|
||||||
Api\NotesController::class,
|
Api\NotesController::class,
|
||||||
'getList'
|
'index'
|
||||||
]
|
]
|
||||||
)->name('api.notes.getList');
|
)->name('api.notes.index');
|
||||||
}
|
}
|
||||||
); // end asset notes API routes
|
); // end asset notes API routes
|
||||||
|
|
||||||
|
|||||||
@ -67,7 +67,7 @@ class AssetNotesTest extends TestCase
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$this->actingAsForApi($user)
|
$this->actingAsForApi($user)
|
||||||
->getJson(route('api.notes.getList', $asset))
|
->getJson(route('api.notes.index', $asset))
|
||||||
->assertOk()
|
->assertOk()
|
||||||
->assertJson([
|
->assertJson([
|
||||||
'messages' => null,
|
'messages' => null,
|
||||||
|
|||||||
Reference in New Issue
Block a user