Use AutoMapper to map API models

This commit is contained in:
Thomas Hounsell 2017-05-25 21:21:40 +01:00
parent 20064159b9
commit 9f18ad6968
8 changed files with 77 additions and 117 deletions

View File

@ -31,6 +31,9 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="AutoMapper, Version=6.0.2.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005, processorArchitecture=MSIL">
<HintPath>..\packages\AutoMapper.6.0.2\lib\net45\AutoMapper.dll</HintPath>
</Reference>
<Reference Include="HtmlAgilityPack, Version=1.4.9.5, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL"> <Reference Include="HtmlAgilityPack, Version=1.4.9.5, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
<HintPath>..\packages\HtmlAgilityPack.1.4.9.5\lib\Net45\HtmlAgilityPack.dll</HintPath> <HintPath>..\packages\HtmlAgilityPack.1.4.9.5\lib\Net45\HtmlAgilityPack.dll</HintPath>
</Reference> </Reference>
@ -98,6 +101,7 @@
<Compile Include="ItemHistory.cs" /> <Compile Include="ItemHistory.cs" />
<Compile Include="ItemHistoryType.cs" /> <Compile Include="ItemHistoryType.cs" />
<Compile Include="MetaItem.cs" /> <Compile Include="MetaItem.cs" />
<Compile Include="ModelMappings.cs" />
<Compile Include="MongoConfig.cs" /> <Compile Include="MongoConfig.cs" />
<Compile Include="ProjectFamily.cs" /> <Compile Include="ProjectFamily.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />

View File

@ -196,9 +196,9 @@ public async Task<Dictionary<ProjectFamily, FrontPage>> SelectFrontPage()
} }
}); });
var dbResults = await query.ToListAsync(); List<BsonDocument> dbResults = await query.ToListAsync();
var results = from g in dbResults var results = (from g in dbResults
select new select new
{ {
Key = new Key = new
@ -219,7 +219,7 @@ public async Task<Dictionary<ProjectFamily, FrontPage>> SelectFrontPage()
Lab = i[nameof(Build.Lab)].AsString, Lab = i[nameof(Build.Lab)].AsString,
BuildTime = i[nameof(Build.BuildTime)].ToNullableUniversalTime() BuildTime = i[nameof(Build.BuildTime)].ToNullableUniversalTime()
} }
}; }).ToArray();
IEnumerable<ProjectFamily> listOfFamilies = results.GroupBy(g => g.Key.Family).Select(g => g.Key).OrderByDescending(k => k); IEnumerable<ProjectFamily> listOfFamilies = results.GroupBy(g => g.Key.Family).Select(g => g.Key).OrderByDescending(k => k);
@ -228,8 +228,11 @@ public async Task<Dictionary<ProjectFamily, FrontPage>> SelectFrontPage()
FrontPage fp = new FrontPage FrontPage fp = new FrontPage
{ {
CurrentCanary = results.Where(g => g.Key.Family == family && !g.Key.LabUrl.Contains("xbox")).SelectMany(g => g.Items).OrderByDescending(b => b.BuildTime).FirstOrDefault(), CurrentCanary = results.Where(g => g.Key.Family == family && !g.Key.LabUrl.Contains("xbox")).SelectMany(g => g.Items).OrderByDescending(b => b.BuildTime).FirstOrDefault(),
CurrentInsider = results.Where(g => g.Key.Family == family && !g.Key.LabUrl.Contains("xbox") && (g.Key.SourceType == TypeOfSource.PublicRelease || g.Key.SourceType == TypeOfSource.UpdateGDR)).SelectMany(g => g.Items).OrderByDescending(b => b.BuildTime).FirstOrDefault(), CurrentInsider = results.Where(g => g.Key.Family == family && !g.Key.LabUrl.Contains("xbox") && (g.Key.SourceType == TypeOfSource.PublicRelease || g.Key.SourceType == TypeOfSource.UpdateGDR)).SelectMany(g => g.Items)
CurrentRelease = results.Where(g => g.Key.Family == family && g.Key.LabUrl.Contains("_release") && !g.Key.LabUrl.Contains("xbox") && (g.Key.SourceType == TypeOfSource.PublicRelease || g.Key.SourceType == TypeOfSource.UpdateGDR)).SelectMany(g => g.Items).OrderByDescending(b => b.BuildTime).FirstOrDefault(), .OrderByDescending(b => b.BuildTime).FirstOrDefault(),
CurrentRelease = results
.Where(g => g.Key.Family == family && g.Key.LabUrl.Contains("_release") && !g.Key.LabUrl.Contains("xbox") && (g.Key.SourceType == TypeOfSource.PublicRelease || g.Key.SourceType == TypeOfSource.UpdateGDR))
.SelectMany(g => g.Items).OrderByDescending(b => b.BuildTime).FirstOrDefault(),
CurrentXbox = results.Where(g => g.Key.Family == family && g.Key.LabUrl.Contains("xbox")).SelectMany(g => g.Items).OrderByDescending(b => b.BuildTime).FirstOrDefault() CurrentXbox = results.Where(g => g.Key.Family == family && g.Key.LabUrl.Contains("xbox")).SelectMany(g => g.Items).OrderByDescending(b => b.BuildTime).FirstOrDefault()
}; };

View File

@ -0,0 +1,16 @@
using AutoMapper;
using BuildFeed.Model.Api;
namespace BuildFeed.Model
{
public static class ModelMappings
{
public static void Initialise()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Build, ApiBuild>();
});
}
}
}

View File

@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="AutoMapper" version="6.0.2" targetFramework="net47" />
<package id="HtmlAgilityPack" version="1.4.9.5" targetFramework="net47" /> <package id="HtmlAgilityPack" version="1.4.9.5" targetFramework="net47" />
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net47" /> <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net47" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net47" /> <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net47" />

View File

@ -62,6 +62,9 @@
<TypeScriptSourceRoot /> <TypeScriptSourceRoot />
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="AutoMapper, Version=6.0.2.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005, processorArchitecture=MSIL">
<HintPath>..\packages\AutoMapper.6.0.2\lib\net45\AutoMapper.dll</HintPath>
</Reference>
<Reference Include="HtmlAgilityPack, Version=1.4.9.5, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL"> <Reference Include="HtmlAgilityPack, Version=1.4.9.5, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
<HintPath>..\packages\HtmlAgilityPack.1.4.9.5\lib\Net45\HtmlAgilityPack.dll</HintPath> <HintPath>..\packages\HtmlAgilityPack.1.4.9.5\lib\Net45\HtmlAgilityPack.dll</HintPath>
</Reference> </Reference>

View File

@ -5,12 +5,12 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Web.Http; using System.Web.Http;
using System.Web.Security; using System.Web.Security;
using AutoMapper;
using BuildFeed.Code; using BuildFeed.Code;
using BuildFeed.Local; using BuildFeed.Local;
using BuildFeed.Model; using BuildFeed.Model;
using BuildFeed.Model.Api; using BuildFeed.Model.Api;
using BuildFeed.Model.View; using BuildFeed.Model.View;
using MongoDB.Bson;
using OneSignal.CSharp.SDK; using OneSignal.CSharp.SDK;
#pragma warning disable SG0016 // Controller method is vulnerable to CSRF - Not relevant for API #pragma warning disable SG0016 // Controller method is vulnerable to CSRF - Not relevant for API
@ -28,31 +28,8 @@ public ApiController()
public async Task<ApiBuild[]> GetBuilds(int limit = 20, int skip = 0) public async Task<ApiBuild[]> GetBuilds(int limit = 20, int skip = 0)
{ {
List<Build> builds = await _bModel.SelectBuildsByOrder(limit, skip); return (from b in await _bModel.SelectBuildsByOrder(limit, skip)
return (from b in builds select Mapper.Map<ApiBuild>(b)).ToArray();
select new ApiBuild
{
Id = b.Id,
MajorVersion = b.MajorVersion,
MinorVersion = b.MinorVersion,
Number = b.Number,
Revision = b.Revision,
Lab = b.Lab,
BuildTime = b.BuildTime,
SourceType = b.SourceType,
SourceDetails = b.SourceDetails,
LeakDate = b.LeakDate,
FullBuildString = b.FullBuildString,
AlternateBuildString = b.AlternateBuildString,
LabUrl = b.LabUrl,
Added = b.Added,
Modified = b.Modified
}).ToArray();
} }
public async Task<FrontBuildGroup[]> GetBuildGroups(int limit = 20, int skip = 0) public async Task<FrontBuildGroup[]> GetBuildGroups(int limit = 20, int skip = 0)
@ -63,68 +40,20 @@ public async Task<FrontBuildGroup[]> GetBuildGroups(int limit = 20, int skip = 0
public async Task<ApiBuild[]> GetBuildsForBuildGroup(uint major, uint minor, uint number, uint? revision = null) public async Task<ApiBuild[]> GetBuildsForBuildGroup(uint major, uint minor, uint number, uint? revision = null)
{ {
List<Build> builds = await _bModel.SelectGroup(new BuildGroup return (from b in await _bModel.SelectGroup(new BuildGroup
{ {
Major = major, Major = major,
Minor = minor, Minor = minor,
Build = number, Build = number,
Revision = revision Revision = revision
}); })
select Mapper.Map<ApiBuild>(b)).ToArray();
return (from b in builds
select new ApiBuild
{
Id = b.Id,
MajorVersion = b.MajorVersion,
MinorVersion = b.MinorVersion,
Number = b.Number,
Revision = b.Revision,
Lab = b.Lab,
BuildTime = b.BuildTime,
SourceType = b.SourceType,
SourceDetails = b.SourceDetails,
LeakDate = b.LeakDate,
FullBuildString = b.FullBuildString,
AlternateBuildString = b.AlternateBuildString,
LabUrl = b.LabUrl,
Added = b.Added,
Modified = b.Modified
}).ToArray();
} }
public async Task<ApiBuild[]> GetBuildsByLab(string lab, int limit = 20, int skip = 0) public async Task<ApiBuild[]> GetBuildsByLab(string lab, int limit = 20, int skip = 0)
{ {
List<Build> builds = await _bModel.SelectLab(lab, limit, skip); return (from b in await _bModel.SelectLab(lab, limit, skip)
select Mapper.Map<ApiBuild>(b)).ToArray();
return (from b in builds
select new ApiBuild
{
Id = b.Id,
MajorVersion = b.MajorVersion,
MinorVersion = b.MinorVersion,
Number = b.Number,
Revision = b.Revision,
Lab = b.Lab,
BuildTime = b.BuildTime,
SourceType = b.SourceType,
SourceDetails = b.SourceDetails,
LeakDate = b.LeakDate,
FullBuildString = b.FullBuildString,
AlternateBuildString = b.AlternateBuildString,
LabUrl = b.LabUrl,
Added = b.Added,
Modified = b.Modified
}).ToArray();
} }
public async Task<List<FamilyOverview>> GetFamilyOverviews() public async Task<List<FamilyOverview>> GetFamilyOverviews()
@ -211,7 +140,7 @@ public async Task<IEnumerable<SearchResult>> GetSearchResult(string id, int maxR
{ {
if (string.IsNullOrWhiteSpace(id)) if (string.IsNullOrWhiteSpace(id))
{ {
return new SearchResult[0]; return Array.Empty<SearchResult>();
} }
var results = new List<SearchResult>(); var results = new List<SearchResult>();

View File

@ -31,6 +31,8 @@ protected void Application_Start()
ModelBinders.Binders.Add(typeof(DateTime), db); ModelBinders.Binders.Add(typeof(DateTime), db);
ModelBinders.Binders.Add(typeof(DateTime?), db); ModelBinders.Binders.Add(typeof(DateTime?), db);
ModelMappings.Initialise();
Roles.CreateRole("Administrators"); Roles.CreateRole("Administrators");
Roles.CreateRole("Editors"); Roles.CreateRole("Editors");
Roles.CreateRole("Users"); Roles.CreateRole("Users");

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="AutoMapper" version="6.0.2" targetFramework="net47" />
<package id="google.analytics.TypeScript.DefinitelyTyped" version="0.3.8" targetFramework="net47" /> <package id="google.analytics.TypeScript.DefinitelyTyped" version="0.3.8" targetFramework="net47" />
<package id="HtmlAgilityPack" version="1.4.9.5" targetFramework="net47" /> <package id="HtmlAgilityPack" version="1.4.9.5" targetFramework="net47" />
<package id="Humanizer" version="2.2.0" targetFramework="net47" /> <package id="Humanizer" version="2.2.0" targetFramework="net47" />