using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using MongoDB.Bson; using MongoDB.Driver; namespace BuildFeed.Model { public partial class BuildRepository { public async Task SelectAllYears(int limit = -1, int skip = 0) { var query = _buildCollection.Aggregate() .Match(Builders.Filter.Ne(b => b.BuildTime, null)) .Group(new BsonDocument("_id", new BsonDocument("$year", $"${nameof(Build.BuildTime)}"))) .Sort(new BsonDocument("_id", -1)) .Skip(skip); if (limit > 0) { query = query.Limit(limit); } var grouping = await query.ToListAsync(); return (from g in grouping where !g["_id"].IsBsonNull select g["_id"].AsInt32).ToArray(); } public async Task SelectAllYearsCount() { var query = await _buildCollection.Aggregate() .Match(Builders.Filter.Ne(b => b.BuildTime, null)) .Group(new BsonDocument("_id", new BsonDocument("$year", $"${nameof(Build.BuildTime)}"))) .ToListAsync(); return query.Count; } public async Task> SelectYear(int year, int limit = -1, int skip = 0) { var query = _buildCollection.Find(Builders.Filter.And(Builders.Filter.Gte(b => b.BuildTime, new DateTime(year, 1, 1, 0, 0, 0, DateTimeKind.Utc)), Builders.Filter.Lte(b => b.BuildTime, new DateTime(year, 12, 31, 23, 59, 59, DateTimeKind.Utc)))) .Sort(sortByCompileDate) .Skip(skip); if (limit > 0) { query = query.Limit(limit); } return await query.ToListAsync(); } public async Task SelectYearCount(int year) => await _buildCollection.CountDocumentsAsync(Builders.Filter.And(Builders.Filter.Gte(b => b.BuildTime, new DateTime(year, 1, 1, 0, 0, 0, DateTimeKind.Utc)), Builders.Filter.Lte(b => b.BuildTime, new DateTime(year, 12, 31, 23, 59, 59, DateTimeKind.Utc)))); } }