public void Update(IHierarchyEntity doc, ITicketAutUser user)
        {
            if (doc?.Id == null)
            {
                return;
            }

            var docColl = CollectionsContainer.GetBsonDocumentContainsId(doc.GetType(), doc.Id.Value);

            if (docColl == null)
            {
                throw new KeyNotFoundException();
            }

            var bson = doc.ToBsonDocument();

            bson.RemoveAt(0);

            var coll   = CollectionsContainer.GetMongoCollection(docColl);
            var filter = Builders <BsonDocument> .Filter.Eq("_id", doc.Id);

            coll.ReplaceOneAsync(filter, bson);

            Auditor?.AuditOperation(OperationType.Update, doc, user);
        }
        public void RestoreDocument(IHierarchyEntity doc, ITicketAutUser user)
        {
            if (doc == null)
            {
                return;
            }

            BsonDocument docColl = null;

            if (doc.ParentId.HasValue)
            {
                docColl = CollectionsContainer.GetBsonDocumentContainsId(doc.GetType(), doc.ParentId.Value);
                if (docColl == null)
                {
                    throw new KeyNotFoundException();
                }
            }

            var bson = doc.ToBsonDocument();

            bson.RemoveAt(0);

            var coll = CollectionsContainer.GetMongoCollection(docColl);

            coll.InsertOneAsync(bson).Wait();

            CollectionsContainer.InsertIdCollection(docColl, doc.Id.Value);

            Auditor?.AuditOperation(OperationType.Insert, doc, user);
        }
        public List <T> GetHierarchicalListCollection <T>(int id)
        {
            var docColl = CollectionsContainer.GetBsonDocumentContainsId(typeof(T), id);

            if (docColl == null)
            {
                throw new KeyNotFoundException();
            }

            var coll = CollectionsContainer.GetMongoCollection <T>(docColl);

            return(coll.FindAsync(x => true).Result.ToListAsync().Result);
        }
        public T GetDocument <T>(int id)
        {
            var docColl = CollectionsContainer.GetBsonDocumentContainsId(typeof(T), id);

            if (docColl == null)
            {
                throw new KeyNotFoundException();
            }

            var coll   = CollectionsContainer.GetMongoCollection(docColl);
            var filter = Builders <BsonDocument> .Filter.Eq("_id", id);

            return(BsonSerializer.Deserialize <T>(coll.FindAsync(filter).Result.ToListAsync().Result.FirstOrDefault()));
        }
        public void Insert(IHierarchyEntity doc, ITicketAutUser user)
        {
            if (doc == null)
            {
                return;
            }

            doc.Id = GetIdDocument(doc.GetType());

            BsonDocument docColl;

            if (doc.ParentId.HasValue)
            {
                docColl = CollectionsContainer.GetBsonDocumentContainsId(doc.GetType(), doc.ParentId.Value);
                if (docColl == null)
                {
                    throw new KeyNotFoundException();
                }
            }
            else
            {
                docColl = CollectionsContainer.CreateCollection(CollectionsContainer.GetNameCollection(doc.GetType(), doc.Id.Value));
                if (docColl == null)
                {
                    throw new KeyNotFoundException("Ошибка при создании коллекции");
                }
            }

            var bson = doc.ToBsonDocument();

            bson.RemoveAt(0);

            var coll = CollectionsContainer.GetMongoCollection(docColl);

            coll.InsertOneAsync(bson).Wait();

            CollectionsContainer.InsertIdCollection(docColl, doc.Id.Value);

            Auditor?.AuditOperation(OperationType.Insert, doc, user);
        }