From 7955109bf8047b8daa0f2e1c803486f2216f628e Mon Sep 17 00:00:00 2001 From: kshitige Date: Tue, 11 Mar 2025 19:05:25 +0530 Subject: [PATCH] UserAssetLink --- .../CustomerApi/UserAssetLinkController.php | 21 ++++++++++++++++--- app/Models/Asset.php | 8 +++---- app/Models/Device.php | 9 ++++---- app/Models/UserAssetLink.php | 14 ++++++------- ...2025_03_11_070912_create_devices_table.php | 1 + 5 files changed, 35 insertions(+), 18 deletions(-) diff --git a/app/Http/Controllers/APIS/CustomerApi/UserAssetLinkController.php b/app/Http/Controllers/APIS/CustomerApi/UserAssetLinkController.php index c3493af..2fecef0 100644 --- a/app/Http/Controllers/APIS/CustomerApi/UserAssetLinkController.php +++ b/app/Http/Controllers/APIS/CustomerApi/UserAssetLinkController.php @@ -11,9 +11,24 @@ class UserAssetLinkController extends Controller { public function index() { - $userAssetLinks = User::id('a5daeb60-f36c-11ef-a9dc-45dd276e4cd5'); - $userAssetLinks = UserAssetLink::with(['user', 'asset.devices'])->get(); + $user = User::where('id', '8898f380-fd9e-11ef-a9dc-45dd276e4cd5')->first(); + + $userAssetLinks = UserAssetLink::with(['user', 'asset.devices']) + ->withCount([ + 'asset as active_devices_count' => function ($query) { + $query->whereHas('devices', function ($q) { + $q->where('active', 1); + }); + }, + 'asset as inactive_devices_count' => function ($query) { + $query->whereHas('devices', function ($q) { + $q->where('active', 0); + }); + } + ]) + ->get(); + return response()->json($userAssetLinks); } -} \ No newline at end of file +} diff --git a/app/Models/Asset.php b/app/Models/Asset.php index 56dde53..3ab270e 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -36,8 +36,8 @@ class Asset extends Model ]; public function devices() -{ - return $this->hasMany(Device::class, 'asset_id', 'id'); -} + { + return $this->hasMany(Device::class, 'asset_id', 'id'); + } -} +} \ No newline at end of file diff --git a/app/Models/Device.php b/app/Models/Device.php index 19e43f8..0171d22 100644 --- a/app/Models/Device.php +++ b/app/Models/Device.php @@ -25,6 +25,7 @@ class Device extends Model 'software_id', 'external_id', 'version', + 'active', 'additional_info', 'device_data', ]; @@ -42,8 +43,8 @@ class Device extends Model ]; public function asset() -{ - return $this->belongsTo(Asset::class, 'asset_id'); -} + { + return $this->belongsTo(Asset::class, 'asset_id', 'id'); + } -} +} \ No newline at end of file diff --git a/app/Models/UserAssetLink.php b/app/Models/UserAssetLink.php index e41902d..78124bd 100644 --- a/app/Models/UserAssetLink.php +++ b/app/Models/UserAssetLink.php @@ -9,20 +9,20 @@ class UserAssetLink extends Model { use HasFactory; - protected $table = 'user_asset_link'; + protected $table = 'user_asset_link'; protected $fillable = [ 'user_id', 'asset_id', ]; - public function user() - { - return $this->belongsTo(User::class, 'user_id'); - } - public function asset() { - return $this->belongsTo(Asset::class, 'asset_id'); + return $this->belongsTo(Asset::class, 'asset_id', 'id'); + } + + public function user() + { + return $this->belongsTo(User::class, 'user_id', 'id'); } } diff --git a/database/migrations/2025_03_11_070912_create_devices_table.php b/database/migrations/2025_03_11_070912_create_devices_table.php index 41426c5..2b3795d 100644 --- a/database/migrations/2025_03_11_070912_create_devices_table.php +++ b/database/migrations/2025_03_11_070912_create_devices_table.php @@ -26,6 +26,7 @@ return new class extends Migration $table->uuid('software_id')->nullable(); $table->uuid('external_id')->nullable(); $table->integer('version')->default(1); + $table->smallInteger('active')->default(1)->comment('1: Active, 0: Inactive'); $table->json('additional_info')->nullable(); $table->json('device_data')->nullable(); $table->timestamps();