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

            var docColl = CollectionsContainer.GetBsonDocumentByType(doc.GetType());

            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 Insert(IEntity doc, ITicketAutUser user)
        {
            if (doc == null)
            {
                return;
            }

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

            var docColl = CollectionsContainer.GetBsonDocumentByType(doc.GetType()) ??
                          CollectionsContainer.CreateCollection(CollectionsContainer.GetNameCollection(doc.GetType()));

            if (docColl == null)
            {
                throw new KeyNotFoundException("Ошибка при создании коллекции");
            }

            var bson = doc.ToBsonDocument();

            bson.RemoveAt(0);

            var coll = CollectionsContainer.GetMongoCollection(docColl);

            coll.InsertOneAsync(bson).Wait();
            Auditor?.AuditOperation(OperationType.Insert, 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);
        }
        private Drawing CopyDrawingToAnotherParent(int idSource, Drawing parent, ITicketAutUser ticket)
        {
            var copy = _dataManagers.GetDocument <Drawing>(idSource);

            if (copy != null)
            {
                copy.ParentId = parent?.Id;
                if (parent != null)
                {
                    copy.CountAll = copy.Count * parent.CountAll;
                }

                copy.RecalculateWeightAll();
                copy.Id = null;

                _dataManagers.Insert(copy, ticket);
                return(copy);
            }

            return(null);
        }
        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);
        }
        public void Delete(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 filter = Builders <BsonDocument> .Filter.Eq("_id", doc.Id);

            var coll = CollectionsContainer.GetMongoCollection(docColl);

            coll.DeleteOneAsync(filter);

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

            Auditor?.AuditOperation(OperationType.Delete, doc, user);
        }
        private void CopyChildrens(int parentIdOld, Drawing parent, List <Drawing> list, ITicketAutUser ticket)
        {
            var childrensToCopy = list.Where(x => x.ParentId == parentIdOld);

            foreach (var drawing in childrensToCopy)
            {
                var newId = CopyDrawingToAnotherParent(drawing.Id.Value, parent, ticket);
                if (newId != null)
                {
                    CopyChildrens(drawing.Id.Value, newId, list, ticket);
                }
            }
        }
        public Task <AuditResult> AuditOperation(OperationType operationType, IEntity doc, ITicketAutUser user)
        {
            var task = new Task <AuditResult>(() =>
            {
                var docJson = doc.ToJson();

                var collection = _mongoDb.GetCollection <Audit>(GetCollAuditName(operationType));
                collection.InsertOneAsync(new Audit()
                {
                    DateOperation    = DateTime.Now,
                    JsonFormatObject = docJson,
                    ObjectType       = doc.GetType().Name,
                    Operation        = operationType.ToString(),
                    Login            = user.Login
                });

                return(AuditResult.Success);
            });

            task.Start();

            return(task);
        }