11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4+ using AutoMapper ;
45using DeviceManager . Api . Data . Management ;
56using DeviceManager . Api . Data . Model ;
67using DeviceManager . Api . Model ;
@@ -11,20 +12,23 @@ namespace DeviceManager.Api.Services
1112 public class DeviceService : IDeviceService
1213 {
1314 private readonly IUnitOfWork unitOfWork ;
15+ private readonly IMapper mapper ;
1416
1517 /// <inheritdoc />
1618 public DeviceService (
17- IUnitOfWork unitOfWork )
19+ IUnitOfWork unitOfWork ,
20+ IMapper mapper )
1821 {
1922 this . unitOfWork = unitOfWork ;
23+ this . mapper = mapper ;
2024 }
2125
2226 /// <inheritdoc />
23- public List < Device > GetDevices ( )
27+ public List < Device > GetDevices ( int page , int pageSize )
2428 {
2529 var deviceRepository = unitOfWork . GetRepository < Device > ( ) ;
2630
27- return deviceRepository . GetAll ( ) . ToList ( ) ;
31+ return deviceRepository . GetAll ( page , pageSize ) . ToList ( ) ;
2832 }
2933
3034 /// <inheritdoc />
@@ -46,13 +50,7 @@ public void CreateDevice(DeviceViewModel deviceViewModel)
4650 {
4751 var deviceRepository = unitOfWork . GetRepository < Device > ( ) ;
4852
49- // Add new device
50- deviceRepository . Add (
51- new Device
52- {
53- DeviceId = Guid . NewGuid ( ) ,
54- DeviceTitle = deviceViewModel . Title
55- } ) ;
53+ deviceRepository . Add ( mapper . Map < DeviceViewModel , Device > ( deviceViewModel ) ) ;
5654
5755 // Commit changes
5856 unitOfWork . Commit ( ) ;
@@ -71,7 +69,6 @@ public void UpdateDevice(Guid deviceId, DeviceViewModel deviceViewModel)
7169 throw new NullReferenceException ( ) ;
7270 }
7371
74- // Update device properties
7572 device . DeviceTitle = deviceViewModel . Title ;
7673
7774 deviceRepository . Update ( device ) ;
0 commit comments