Add AutoMapper

This commit is contained in:
2019-03-12 22:23:38 +01:00
parent 3b16c5feaf
commit 30f64257e7
16 changed files with 137 additions and 59 deletions

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LaDOSE.Business.Interface;
using LaDOSE.DTO;
using LaDOSE.Entity;
using LaDOSE.Entity.Context;
using Microsoft.AspNetCore.Authorization;
@@ -13,7 +14,7 @@ namespace LaDOSE.Api.Controllers
[Authorize]
[Route("api/[controller]")]
[Produces("application/json")]
public class GameController : GenericController<IGameService, Game>
public class GameController : GenericControllerDTO<IGameService, Game, GameDTO>
{
public GameController(IGameService service) : base(service)
{

View File

@@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Mvc;
namespace LaDOSE.Api.Controllers
{
public class GenericController<T,TU> :Controller where TU : Entity.Context.Entity where T : IBaseService<TU>
public class GenericController<T, TU> : Controller where TU : Entity.Context.Entity where T : IBaseService<TU>
{
protected T _service;
@@ -16,10 +16,11 @@ namespace LaDOSE.Api.Controllers
}
[HttpPost]
public TU Post([FromBody]TU dto)
public TU Post([FromBody] TU dto)
{
return _service.AddOrUpdate(dto);
}
[HttpGet]
public List<TU> Get()
{
@@ -27,6 +28,7 @@ namespace LaDOSE.Api.Controllers
return _service.GetAll().ToList();
}
[HttpGet("{id}")]
public TU Get(int id)
{

View File

@@ -0,0 +1,37 @@
using System.Collections.Generic;
using System.Linq;
using LaDOSE.Business.Interface;
using Microsoft.AspNetCore.Mvc;
namespace LaDOSE.Api.Controllers
{
public class GenericControllerDTO<T, TU, D> : Controller where TU : Entity.Context.Entity where T : IBaseService<TU>
{
protected T _service;
public GenericControllerDTO(T service)
{
_service = service;
}
[HttpPost]
public D Post([FromBody]D dto)
{
TU entity = AutoMapper.Mapper.Map<TU>(dto);
return AutoMapper.Mapper.Map<D>(_service.AddOrUpdate(entity));
}
[HttpGet]
public List<D> Get()
{
return AutoMapper.Mapper.Map<List<D>>(_service.GetAll().ToList());
}
[HttpGet("{id}")]
public D Get(int id)
{
return AutoMapper.Mapper.Map<D>(_service.GetById(id));
}
}
}

View File

@@ -1,5 +1,7 @@
using System.Collections.Generic;
using AutoMapper;
using LaDOSE.Business.Interface;
using LaDOSE.DTO;
using LaDOSE.Entity.Wordpress;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@@ -24,7 +26,7 @@ namespace LaDOSE.Api.Controllers
[HttpGet("WPEvent")]
public List<WPEvent> Event()
public List<WPEventDTO> Event()
{
var wpEvents = _service.GetWpEvent();
foreach (var wpEvent in wpEvents)
@@ -36,22 +38,22 @@ namespace LaDOSE.Api.Controllers
wpEventWpBooking.WPUser.WPBookings = null;
}
}
return wpEvents;
return Mapper.Map<List<WPEventDTO>>(wpEvents);
}
[HttpGet("GetUsers/{wpEventId}/{gameId}")]
public List<WPUser> GetUsers(int wpEventId, int gameId)
public List<WPUserDTO> GetUsers(int wpEventId, int gameId)
{
var game = GameService.GetById(gameId);
return _service.GetBooking(wpEventId, game);
return Mapper.Map<List<WPUserDTO>>(_service.GetBooking(wpEventId, game));
}
[HttpGet("GetUsersOptions/{wpEventId}/{gameId}")]
public List<WPUser> GetUsersOptions(int wpEventId, int gameId)
public List<WPUserDTO> GetUsersOptions(int wpEventId, int gameId)
{
var game = GameService.GetById(gameId);
return _service.GetBookingOptions(wpEventId, game);
return Mapper.Map<List<WPUserDTO>>(_service.GetBookingOptions(wpEventId, game));
}

View File

@@ -0,0 +1,15 @@
using AutoMapper;
using AutoMapper.Configuration;
namespace LaDOSE.Api.Helpers
{
public static class AutoMapperTwoWay
{
public static void CreateMapTwoWay<TSource, TDestination>(this IMapperConfigurationExpression mapper)
{
mapper.CreateMap<TSource, TDestination>().IgnoreAllPropertiesWithAnInaccessibleSetter().IgnoreAllSourcePropertiesWithAnInaccessibleSetter();
mapper.CreateMap<TDestination, TSource>().IgnoreAllPropertiesWithAnInaccessibleSetter().IgnoreAllSourcePropertiesWithAnInaccessibleSetter();
}
}
}

View File

@@ -5,11 +5,11 @@
</PropertyGroup>
<ItemGroup>
<Folder Include="Services\" />
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.4" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
@@ -17,6 +17,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LaDOSE.DTO\LaDOSE.DTO.csproj" />
<ProjectReference Include="..\LaDOSE.Entity\LaDOSE.Entity.csproj" />
<ProjectReference Include="..\LaDOSE.Service\LaDOSE.Business.csproj" />
</ItemGroup>

View File

@@ -23,6 +23,9 @@ using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using Pomelo.EntityFrameworkCore.MySql;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
using AutoMapper;
using LaDOSE.Api.Helpers;
using LaDOSE.Entity.Wordpress;
namespace LaDOSE.Api
{
@@ -56,7 +59,7 @@ namespace LaDOSE.Api
Console.WriteLine($"Fix Gentoo NOK : {exception.Message}");
}
}
services.AddCors();
services.AddMvc().AddJsonOptions(x =>
{
@@ -105,9 +108,19 @@ namespace LaDOSE.Api
ValidateAudience = false
};
});
// configure DI for application services
AddDIConfig(services);
Mapper.Initialize(cfg =>
{
cfg.CreateMap<WPUser, LaDOSE.DTO.WPUserDTO>();
cfg.CreateMap<WPUser, LaDOSE.DTO.WPUserDTO>();
cfg.CreateMap<WPEvent, LaDOSE.DTO.WPEventDTO>();
cfg.CreateMap<ApplicationUser, LaDOSE.DTO.ApplicationUser>();
cfg.CreateMap<WPBooking, LaDOSE.DTO.WPBookingDTO>();
cfg.CreateMapTwoWay<Game, LaDOSE.DTO.GameDTO>();
});
}
private void AddDIConfig(IServiceCollection services)