44
55namespace TechInventory . Services . DeviceModel ;
66
7- public class DeviceModelService : IDeviceModelService
7+ public class DeviceModelService : IDeviceModelService , IDisposable
88{
99 private readonly UnitOfWork _unitOfWork ;
1010 private readonly IRepository < Models . DeviceModel > _deviceModelRepository ;
1111 private readonly IRepository < Models . Brand > _brandRepository ;
12-
12+ private bool _disposed = false ;
13+
1314 public DeviceModelService ( IUnitOfWork unitOfWork )
1415 {
1516 _unitOfWork = ( UnitOfWork ) unitOfWork ;
@@ -47,10 +48,17 @@ public async Task<Result<bool>> UpdateDeviceModel(Models.DeviceModel deviceModel
4748 return await _unitOfWork . CommitAsync ( ) ;
4849 }
4950
50- public Task < Result < bool > > DeleteDeviceModel ( int id )
51+ public async Task < Result < bool > > DeleteDeviceModel ( int id )
5152 {
52- _deviceModelRepository . DeleteAsync ( id ) ;
53- return _unitOfWork . CommitAsync ( ) ;
53+ var model = await _deviceModelRepository . GetAsync ( id ) ;
54+ if ( model is null )
55+ return Result < bool > . Failure ( $ "Modelo de dispositivo com Id { id } não foi encontrado", false ) ;
56+
57+ var deleteResult = await _deviceModelRepository . DeleteAsync ( id ) ;
58+ if ( ! deleteResult . IsSuccessful )
59+ return Result < bool > . Failure ( deleteResult . Message , false ) ;
60+
61+ return await _unitOfWork . CommitAsync ( ) ;
5462 }
5563
5664 public async Task < List < Models . DeviceModel > > GetDeviceModelsByBrandId ( int brandId )
@@ -66,4 +74,24 @@ public async Task<Result<bool>> CheckIncludes(Models.DeviceModel deviceModel)
6674 return Result < bool > . Failure ( $ "A marca com id { deviceModel . BrandId } não existe", false ) ;
6775 return Result < bool > . Success ( true ) ;
6876 }
77+
78+ protected virtual void Dispose ( bool disposing )
79+ {
80+ if ( ! _disposed )
81+ {
82+ if ( disposing )
83+ {
84+ // Nothing to dispose here as per the request.
85+ // _unitOfWork is managed by DI container.
86+ }
87+
88+ _disposed = true ;
89+ }
90+ }
91+
92+ public void Dispose ( )
93+ {
94+ Dispose ( true ) ;
95+ GC . SuppressFinalize ( this ) ;
96+ }
6997}
0 commit comments