BuildFeed/BuildFeed.Model/MetaItem.cs

132 lines
4.5 KiB
C#
Raw Normal View History

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;
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;
namespace BuildFeed.Model
2014-12-16 22:43:26 +08:00
{
2015-09-12 07:45:17 +08:00
[DataObject]
public class MetaItemModel
{
[Key]
[BsonId]
[@Required]
public MetaItemKey Id { get; set; }
[DisplayName("Meta Description")]
public string MetaDescription { get; set; }
2015-09-12 07:45:17 +08:00
[DisplayName("Page Content")]
[AllowHtml]
public string PageContent { get; set; }
}
public class MetaItem
{
private const string _metaCollectionName = "metaitem";
private readonly MongoClient _dbClient;
private readonly IMongoCollection<MetaItemModel> _metaCollection;
private readonly BuildRepository bModel;
2015-09-12 07:45:17 +08:00
public MetaItem()
{
_dbClient = new MongoClient(new MongoClientSettings
2015-09-12 07:45:17 +08:00
{
Server = new MongoServerAddress(MongoConfig.Host, MongoConfig.Port)
});
_metaCollection = _dbClient.GetDatabase(MongoConfig.Database).GetCollection<MetaItemModel>(_metaCollectionName);
bModel = new BuildRepository();
2015-09-12 07:45:17 +08:00
}
[DataObjectMethod(DataObjectMethodType.Select, false)]
public async Task<IEnumerable<MetaItemModel>> Select() { return await _metaCollection.Find(new BsonDocument()).ToListAsync(); }
2015-09-12 07:45:17 +08:00
[DataObjectMethod(DataObjectMethodType.Select, true)]
public async Task<IEnumerable<MetaItemModel>> SelectByType(MetaType type) { return await _metaCollection.Find(f => f.Id.Type == type).ToListAsync(); }
2015-09-12 07:45:17 +08:00
[DataObjectMethod(DataObjectMethodType.Select, false)]
public async Task<MetaItemModel> SelectById(MetaItemKey id) { return await _metaCollection.Find(f => f.Id.Type == id.Type && f.Id.Value == id.Value).SingleOrDefaultAsync(); }
2015-09-12 07:45:17 +08:00
[DataObjectMethod(DataObjectMethodType.Select, false)]
public async Task<IEnumerable<string>> SelectUnusedLabs()
{
string[] labs = await bModel.SelectAllLabs();
2015-09-12 07:45:17 +08:00
List<MetaItemModel> usedLabs = await _metaCollection.Find(f => f.Id.Type == MetaType.Lab).ToListAsync();
2015-09-12 07:45:17 +08:00
return from l in labs
where usedLabs.All(ul => ul.Id.Value != l)
select l;
}
[DataObjectMethod(DataObjectMethodType.Select, false)]
public async Task<IEnumerable<string>> SelectUnusedVersions()
{
BuildVersion[] versions = await bModel.SelectAllVersions();
2015-09-12 07:45:17 +08:00
List<MetaItemModel> usedVersions = await _metaCollection.Find(f => f.Id.Type == MetaType.Version).ToListAsync();
2015-09-12 07:45:17 +08:00
return from v in versions
where usedVersions.All(ul => ul.Id.Value != v.ToString())
select v.ToString();
}
[DataObjectMethod(DataObjectMethodType.Select, false)]
public async Task<IEnumerable<string>> SelectUnusedYears()
{
int[] years = await bModel.SelectAllYears();
2015-09-12 07:45:17 +08:00
List<MetaItemModel> usedYears = await _metaCollection.Find(f => f.Id.Type == MetaType.Year).ToListAsync();
2015-09-12 07:45:17 +08:00
return from y in years
where usedYears.All(ul => ul.Id.Value != y.ToString())
select y.ToString();
}
[DataObjectMethod(DataObjectMethodType.Insert, true)]
public async Task Insert(MetaItemModel item) { await _metaCollection.InsertOneAsync(item); }
2015-09-12 07:45:17 +08:00
[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); }
2015-09-12 07:45:17 +08:00
[DataObjectMethod(DataObjectMethodType.Insert, false)]
public async Task InsertAll(IEnumerable<MetaItemModel> items) { await _metaCollection.InsertManyAsync(items); }
2015-09-12 07:45:17 +08:00
[DataObjectMethod(DataObjectMethodType.Delete, true)]
public async Task DeleteById(MetaItemKey id) { await _metaCollection.DeleteOneAsync(f => f.Id.Type == id.Type && f.Id.Value == id.Value); }
2015-09-12 07:45:17 +08:00
}
public class MetaItemKey
{
public MetaType Type { get; set; }
public string Value { get; set; }
2015-09-12 07:45:17 +08:00
public MetaItemKey() { }
2015-09-12 07:45:17 +08:00
public MetaItemKey(string id)
{
string[] items = id.Split(':');
2015-09-12 07:45:17 +08:00
Type = (MetaType)Enum.Parse(typeof(MetaType), items[0]);
Value = items[1];
}
public override string ToString() { return $"{Type}:{Value}"; }
2015-09-12 07:45:17 +08:00
}
public enum MetaType
{
Lab,
Version,
Source,
Year
}
2014-12-16 22:43:26 +08:00
}