mirror of
https://github.com/chsakell/aspnet-core-signalr-angular.git
synced 2025-12-22 17:27:48 +00:00
add automapper
This commit is contained in:
@@ -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
|
||||
|
||||
15
Mappings/AutoMapperConfiguration.cs
Normal file
15
Mappings/AutoMapperConfiguration.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using AutoMapper;
|
||||
|
||||
namespace LiveGameFeed.Mappings
|
||||
{
|
||||
public class AutoMapperConfiguration
|
||||
{
|
||||
public static void Configure()
|
||||
{
|
||||
Mapper.Initialize(x =>
|
||||
{
|
||||
x.AddProfile<DomainToViewModelMappingProfile>();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Mappings/DomainToViewModelMappingProfile.cs
Normal file
14
Mappings/DomainToViewModelMappingProfile.cs
Normal 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
12
Models/FeedViewModel.cs
Normal 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
16
Models/MatchViewModel.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using LiveGameFeed.Mappings;
|
||||
|
||||
namespace LiveGameFeed
|
||||
{
|
||||
@@ -37,6 +38,9 @@ namespace LiveGameFeed
|
||||
services.AddScoped<IMatchRepository, MatchRepository>();
|
||||
services.AddScoped<IFeedRepository, FeedRepository>();
|
||||
|
||||
// Automapper Configuration
|
||||
AutoMapperConfiguration.Configure();
|
||||
|
||||
// Add framework services.
|
||||
services.AddMvc();
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"AutoMapper.Data": "1.0.0-beta1",
|
||||
"Microsoft.NETCore.App": {
|
||||
"version": "1.0.0-*",
|
||||
"type": "platform"
|
||||
|
||||
Reference in New Issue
Block a user