Add AutoMapper
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
37
LaDOSE.Src/LaDOSE.Api/Controllers/GenericControllerDTO.cs
Normal file
37
LaDOSE.Src/LaDOSE.Api/Controllers/GenericControllerDTO.cs
Normal 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));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
|
||||
}
|
||||
|
||||
|
||||
15
LaDOSE.Src/LaDOSE.Api/Helpers/AutoMapperTwoWay.cs
Normal file
15
LaDOSE.Src/LaDOSE.Api/Helpers/AutoMapperTwoWay.cs
Normal 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();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
@@ -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
|
||||
{
|
||||
@@ -108,6 +111,16 @@ namespace LaDOSE.Api
|
||||
|
||||
// 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)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace LaDOSE.DTO
|
||||
{
|
||||
public class Game
|
||||
public class GameDTO
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
@@ -1,8 +1,8 @@
|
||||
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 Meta { get; set; }
|
||||
@@ -4,7 +4,7 @@ using System.Collections.Generic;
|
||||
|
||||
namespace LaDOSE.DTO
|
||||
{
|
||||
public class WPEvent
|
||||
public class WPEventDTO
|
||||
{
|
||||
|
||||
// Id, Name, Slug, Date
|
||||
@@ -12,6 +12,6 @@ namespace LaDOSE.DTO
|
||||
public string Name { get; set; }
|
||||
public string Slug { get; set; }
|
||||
public DateTime? Date { get; set; }
|
||||
public List<WPBooking> WpBookings { get; set; }
|
||||
public List<WPBookingDTO> WpBookings { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace LaDOSE.DTO
|
||||
{
|
||||
public class WPUser
|
||||
public class WPUserDTO
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
@@ -17,7 +17,9 @@ namespace LaDOSE.DesktopApp.Services
|
||||
|
||||
public RestService()
|
||||
{
|
||||
|
||||
#if DEBUG
|
||||
MessageBox.Show("WAIT");
|
||||
#endif
|
||||
var appSettings = ConfigurationManager.AppSettings;
|
||||
string url = (string) appSettings["ApiUri"];
|
||||
string user = (string)appSettings["ApiUser"];
|
||||
@@ -95,10 +97,10 @@ namespace LaDOSE.DesktopApp.Services
|
||||
#endregion
|
||||
|
||||
#region WordPress
|
||||
public List<WPEvent> GetEvents()
|
||||
public List<WPEventDTO> GetEvents()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -109,10 +111,10 @@ namespace LaDOSE.DesktopApp.Services
|
||||
var restResponse = Client.Get(restRequest);
|
||||
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;
|
||||
}
|
||||
public bool RefreshDb()
|
||||
@@ -122,17 +124,17 @@ namespace LaDOSE.DesktopApp.Services
|
||||
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 restResponse = Client.Get<List<WPUser>>(restRequest);
|
||||
var restResponse = Client.Get<List<WPUserDTO>>(restRequest);
|
||||
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 restResponse = Client.Get<List<WPUser>>(restRequest);
|
||||
var restResponse = Client.Get<List<WPUserDTO>>(restRequest);
|
||||
return restResponse.Data;
|
||||
}
|
||||
|
||||
@@ -140,14 +142,14 @@ namespace LaDOSE.DesktopApp.Services
|
||||
#endregion
|
||||
|
||||
#region Games
|
||||
public List<Game> GetGames()
|
||||
public List<GameDTO> GetGames()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
public Game UpdateGame(Game eventUpdate)
|
||||
public GameDTO UpdateGame(GameDTO eventUpdate)
|
||||
{
|
||||
return Post("Api/Game", eventUpdate);
|
||||
}
|
||||
|
||||
@@ -9,13 +9,13 @@ namespace LaDOSE.DesktopApp.ViewModels
|
||||
{
|
||||
public override string DisplayName => "Games";
|
||||
|
||||
private Game _currentGame;
|
||||
private List<Game> _games;
|
||||
private GameDTO _currentGame;
|
||||
private List<GameDTO> _games;
|
||||
private RestService RestService { get; set; }
|
||||
public GameViewModel(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");
|
||||
}
|
||||
|
||||
public List<Game> Games
|
||||
public List<GameDTO> Games
|
||||
{
|
||||
get => _games;
|
||||
set
|
||||
@@ -42,7 +42,7 @@ namespace LaDOSE.DesktopApp.ViewModels
|
||||
}
|
||||
}
|
||||
|
||||
public Game CurrentGame
|
||||
public GameDTO CurrentGame
|
||||
{
|
||||
get => _currentGame;
|
||||
set
|
||||
@@ -59,7 +59,7 @@ namespace LaDOSE.DesktopApp.ViewModels
|
||||
}
|
||||
public void AddGame()
|
||||
{
|
||||
var item = new Game();
|
||||
var item = new GameDTO();
|
||||
this.Games.Add(item);
|
||||
this.CurrentGame = item;
|
||||
}
|
||||
|
||||
@@ -18,20 +18,20 @@ namespace LaDOSE.DesktopApp.ViewModels
|
||||
public class WordPressViewModel : Screen
|
||||
{
|
||||
public override string DisplayName => "Events";
|
||||
private WPEvent _selectedWpEvent;
|
||||
private Game _selectedGame;
|
||||
private ObservableCollection<WPUser> _players;
|
||||
private ObservableCollection<WPUser> _playersOptions;
|
||||
private ObservableCollection<WPUser> _optionalPlayers;
|
||||
private WPEventDTO _selectedWpEvent;
|
||||
private GameDTO _selectedGame;
|
||||
private ObservableCollection<WPUserDTO> _players;
|
||||
private ObservableCollection<WPUserDTO> _playersOptions;
|
||||
private ObservableCollection<WPUserDTO> _optionalPlayers;
|
||||
|
||||
private RestService RestService { get; set; }
|
||||
|
||||
public WordPressViewModel(RestService restService)
|
||||
{
|
||||
this.RestService = restService;
|
||||
Players = new ObservableCollection<WPUser>();
|
||||
PlayersOptions = new ObservableCollection<WPUser>();
|
||||
OptionalPlayers = new ObservableCollection<WPUser>();
|
||||
Players = new ObservableCollection<WPUserDTO>();
|
||||
PlayersOptions = new ObservableCollection<WPUserDTO>();
|
||||
OptionalPlayers = new ObservableCollection<WPUserDTO>();
|
||||
}
|
||||
|
||||
#region Auto Property
|
||||
@@ -49,9 +49,9 @@ namespace LaDOSE.DesktopApp.ViewModels
|
||||
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;
|
||||
set
|
||||
@@ -62,7 +62,7 @@ namespace LaDOSE.DesktopApp.ViewModels
|
||||
}
|
||||
}
|
||||
|
||||
public Game SelectedGame
|
||||
public GameDTO SelectedGame
|
||||
{
|
||||
get => _selectedGame;
|
||||
set
|
||||
@@ -82,7 +82,7 @@ namespace LaDOSE.DesktopApp.ViewModels
|
||||
}
|
||||
}
|
||||
|
||||
public ObservableCollection<WPUser> Players
|
||||
public ObservableCollection<WPUserDTO> Players
|
||||
{
|
||||
get => _players;
|
||||
set
|
||||
@@ -92,7 +92,7 @@ namespace LaDOSE.DesktopApp.ViewModels
|
||||
}
|
||||
}
|
||||
|
||||
public ObservableCollection<WPUser> PlayersOptions
|
||||
public ObservableCollection<WPUserDTO> PlayersOptions
|
||||
{
|
||||
get => _playersOptions;
|
||||
set
|
||||
@@ -102,7 +102,7 @@ namespace LaDOSE.DesktopApp.ViewModels
|
||||
}
|
||||
}
|
||||
|
||||
public ObservableCollection<WPUser> OptionalPlayers
|
||||
public ObservableCollection<WPUserDTO> OptionalPlayers
|
||||
{
|
||||
get => _optionalPlayers;
|
||||
set
|
||||
@@ -112,8 +112,8 @@ namespace LaDOSE.DesktopApp.ViewModels
|
||||
}
|
||||
}
|
||||
|
||||
public ObservableCollection<Game> GamesFound { get; set; }
|
||||
public List<Game> Games { get; set; }
|
||||
public ObservableCollection<GameDTO> GamesFound { get; set; }
|
||||
public List<GameDTO> Games { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -136,7 +136,7 @@ namespace LaDOSE.DesktopApp.ViewModels
|
||||
|
||||
public void Generate()
|
||||
{
|
||||
List<WPUser> test = new List<WPUser>();
|
||||
List<WPUserDTO> test = new List<WPUserDTO>();
|
||||
test = OptionalPlayers.ToList();
|
||||
var messageBoxText = this.RestService.CreateChallonge2(SelectedGame.Id, SelectedWpEvent.Id, test);
|
||||
|
||||
@@ -163,7 +163,7 @@ namespace LaDOSE.DesktopApp.ViewModels
|
||||
|
||||
#endregion
|
||||
|
||||
private void ParseGame(WPEvent selectedWpEvent)
|
||||
private void ParseGame(WPEventDTO selectedWpEvent)
|
||||
{
|
||||
var reservation = SelectedWpEvent.WpBookings.FirstOrDefault();
|
||||
var games = WpEventDeserialize.Parse(reservation.Meta);
|
||||
@@ -210,7 +210,7 @@ namespace LaDOSE.DesktopApp.ViewModels
|
||||
{
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
System.Windows.Input.Mouse.OverrideCursor = Cursors.Wait);
|
||||
GamesFound = new ObservableCollection<Game>();
|
||||
GamesFound = new ObservableCollection<GameDTO>();
|
||||
this.Games = this.RestService.GetGames();
|
||||
this.Events = this.RestService.GetEvents();
|
||||
|
||||
@@ -219,15 +219,15 @@ namespace LaDOSE.DesktopApp.ViewModels
|
||||
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;
|
||||
selectedGameWpId = !optional ? game.WordPressTag.Split(';') : game.WordPressTagOs.Split(';');
|
||||
|
||||
var currentWpEvent = this.Events.Where(e => e.Id == wpEventId).ToList();
|
||||
List<WPBooking> bookings = currentWpEvent.SelectMany(e => e.WpBookings).ToList();
|
||||
List<WPUser> users = new List<WPUser>();
|
||||
List<WPBookingDTO> bookings = currentWpEvent.SelectMany(e => e.WpBookings).ToList();
|
||||
List<WPUserDTO> users = new List<WPUserDTO>();
|
||||
foreach (var booking in bookings)
|
||||
{
|
||||
var reservations = WpEventDeserialize.Parse(booking.Meta);
|
||||
|
||||
@@ -46,7 +46,7 @@
|
||||
<ColumnDefinition Width="2*"></ColumnDefinition>
|
||||
</Grid.ColumnDefinitions>
|
||||
<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>
|
||||
<DataTemplate>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
|
||||
5
LaDOSE.Src/LaDOSE.Service/Interface/IBaseServiceDTO.cs
Normal file
5
LaDOSE.Src/LaDOSE.Service/Interface/IBaseServiceDTO.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace LaDOSE.Business.Interface
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user