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

View File

@@ -20,6 +20,7 @@ namespace LaDOSE.Api.Controllers
{ {
return _service.AddOrUpdate(dto); return _service.AddOrUpdate(dto);
} }
[HttpGet] [HttpGet]
public List<TU> Get() public List<TU> Get()
{ {
@@ -27,6 +28,7 @@ namespace LaDOSE.Api.Controllers
return _service.GetAll().ToList(); return _service.GetAll().ToList();
} }
[HttpGet("{id}")] [HttpGet("{id}")]
public TU Get(int 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 System.Collections.Generic;
using AutoMapper;
using LaDOSE.Business.Interface; using LaDOSE.Business.Interface;
using LaDOSE.DTO;
using LaDOSE.Entity.Wordpress; using LaDOSE.Entity.Wordpress;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@@ -24,7 +26,7 @@ namespace LaDOSE.Api.Controllers
[HttpGet("WPEvent")] [HttpGet("WPEvent")]
public List<WPEvent> Event() public List<WPEventDTO> Event()
{ {
var wpEvents = _service.GetWpEvent(); var wpEvents = _service.GetWpEvent();
foreach (var wpEvent in wpEvents) foreach (var wpEvent in wpEvents)
@@ -36,22 +38,22 @@ namespace LaDOSE.Api.Controllers
wpEventWpBooking.WPUser.WPBookings = null; wpEventWpBooking.WPUser.WPBookings = null;
} }
} }
return wpEvents; return Mapper.Map<List<WPEventDTO>>(wpEvents);
} }
[HttpGet("GetUsers/{wpEventId}/{gameId}")] [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); var game = GameService.GetById(gameId);
return _service.GetBooking(wpEventId, game); return Mapper.Map<List<WPUserDTO>>(_service.GetBooking(wpEventId, game));
} }
[HttpGet("GetUsersOptions/{wpEventId}/{gameId}")] [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); 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> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Services\" />
<Folder Include="wwwroot\" /> <Folder Include="wwwroot\" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AutoMapper" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" /> <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.4" /> <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.4" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" /> <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
@@ -17,6 +17,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\LaDOSE.DTO\LaDOSE.DTO.csproj" />
<ProjectReference Include="..\LaDOSE.Entity\LaDOSE.Entity.csproj" /> <ProjectReference Include="..\LaDOSE.Entity\LaDOSE.Entity.csproj" />
<ProjectReference Include="..\LaDOSE.Service\LaDOSE.Business.csproj" /> <ProjectReference Include="..\LaDOSE.Service\LaDOSE.Business.csproj" />
</ItemGroup> </ItemGroup>

View File

@@ -23,6 +23,9 @@ using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
using Pomelo.EntityFrameworkCore.MySql; using Pomelo.EntityFrameworkCore.MySql;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure; using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
using AutoMapper;
using LaDOSE.Api.Helpers;
using LaDOSE.Entity.Wordpress;
namespace LaDOSE.Api namespace LaDOSE.Api
{ {
@@ -108,6 +111,16 @@ namespace LaDOSE.Api
// configure DI for application services // configure DI for application services
AddDIConfig(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) private void AddDIConfig(IServiceCollection services)

View File

@@ -1,6 +1,6 @@
namespace LaDOSE.DTO namespace LaDOSE.DTO
{ {
public class Game public class GameDTO
{ {
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } public string Name { get; set; }

View File

@@ -1,8 +1,8 @@
namespace LaDOSE.DTO namespace LaDOSE.DTO
{ {
public class WPBooking public class WPBookingDTO
{ {
public WPUser WpUser { get; set; } public WPUserDTO WpUser { get; set; }
public string Message { get; set; } public string Message { get; set; }
public string Meta { get; set; } public string Meta { get; set; }

View File

@@ -4,7 +4,7 @@ using System.Collections.Generic;
namespace LaDOSE.DTO namespace LaDOSE.DTO
{ {
public class WPEvent public class WPEventDTO
{ {
// Id, Name, Slug, Date // Id, Name, Slug, Date
@@ -12,6 +12,6 @@ namespace LaDOSE.DTO
public string Name { get; set; } public string Name { get; set; }
public string Slug { get; set; } public string Slug { get; set; }
public DateTime? Date { get; set; } public DateTime? Date { get; set; }
public List<WPBooking> WpBookings { get; set; } public List<WPBookingDTO> WpBookings { get; set; }
} }
} }

View File

@@ -1,6 +1,6 @@
namespace LaDOSE.DTO namespace LaDOSE.DTO
{ {
public class WPUser public class WPUserDTO
{ {
public string Id { get; set; } public string Id { get; set; }
public string Name { get; set; } public string Name { get; set; }

View File

@@ -17,7 +17,9 @@ namespace LaDOSE.DesktopApp.Services
public RestService() public RestService()
{ {
#if DEBUG
MessageBox.Show("WAIT");
#endif
var appSettings = ConfigurationManager.AppSettings; var appSettings = ConfigurationManager.AppSettings;
string url = (string) appSettings["ApiUri"]; string url = (string) appSettings["ApiUri"];
string user = (string)appSettings["ApiUser"]; string user = (string)appSettings["ApiUser"];
@@ -95,10 +97,10 @@ namespace LaDOSE.DesktopApp.Services
#endregion #endregion
#region WordPress #region WordPress
public List<WPEvent> GetEvents() public List<WPEventDTO> GetEvents()
{ {
var restRequest = new RestRequest("/api/wordpress/WPEvent", Method.GET); var restRequest = new RestRequest("/api/wordpress/WPEvent", Method.GET);
var restResponse = Client.Get<List<WPEvent>>(restRequest); var restResponse = Client.Get<List<WPEventDTO>>(restRequest);
return restResponse.Data; return restResponse.Data;
} }
@@ -109,10 +111,10 @@ namespace LaDOSE.DesktopApp.Services
var restResponse = Client.Get(restRequest); var restResponse = Client.Get(restRequest);
return restResponse.Content; return restResponse.Content;
} }
public string CreateChallonge2(int gameId, int eventId, List<WPUser> optionalPlayers) public string CreateChallonge2(int gameId, int eventId, List<WPUserDTO> optionalPlayers)
{ {
var restResponse = Post<List<WPUser>,string>($"/api/wordpress/CreateChallonge/{gameId}/{eventId}",optionalPlayers); var restResponse = Post<List<WPUserDTO>,string>($"/api/wordpress/CreateChallonge/{gameId}/{eventId}",optionalPlayers);
return restResponse; return restResponse;
} }
public bool RefreshDb() public bool RefreshDb()
@@ -122,17 +124,17 @@ namespace LaDOSE.DesktopApp.Services
return restResponse.Data; return restResponse.Data;
} }
public List<WPUser> GetUsers(int wpEventId, int gameId) public List<WPUserDTO> GetUsers(int wpEventId, int gameId)
{ {
var restRequest = new RestRequest($"/api/Wordpress/GetUsers/{wpEventId}/{gameId}", Method.GET); var restRequest = new RestRequest($"/api/Wordpress/GetUsers/{wpEventId}/{gameId}", Method.GET);
var restResponse = Client.Get<List<WPUser>>(restRequest); var restResponse = Client.Get<List<WPUserDTO>>(restRequest);
return restResponse.Data; return restResponse.Data;
} }
public List<WPUser> GetUsersOptions(int wpEventId, int gameId) public List<WPUserDTO> GetUsersOptions(int wpEventId, int gameId)
{ {
var restRequest = new RestRequest($"/api/Wordpress/GetUsersOptions/{wpEventId}/{gameId}", Method.GET); var restRequest = new RestRequest($"/api/Wordpress/GetUsersOptions/{wpEventId}/{gameId}", Method.GET);
var restResponse = Client.Get<List<WPUser>>(restRequest); var restResponse = Client.Get<List<WPUserDTO>>(restRequest);
return restResponse.Data; return restResponse.Data;
} }
@@ -140,14 +142,14 @@ namespace LaDOSE.DesktopApp.Services
#endregion #endregion
#region Games #region Games
public List<Game> GetGames() public List<GameDTO> GetGames()
{ {
var restRequest = new RestRequest("/api/Game", Method.GET); var restRequest = new RestRequest("/api/Game", Method.GET);
var restResponse = Client.Get<List<Game>>(restRequest); var restResponse = Client.Get<List<GameDTO>>(restRequest);
return restResponse.Data; return restResponse.Data;
} }
public Game UpdateGame(Game eventUpdate) public GameDTO UpdateGame(GameDTO eventUpdate)
{ {
return Post("Api/Game", eventUpdate); return Post("Api/Game", eventUpdate);
} }

View File

@@ -9,13 +9,13 @@ namespace LaDOSE.DesktopApp.ViewModels
{ {
public override string DisplayName => "Games"; public override string DisplayName => "Games";
private Game _currentGame; private GameDTO _currentGame;
private List<Game> _games; private List<GameDTO> _games;
private RestService RestService { get; set; } private RestService RestService { get; set; }
public GameViewModel(RestService restService) public GameViewModel(RestService restService)
{ {
this.RestService = restService; this.RestService = restService;
this.Games=new List<Game>(); this.Games=new List<GameDTO>();
} }
@@ -32,7 +32,7 @@ namespace LaDOSE.DesktopApp.ViewModels
NotifyOfPropertyChange("Games"); NotifyOfPropertyChange("Games");
} }
public List<Game> Games public List<GameDTO> Games
{ {
get => _games; get => _games;
set set
@@ -42,7 +42,7 @@ namespace LaDOSE.DesktopApp.ViewModels
} }
} }
public Game CurrentGame public GameDTO CurrentGame
{ {
get => _currentGame; get => _currentGame;
set set
@@ -59,7 +59,7 @@ namespace LaDOSE.DesktopApp.ViewModels
} }
public void AddGame() public void AddGame()
{ {
var item = new Game(); var item = new GameDTO();
this.Games.Add(item); this.Games.Add(item);
this.CurrentGame = item; this.CurrentGame = item;
} }

View File

@@ -18,20 +18,20 @@ namespace LaDOSE.DesktopApp.ViewModels
public class WordPressViewModel : Screen public class WordPressViewModel : Screen
{ {
public override string DisplayName => "Events"; public override string DisplayName => "Events";
private WPEvent _selectedWpEvent; private WPEventDTO _selectedWpEvent;
private Game _selectedGame; private GameDTO _selectedGame;
private ObservableCollection<WPUser> _players; private ObservableCollection<WPUserDTO> _players;
private ObservableCollection<WPUser> _playersOptions; private ObservableCollection<WPUserDTO> _playersOptions;
private ObservableCollection<WPUser> _optionalPlayers; private ObservableCollection<WPUserDTO> _optionalPlayers;
private RestService RestService { get; set; } private RestService RestService { get; set; }
public WordPressViewModel(RestService restService) public WordPressViewModel(RestService restService)
{ {
this.RestService = restService; this.RestService = restService;
Players = new ObservableCollection<WPUser>(); Players = new ObservableCollection<WPUserDTO>();
PlayersOptions = new ObservableCollection<WPUser>(); PlayersOptions = new ObservableCollection<WPUserDTO>();
OptionalPlayers = new ObservableCollection<WPUser>(); OptionalPlayers = new ObservableCollection<WPUserDTO>();
} }
#region Auto Property #region Auto Property
@@ -49,9 +49,9 @@ namespace LaDOSE.DesktopApp.ViewModels
get { return SelectedWpEvent != null && SelectedGame != null && Players?.Count() > 0; } get { return SelectedWpEvent != null && SelectedGame != null && Players?.Count() > 0; }
} }
public List<WPEvent> Events { get; set; } public List<WPEventDTO> Events { get; set; }
public WPEvent SelectedWpEvent public WPEventDTO SelectedWpEvent
{ {
get => _selectedWpEvent; get => _selectedWpEvent;
set set
@@ -62,7 +62,7 @@ namespace LaDOSE.DesktopApp.ViewModels
} }
} }
public Game SelectedGame public GameDTO SelectedGame
{ {
get => _selectedGame; get => _selectedGame;
set set
@@ -82,7 +82,7 @@ namespace LaDOSE.DesktopApp.ViewModels
} }
} }
public ObservableCollection<WPUser> Players public ObservableCollection<WPUserDTO> Players
{ {
get => _players; get => _players;
set set
@@ -92,7 +92,7 @@ namespace LaDOSE.DesktopApp.ViewModels
} }
} }
public ObservableCollection<WPUser> PlayersOptions public ObservableCollection<WPUserDTO> PlayersOptions
{ {
get => _playersOptions; get => _playersOptions;
set set
@@ -102,7 +102,7 @@ namespace LaDOSE.DesktopApp.ViewModels
} }
} }
public ObservableCollection<WPUser> OptionalPlayers public ObservableCollection<WPUserDTO> OptionalPlayers
{ {
get => _optionalPlayers; get => _optionalPlayers;
set set
@@ -112,8 +112,8 @@ namespace LaDOSE.DesktopApp.ViewModels
} }
} }
public ObservableCollection<Game> GamesFound { get; set; } public ObservableCollection<GameDTO> GamesFound { get; set; }
public List<Game> Games { get; set; } public List<GameDTO> Games { get; set; }
#endregion #endregion
@@ -136,7 +136,7 @@ namespace LaDOSE.DesktopApp.ViewModels
public void Generate() public void Generate()
{ {
List<WPUser> test = new List<WPUser>(); List<WPUserDTO> test = new List<WPUserDTO>();
test = OptionalPlayers.ToList(); test = OptionalPlayers.ToList();
var messageBoxText = this.RestService.CreateChallonge2(SelectedGame.Id, SelectedWpEvent.Id, test); var messageBoxText = this.RestService.CreateChallonge2(SelectedGame.Id, SelectedWpEvent.Id, test);
@@ -163,7 +163,7 @@ namespace LaDOSE.DesktopApp.ViewModels
#endregion #endregion
private void ParseGame(WPEvent selectedWpEvent) private void ParseGame(WPEventDTO selectedWpEvent)
{ {
var reservation = SelectedWpEvent.WpBookings.FirstOrDefault(); var reservation = SelectedWpEvent.WpBookings.FirstOrDefault();
var games = WpEventDeserialize.Parse(reservation.Meta); var games = WpEventDeserialize.Parse(reservation.Meta);
@@ -210,7 +210,7 @@ namespace LaDOSE.DesktopApp.ViewModels
{ {
Application.Current.Dispatcher.Invoke(() => Application.Current.Dispatcher.Invoke(() =>
System.Windows.Input.Mouse.OverrideCursor = Cursors.Wait); System.Windows.Input.Mouse.OverrideCursor = Cursors.Wait);
GamesFound = new ObservableCollection<Game>(); GamesFound = new ObservableCollection<GameDTO>();
this.Games = this.RestService.GetGames(); this.Games = this.RestService.GetGames();
this.Events = this.RestService.GetEvents(); this.Events = this.RestService.GetEvents();
@@ -219,15 +219,15 @@ namespace LaDOSE.DesktopApp.ViewModels
System.Windows.Input.Mouse.OverrideCursor = null); System.Windows.Input.Mouse.OverrideCursor = null);
} }
public List<WPUser> FindUser(int wpEventId, Game game,bool optional = false) public List<WPUserDTO> FindUser(int wpEventId, GameDTO game,bool optional = false)
{ {
string[] selectedGameWpId; string[] selectedGameWpId;
selectedGameWpId = !optional ? game.WordPressTag.Split(';') : game.WordPressTagOs.Split(';'); selectedGameWpId = !optional ? game.WordPressTag.Split(';') : game.WordPressTagOs.Split(';');
var currentWpEvent = this.Events.Where(e => e.Id == wpEventId).ToList(); var currentWpEvent = this.Events.Where(e => e.Id == wpEventId).ToList();
List<WPBooking> bookings = currentWpEvent.SelectMany(e => e.WpBookings).ToList(); List<WPBookingDTO> bookings = currentWpEvent.SelectMany(e => e.WpBookings).ToList();
List<WPUser> users = new List<WPUser>(); List<WPUserDTO> users = new List<WPUserDTO>();
foreach (var booking in bookings) foreach (var booking in bookings)
{ {
var reservations = WpEventDeserialize.Parse(booking.Meta); var reservations = WpEventDeserialize.Parse(booking.Meta);

View File

@@ -46,7 +46,7 @@
<ColumnDefinition Width="2*"></ColumnDefinition> <ColumnDefinition Width="2*"></ColumnDefinition>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<ListView Grid.Column="0" ItemsSource="{Binding ElementName=EventsList,Path=SelectedItem.WpBookings}" <ListView Grid.Column="0" ItemsSource="{Binding ElementName=EventsList,Path=SelectedItem.WpBookings}"
x:Name="BookingList" IsTextSearchEnabled="True" TextSearch.TextPath="WpUser.Name"> x:Name="BookingList" IsTextSearchEnabled="True" TextSearch.TextPath="WpUserDto.Name">
<ListView.ItemTemplate> <ListView.ItemTemplate>
<DataTemplate> <DataTemplate>
<StackPanel Orientation="Horizontal"> <StackPanel Orientation="Horizontal">

View File

@@ -0,0 +1,5 @@
using System.Collections.Generic;
namespace LaDOSE.Business.Interface
{
}