/// <inheritdoc />
        public async Task <CosmosDocument <T> > GetByIdAndPartitionKey <T>(string id, string partitionKey)
            where T : class
        {
            CosmosDocument <T> cosmosDocument = new CosmosDocument <T>();

            try
            {
                // Get the Resource Response
                Uri            documentUri    = UriFactory.CreateDocumentUri(this.database, this.collection, id);
                RequestOptions requestOptions = new RequestOptions {
                    PartitionKey = new PartitionKey(partitionKey)
                };
                ResourceResponse <Document> resourceResponse =
                    await this.client.ReadDocumentAsync(documentUri, requestOptions);

                // Process Response
                Document document = resourceResponse.Resource;
                cosmosDocument.Etag     = document.ETag;
                cosmosDocument.Id       = document.Id;
                cosmosDocument.Document = (T)(dynamic)document;
            }
            catch (DocumentClientException documentClientException)
            {
                throw new CosmosDatabaseException(documentClientException);
            }
            catch (Exception exception)
            {
                throw new DefaultException(exception);
            }

            // Return the model
            return(cosmosDocument);
        }
        /// <inheritdoc />
        public async Task Update <T>(CosmosDocument <T> cosmosDocument)
            where T : class
        {
            try
            {
                var requestOptions = new RequestOptions
                {
                    AccessCondition = new AccessCondition
                    {
                        Condition = cosmosDocument.Etag,
                        Type      = AccessConditionType.IfMatch,
                    },
                };

                Uri documentUri = UriFactory.CreateDocumentUri(this.database, this.collection, cosmosDocument.Id);
                await this.client.ReplaceDocumentAsync(documentUri, cosmosDocument.Document, requestOptions);
            }
            catch (DocumentClientException documentClientException)
            {
                throw new CosmosDatabaseException(documentClientException);
            }
            catch (Exception exception)
            {
                throw new DefaultException(exception);
            }
        }
示例#3
0
        /// <inheritdoc/>
        public async Task DeletePackage(string packageIdentifier)
        {
            CosmosDocument <CosmosPackageManifest> cosmosDocument = new CosmosDocument <CosmosPackageManifest>
            {
                Id           = packageIdentifier,
                PartitionKey = packageIdentifier,
            };

            // Delete Document
            await this.cosmosDatabase.Delete <CosmosPackageManifest>(cosmosDocument);
        }
示例#4
0
        /// <inheritdoc />
        public async Task AddPackageManifest(PackageManifest packageManifest)
        {
            // Create Document and add to cosmos.
            CosmosPackageManifest cPackageManifest = new CosmosPackageManifest(packageManifest);
            CosmosDocument <CosmosPackageManifest> cosmosDocument = new CosmosDocument <CosmosPackageManifest>
            {
                Document = cPackageManifest,
            };

            await this.cosmosDatabase.Add <CosmosPackageManifest>(cosmosDocument);
        }
示例#5
0
        /// <inheritdoc />
        public async Task UpdatePackageManifest(string packageIdentifier, PackageManifest packageManifest)
        {
            CosmosPackageManifest cPackageManifest = new CosmosPackageManifest(packageManifest);
            CosmosDocument <CosmosPackageManifest> cosmosDocument = new CosmosDocument <CosmosPackageManifest>
            {
                Document     = cPackageManifest,
                Id           = packageIdentifier,
                PartitionKey = packageIdentifier,
            };

            await this.cosmosDatabase.Update <CosmosPackageManifest>(cosmosDocument);
        }
示例#6
0
        /// <inheritdoc />
        public async Task UpdateLocale(string packageIdentifier, string packageVersion, string packageLocale, Locale locale)
        {
            // Fetch Current Package
            CosmosDocument <CosmosPackageManifest> cosmosDocument =
                await this.cosmosDatabase.GetByIdAndPartitionKey <CosmosPackageManifest>(packageIdentifier, packageIdentifier);

            // Update locale
            cosmosDocument.Document.UpdateLocale(locale, packageVersion);

            // Save Document
            ApiDataValidator.Validate(cosmosDocument.Document);
            await this.cosmosDatabase.Update <CosmosPackageManifest>(cosmosDocument);
        }
示例#7
0
        /// <inheritdoc />
        public async Task DeleteInstaller(string packageIdentifier, string packageVersion, string installerIdentifier)
        {
            // Fetch Current Package
            CosmosDocument <CosmosPackageManifest> cosmosDocument =
                await this.cosmosDatabase.GetByIdAndPartitionKey <CosmosPackageManifest>(packageIdentifier, packageIdentifier);

            // Remove Installer
            cosmosDocument.Document.RemoveInstaller(installerIdentifier, packageVersion);

            // Save Document
            ApiDataValidator.Validate(cosmosDocument.Document);
            await this.cosmosDatabase.Update <CosmosPackageManifest>(cosmosDocument);
        }
示例#8
0
        /// <inheritdoc />
        public async Task UpdateVersion(string packageIdentifier, string packageVersion, Version version)
        {
            // Fetch Current Package
            CosmosDocument <CosmosPackageManifest> cosmosDocument =
                await this.cosmosDatabase.GetByIdAndPartitionKey <CosmosPackageManifest>(packageIdentifier, packageIdentifier);

            // Update
            cosmosDocument.Document.Versions.Update(version);

            // Save Package
            ApiDataValidator.Validate(cosmosDocument.Document);
            await this.cosmosDatabase.Update <CosmosPackageManifest>(cosmosDocument);
        }
示例#9
0
        /// <inheritdoc />
        public async Task AddPackage(Package package)
        {
            // Convert Package to Manifest for storage
            PackageManifest       packageManifest       = new PackageManifest(package);
            CosmosPackageManifest cosmosPackageManifest = new CosmosPackageManifest(packageManifest);

            // Create Document and add to cosmos.
            CosmosDocument <CosmosPackageManifest> cosmosDocument = new CosmosDocument <CosmosPackageManifest>
            {
                Document = cosmosPackageManifest,
            };

            ApiDataValidator.Validate(cosmosDocument.Document);
            await this.cosmosDatabase.Add <CosmosPackageManifest>(cosmosDocument);
        }
 /// <inheritdoc />
 public async Task Upsert <T>(CosmosDocument <T> cosmosDocument)
     where T : class
 {
     try
     {
         Uri documentCollectionUri = UriFactory.CreateDocumentCollectionUri(this.database, this.collection);
         await this.client.UpsertDocumentAsync(documentCollectionUri, cosmosDocument.Document);
     }
     catch (DocumentClientException documentClientException)
     {
         throw new CosmosDatabaseException(documentClientException);
     }
     catch (Exception exception)
     {
         throw new DefaultException(exception);
     }
 }
 /// <inheritdoc />
 public async Task Delete <T>(CosmosDocument <T> cosmosDocument)
     where T : class
 {
     try
     {
         Uri documentUri = UriFactory.CreateDocumentUri(this.database, this.collection, cosmosDocument.Id);
         await this.client.DeleteDocumentAsync(
             documentUri,
             new RequestOptions
         {
             PartitionKey = new PartitionKey(cosmosDocument.PartitionKey),
         });
     }
     catch (DocumentClientException documentClientException)
     {
         throw new CosmosDatabaseException(documentClientException);
     }
     catch (Exception exception)
     {
         throw new DefaultException(exception);
     }
 }
示例#12
0
        /// <inheritdoc />
        public async Task <ApiDataPage <Locale> > GetLocales(string packageIdentifier, string packageVersion, string packageLocale, IQueryCollection queryParameters)
        {
            // Process Continuation token
            string continuationToken = null;

            if (queryParameters != null)
            {
                continuationToken = queryParameters[QueryConstants.ContinuationToken];
            }

            continuationToken = continuationToken != null?StringEncoder.DecodeContinuationToken(continuationToken) : null;

            // Fetch Current Package
            CosmosDocument <CosmosPackageManifest> cosmosDocument =
                await this.cosmosDatabase.GetByIdAndPartitionKey <CosmosPackageManifest>(packageIdentifier, packageIdentifier);

            // Get Versions and convert.
            ApiDataPage <Locale> locales = new ApiDataPage <Locale>();

            locales.Items             = cosmosDocument.Document.GetLocale(packageLocale, packageVersion).Select(locale => new Locale(locale)).ToList();
            locales.ContinuationToken = null;
            return(locales);
        }