Skip to content

Commit 8af0a4b

Browse files
authored
Merge pull request #8 from Boriszn/feature/ISS-1-Add-Automapper
Feature/iss 1 add automapper
2 parents adfca04 + b81b9fd commit 8af0a4b

File tree

17 files changed

+212
-55
lines changed

17 files changed

+212
-55
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
Web API Solution demonstrates mutliteantcy architecture, using Entity Framework, UnitOfWork, Repository patterns
44

5+
![alt text](https://github.com/Boriszn/DeviceManager.Api/blob/feature/ISS-1-Add-Automapper/assets/arhitecture-diag.png "Logo Title Text 1")
6+
7+
58
## Installation
69

710
1. Clone repository

assets/arhitecture-diag.png

34.2 KB
Loading

src/DeviceManager.Api/Controllers/DevicesController.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ public DevicesController(IDeviceService deviceService)
2525
[HttpGet]
2626
[SwaggerOperation("GetDevices")]
2727
[ValidateActionParameters]
28-
public IActionResult Get([FromQuery][Required]string page, [FromQuery][Required]string pageSize)
28+
public IActionResult Get([FromQuery][Required]int page, [FromQuery][Required]int pageSize)
2929
{
3030
if (!this.ModelState.IsValid)
3131
{
3232
return new BadRequestObjectResult(this.ModelState);
3333
}
3434

35-
return new ObjectResult(deviceService.GetDevices());
35+
return new ObjectResult(deviceService.GetDevices(page, pageSize));
3636
}
3737

3838
/// <summary>
@@ -78,6 +78,8 @@ public IActionResult GetDeviceByTitle(string deviceTitle)
7878
/// <returns></returns>
7979
[HttpPost]
8080
[SwaggerOperation("CreateDevice")]
81+
[SwaggerResponse(204, null, "Device was saved successfuly")]
82+
[SwaggerResponse(400, null, "Error in saving the Device")]
8183
public IActionResult Post([FromBody]DeviceViewModel deviceViewModel)
8284
{
8385
if (!this.ModelState.IsValid)

src/DeviceManager.Api/Data/Management/ContextFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ private static void ValidateTenantId(string tenantId)
109109
{
110110
throw new ArgumentNullException(nameof(tenantId));
111111
}
112+
113+
if (!Guid.TryParse(tenantId, out Guid tenantGuid))
114+
{
115+
throw new ArgumentNullException(nameof(tenantId));
116+
}
112117
}
113118
}
114119
}

src/DeviceManager.Api/Data/Management/IRepository.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public interface IRepository<T>
5656
/// <returns>List of entities</returns>
5757
IQueryable<T> GetAll();
5858

59+
/// <summary>
60+
/// Gets all. With data pagination.
61+
/// </summary>
62+
/// <param name="page">The page.</param>
63+
/// <param name="pageCount">The page count.</param>
64+
/// <returns></returns>
65+
IQueryable<T> GetAll(int page, int pageCount);
66+
5967
/// <summary>
6068
/// Gets all and offers to include a related table
6169
/// </summary>

src/DeviceManager.Api/Data/Management/Repository.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using System.Linq.Expressions;
44
using System.Threading.Tasks;
5+
using Microsoft.ApplicationInsights.DataContracts;
56
using Microsoft.EntityFrameworkCore;
67

78
namespace DeviceManager.Api.Data.Management
@@ -76,6 +77,13 @@ public IQueryable<T> GetAll()
7677
return this.dbSet;
7778
}
7879

80+
public IQueryable<T> GetAll(int page, int pageCount)
81+
{
82+
var pageSize = (page - 1) * pageCount;
83+
84+
return this.dbSet.Skip(pageSize).Take(pageCount);
85+
}
86+
7987
/// <inheritdoc />
8088
public IQueryable<T> GetAll(string include)
8189
{

src/DeviceManager.Api/DeviceManager.Api.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<Folder Include="wwwroot\" />
1111
</ItemGroup>
1212
<ItemGroup>
13+
<PackageReference Include="AutoMapper" Version="6.2.2" />
14+
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="2.0.1" />
1315
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
1416
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
1517
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using AutoMapper.Configuration;
3+
using DeviceManager.Api.Data.Model;
4+
using DeviceManager.Api.Model;
5+
6+
namespace DeviceManager.Api.Mappings
7+
{
8+
/// <summary>
9+
/// Contains objects mapping
10+
/// </summary>
11+
/// <seealso cref="AutoMapper.Configuration.MapperConfigurationExpression" />
12+
public class MapsProfile : MapperConfigurationExpression
13+
{
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="MapsProfile"/> class
16+
/// </summary>
17+
public MapsProfile()
18+
{
19+
// Device ViewModel To Device
20+
this.CreateMap<DeviceViewModel, Device>()
21+
.ForMember(dest => dest.DeviceTitle, opt => opt.MapFrom(src => src.Title))
22+
.ForMember(dest => dest.DeviceId, opt => opt.MapFrom(src => Guid.NewGuid()))
23+
;
24+
}
25+
}
26+
}

src/DeviceManager.Api/Model/DeviceViewModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.ComponentModel.DataAnnotations;
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
23

34
namespace DeviceManager.Api.Model
45
{

src/DeviceManager.Api/Services/DeviceService.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using AutoMapper;
45
using DeviceManager.Api.Data.Management;
56
using DeviceManager.Api.Data.Model;
67
using 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

Comments
 (0)