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,30 +196,30 @@ 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
{ {
Family = (ProjectFamily)g["_id"].AsBsonDocument[nameof(Build.Family)].AsInt32, Family = (ProjectFamily)g["_id"].AsBsonDocument[nameof(Build.Family)].AsInt32,
LabUrl = g["_id"].AsBsonDocument[nameof(Build.LabUrl)].AsString, LabUrl = g["_id"].AsBsonDocument[nameof(Build.LabUrl)].AsString,
SourceType = (TypeOfSource)g["_id"].AsBsonDocument[nameof(Build.SourceType)].AsInt32 SourceType = (TypeOfSource)g["_id"].AsBsonDocument[nameof(Build.SourceType)].AsInt32
}, },
Items = from i in g["items"].AsBsonArray Items = from i in g["items"].AsBsonArray
select new FrontPageBuild select new FrontPageBuild
{ {
Id = i[nameof(Build.Id)].AsGuid, Id = i[nameof(Build.Id)].AsGuid,
MajorVersion = (uint)i[nameof(Build.MajorVersion)].AsInt32, MajorVersion = (uint)i[nameof(Build.MajorVersion)].AsInt32,
MinorVersion = (uint)i[nameof(Build.MinorVersion)].AsInt32, MinorVersion = (uint)i[nameof(Build.MinorVersion)].AsInt32,
Number = (uint)i[nameof(Build.Number)].AsInt32, Number = (uint)i[nameof(Build.Number)].AsInt32,
Revision = (uint?)i[nameof(Build.Revision)].AsNullableInt32, Revision = (uint?)i[nameof(Build.Revision)].AsNullableInt32,
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,12 +1,14 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="HtmlAgilityPack" version="1.4.9.5" targetFramework="net47" /> <package id="AutoMapper" version="6.0.2" targetFramework="net47" />
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net47" /> <package id="HtmlAgilityPack" version="1.4.9.5" targetFramework="net47" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net47" /> <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net47" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net47" /> <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net47" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net47" /> <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net47" />
<package id="MongoDB.Bson" version="2.4.3" targetFramework="net47" /> <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net47" />
<package id="MongoDB.Driver" version="2.4.3" targetFramework="net47" /> <package id="MongoDB.Bson" version="2.4.3" targetFramework="net47" />
<package id="MongoDB.Driver.Core" version="2.4.3" targetFramework="net47" /> <package id="MongoDB.Driver" version="2.4.3" targetFramework="net47" />
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net47" /> <package id="MongoDB.Driver.Core" version="2.4.3" targetFramework="net47" />
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net47" />
</packages> </packages>

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,
Minor = minor,
Build = number,
Revision = revision
});
return (from b in builds
select new ApiBuild
{ {
Id = b.Id, Major = major,
Minor = minor,
MajorVersion = b.MajorVersion, Build = number,
MinorVersion = b.MinorVersion, Revision = revision
Number = b.Number, })
Revision = b.Revision, select Mapper.Map<ApiBuild>(b)).ToArray();
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" />