2016-07-15 21:39:30 +08:00
|
|
|
|
using System;
|
2014-12-16 22:43:26 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.Linq;
|
2015-09-12 07:45:17 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2014-12-16 22:43:26 +08:00
|
|
|
|
using System.Web.Mvc;
|
2016-07-15 21:39:30 +08:00
|
|
|
|
using MongoDB.Bson;
|
|
|
|
|
using MongoDB.Bson.Serialization.Attributes;
|
|
|
|
|
using MongoDB.Driver;
|
2014-12-16 22:43:26 +08:00
|
|
|
|
using Required = System.ComponentModel.DataAnnotations.RequiredAttribute;
|
|
|
|
|
|
2016-08-20 05:10:55 +08:00
|
|
|
|
namespace BuildFeed.Model
|
2014-12-16 22:43:26 +08:00
|
|
|
|
{
|
2017-02-24 04:53:49 +08:00
|
|
|
|
[DataObject]
|
|
|
|
|
public class MetaItemModel
|
|
|
|
|
{
|
|
|
|
|
[Key]
|
|
|
|
|
[BsonId]
|
|
|
|
|
[@Required]
|
|
|
|
|
public MetaItemKey Id { get; set; }
|
|
|
|
|
|
|
|
|
|
[DisplayName("Meta Description")]
|
|
|
|
|
public string MetaDescription { get; set; }
|
|
|
|
|
|
|
|
|
|
[DisplayName("Page Content")]
|
|
|
|
|
[AllowHtml]
|
|
|
|
|
public string PageContent { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class MetaItem
|
|
|
|
|
{
|
|
|
|
|
private const string META_COLLECTION_NAME = "metaitem";
|
|
|
|
|
private readonly BuildRepository _bModel;
|
|
|
|
|
|
|
|
|
|
private readonly IMongoCollection<MetaItemModel> _metaCollection;
|
|
|
|
|
|
|
|
|
|
public MetaItem()
|
|
|
|
|
{
|
2018-02-06 06:24:29 +08:00
|
|
|
|
var settings = new MongoClientSettings
|
2016-10-04 06:06:40 +08:00
|
|
|
|
{
|
2017-02-24 04:53:49 +08:00
|
|
|
|
Server = new MongoServerAddress(MongoConfig.Host, MongoConfig.Port)
|
2016-10-04 06:06:40 +08:00
|
|
|
|
};
|
2017-02-24 04:53:49 +08:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(MongoConfig.Username) && !string.IsNullOrEmpty(MongoConfig.Password))
|
|
|
|
|
{
|
2018-01-06 03:40:12 +08:00
|
|
|
|
settings.Credential =
|
|
|
|
|
MongoCredential.CreateCredential(MongoConfig.Database, MongoConfig.Username, MongoConfig.Password);
|
2017-02-24 04:53:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-06 06:24:29 +08:00
|
|
|
|
var dbClient = new MongoClient(settings);
|
2017-02-24 04:53:49 +08:00
|
|
|
|
|
2018-02-06 06:24:29 +08:00
|
|
|
|
_metaCollection = dbClient.GetDatabase(MongoConfig.Database)
|
|
|
|
|
.GetCollection<MetaItemModel>(META_COLLECTION_NAME);
|
2017-02-24 04:53:49 +08:00
|
|
|
|
_bModel = new BuildRepository();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DataObjectMethod(DataObjectMethodType.Select, false)]
|
|
|
|
|
public async Task<IEnumerable<MetaItemModel>> Select()
|
2018-02-06 06:24:29 +08:00
|
|
|
|
=> await _metaCollection.Find(new BsonDocument()).ToListAsync();
|
2017-02-24 04:53:49 +08:00
|
|
|
|
|
|
|
|
|
[DataObjectMethod(DataObjectMethodType.Select, true)]
|
|
|
|
|
public async Task<IEnumerable<MetaItemModel>> SelectByType(MetaType type)
|
|
|
|
|
{
|
|
|
|
|
return await _metaCollection.Find(f => f.Id.Type == type).ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DataObjectMethod(DataObjectMethodType.Select, false)]
|
|
|
|
|
public async Task<MetaItemModel> SelectById(MetaItemKey id)
|
|
|
|
|
{
|
2018-02-06 06:24:29 +08:00
|
|
|
|
return await _metaCollection.Find(f => f.Id.Type == id.Type && f.Id.Value == id.Value)
|
|
|
|
|
.SingleOrDefaultAsync();
|
2017-02-24 04:53:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DataObjectMethod(DataObjectMethodType.Select, false)]
|
|
|
|
|
public async Task<IEnumerable<string>> SelectUnusedLabs()
|
|
|
|
|
{
|
2018-02-06 06:24:29 +08:00
|
|
|
|
var labs = await _bModel.SelectAllLabs();
|
2017-02-24 04:53:49 +08:00
|
|
|
|
|
2018-02-06 06:24:29 +08:00
|
|
|
|
var usedLabs = await _metaCollection.Find(f => f.Id.Type == MetaType.Lab).ToListAsync();
|
2017-02-24 04:53:49 +08:00
|
|
|
|
|
|
|
|
|
return from l in labs
|
2018-02-06 06:24:29 +08:00
|
|
|
|
where usedLabs.All(ul => ul.Id.Value != l)
|
|
|
|
|
select l;
|
2017-02-24 04:53:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DataObjectMethod(DataObjectMethodType.Select, false)]
|
|
|
|
|
public async Task<IEnumerable<string>> SelectUnusedVersions()
|
|
|
|
|
{
|
2018-02-06 06:24:29 +08:00
|
|
|
|
var versions = await _bModel.SelectAllVersions();
|
2017-02-24 04:53:49 +08:00
|
|
|
|
|
2018-02-06 06:24:29 +08:00
|
|
|
|
var usedVersions = await _metaCollection.Find(f => f.Id.Type == MetaType.Version).ToListAsync();
|
2017-02-24 04:53:49 +08:00
|
|
|
|
|
|
|
|
|
return from v in versions
|
2018-02-06 06:24:29 +08:00
|
|
|
|
where usedVersions.All(ul => ul.Id.Value != v.ToString())
|
|
|
|
|
select v.ToString();
|
2017-02-24 04:53:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DataObjectMethod(DataObjectMethodType.Select, false)]
|
|
|
|
|
public async Task<IEnumerable<string>> SelectUnusedYears()
|
|
|
|
|
{
|
2018-02-06 06:24:29 +08:00
|
|
|
|
var years = await _bModel.SelectAllYears();
|
2017-02-24 04:53:49 +08:00
|
|
|
|
|
2018-02-06 06:24:29 +08:00
|
|
|
|
var usedYears = await _metaCollection.Find(f => f.Id.Type == MetaType.Year).ToListAsync();
|
2017-02-24 04:53:49 +08:00
|
|
|
|
|
|
|
|
|
return from y in years
|
2018-02-06 06:24:29 +08:00
|
|
|
|
where usedYears.All(ul => ul.Id.Value != y.ToString())
|
|
|
|
|
select y.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DataObjectMethod(DataObjectMethodType.Select, false)]
|
|
|
|
|
public async Task<IEnumerable<string>> SelectUnusedFamilies()
|
|
|
|
|
{
|
|
|
|
|
var families = await _bModel.SelectAllFamilies();
|
|
|
|
|
|
|
|
|
|
var usedFamilies = await _metaCollection.Find(f => f.Id.Type == MetaType.Family).ToListAsync();
|
|
|
|
|
|
|
|
|
|
return from y in families
|
|
|
|
|
where usedFamilies.All(ul => ul.Id.Value != y.ToString())
|
|
|
|
|
select y.ToString();
|
2017-02-24 04:53:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DataObjectMethod(DataObjectMethodType.Insert, true)]
|
|
|
|
|
public async Task Insert(MetaItemModel item)
|
|
|
|
|
{
|
|
|
|
|
await _metaCollection.InsertOneAsync(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DataObjectMethod(DataObjectMethodType.Update, true)]
|
|
|
|
|
public async Task Update(MetaItemModel item)
|
|
|
|
|
{
|
|
|
|
|
await _metaCollection.ReplaceOneAsync(f => f.Id.Type == item.Id.Type && f.Id.Value == item.Id.Value, item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DataObjectMethod(DataObjectMethodType.Insert, false)]
|
|
|
|
|
public async Task InsertAll(IEnumerable<MetaItemModel> items)
|
|
|
|
|
{
|
|
|
|
|
await _metaCollection.InsertManyAsync(items);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DataObjectMethod(DataObjectMethodType.Delete, true)]
|
|
|
|
|
public async Task DeleteById(MetaItemKey id)
|
|
|
|
|
{
|
|
|
|
|
await _metaCollection.DeleteOneAsync(f => f.Id.Type == id.Type && f.Id.Value == id.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class MetaItemKey
|
|
|
|
|
{
|
|
|
|
|
public string Value { get; set; }
|
2017-05-21 06:58:04 +08:00
|
|
|
|
public MetaType Type { get; set; }
|
2017-02-24 04:53:49 +08:00
|
|
|
|
|
|
|
|
|
public MetaItemKey()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MetaItemKey(string id)
|
|
|
|
|
{
|
2018-02-06 06:24:29 +08:00
|
|
|
|
var items = id.Split(':');
|
2017-02-24 04:53:49 +08:00
|
|
|
|
Type = (MetaType)Enum.Parse(typeof(MetaType), items[0]);
|
|
|
|
|
Value = items[1];
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-06 06:24:29 +08:00
|
|
|
|
public override string ToString() => $"{Type}:{Value}";
|
2017-02-24 04:53:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum MetaType
|
|
|
|
|
{
|
|
|
|
|
Lab,
|
|
|
|
|
Version,
|
|
|
|
|
Source,
|
2018-02-06 06:24:29 +08:00
|
|
|
|
Year,
|
|
|
|
|
Family
|
2017-02-24 04:53:49 +08:00
|
|
|
|
}
|
2014-12-16 22:43:26 +08:00
|
|
|
|
}
|