2023-06-11 23:46:02 +00:00
|
|
|
using BotSharp.Abstraction.Agents;
|
|
|
|
|
using BotSharp.Abstraction.Agents.Models;
|
2023-06-12 13:28:49 +00:00
|
|
|
using BotSharp.Abstraction.Users;
|
2023-06-11 23:46:02 +00:00
|
|
|
|
|
|
|
|
namespace BotSharp.Core.Agents.Services;
|
|
|
|
|
|
|
|
|
|
public class AgentService : IAgentService
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _services;
|
2023-06-17 02:42:35 +00:00
|
|
|
private readonly IUserIdentity _user;
|
2023-06-12 13:28:49 +00:00
|
|
|
|
2023-06-17 02:42:35 +00:00
|
|
|
public AgentService(IServiceProvider services, IUserIdentity user)
|
2023-06-11 23:46:02 +00:00
|
|
|
{
|
|
|
|
|
_services = services;
|
2023-06-12 13:28:49 +00:00
|
|
|
_user = user;
|
2023-06-11 23:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-12 13:28:49 +00:00
|
|
|
public async Task<Agent> CreateAgent(Agent agent)
|
2023-06-11 23:46:02 +00:00
|
|
|
{
|
|
|
|
|
var db = _services.GetRequiredService<AgentDbContext>();
|
2023-06-12 13:28:49 +00:00
|
|
|
var record = db.Agent.FirstOrDefault(x => x.OwnerId == _user.Id && x.Name == agent.Name);
|
|
|
|
|
if (record != null)
|
|
|
|
|
{
|
|
|
|
|
return record.ToAgent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
record = AgentRecord.FromAgent(agent);
|
|
|
|
|
record.Id = Guid.NewGuid().ToString();
|
|
|
|
|
record.OwnerId = _user.Id;
|
|
|
|
|
record.CreatedDateTime = DateTime.UtcNow;
|
|
|
|
|
record.UpdatedDateTime = DateTime.UtcNow;
|
2023-06-11 23:46:02 +00:00
|
|
|
|
|
|
|
|
db.Transaction<IAgentTable>(delegate
|
|
|
|
|
{
|
|
|
|
|
db.Add<IAgentTable>(record);
|
|
|
|
|
});
|
|
|
|
|
|
2023-06-12 13:28:49 +00:00
|
|
|
return record.ToAgent();
|
2023-06-11 23:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<bool> DeleteAgent(string id)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 11:36:03 +00:00
|
|
|
public async Task<List<Agent>> GetAgents()
|
|
|
|
|
{
|
|
|
|
|
var db = _services.GetRequiredService<AgentDbContext>();
|
|
|
|
|
var query = from agent in db.Agent
|
|
|
|
|
where agent.OwnerId == _user.Id
|
|
|
|
|
select agent.ToAgent();
|
|
|
|
|
return query.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-11 23:46:02 +00:00
|
|
|
public Task UpdateAgent(Agent agent)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
}
|