private async Task <TwinServiceListModel> GetTwins(string tenantId, string collectionId)
        {
            var         sql          = CosmosOperations.GetDocumentsByCollectionId("CollectionId", collectionId);
            FeedOptions queryOptions = new FeedOptions
            {
                EnableCrossPartitionQuery = true,
                EnableScanInQuery         = true,
            };

            List <Document> docs = new List <Document>();

            try
            {
                CosmosOperations storageClient = await CosmosOperations.GetClientAsync();

                docs = await storageClient.QueryAllDocumentsAsync(
                    "pcs-storage",
                    $"pcs-{tenantId}",
                    queryOptions,
                    sql);

                var result = docs == null ?
                             new List <TwinServiceModel>() :
                             docs
                             .Select(doc => new ValueServiceModel(doc)).Select(x => JsonConvert.DeserializeObject <TwinServiceModel>(x.Data))
                             .ToList();
                return(new TwinServiceListModel(result, null));
            }
            catch (ResourceNotFoundException e)
            {
                throw new ResourceNotFoundException($"No records exist in CosmosDb. The CollectionId {collectionId} does not exist.", e);
            }
        }
        private async Task SaveDeviceStatuses(IDictionary <string, DeploymentStatus> deviceStatuses, string deploymentId, string tenantId)
        {
            CosmosOperations storageClient = await CosmosOperations.GetClientAsync();

            var sql = CosmosOperations.GetDocumentsByCollectionId("CollectionId", string.Format(DeviceStatusesCollection, deploymentId));

            var existingDeviceStatuses = await storageClient.QueryAllDocumentsAsync(
                "pcs-storage",
                $"pcs-{tenantId}",
                this.DefaultQueryOptions,
                sql);

            if (existingDeviceStatuses != null && existingDeviceStatuses.Count > 0)
            {
                foreach (var item in existingDeviceStatuses)
                {
                    await storageClient.DeleteDocumentAsync(item.Id, this.GenerateCollectionLink(tenantId));
                }
            }

            if (deviceStatuses != null)
            {
                for (int i = 0; i < deviceStatuses.Count; i = i + DeviceStatusLength)
                {
                    var items = deviceStatuses.Skip(i).Take(DeviceStatusLength).ToDictionary(p => p.Key, p => p.Value);
                    var value = JsonConvert.SerializeObject(
                        new DeviceStatusServiceModel
                    {
                        DeviceStatuses = items,
                        DeploymentId   = deploymentId,
                    },
                        Formatting.Indented,
                        new JsonSerializerSettings
                    {
                        NullValueHandling = NullValueHandling.Ignore,
                    });
                    await storageClient.SaveDocumentAsync(string.Format(DeviceStatusesCollection, deploymentId), Guid.NewGuid().ToString(), new ValueServiceModel()
                    {
                        Data = value
                    }, this.GenerateCollectionLink(tenantId));
                }
            }
        }
        private async Task <IDictionary <string, DeploymentStatus> > FetchDeviceStatuses(string tenantId, string deploymentId)
        {
            CosmosOperations storageClient = await CosmosOperations.GetClientAsync();

            var sql      = CosmosOperations.GetDocumentsByCollectionId("CollectionId", string.Format(DeviceStatusesCollection, deploymentId));
            var statuses = new Dictionary <string, DeploymentStatus>();

            var existingDeviceStatuses = await storageClient.QueryAllDocumentsAsync(
                "pcs-storage",
                $"pcs-{tenantId}",
                this.DefaultQueryOptions,
                sql);

            if (existingDeviceStatuses != null && existingDeviceStatuses.Count > 0)
            {
                foreach (var item in existingDeviceStatuses.Select(doc => new ValueServiceModel(doc)))
                {
                    statuses = statuses.Union(JsonConvert.DeserializeObject <DeviceStatusServiceModel>(item.Data).DeviceStatuses).ToDictionary(k => k.Key, v => v.Value);
                }
            }

            return(statuses);
        }