Пример #1
0
        internal static 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 = query.FirstOrDefault(d => d.BotStoreType == botStoreType &&
                                              d.ChannelId == key.ChannelId &&
                                              d.ConversationId == key.ConversationId);
                break;

            case BotStoreType.BotUserData:
                entity = query.FirstOrDefault(d => d.BotStoreType == botStoreType &&
                                              d.ChannelId == key.ChannelId &&
                                              d.UserId == key.UserId);
                break;

            case BotStoreType.BotPrivateConversationData:
                entity = query.FirstOrDefault(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);
        }
Пример #2
0
        async Task <BotData> IBotDataStore <BotData> .LoadAsync(IAddress key, BotStoreType botStoreType, CancellationToken cancellationToken)
        {
            using (var context = new SqlBotDataContext(_connectionStringName))
            {
                try
                {
                    SqlBotDataEntity entity = SqlBotDataEntity.GetSqlBotDataEntity(key, botStoreType, context);

                    if (entity == null)
                    {
                        return(new BotData(eTag: String.Empty, data: null));
                    }

                    return(new BotData(entity.ETag, entity.GetData()));
                }
                catch (Exception ex)
                {
                    throw new HttpException((int)HttpStatusCode.InternalServerError, ex.Message);
                }
            }
        }
Пример #3
0
        async Task IBotDataStore <BotData> .SaveAsync(IAddress key, BotStoreType botStoreType, BotData botData, CancellationToken cancellationToken)
        {
            SqlBotDataEntity 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(_connectionStringName))
            {
                try
                {
                    if (String.IsNullOrEmpty(entity.ETag))
                    {
                        context.BotData.Add(entity);
                    }
                    else if (entity.ETag == "*")
                    {
                        var foundData = 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 = 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);
                            }
                        }
                    }
                    context.SaveChanges();
                }
                catch (System.Data.SqlClient.SqlException err)
                {
                    throw new HttpException((int)HttpStatusCode.InternalServerError, err.Message);
                }
            }
        }