2016-07-15 21:39:30 +08:00
|
|
|
|
using System.Collections.Generic;
|
2015-11-10 00:43:05 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-01-12 05:16:29 +08:00
|
|
|
|
using MongoDB.Bson;
|
|
|
|
|
using MongoDB.Driver;
|
2015-11-10 00:43:05 +08:00
|
|
|
|
|
2016-08-20 05:10:55 +08:00
|
|
|
|
namespace BuildFeed.Model
|
2015-11-10 00:43:05 +08:00
|
|
|
|
{
|
2017-02-24 04:53:49 +08:00
|
|
|
|
public partial class BuildRepository
|
|
|
|
|
{
|
|
|
|
|
public async Task<BuildVersion[]> SelectAllVersions(int limit = -1, int skip = 0)
|
|
|
|
|
{
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var query = _buildCollection.Aggregate()
|
|
|
|
|
.Group(new BsonDocument("_id",
|
|
|
|
|
new BsonDocument
|
|
|
|
|
{
|
|
|
|
|
new BsonElement(nameof(BuildVersion.Major), $"${nameof(Build.MajorVersion)}"),
|
|
|
|
|
new BsonElement(nameof(BuildVersion.Minor), $"${nameof(Build.MinorVersion)}")
|
|
|
|
|
}))
|
|
|
|
|
.Sort(new BsonDocument
|
2017-02-24 04:53:49 +08:00
|
|
|
|
{
|
2018-02-07 06:16:37 +08:00
|
|
|
|
new BsonElement($"_id.{nameof(BuildVersion.Major)}", -1),
|
|
|
|
|
new BsonElement($"_id.{nameof(BuildVersion.Minor)}", -1)
|
|
|
|
|
})
|
|
|
|
|
.Skip(skip);
|
2015-11-10 00:43:05 +08:00
|
|
|
|
|
2017-02-24 04:53:49 +08:00
|
|
|
|
if (limit > 0)
|
|
|
|
|
{
|
|
|
|
|
query = query.Limit(limit);
|
|
|
|
|
}
|
2015-11-10 00:43:05 +08:00
|
|
|
|
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var grouping = await query.ToListAsync();
|
2015-11-10 00:43:05 +08:00
|
|
|
|
|
2017-02-24 04:53:49 +08:00
|
|
|
|
return (from g in grouping
|
2018-02-07 06:16:37 +08:00
|
|
|
|
select new BuildVersion
|
|
|
|
|
{
|
|
|
|
|
Major = (uint)g["_id"].AsBsonDocument[nameof(BuildVersion.Major)].AsInt32,
|
|
|
|
|
Minor = (uint)g["_id"].AsBsonDocument[nameof(BuildVersion.Minor)].AsInt32
|
|
|
|
|
}).ToArray();
|
2017-02-24 04:53:49 +08:00
|
|
|
|
}
|
2016-01-12 05:16:29 +08:00
|
|
|
|
|
2017-02-24 04:53:49 +08:00
|
|
|
|
public async Task<long> SelectAllVersionsCount()
|
|
|
|
|
{
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var query = await _buildCollection.Aggregate()
|
|
|
|
|
.Group(new BsonDocument("_id",
|
|
|
|
|
new BsonDocument
|
|
|
|
|
{
|
|
|
|
|
new BsonElement(nameof(BuildVersion.Major), $"${nameof(Build.MajorVersion)}"),
|
|
|
|
|
new BsonElement(nameof(BuildVersion.Minor), $"${nameof(Build.MinorVersion)}")
|
|
|
|
|
}))
|
|
|
|
|
.ToListAsync();
|
2017-02-24 04:53:49 +08:00
|
|
|
|
return query.Count;
|
|
|
|
|
}
|
2016-01-12 05:16:29 +08:00
|
|
|
|
|
2017-02-24 04:53:49 +08:00
|
|
|
|
public async Task<List<Build>> SelectVersion(uint major, uint minor, int limit = -1, int skip = 0)
|
|
|
|
|
{
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var query = _buildCollection.Find(new BsonDocument
|
|
|
|
|
{
|
|
|
|
|
new BsonElement(nameof(Build.MajorVersion), major),
|
|
|
|
|
new BsonElement(nameof(Build.MinorVersion), minor)
|
|
|
|
|
})
|
|
|
|
|
.Sort(sortByOrder)
|
|
|
|
|
.Skip(skip);
|
2016-01-12 05:16:29 +08:00
|
|
|
|
|
2017-02-24 04:53:49 +08:00
|
|
|
|
if (limit > 0)
|
|
|
|
|
{
|
|
|
|
|
query = query.Limit(limit);
|
|
|
|
|
}
|
2016-01-12 05:16:29 +08:00
|
|
|
|
|
2017-02-24 04:53:49 +08:00
|
|
|
|
return await query.ToListAsync();
|
|
|
|
|
}
|
2016-01-12 05:16:29 +08:00
|
|
|
|
|
2018-10-27 05:41:11 +08:00
|
|
|
|
public async Task<long> SelectVersionCount(uint major, uint minor) => await _buildCollection.CountDocumentsAsync(new BsonDocument
|
|
|
|
|
{
|
|
|
|
|
new BsonElement(nameof(Build.MajorVersion), major),
|
|
|
|
|
new BsonElement(nameof(Build.MinorVersion), minor)
|
|
|
|
|
});
|
2017-02-24 04:53:49 +08:00
|
|
|
|
}
|
2015-11-10 00:43:05 +08:00
|
|
|
|
}
|