Exemplo n.º 1
0
        public override async Task Validate(AddAssetCommand command)
        {
            if (!deviceTypeDataService.Exists(command.Model.DeviceTypeId))
            {
                ApiResponse.AddError(nameof(AddAssetRequestModel.DeviceTypeId), "The device type is not valid.");

                return;
            }

            if (string.IsNullOrEmpty(command.Model.Name))
            {
                ApiResponse.AddError(nameof(AddAssetRequestModel.Name), "The name is required.");
            }
            else if (await repository.GetEntities <UserAssetEntity>().AnyAsync(x =>
                                                                               x.UserId == command.UserId &&
                                                                               EF.Functions.Like(x.Asset.Name.ToLower(), $"{command.Model.Name.ToLower()}")))
            {
                ApiResponse.AddError(nameof(AddAssetRequestModel.Name),
                                     "You already have an asset with the same name.");
            }

            if (string.IsNullOrEmpty(command.Model.DeviceId))
            {
                ApiResponse.AddError(nameof(AddAssetRequestModel.DeviceId),
                                     "The device id is required.");
            }
            else if (await deviceService.DeviceIsUsed(command.Model.DeviceId, command.Model.DeviceTypeId))
            {
                ApiResponse.AddError(nameof(AddAssetRequestModel.DeviceId),
                                     "The device id is already used by another device on the same protocol.");
            }
        }
 public override async Task Validate(ChangeDeviceCommand command)
 {
     if (!deviceTypeDataService.Exists(command.Model.DeviceTypeId))
     {
         ApiResponse.AddError(nameof(ChangeDeviceRequestModel.DeviceTypeId),
                              "The device type does not exist.");
     }
     else if (string.IsNullOrEmpty(command.Model.DeviceId))
     {
         ApiResponse.AddError(nameof(ChangeDeviceRequestModel.DeviceId),
                              "The device ID is required.");
     }
     else if (await deviceService.DeviceIsUsed(command.Model.DeviceId, command.Model.DeviceTypeId,
                                               command.AssetId))
     {
         ApiResponse.AddError(nameof(ChangeDeviceRequestModel.DeviceId),
                              "The device ID is already assigned to another asset.");
     }
     else if (await assetDataService.HasActiveDeviceId(command.AssetId, command.Model.DeviceId))
     {
         ApiResponse.AddError(nameof(ChangeDeviceRequestModel.DeviceId),
                              "The device ID is already assigned to this asset.");
     }
 }