mirror of
https://gitlab.com/buildfeed/BuildFeed.git
synced 2024-03-22 21:10:34 +08:00
Build Model MongoDB Part 1
This commit is contained in:
parent
c7a1f85ca3
commit
5e7e88fa73
|
@ -195,6 +195,7 @@
|
||||||
<Compile Include="App_Start\RouteConfig.cs" />
|
<Compile Include="App_Start\RouteConfig.cs" />
|
||||||
<Compile Include="Areas\admin\adminAreaRegistration.cs" />
|
<Compile Include="Areas\admin\adminAreaRegistration.cs" />
|
||||||
<Compile Include="Areas\admin\Controllers\usersController.cs" />
|
<Compile Include="Areas\admin\Controllers\usersController.cs" />
|
||||||
|
<Compile Include="Code\LongIdGenerator.cs" />
|
||||||
<Compile Include="Code\MvcIntrinsics.cs" />
|
<Compile Include="Code\MvcIntrinsics.cs" />
|
||||||
<Compile Include="Controllers\apiController.cs" />
|
<Compile Include="Controllers\apiController.cs" />
|
||||||
<Compile Include="Controllers\frontController.cs" />
|
<Compile Include="Controllers\frontController.cs" />
|
||||||
|
|
42
BuildFeed/Code/LongIdGenerator.cs
Normal file
42
BuildFeed/Code/LongIdGenerator.cs
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
using System;
|
||||||
|
using MongoDB.Bson;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Bson.Serialization;
|
||||||
|
|
||||||
|
namespace BuildFeed.Code
|
||||||
|
{
|
||||||
|
public class LongIdGenerator : IIdGenerator
|
||||||
|
{
|
||||||
|
private const string _idCollectionName = "IDs";
|
||||||
|
private MongoClient _dbClient;
|
||||||
|
private IMongoCollection<long> _idCollection;
|
||||||
|
|
||||||
|
public LongIdGenerator()
|
||||||
|
{
|
||||||
|
_dbClient = new MongoClient(new MongoClientSettings()
|
||||||
|
{
|
||||||
|
Server = new MongoServerAddress(MongoConfig.Host, MongoConfig.Port)
|
||||||
|
});
|
||||||
|
|
||||||
|
_idCollection = _dbClient.GetDatabase(MongoConfig.Database).GetCollection<long>(_idCollectionName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public object GenerateId(object container, object document)
|
||||||
|
{
|
||||||
|
var query = Query.EQ("_id", (container).Name);
|
||||||
|
|
||||||
|
return (_idCollection.FindOneAndUpdateAsync(new FindAndModifyArgs()
|
||||||
|
{
|
||||||
|
Query = query,
|
||||||
|
Update = CreateUpdateBuilder(),
|
||||||
|
VersionReturned = FindAndModifyDocumentVersion.Modified,
|
||||||
|
Upsert = true
|
||||||
|
}).AsInt64.ModifiedDocument["seq"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsEmpty(object id)
|
||||||
|
{
|
||||||
|
return ((long)id) == 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,15 +8,16 @@
|
||||||
using Required = System.ComponentModel.DataAnnotations.RequiredAttribute;
|
using Required = System.ComponentModel.DataAnnotations.RequiredAttribute;
|
||||||
using System.Web.Mvc;
|
using System.Web.Mvc;
|
||||||
using BuildFeed.Local;
|
using BuildFeed.Local;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Bson;
|
||||||
|
using MongoDB.Bson.Serialization.Attributes;
|
||||||
|
|
||||||
namespace BuildFeed.Models
|
namespace BuildFeed.Models
|
||||||
{
|
{
|
||||||
[DataObject]
|
[DataObject]
|
||||||
public class Build
|
public class BuildModel
|
||||||
{
|
{
|
||||||
[Key]
|
[Key, BsonId]
|
||||||
[AutoIncrement]
|
|
||||||
[Index]
|
|
||||||
public long Id { get; set; }
|
public long Id { get; set; }
|
||||||
|
|
||||||
[@Required]
|
[@Required]
|
||||||
|
@ -109,121 +110,115 @@ public string FullBuildString
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Build
|
||||||
|
{
|
||||||
|
private const string _buildCollectionName = "builds";
|
||||||
|
|
||||||
|
private MongoClient _dbClient;
|
||||||
|
private IMongoCollection<BuildModel> _buildCollection;
|
||||||
|
|
||||||
|
public Build()
|
||||||
|
{
|
||||||
|
_dbClient = new MongoClient(new MongoClientSettings()
|
||||||
|
{
|
||||||
|
Server = new MongoServerAddress(MongoConfig.Host, MongoConfig.Port)
|
||||||
|
});
|
||||||
|
|
||||||
|
_buildCollection = _dbClient.GetDatabase(MongoConfig.Database).GetCollection<BuildModel>(_buildCollectionName);
|
||||||
|
}
|
||||||
|
|
||||||
[DataObjectMethod(DataObjectMethodType.Select, true)]
|
[DataObjectMethod(DataObjectMethodType.Select, true)]
|
||||||
public static IEnumerable<Build> Select()
|
public IEnumerable<BuildModel> Select()
|
||||||
{
|
{
|
||||||
using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
|
var task = _buildCollection.Find(new BsonDocument()).ToListAsync();
|
||||||
{
|
task.Wait();
|
||||||
var client = rClient.As<Build>();
|
return task.Result;
|
||||||
return client.GetAll();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[DataObjectMethod(DataObjectMethodType.Select, false)]
|
[DataObjectMethod(DataObjectMethodType.Select, false)]
|
||||||
public static Build SelectById(long id)
|
public BuildModel SelectById(long id)
|
||||||
{
|
{
|
||||||
using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
|
var task = _buildCollection.Find(f => f.Id == id).SingleOrDefaultAsync();
|
||||||
{
|
task.Wait();
|
||||||
var client = rClient.As<Build>();
|
return task.Result;
|
||||||
return client.GetById(id);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[DataObjectMethod(DataObjectMethodType.Select, false)]
|
[DataObjectMethod(DataObjectMethodType.Select, false)]
|
||||||
public static IEnumerable<Build> SelectInBuildOrder()
|
public IEnumerable<BuildModel> SelectInBuildOrder()
|
||||||
{
|
{
|
||||||
using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
|
var task = _buildCollection.Find(new BsonDocument())
|
||||||
{
|
.SortByDescending(b => b.BuildTime)
|
||||||
var client = rClient.As<Build>();
|
.ThenByDescending(b => b.MajorVersion)
|
||||||
return client.GetAll()
|
.ThenByDescending(b => b.MinorVersion)
|
||||||
.OrderByDescending(b => b.BuildTime)
|
.ThenByDescending(b => b.Number)
|
||||||
.ThenByDescending(b => b.MajorVersion)
|
.ThenByDescending(b => b.Revision)
|
||||||
.ThenByDescending(b => b.MinorVersion)
|
.ToListAsync();
|
||||||
.ThenByDescending(b => b.Number)
|
task.Wait();
|
||||||
.ThenByDescending(b => b.Revision);
|
return task.Result;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[DataObjectMethod(DataObjectMethodType.Select, false)]
|
[DataObjectMethod(DataObjectMethodType.Select, false)]
|
||||||
public static IEnumerable<Build> SelectInVersionOrder()
|
public IEnumerable<BuildModel> SelectInVersionOrder()
|
||||||
{
|
{
|
||||||
using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
|
var task = _buildCollection.Find(new BsonDocument())
|
||||||
{
|
.SortByDescending(b => b.MajorVersion)
|
||||||
var client = rClient.As<Build>();
|
.ThenByDescending(b => b.MinorVersion)
|
||||||
return client.GetAll()
|
.ThenByDescending(b => b.Number)
|
||||||
.OrderByDescending(b => b.MajorVersion)
|
.ThenByDescending(b => b.Revision)
|
||||||
.ThenByDescending(b => b.MinorVersion)
|
.ThenByDescending(b => b.BuildTime)
|
||||||
.ThenByDescending(b => b.Number)
|
.ToListAsync();
|
||||||
.ThenByDescending(b => b.Revision)
|
task.Wait();
|
||||||
.ThenByDescending(b => b.BuildTime);
|
return task.Result;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[DataObjectMethod(DataObjectMethodType.Select, false)]
|
[DataObjectMethod(DataObjectMethodType.Select, false)]
|
||||||
public static IEnumerable<BuildVersion> SelectBuildVersions()
|
public IEnumerable<BuildVersion> SelectBuildVersions()
|
||||||
{
|
{
|
||||||
using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
|
var task = _buildCollection.DistinctAsync(b => new BuildVersion() { Major = b.MajorVersion, Minor = b.MinorVersion }, b => true);
|
||||||
{
|
task.Wait();
|
||||||
var client = rClient.As<Build>();
|
var outTask = task.Result.ToListAsync();
|
||||||
var results = client.GetAll()
|
outTask.Wait();
|
||||||
.GroupBy(b => new BuildVersion() { Major = b.MajorVersion, Minor = b.MinorVersion })
|
return outTask.Result
|
||||||
.Select(b => new BuildVersion() { Major = b.First().MajorVersion, Minor = b.First().MinorVersion })
|
.OrderByDescending(y => y.Major)
|
||||||
.OrderByDescending(y => y.Major)
|
.ThenByDescending(y => y.Minor);
|
||||||
.ThenByDescending(y => y.Minor);
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[DataObjectMethod(DataObjectMethodType.Select, false)]
|
[DataObjectMethod(DataObjectMethodType.Select, false)]
|
||||||
public static IEnumerable<int> SelectBuildYears()
|
public IEnumerable<int> SelectBuildYears()
|
||||||
{
|
{
|
||||||
using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
|
var task = _buildCollection.DistinctAsync(b => b.BuildTime.Value.Year, b => b.BuildTime.HasValue);
|
||||||
{
|
task.Wait();
|
||||||
var client = rClient.As<Build>();
|
var outTask = task.Result.ToListAsync();
|
||||||
var results = client.GetAll().Where(b => b.BuildTime.HasValue)
|
outTask.Wait();
|
||||||
.GroupBy(b => b.BuildTime.Value.Year)
|
return outTask.Result.OrderBy(b => b);
|
||||||
.Select(b => b.Key)
|
|
||||||
.OrderByDescending(y => y);
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[DataObjectMethod(DataObjectMethodType.Select, false)]
|
[DataObjectMethod(DataObjectMethodType.Select, false)]
|
||||||
public static IEnumerable<string> SelectBuildLabs()
|
public IEnumerable<string> SelectBuildLabs()
|
||||||
{
|
{
|
||||||
using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
|
var task = _buildCollection.DistinctAsync(b => b.Lab.ToLower(), b => !string.IsNullOrWhiteSpace(b.Lab));
|
||||||
{
|
task.Wait();
|
||||||
var client = rClient.As<Build>();
|
var outTask = task.Result.ToListAsync();
|
||||||
var results = client.GetAll()
|
outTask.Wait();
|
||||||
.Where(b => !string.IsNullOrWhiteSpace(b.Lab))
|
return outTask.Result.OrderBy(b => b);
|
||||||
.GroupBy(b => b.Lab.ToLower())
|
|
||||||
.Select(b => b.Key)
|
|
||||||
.OrderBy(s => s);
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[DataObjectMethod(DataObjectMethodType.Select, false)]
|
[DataObjectMethod(DataObjectMethodType.Select, false)]
|
||||||
public static IEnumerable<string> SelectBuildLabs(byte major, byte minor)
|
public IEnumerable<string> SelectBuildLabs(byte major, byte minor)
|
||||||
{
|
{
|
||||||
using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
|
var task = _buildCollection.DistinctAsync(b => b.Lab.ToLower(), b => !string.IsNullOrWhiteSpace(b.Lab) && b.MajorVersion == major && b.MinorVersion == minor);
|
||||||
{
|
task.Wait();
|
||||||
var client = rClient.As<Build>();
|
var outTask = task.Result.ToListAsync();
|
||||||
var results = client.GetAll()
|
outTask.Wait();
|
||||||
.Where(b => !string.IsNullOrWhiteSpace(b.Lab))
|
return outTask.Result.OrderBy(b => b);
|
||||||
.Where(b => b.MajorVersion == major)
|
|
||||||
.Where(b => b.MinorVersion == minor)
|
|
||||||
.GroupBy(b => b.Lab.ToLower())
|
|
||||||
.Select(b => b.Key)
|
|
||||||
.OrderBy(s => s);
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[DataObjectMethod(DataObjectMethodType.Insert, true)]
|
[DataObjectMethod(DataObjectMethodType.Insert, true)]
|
||||||
public static void Insert(Build item)
|
public void Insert(BuildModel item)
|
||||||
{
|
{
|
||||||
|
item.Id =
|
||||||
using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
|
using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
|
||||||
{
|
{
|
||||||
var client = rClient.As<Build>();
|
var client = rClient.As<Build>();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user