public static async Task AddItemsToContainerAsync(BotConversationState botConversationState, ILogger log) { try { //Connect to DB await EnsureCosmosDBAsync(log); // Read the item to see if it exists. ItemResponse <BotConversationState> botConversationStateResponse = await botStateContainer.ReadItemAsync <BotConversationState>(botConversationState.FreshDeskId, new PartitionKey(botConversationState.FreshDeskId)); log.LogInformation("Conversation in database corresponding to FreshDeskId: {0} already exists\n", botConversationStateResponse.Resource.BotConversationId); } catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.NotFound) { try { // Create an item in the container ItemResponse <BotConversationState> botConversationStateResponse = await botStateContainer.CreateItemAsync(botConversationState, new PartitionKey(botConversationState.FreshDeskId)); log.LogInformation("Created item in database with ConversationId: {0} \n", botConversationStateResponse.Resource.BotConversationId); } catch (Exception ex2) { log.LogError("Exception occurred in AddItemsToContainerAsync: {1}", ex2); throw; } } }
public static async Task <DateTime> UpdateLastRun(ILogger log) { // Set variable to keep lastRun time DateTime lastRun; // Bootstrap object to update DB with new date BotLastRun freshDeskBotLastRun = new BotLastRun() { Id = "0" }; try { //Connect to DB await EnsureCosmosDBAsync(log); // Create LastRun container in DB if not existing lastRunContainer = await database.CreateContainerIfNotExistsAsync("LastRun", "/id"); // Read the item to see if it exists. ItemResponse <BotLastRun> freshDeskBotLastRunResponse; freshDeskBotLastRunResponse = await lastRunContainer.ReadItemAsync <BotLastRun>(freshDeskBotLastRun.Id, new PartitionKey(freshDeskBotLastRun.Id)); log.LogInformation("Last run time (GMT) was: {0}\n", freshDeskBotLastRunResponse.Resource.LastRun); // Keep the last run time as return parameter of the function lastRun = freshDeskBotLastRunResponse.Resource.LastRun; // Update the lastrun time in DB freshDeskBotLastRun.LastRun = DateTime.Now.ToUniversalTime(); freshDeskBotLastRunResponse = await lastRunContainer.ReplaceItemAsync <BotLastRun>(freshDeskBotLastRun, freshDeskBotLastRun.Id, new PartitionKey(freshDeskBotLastRun.Id)); return(lastRun); } catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.NotFound) { try { lastRun = DateTime.Now; // Set current time as initial value in DB log.LogInformation("Setting initial last run time to: {0}", freshDeskBotLastRun.LastRun); freshDeskBotLastRun.LastRun = DateTime.Now.ToUniversalTime(); ItemResponse <BotLastRun> botConversationStateResponse = await lastRunContainer.CreateItemAsync(freshDeskBotLastRun, new PartitionKey(freshDeskBotLastRun.Id)); return(lastRun); } catch (Exception ex2) { log.LogError("Exception occurred in ReplaceFreshDeskBotStateAsync: {1}", ex2); throw; } } }
public async Task <T> GetItemAsync(string id) { try { ItemResponse <T> response = await _container.ReadItemAsync <T>(id, ResolvePartitionKey(id)); return(response.Resource); } catch (CosmosException ex) when(ex.StatusCode == System.Net.HttpStatusCode.NotFound) { return(null); } }