From e244c823ab94d7425b5dcd7dc5de6709aeb003ca Mon Sep 17 00:00:00 2001 From: Thomas Hounsell Date: Sat, 22 Apr 2017 23:21:02 +0100 Subject: [PATCH] Add a project family overview API --- BuildFeed.Model/Api/FamilyOverview.cs | 14 +++++++ BuildFeed.Model/BuildFeed.Model.csproj | 2 + BuildFeed.Model/BuildRepository-Family.cs | 50 +++++++++++++++++++++++ BuildFeed/Controllers/apiController.cs | 6 +++ 4 files changed, 72 insertions(+) create mode 100644 BuildFeed.Model/Api/FamilyOverview.cs create mode 100644 BuildFeed.Model/BuildRepository-Family.cs diff --git a/BuildFeed.Model/Api/FamilyOverview.cs b/BuildFeed.Model/Api/FamilyOverview.cs new file mode 100644 index 0000000..455007a --- /dev/null +++ b/BuildFeed.Model/Api/FamilyOverview.cs @@ -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; } + } +} \ No newline at end of file diff --git a/BuildFeed.Model/BuildFeed.Model.csproj b/BuildFeed.Model/BuildFeed.Model.csproj index 08c2889..28eb0cb 100644 --- a/BuildFeed.Model/BuildFeed.Model.csproj +++ b/BuildFeed.Model/BuildFeed.Model.csproj @@ -90,11 +90,13 @@ + + diff --git a/BuildFeed.Model/BuildRepository-Family.cs b/BuildFeed.Model/BuildRepository-Family.cs new file mode 100644 index 0000000..b2bbf76 --- /dev/null +++ b/BuildFeed.Model/BuildRepository-Family.cs @@ -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 SelectAllFamilies(int limit = -1, int skip = 0) => Task.Run(() => Enum.GetValues(typeof(ProjectFamily)) as ProjectFamily[]); + + public Task SelectAllFamiliesCount() => Task.Run(() => Enum.GetValues(typeof(ProjectFamily)).LongLength); + + public async Task> SelectFamily(ProjectFamily family, int limit = -1, int skip = 0) + { + IFindFluent 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 SelectFamilyCount(ProjectFamily family) => await _buildCollection.CountAsync(new BsonDocument(nameof(Build.Family), family)); + + public async Task> SelectFamilyOverviews() + { + IAggregateFluent 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 result = await families.ToListAsync(); + + return (from o in result + select BsonSerializer.Deserialize(o)).ToList(); + } + } +} \ No newline at end of file diff --git a/BuildFeed/Controllers/apiController.cs b/BuildFeed/Controllers/apiController.cs index 7be73ff..a7a04d3 100644 --- a/BuildFeed/Controllers/apiController.cs +++ b/BuildFeed/Controllers/apiController.cs @@ -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 GetBuildsByLab(string lab, int limit = 20, int ski }).ToArray(); } + public async Task> GetFamilyOverviews() + { + return await _bModel.SelectFamilyOverviews(); + } + public async Task> GetWin10Labs() { var labs = new List();