Exemplo n.º 1
0
        public override BulkInsertResult <string> InsertMany(IEnumerable <T> entities, InsertMode insertMode)
        {
            if (insertMode != InsertMode.db_implementation)
            {
                throw new NotImplementedException();
            }

            var insertResult = new BulkInsertResult <string>();

            var creationDate = NoSQLRepoHelper.DateTimeUtcNow();

            foreach (var entity in entities)
            {
                if (AutoGeneratedEntityDate)
                {
                    entity.SystemCreationDate = creationDate;
                }
                if (AutoGeneratedEntityDate)
                {
                    entity.SystemLastUpdateDate = creationDate;
                }

                NoSQLRepoHelper.SetIds(entity);

                var entityToStore = NewtonJsonHelper.CloneJson(entity, DateTimeZoneHandling.Utc);
                localDb[entity.Id] = entityToStore;
                config.ExpireAt(entity.Id, null);
                insertResult[entity.Id] = InsertResult.unknown;
            }
            SaveJSONFile();

            return(insertResult);
        }
        public override InsertResult InsertOne(T entity, InsertMode insertMode)
        {
            CheckOpenedConnection();

            var entitydomain = entity;

            NoSQLRepoHelper.SetIds(entitydomain);

            var updateddate = NoSQLRepoHelper.DateTimeUtcNow();
            var createdDate = NoSQLRepoHelper.DateTimeUtcNow();

            if (!string.IsNullOrEmpty(entity.Id) && localDb.ContainsKey(entity.Id))
            {
                // Document already exists
                switch (insertMode)
                {
                case InsertMode.error_if_key_exists:
                    throw new DupplicateKeyNoSQLException();

                case InsertMode.erase_existing:
                    createdDate = localDb[entity.Id].SystemCreationDate;
                    localDb.Remove(entity.Id);
                    break;

                case InsertMode.do_nothing_if_key_exists:
                    return(InsertResult.not_affected);

                default:
                    break;
                }
            }

            if (AutoGeneratedEntityDate)
            {
                entitydomain.SystemCreationDate   = createdDate;
                entitydomain.SystemLastUpdateDate = updateddate;
            }

            // Clone to not shared reference between App instance and repository persisted value
            // UTC : Ensure to store only utc datetime
            var entityToStore = NewtonJsonHelper.CloneJson(entitydomain, DateTimeZoneHandling.Utc);

            if (localDb.ContainsKey(entity.Id))
            {
                localDb[entity.Id] = entityToStore;
            }
            else
            {
                localDb.Add(entity.Id, entityToStore);
            }

            config.ExpireAt(entity.Id, null);

            SaveJSONFile();

            return(InsertResult.inserted);
        }
Exemplo n.º 3
0
        public override InsertResult InsertOne(T entity, InsertMode keyExistsAction)
        {
            var insertResult = default(InsertResult);

            var date = NoSQLRepoHelper.DateTimeUtcNow();

            if (AutoGeneratedEntityDate)
            {
                entity.SystemCreationDate   = date;
                entity.SystemLastUpdateDate = date;
            }

            NoSQLRepoHelper.SetIds(entity);

            if (localDb.ContainsKey(entity.Id))
            {
                if (keyExistsAction == InsertMode.error_if_key_exists)
                {
                    throw new DupplicateKeyNoSQLException();
                }
                else if (keyExistsAction == InsertMode.do_nothing_if_key_exists)
                {
                    return(InsertResult.not_affected);
                }
                else if (keyExistsAction == InsertMode.erase_existing)
                {
                    entity.SystemCreationDate = localDb[entity.Id].SystemCreationDate; // keep the origin creation date of the entity
                    // Continue execution
                }
                insertResult = InsertResult.updated;
            }
            else
            {
                insertResult = InsertResult.inserted;
            }

            // Clone to not shared reference between App instance and repository persisted value
            // UTC : Ensure to store only utc datetime
            var entityToStore = NewtonJsonHelper.CloneJson(entity, DateTimeZoneHandling.Utc);

            localDb[entity.Id] = entityToStore;
            config.ExpireAt(entity.Id, null);

            SaveJSONFile();
            return(insertResult);
        }