From 265e4bcf4b21b2317a88171b3326f65e58515d9c Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Tue, 6 Aug 2019 12:51:10 +0300 Subject: v2 Added Contact,Address Added Indices... --- .../Visual_Studio_v2/Tango.DAL.Mongo/MongoIndex.cs | 17 +++++++++++++++ .../Tango.DAL.Mongo/MongoRepository.cs | 24 +++++++++++++++++++++- .../Tango.DAL.Mongo/Tango.DAL.Mongo.csproj | 1 + 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoIndex.cs (limited to 'Software/Visual_Studio_v2/Tango.DAL.Mongo') diff --git a/Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoIndex.cs b/Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoIndex.cs new file mode 100644 index 000000000..c98543dd2 --- /dev/null +++ b/Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoIndex.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tango.DAL.Mongo +{ + public class MongoIndex + { + public int v { get; set; } + + public string name { get; set; } + + public string ns { get; set; } + + public Dictionary key { get; set; } + } +} diff --git a/Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoRepository.cs b/Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoRepository.cs index d268f434d..864bb17cc 100644 --- a/Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoRepository.cs +++ b/Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoRepository.cs @@ -6,6 +6,8 @@ using System.Reflection; using Tango.DAL.Attributes; using System.Linq.Expressions; using MongoDB.Driver.Linq; +using System.Linq; +using Newtonsoft.Json; namespace Tango.DAL.Mongo { @@ -25,7 +27,27 @@ namespace Tango.DAL.Mongo { if (_collection == null) { - _collection = _database.GetCollection(typeof(T).GetCustomAttribute().Name); + _collection = _database.GetCollection(typeof(T).GetCustomAttribute().Name); + + foreach (var prop in typeof(T).GetProperties()) + { + if (prop.GetCustomAttribute() != null) + { + var indices = _collection.Indexes.List().ToList().Select(x => JsonConvert.DeserializeObject(x.ToString())).ToList(); + + if (!indices.Exists(x => x.name == prop.Name)) + { + var builder = Builders.IndexKeys; + var indexModel = new CreateIndexModel(builder.Ascending(prop.Name), new CreateIndexOptions() + { + Unique = true, + Name = prop.Name, + }); + + _collection.Indexes.CreateOne(indexModel); + } + } + } } return _collection; diff --git a/Software/Visual_Studio_v2/Tango.DAL.Mongo/Tango.DAL.Mongo.csproj b/Software/Visual_Studio_v2/Tango.DAL.Mongo/Tango.DAL.Mongo.csproj index 7f0e7a2f0..a2f036e9d 100644 --- a/Software/Visual_Studio_v2/Tango.DAL.Mongo/Tango.DAL.Mongo.csproj +++ b/Software/Visual_Studio_v2/Tango.DAL.Mongo/Tango.DAL.Mongo.csproj @@ -6,6 +6,7 @@ + -- cgit v1.3.1