using BotSharp.Platform.Abstraction;
using BotSharp.Platform.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BotSharp.Core
{
///
/// Save agent instance into memory.
/// Caution: Data will be lost once application restarts.
///
public class AgentStorageInMemory : IAgentStorage
{
private static Dictionary> agents;
public AgentStorageInMemory()
{
if (agents == null) agents = new Dictionary>();
}
public StandardAgent FetchById(string agentId)
{
if (agents.ContainsKey(agentId))
{
return agents[agentId];
}
else
{
return null;
}
}
public StandardAgent FetchByName(string agentName)
{
var data = agents.FirstOrDefault(x => x.Value.Name == agentName);
return data.Value;
}
public bool Persist(StandardAgent agent)
{
if (String.IsNullOrEmpty(agent.Id))
{
agent.Id = Guid.NewGuid().ToString();
agents[agent.Id] = agent;
}
else
{
agents[agent.Id] = agent;
}
return true;
}
public List> Query()
{
return agents.Select(x => x.Value).ToList();
}
}
}