Ejemplo n.º 1
0
        internal static async Task <SqlBotDataEntity> GetSqlBotDataEntity(IAddress key, BotStoreType botStoreType, SqlBotDataContext context)
        {
            SqlBotDataEntity 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);
        }
Ejemplo n.º 2
0
        public async Task <BotData> LoadAsync(IAddress key, BotStoreType botStoreType, CancellationToken cancellationToken)
        {
            using (var context = new SqlBotDataContext(_connectionString))
            {
                try
                {
                    var entity = await SqlBotDataEntity.GetSqlBotDataEntity(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);
                }
            }
        }
Ejemplo n.º 3
0
        public async Task SaveAsync(IAddress key, BotStoreType botStoreType, BotData botData, CancellationToken cancellationToken)
        {
            var entity = new SqlBotDataEntity(botStoreType, key.BotId, key.ChannelId, key.ConversationId, key.UserId, botData.Data)
            {
                ETag       = botData.ETag,
                ServiceUrl = key.ServiceUrl
            };

            using (var context = new SqlBotDataContext(_connectionString))
            {
                try
                {
                    if (string.IsNullOrEmpty(botData.ETag))
                    {
                        context.BotData.Add(entity);
                    }
                    else if (entity.ETag == "*")
                    {
                        var foundData = await SqlBotDataEntity.GetSqlBotDataEntity(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 SqlBotDataEntity.GetSqlBotDataEntity(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);
                }
            }
        }