Add a project family overview API

This commit is contained in:
Thomas Hounsell 2017-04-22 23:21:02 +01:00
parent 455f48c2ac
commit e244c823ab
4 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1,14 @@
using MongoDB.Bson.Serialization.Attributes;
namespace BuildFeed.Model.Api
{
public class FamilyOverview
{
[BsonElement("_id")]
public int Family { get; set; }
public ulong Count { get; set; }
public BuildDetails Latest { get; set; }
}
}

View File

@ -90,11 +90,13 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Api\ApiBuild.cs" />
<Compile Include="Api\FamilyOverview.cs" />
<Compile Include="Api\NewBuild.cs" />
<Compile Include="Api\SearchResult.cs" />
<Compile Include="BuildDetails.cs" />
<Compile Include="BuildRepository-Group.cs" />
<Compile Include="BuildRepository-Lab.cs" />
<Compile Include="BuildRepository-Family.cs" />
<Compile Include="BuildRepository-Source.cs" />
<Compile Include="BuildRepository-Version.cs" />
<Compile Include="BuildRepository-Year.cs" />

View File

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BuildFeed.Model.Api;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
namespace BuildFeed.Model
{
public partial class BuildRepository
{
public Task<ProjectFamily[]> SelectAllFamilies(int limit = -1, int skip = 0) => Task.Run(() => Enum.GetValues(typeof(ProjectFamily)) as ProjectFamily[]);
public Task<long> SelectAllFamiliesCount() => Task.Run(() => Enum.GetValues(typeof(ProjectFamily)).LongLength);
public async Task<List<Build>> SelectFamily(ProjectFamily family, int limit = -1, int skip = 0)
{
IFindFluent<Build, Build> query = _buildCollection.Find(new BsonDocument(nameof(Build.Family), family)).Sort(sortByOrder).Skip(skip);
if (limit > 0)
{
query = query.Limit(limit);
}
return await query.ToListAsync();
}
public async Task<long> SelectFamilyCount(ProjectFamily family) => await _buildCollection.CountAsync(new BsonDocument(nameof(Build.Family), family));
public async Task<List<FamilyOverview>> SelectFamilyOverviews()
{
IAggregateFluent<BsonDocument> families = _buildCollection.Aggregate()
.Sort(sortByOrder)
.Group(new BsonDocument
{
new BsonElement("_id", $"${nameof(Build.Family)}"),
new BsonElement(nameof(FamilyOverview.Count), new BsonDocument("$sum", 1)),
new BsonElement(nameof(FamilyOverview.Latest), new BsonDocument("$first", "$$CURRENT"))
})
.Sort(new BsonDocument("_id", -1));
List<BsonDocument> result = await families.ToListAsync();
return (from o in result
select BsonSerializer.Deserialize<FamilyOverview>(o)).ToList();
}
}
}

View File

@ -10,6 +10,7 @@
using BuildFeed.Model;
using BuildFeed.Model.Api;
using BuildFeed.Model.View;
using MongoDB.Bson;
using OneSignal.CSharp.SDK;
#pragma warning disable SG0016 // Controller method is vulnerable to CSRF - Not relevant for API
@ -126,6 +127,11 @@ public async Task<ApiBuild[]> GetBuildsByLab(string lab, int limit = 20, int ski
}).ToArray();
}
public async Task<List<FamilyOverview>> GetFamilyOverviews()
{
return await _bModel.SelectFamilyOverviews();
}
public async Task<IEnumerable<string>> GetWin10Labs()
{
var labs = new List<string>();