public async Task <BotData> LoadAsync(IAddress key, BotStoreType botStoreType, CancellationToken cancellationToken) { using (var context = new ASAChatDataContext(_connectionString)) { try { var entity = await ASAChatDataEntity.GetASAChatDataEntity(key, botStoreType, context); return(entity == null ? new BotData(string.Empty) : new BotData(entity.ETag, entity.GetData())); // return botdata } catch (System.Data.SqlClient.SqlException err) { throw new HttpException((int)HttpStatusCode.InternalServerError, err.Message); } } }
/// <summary> /// Throw if the database or SqlBotDataEntities table have not been created. /// </summary> internal static void AssertDatabaseReady() { var connectionString = ConfigurationManager.ConnectionStrings["ASAChatDataContextConnectionString"] .ConnectionString; using (var context = new ASAChatDataContext(connectionString)) { if (!context.Database.Exists()) { throw new ArgumentException("The sql database defined in the connection has not been created."); } if (context.Database.SqlQuery <int>(@"IF EXISTS (SELECT * FROM sys.tables WHERE name = 'UserLog') SELECT 1 ELSE SELECT 0").SingleOrDefault() != 1) { throw new ArgumentException("The UserLog table has not been created in the database."); } } }
public async Task LogActivity(Activity activity) { using (var context = new ASAChatDataContext(_connectionString)) { int maxLength = Math.Min(activity.Text.Length, 500); // Create a new UserLog object UserLog newUserLog = new UserLog(); // Set the properties on the UserLog object newUserLog.Channel = activity.ChannelId; newUserLog.UserID = activity.From.Id; newUserLog.UserName = activity.From.Name; newUserLog.created = DateTime.UtcNow; newUserLog.Message = activity.Text.Substring(0, maxLength); context.SaveChanges(); // Add the UserLog object to UserLogs //db.UserLogs.Add(newUserLog); //// Save the changes to the database //db.SaveChanges(); } }
protected override void Load(ContainerBuilder builder) { builder.RegisterType <ConnectorStore>() .AsSelf() .InstancePerLifetimeScope(); ASAChatDataContext.AssertDatabaseReady(); var store = new ASAChatDataStore(ConfigurationManager.ConnectionStrings["ASAChatDataContextConnectionString"] .ConnectionString); builder.Register(c => store) .Keyed <IBotDataStore <BotData> >(KeyDataStore) .AsSelf() .SingleInstance(); builder.Register(c => new CachingBotDataStore(c.ResolveKeyed <IBotDataStore <BotData> >(KeyDataStore), CachingBotDataStoreConsistencyPolicy.LastWriteWins)) .As <IBotDataStore <BotData> >() .AsSelf() .InstancePerLifetimeScope(); }
internal static async Task <ASAChatDataEntity> GetASAChatDataEntity(IAddress key, BotStoreType botStoreType, ASAChatDataContext context) { ASAChatDataEntity entity = null; var query = context.BotData.OrderByDescending(d => d.Timestamp); switch (botStoreType) { case BotStoreType.BotConversationData: entity = await query.FirstOrDefaultAsync(d => d.BotStoreType == botStoreType && d.ChannelId == key.ChannelId && d.ConversationId == key.ConversationId); break; case BotStoreType.BotUserData: entity = await query.FirstOrDefaultAsync(d => d.BotStoreType == botStoreType && d.ChannelId == key.ChannelId && d.UserId == key.UserId); break; case BotStoreType.BotPrivateConversationData: entity = await query.FirstOrDefaultAsync(d => d.BotStoreType == botStoreType && d.ChannelId == key.ChannelId && d.ConversationId == key.ConversationId && d.UserId == key.UserId); break; default: throw new ArgumentException("Unsupported bot store type!"); } return(entity); }
public async Task SaveAsync(IAddress key, BotStoreType botStoreType, BotData botData, CancellationToken cancellationToken) { var entity = new ASAChatDataEntity(botStoreType, key.BotId, key.ChannelId, key.ConversationId, key.UserId, botData.Data) { ETag = botData.ETag, ServiceUrl = key.ServiceUrl }; using (var context = new ASAChatDataContext(_connectionString)) { try { if (string.IsNullOrEmpty(botData.ETag)) { context.BotData.Add(entity); } else if (entity.ETag == "*") { var foundData = await ASAChatDataEntity.GetASAChatDataEntity(key, botStoreType, context); if (botData.Data != null) { if (foundData == null) { context.BotData.Add(entity); } else { foundData.Data = entity.Data; foundData.ServiceUrl = entity.ServiceUrl; } } else { if (foundData != null) { context.BotData.Remove(foundData); } } } else { var foundData = await ASAChatDataEntity.GetASAChatDataEntity(key, botStoreType, context); if (botData.Data != null) { if (foundData == null) { context.BotData.Add(entity); } else { foundData.Data = entity.Data; foundData.ServiceUrl = entity.ServiceUrl; foundData.ETag = entity.ETag; } } else { if (foundData != null) { context.BotData.Remove(foundData); } } } await context.SaveChangesAsync(cancellationToken); } catch (System.Data.SqlClient.SqlException err) { throw new HttpException((int)HttpStatusCode.InternalServerError, err.Message); } } }