BotSharp/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs

60 lines
1.6 KiB
C#
Raw Normal View History

2023-06-11 23:46:02 +00:00
using BotSharp.Abstraction.Agents;
using BotSharp.Abstraction.Agents.Models;
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;
private readonly ICurrentUser _user;
public AgentService(IServiceProvider services, ICurrentUser user)
2023-06-11 23:46:02 +00:00
{
_services = services;
_user = user;
2023-06-11 23:46:02 +00:00
}
public async Task<Agent> CreateAgent(Agent agent)
2023-06-11 23:46:02 +00:00
{
var db = _services.GetRequiredService<AgentDbContext>();
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);
});
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();
}
}