Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the catalog entry by the initial instance set.
        /// </summary>
        /// <param name="entry">The initial catalog entry set.</param>
        /// <returns>The instance of <see cref="CatalogEntry"/> type.</returns>
        public async Task <CatalogEntry> GetCatalogEntry(CatalogEntry entry)
        {
            FileContract    fileContract    = null;
            CatalogContract catalogContract = null;

            using (IDbContextScope scope = this.dataContextScopeFactory.CreateDbContextScope(this.connectionStrings.DataStorageDB, false))
            {
                IFileRepository fileRepository = scope.GetRepository <IFileRepository>();
                fileContract = await fileRepository.GetAsync(entry.ID);

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

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

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

            return(entry);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the list of catalog entries located in specified catalog.
        /// </summary>
        /// <param name="catalog">The catalog of <see cref="CatalogRoot"/> type.</param>
        /// <param name="offset">The offset index.</param>
        /// <param name="limit">The number of records to return.</param>
        /// <returns>
        /// The list of instances of <see cref="CatalogEntry" /> type.
        /// </returns>
        public async Task <IPaginable <CatalogEntry> > GetCatalogEntries(CatalogRoot catalog, int offset = 0, int limit = 20)
        {
            if (limit == 0)
            {
                return(await Task.FromResult(new PagedList <CatalogEntry>()
                {
                    Offset = offset,
                    Limit = limit
                }));
            }

            IEnumerable <FileContract> data = null;
            int count = 0;

            FileContract contract = new FileContract()
            {
                DirectoryID = catalog.ID
            };

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

                await Task.WhenAll(
                    Task.Run(async() => data  = await fileRepository.FindAsync(contract, offset, limit)),
                    Task.Run(async() => count = await fileRepository.GetCountAsync(contract)));
            }

            IPaginable <CatalogEntry> result = (await this.mapper.MapNewAsync <FileContract, CatalogEntry>(data)).Select(entry =>
            {
                entry.Catalog = (Catalog)catalog;

                return(entry);
            }).AsPaginable();

            result.Offset     = offset;
            result.Limit      = limit;
            result.TotalCount = count;

            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets a value indicating whether the specified catalog entry already exists.
        /// </summary>
        /// <param name="entry">The catalog entry.</param>
        /// <returns><c>true</c> if the catalog entry exists. Otherwise <c>false.</c></returns>
        public async Task <bool> CatalogEntryExists(CatalogEntry entry)
        {
            FileContract contract = await this.mapper.MapNewAsync <CatalogEntry, FileContract>(entry);

            if (string.IsNullOrWhiteSpace(contract.Name) && contract.ID == Guid.Empty)
            {
                return(false);
            }

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

                if (!string.IsNullOrWhiteSpace(contract.Name))
                {
                    FileContract fileResult = (await fileRepository.FindAsync(contract, 0, 1)).FirstOrDefault();

                    return(!(fileResult is null || (contract.ID != Guid.Empty && contract.ID == fileResult.ID)));
                }

                return((await fileRepository.GetAsync(contract.ID)) != null);
            }
        }