/// <summary> /// Updates the specified storage. /// </summary> /// <param name="storage">The instance of <see cref="Storage" /> type to update.</param> /// <returns>The updated instance of <see cref="Storage"/> type.</returns> public async Task <Storage> UpdateStorage(Storage storage) { StorageContract storageContract = null; CatalogContract catalogContract = null; using (IDbContextScope scope = this.dataContextScopeFactory.CreateDbContextScope(this.connectionStrings.DataStorageDB, true)) { IStorageRepository storageRepository = scope.GetRepository <IStorageRepository>(); storageContract = await storageRepository.GetAsync(storage.ID); storageContract = await this.mapper.MapAsync(storage, storageContract); if (storageContract.IsChanged) { storageContract = await storageRepository.SaveAsync(storageContract); } ICatalogRepository catalogRepository = scope.GetRepository <ICatalogRepository>(); catalogContract = await catalogRepository.GetAsync(storage.ID); scope.Commit(); } storage = await this.mapper.MapAsync(storageContract, storage); storage = await this.mapper.MapAsync(catalogContract, storage); return(storage); }
/// <summary> /// Creates the specified storage. /// </summary> /// <param name="storage">The instance of <see cref="Storage" /> type to create.</param> /// <returns>The newly created instance of <see cref="Storage" /> type.</returns> public async Task <Storage> CreateStorage(Storage storage) { StorageContract storageContract = await this.mapper.MapNewAsync <Storage, StorageContract>(storage); CatalogContract catalogContract = await this.mapper.MapNewAsync <Storage, CatalogContract>(storage); using (IDbContextScope scope = this.dataContextScopeFactory.CreateDbContextScope(this.connectionStrings.DataStorageDB, true)) { ICatalogRepository catalogRepository = scope.GetRepository <ICatalogRepository>(); catalogContract = await catalogRepository.SaveAsync(catalogContract); storageContract.ID = catalogContract.ID; IStorageRepository storageRepository = scope.GetRepository <IStorageRepository>(); storageContract = await storageRepository.SaveAsync(storageContract); scope.Commit(); } storage = await this.mapper.MapAsync(storageContract, storage); storage = await this.mapper.MapAsync(catalogContract, storage); return(storage); }
/// <summary> /// Gets storage by initial instance set. /// </summary> /// <param name="storage">The initial storage set.</param> /// <returns>The instance of <see cref="Storage"/> type.</returns> public async Task <Storage> GetStorage(Storage storage) { StorageContract storageContract = null; CatalogContract catalogContract = null; using (IDbContextScope scope = this.dataContextScopeFactory.CreateDbContextScope(this.connectionStrings.DataStorageDB, false)) { IStorageRepository storageRepository = scope.GetRepository <IStorageRepository>(); storageContract = await storageRepository.GetAsync(storage.ID); ICatalogRepository catalogRepository = scope.GetRepository <ICatalogRepository>(); catalogContract = await catalogRepository.GetAsync(storage.ID); } storage = await this.mapper.MapNewAsync <StorageContract, Storage>(storageContract); storage = await this.mapper.MapAsync(catalogContract, storage); return(storage); }
/// <summary> /// Gets a value indicating whether the specified storage already exists. /// </summary> /// <param name="storage">The storage.</param> /// <returns><c>true</c> if the storage exists. Otherwise <c>false.</c></returns> public async Task <bool> StorageExists(Storage storage) { StorageContract contract = await this.mapper.MapNewAsync <Storage, StorageContract>(storage); if (string.IsNullOrWhiteSpace(contract.Name) && contract.ID == Guid.Empty) { return(false); } using (IDbContextScope scope = this.dataContextScopeFactory.CreateDbContextScope(this.connectionStrings.DataStorageDB, false)) { IStorageRepository storageRepository = scope.GetRepository <IStorageRepository>(); if (!string.IsNullOrWhiteSpace(contract.Name)) { StorageContract storageResult = (await storageRepository.FindAsync(contract, 0, 1)).FirstOrDefault(); return(!(storageResult is null || (contract.ID != Guid.Empty && contract.ID == storageResult.ID))); } return((await storageRepository.GetAsync(contract.ID)) != null); } }