add automapper

This commit is contained in:
chsakell
2016-09-30 10:36:22 +03:00
parent a0cc12f3fb
commit d0690695b6
8 changed files with 71 additions and 5 deletions

View File

@@ -7,6 +7,7 @@ using Microsoft.AspNetCore.SignalR.Infrastructure;
using LiveGameFeed.Hubs;
using LiveGameFeed.Data.Abstract;
using LiveGameFeed.Models;
using AutoMapper;
namespace LiveGameFeed.Controllers
{
@@ -24,18 +25,21 @@ namespace LiveGameFeed.Controllers
// GET api/values
[HttpGet]
public IEnumerable<Match> Get()
public IEnumerable<MatchViewModel> Get()
{
IEnumerable<Match> _matches = _matchRepository.GetAll();
IEnumerable<MatchViewModel> _matchesVM = Mapper.Map<IEnumerable<Match>, IEnumerable<MatchViewModel>>(_matches);
return _matches;
return _matchesVM;
}
// GET api/values/5
[HttpGet("{id}")]
public Match Get(int id)
public MatchViewModel Get(int id)
{
return _matchRepository.GetSingle(id);
Match _match = _matchRepository.GetSingle(id);
MatchViewModel _matchVM = Mapper.Map<Match, MatchViewModel>(_match);
return _matchVM;
}
// POST api/values

View File

@@ -0,0 +1,15 @@
using AutoMapper;
namespace LiveGameFeed.Mappings
{
public class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(x =>
{
x.AddProfile<DomainToViewModelMappingProfile>();
});
}
}
}

View File

@@ -0,0 +1,14 @@
using AutoMapper;
using LiveGameFeed.Models;
namespace LiveGameFeed.Mappings
{
public class DomainToViewModelMappingProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<Match, MatchViewModel>();
Mapper.CreateMap<Feed, FeedViewModel>();
}
}
}

12
Models/FeedViewModel.cs Normal file
View File

@@ -0,0 +1,12 @@
using System;
namespace LiveGameFeed.Models
{
public class FeedViewModel : IEntityBase
{
public int Id { get; set; }
public string Description { get; set; }
public DateTime CreatedAt { get; set; }
public int MatchId { get; set; }
}
}

16
Models/MatchViewModel.cs Normal file
View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
namespace LiveGameFeed.Models
{
public class MatchViewModel : IEntityBase
{
public int Id { get; set; }
public string Host { get; set; }
public string Guest { get; set; }
public int HostScore { get; set; }
public int GuestScore { get; set; }
public DateTime MatchDate { get; set; }
public string League { get; set; }
}
}

View File

@@ -11,6 +11,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using LiveGameFeed.Mappings;
namespace LiveGameFeed
{
@@ -36,6 +37,9 @@ namespace LiveGameFeed
// Repositories
services.AddScoped<IMatchRepository, MatchRepository>();
services.AddScoped<IFeedRepository, FeedRepository>();
// Automapper Configuration
AutoMapperConfiguration.Configure();
// Add framework services.
services.AddMvc();

View File

@@ -19,7 +19,7 @@
<tr>
<td colspan="2">
<button type="button" class="btn btn-default btn-lg btn-block">
Subscribe
Subscribe to feed
</button>
</td>
</tr>

View File

@@ -1,5 +1,6 @@
{
"dependencies": {
"AutoMapper.Data": "1.0.0-beta1",
"Microsoft.NETCore.App": {
"version": "1.0.0-*",
"type": "platform"