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); }
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); }
public override UpdateResult Update(T entity, UpdateMode updateMode) { var updateResult = default(UpdateResult); if (updateMode == UpdateMode.upsert_if_missing_key) { throw new NotImplementedException(); } var updateDate = NoSQLRepoHelper.DateTimeUtcNow(); entity.SystemLastUpdateDate = updateDate; if (updateMode == UpdateMode.upsert_if_missing_key) { NoSQLRepoHelper.SetIds(entity); entity.SystemCreationDate = updateDate; } if (!localDb.ContainsKey(entity.Id)) { if (updateMode == UpdateMode.error_if_missing_key) { throw new KeyNotFoundNoSQLException("Misssing key '" + entity.Id + "'"); } else if (updateMode == UpdateMode.do_nothing_if_missing_key) { return(UpdateResult.not_affected); } updateResult = UpdateResult.inserted; } localDb[entity.Id] = entity; SaveJSONFile(); updateResult = UpdateResult.updated; return(updateResult); }