diff --git a/DMS.Application/DTOs/UpdateDeviceDto.cs b/DMS.Application/DTOs/UpdateDeviceDto.cs index 9be4f77..ed182f9 100644 --- a/DMS.Application/DTOs/UpdateDeviceDto.cs +++ b/DMS.Application/DTOs/UpdateDeviceDto.cs @@ -8,12 +8,12 @@ namespace DMS.Application.DTOs; public class UpdateDeviceDto { public int Id { get; set; } - public string Name { get; set; } - public ProtocolType Protocol { get; set; } - public string IpAddress { get; set; } - public int Port { get; set; } - public int Rack { get; set; } - public int Slot { get; set; } - public string OpcUaServerUrl { get; set; } - public bool IsActive { get; set; } + public string? Name { get; set; } + public ProtocolType? Protocol { get; set; } + public string? IpAddress { get; set; } + public int? Port { get; set; } + public int? Rack { get; set; } + public int? Slot { get; set; } + public string? OpcUaServerUrl { get; set; } + public bool? IsActive { get; set; } } \ No newline at end of file diff --git a/DMS.Application/Interfaces/IDeviceAppService.cs b/DMS.Application/Interfaces/IDeviceAppService.cs index d60755e..23cd4d4 100644 --- a/DMS.Application/Interfaces/IDeviceAppService.cs +++ b/DMS.Application/Interfaces/IDeviceAppService.cs @@ -29,7 +29,7 @@ public interface IDeviceAppService /// /// 异步更新一个已存在的设备。 /// - Task UpdateDeviceAsync(UpdateDeviceDto deviceDto); + Task UpdateDeviceAsync(UpdateDeviceDto deviceDto); /// /// 异步删除一个设备。 diff --git a/DMS.Application/Profiles/MappingProfile.cs b/DMS.Application/Profiles/MappingProfile.cs index 33ee62f..33f6963 100644 --- a/DMS.Application/Profiles/MappingProfile.cs +++ b/DMS.Application/Profiles/MappingProfile.cs @@ -17,10 +17,22 @@ public class MappingProfile : Profile .ForMember(dest => dest.VariableTables, opt => opt.Ignore()); CreateMap() + // 1. 首先,忽略那些永远不应从DTO更新的属性 + .ForMember(dest => dest.Id, opt => opt.Ignore()) .ForMember(dest => dest.Description, opt => opt.Ignore()) .ForMember(dest => dest.VariableTables, opt => opt.Ignore()) .ForMember(dest => dest.CpuType, opt => opt.Ignore()) - .ForMember(dest => dest.DeviceType, opt => opt.Ignore()); + .ForMember(dest => dest.DeviceType, opt => opt.Ignore()) + + // 2. 然后,为每个可空属性单独设置条件 + .ForMember(dest => dest.Name, opt => opt.Condition(src => src.Name != null)) + .ForMember(dest => dest.Protocol, opt => opt.Condition(src => src.Protocol.HasValue)) + .ForMember(dest => dest.IpAddress, opt => opt.Condition(src => src.IpAddress != null)) + .ForMember(dest => dest.Port, opt => opt.Condition(src => src.Port.HasValue)) + .ForMember(dest => dest.Rack, opt => opt.Condition(src => src.Rack.HasValue)) + .ForMember(dest => dest.Slot, opt => opt.Condition(src => src.Slot.HasValue)) + .ForMember(dest => dest.OpcUaServerUrl, opt => opt.Condition(src => src.OpcUaServerUrl != null)) + .ForMember(dest => dest.IsActive, opt => opt.Condition(src => src.IsActive.HasValue)); CreateMap() .ForMember(dest => dest.Protocol, opt => opt.MapFrom(src => src.Protocol.ToString())) diff --git a/DMS.Application/Services/DeviceService.cs b/DMS.Application/Services/DeviceService.cs index 9347e4f..7db0ccb 100644 --- a/DMS.Application/Services/DeviceService.cs +++ b/DMS.Application/Services/DeviceService.cs @@ -119,8 +119,9 @@ public class DeviceService : IDeviceAppService /// /// 异步更新一个已存在的设备。 /// - public async Task UpdateDeviceAsync(UpdateDeviceDto deviceDto) + public async Task UpdateDeviceAsync(UpdateDeviceDto deviceDto) { + await _repoManager.BeginTranAsync(); var device = await _repoManager.Devices.GetByIdAsync(deviceDto.Id); if (device == null) { @@ -128,8 +129,9 @@ public class DeviceService : IDeviceAppService } _mapper.Map(deviceDto, device); - await _repoManager.Devices.UpdateAsync(device); + int res=await _repoManager.Devices.UpdateAsync(device); await _repoManager.CommitAsync(); + return res; } /// diff --git a/DMS.Infrastructure.UnitTests/Services/DeviceServiceTest.cs b/DMS.Infrastructure.UnitTests/Services/DeviceServiceTest.cs index 58a4990..edcf429 100644 --- a/DMS.Infrastructure.UnitTests/Services/DeviceServiceTest.cs +++ b/DMS.Infrastructure.UnitTests/Services/DeviceServiceTest.cs @@ -48,4 +48,20 @@ public class DeviceServiceTest : BaseServiceTest // 继承基类 // Assert Assert.Equal(isSuccess,true); } + + [Fact] + public async Task UpdateDeviceAsyncTest() + { + UpdateDeviceDto dto = new UpdateDeviceDto() + { + Id = 5, + Name = "lalala", + IsActive = true, + Rack = 0, + Slot = 0 + + }; + var res = await _deviceService.UpdateDeviceAsync(dto); + Assert.NotEqual(res,0); + } } \ No newline at end of file