/// <summary>
        /// Load the program from CosmosDB
        /// </summary>
        public async Task <bool> Load()
        {
            try
            {
                // Read the item to see if it exists.
                VodProgram item = await Database.VodProgram.ReadItemAsync <VodProgram>(this.VodId, new PartitionKey(this.PartitionKey));

                this.OnAir            = item.OnAir;
                this.PgmNo            = item.PgmNo;
                this.Path1080P        = item.Path1080P;
                this.Path720P         = item.Path720P;
                this.HasReferenceFile = item.HasReferenceFile;
                this.Plot             = item.Plot;
                this.ProgramUuid      = item.ProgramUuid;
                this.Width            = item.Width;
                this.Height           = item.Height;
                this.Aspect           = item.Aspect;
                this.Duration         = item.Duration;
                this.Title            = item.Title;
                this.LastUpdate       = item.LastUpdate;
                return(true);
            }
            catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.NotFound)
            {
                log.LogDebug(String.Format("Episode does not exist in CosmosDB : {0}", this.VodId));
                return(false);
            }
            catch (CosmosException ex)
            {
                // Unexpected exception
                log.LogError(String.Format("Unexpected: Couldn't load episode from CosmosDB : {0} - Status code: {1}", this.VodId, ex.StatusCode));
                throw;
            }
        }
Example #2
0
        /// <summary>
        /// Populates CosmosDB with the latest list of programs
        /// </summary>
        /// <returns>Number of items written to CosmosDB</returns>
        public static async Task <bool> PopulateCloudCache(ILogger log)
        {
            var episodes = await NhkApi.GetVodIdList(log);

            // Get the existing entries from CosmosDB
            var                       sqlQueryText           = "SELECT c.id FROM c";
            QueryDefinition           queryDefinition        = new QueryDefinition(sqlQueryText);
            FeedIterator <VodProgram> queryResultSetIterator = Database.VodProgram.GetItemQueryIterator <VodProgram>(queryDefinition);


            var  dbEpisodes    = new List <VodProgram>();
            int  insertCounter = 0;
            int  deleteCounter = 0;
            bool success       = false;

            while (queryResultSetIterator.HasMoreResults)
            {
                FeedResponse <VodProgram> currentResultSet = await queryResultSetIterator.ReadNextAsync();

                dbEpisodes.AddRange(currentResultSet);
            }

            var dbVodIds = from p in dbEpisodes
                           select p.VodId;

            // Update CosmosDB with new entries
            foreach (var vodId in episodes)
            {
                // Check if vodId is already in CosmosDB
                if (!dbVodIds.Contains(vodId))
                {
                    // No, create it it!
                    success = false;
                    var program = new VodProgram(vodId, log);
                    success = await program.Get();

                    if (success)
                    {
                        insertCounter++;
                    }
                }
            }

            // Delete stale DB Entries
            foreach (var vodId in dbVodIds)
            {
                // Check if vodId no longer exists in NHK episode list
                if (!episodes.Contains(vodId))
                {
                    // It doesn't exist, delete it
                    success = false;
                    var program = new VodProgram(vodId, log);
                    success = await program.Delete();

                    if (success)
                    {
                        deleteCounter++;
                    }
                }
            }

            log.LogInformation(String.Format("Inserted {0} - deleted {1} episodes from CosmosDB", insertCounter, deleteCounter));

            return(true);
        }