BuildFeed/BuildFeed.Model/BuildRepository-Lab.cs

92 lines
3.0 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
namespace BuildFeed.Model
{
2017-02-24 04:53:49 +08:00
public partial class BuildRepository
{
public async Task<string[]> SelectAllLabs(int limit = -1, int skip = 0)
{
2018-02-07 06:16:37 +08:00
var query = _buildCollection.Aggregate()
.Group(new BsonDocument("_id", $"${nameof(Build.Lab)}"))
.Sort(new BsonDocument("_id", 1))
.Skip(skip);
2017-02-24 04:53:49 +08:00
if (limit > 0)
{
query = query.Limit(limit);
}
2018-02-07 06:16:37 +08:00
var grouping = await query.ToListAsync();
2017-02-24 04:53:49 +08:00
return (from g in grouping
2018-02-07 06:16:37 +08:00
where !g["_id"].IsBsonNull
select g["_id"].AsString).ToArray();
2017-02-24 04:53:49 +08:00
}
public async Task<string[]> SelectLabsForVersion(int major, int minor)
{
2018-02-07 06:16:37 +08:00
var query = _buildCollection.Aggregate()
.Match(new BsonDocument
{
new BsonElement(nameof(Build.MajorVersion), major),
new BsonElement(nameof(Build.MinorVersion), minor)
})
.Group(new BsonDocument("_id", $"${nameof(Build.Lab)}"))
.Sort(new BsonDocument("_id", 1));
2017-02-24 04:53:49 +08:00
2018-02-07 06:16:37 +08:00
var grouping = await query.ToListAsync();
2017-02-24 04:53:49 +08:00
return (from g in grouping
2018-02-07 06:16:37 +08:00
where !g["_id"].IsBsonNull
select g["_id"].AsString).ToArray();
2017-02-24 04:53:49 +08:00
}
public async Task<List<string>> SearchLabs(string search)
{
2018-02-07 06:16:37 +08:00
var result = await _buildCollection.Aggregate()
.Match(b => b.Lab != null)
.Match(b => b.Lab != "")
.Match(b => b.Lab.ToLower().Contains(search.ToLower()))
.Group(b => b.Lab.ToLower(),
// incoming bullshit hack
bg => new Tuple<string>(bg.Key))
.ToListAsync();
2017-02-24 04:53:49 +08:00
// work ourselves out of aforementioned bullshit hack
return result.Select(b => b.Item1).ToList();
}
public async Task<long> SelectAllLabsCount()
{
2018-02-07 06:16:37 +08:00
var query = _buildCollection.Aggregate()
.Group(new BsonDocument("_id", new BsonDocument(nameof(Build.Lab), $"${nameof(Build.Lab)}")))
.Sort(new BsonDocument("_id", 1));
2017-02-24 04:53:49 +08:00
2018-02-07 06:16:37 +08:00
var grouping = await query.ToListAsync();
2017-02-24 04:53:49 +08:00
return grouping.Count;
}
public async Task<List<Build>> SelectLab(string lab, int limit = -1, int skip = 0)
{
2018-02-07 06:16:37 +08:00
var query = _buildCollection.Find(new BsonDocument(nameof(Build.LabUrl), lab))
.Sort(sortByCompileDate)
.Skip(skip);
2017-02-24 04:53:49 +08:00
if (limit > 0)
{
query = query.Limit(limit);
}
return await query.ToListAsync();
}
2018-02-07 06:16:37 +08:00
public async Task<long> SelectLabCount(string lab)
=> await _buildCollection.CountDocumentsAsync(new BsonDocument(nameof(Build.LabUrl), lab));
2017-02-24 04:53:49 +08:00
}
}