aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio_v2/Tango.DAL.Mongo
diff options
context:
space:
mode:
authorRoy Ben-Shabat <Roy@Twine-s.com>2019-08-06 12:51:10 +0300
committerRoy Ben-Shabat <Roy@Twine-s.com>2019-08-06 12:51:10 +0300
commit265e4bcf4b21b2317a88171b3326f65e58515d9c (patch)
treeac41d914f2d0643265afcec2f48b7930cd24124a /Software/Visual_Studio_v2/Tango.DAL.Mongo
parente86fe2e8cf34343cf3ebbf4640b2be5e85899615 (diff)
downloadTango-265e4bcf4b21b2317a88171b3326f65e58515d9c.tar.gz
Tango-265e4bcf4b21b2317a88171b3326f65e58515d9c.zip
v2 Added Contact,Address
Added Indices...
Diffstat (limited to 'Software/Visual_Studio_v2/Tango.DAL.Mongo')
-rw-r--r--Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoIndex.cs17
-rw-r--r--Software/Visual_Studio_v2/Tango.DAL.Mongo/MongoRepository.cs24
-rw-r--r--Software/Visual_Studio_v2/Tango.DAL.Mongo/Tango.DAL.Mongo.csproj1
3 files changed, 41 insertions, 1 deletions
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<object, object> 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<T>(typeof(T).GetCustomAttribute<MongoCollectionAttribute>().Name);
+ _collection = _database.GetCollection<T>(typeof(T).GetCustomAttribute<CollectionAttribute>().Name);
+
+ foreach (var prop in typeof(T).GetProperties())
+ {
+ if (prop.GetCustomAttribute<UniqueAttribute>() != null)
+ {
+ var indices = _collection.Indexes.List().ToList().Select(x => JsonConvert.DeserializeObject<MongoIndex>(x.ToString())).ToList();
+
+ if (!indices.Exists(x => x.name == prop.Name))
+ {
+ var builder = Builders<T>.IndexKeys;
+ var indexModel = new CreateIndexModel<T>(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 @@
<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.8.1" />
+ <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup>
<ItemGroup>