/// <summary>
        /// Creates the specified catalog entry.
        /// </summary>
        /// <param name="stream">The stream of <see cref="CatalogEntryStream" /> type to create the entry from.</param>
        /// <returns>The newly created instance of <see cref="CatalogEntry" /> type.</returns>
        public async Task <CatalogEntry> CreateCatalogEntry(CatalogEntryStream stream)
        {
            CatalogEntry entry = stream.Entry;

            FileContract fileContract = await this.mapper.MapNewAsync <CatalogEntry, FileContract>(entry);

            CatalogContract catalogContract = null;

            using (IDbContextScope scope = this.dataContextScopeFactory.CreateDbContextScope(this.connectionStrings.DataStorageDB, true))
            {
                IFileRepository fileRepository = scope.GetRepository <IFileRepository>();
                fileContract = await fileRepository.SaveAsync(fileContract);

                ICatalogRepository catalogRepository = scope.GetRepository <ICatalogRepository>();
                catalogContract = await catalogRepository.GetAsync(fileContract.DirectoryID);

                scope.Commit();
            }

            entry = await this.mapper.MapAsync(fileContract, entry);

            entry.Catalog = await this.mapper.MapAsync(catalogContract, entry.Catalog);

            return(entry);
        }
        /// <summary>
        /// Updates the specified catalog.
        /// </summary>
        /// <param name="catalog">The instance of <see cref="Catalog" /> type to update.</param>
        /// <returns>The updated instance of <see cref="Catalog"/> type.</returns>
        public async Task <Catalog> UpdateCatalog(Catalog catalog)
        {
            CatalogContract catalogContract       = null;
            CatalogContract parentCatalogContract = null;

            using (IDbContextScope scope = this.dataContextScopeFactory.CreateDbContextScope(this.connectionStrings.DataStorageDB, true))
            {
                ICatalogRepository catalogRepository = scope.GetRepository <ICatalogRepository>();

                catalogContract = await catalogRepository.GetAsync(catalog.ID);

                catalogContract = await this.mapper.MapAsync(catalog, catalogContract);

                if (catalogContract.IsChanged)
                {
                    catalogContract = await catalogRepository.SaveAsync(catalogContract);
                }

                if ((catalogContract?.ParentID).HasValue)
                {
                    parentCatalogContract = await catalogRepository.GetAsync(catalogContract.ParentID.Value);
                }

                scope.Commit();
            }

            catalog = await this.mapper.MapAsync(catalogContract, catalog);

            catalog.Parent = await this.mapper.MapAsync(parentCatalogContract, catalog.Parent);

            return(catalog);
        }
        /// <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>
        /// Deletes the specified catalog entry.
        /// </summary>
        /// <param name="entry">The instance of <see cref="CatalogEntry" /> type to delete.</param>
        /// <returns>
        /// The deleted instance of <see cref="CatalogEntry"/> type.
        /// </returns>
        public async Task <CatalogEntry> DeleteCatalogEntry(CatalogEntry entry)
        {
            using (IDbContextScope scope = this.dataContextScopeFactory.CreateDbContextScope(this.connectionStrings.DataStorageDB, true))
            {
                IFileRepository fileRepository = scope.GetRepository <IFileRepository>();
                await fileRepository.DeleteAsync(entry.ID);

                scope.Commit();
            }

            return(entry);
        }
        /// <summary>
        /// Deletes the specified catalog.
        /// </summary>
        /// <param name="catalog">The instance of <see cref="Catalog" /> type to delete.</param>
        /// <returns>
        /// The deleted instance of <see cref="Catalog"/> type.
        /// </returns>
        public async Task <Catalog> DeleteCatalog(Catalog catalog)
        {
            using (IDbContextScope scope = this.dataContextScopeFactory.CreateDbContextScope(this.connectionStrings.DataStorageDB, true))
            {
                ICatalogRepository catalogRepository = scope.GetRepository <ICatalogRepository>();
                await catalogRepository.DeleteAsync(catalog.ID);

                scope.Commit();
            }

            return(catalog);
        }
        /// <summary>
        /// Deletes the specified storage.
        /// </summary>
        /// <param name="storage">The instance of <see cref="Storage" /> type to delete.</param>
        /// <returns>
        /// The deleted instance of <see cref="Storage"/> type.
        /// </returns>
        public async Task <Storage> DeleteStorage(Storage storage)
        {
            using (IDbContextScope scope = this.dataContextScopeFactory.CreateDbContextScope(this.connectionStrings.DataStorageDB, true))
            {
                IStorageRepository storageRepository = scope.GetRepository <IStorageRepository>();
                await storageRepository.DeleteAsync(storage.ID);

                scope.Commit();
            }

            return(storage);
        }