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

43 lines
1.2 KiB
C#
Raw Normal View History

2023-06-23 04:43:00 +00:00
using BotSharp.Abstraction.Agents.Models;
namespace BotSharp.Core.Agents.Services;
public partial class AgentService
{
public async Task<Agent> CreateAgent(Agent agent)
{
2023-07-21 21:56:14 +00:00
var db = _services.GetRequiredService<BotSharpDbContext>();
2023-07-25 02:22:18 +00:00
var record = (from a in db.Agent
join ua in db.UserAgent on a.Id equals ua.AgentId
where ua.UserId == _user.Id && a.Name == agent.Name
select a).FirstOrDefault();
2023-06-23 04:43:00 +00:00
if (record != null)
{
return record.ToAgent();
}
record = AgentRecord.FromAgent(agent);
record.Id = Guid.NewGuid().ToString();
record.CreatedDateTime = DateTime.UtcNow;
record.UpdatedDateTime = DateTime.UtcNow;
2023-07-25 02:22:18 +00:00
var userAgentRecord = new UserAgentRecord
{
UserId = _user.Id,
AgentId = record.Id,
CreatedTime = DateTime.UtcNow,
UpdatedTime = DateTime.UtcNow
};
2023-07-21 21:56:14 +00:00
db.Transaction<IBotSharpTable>(delegate
2023-06-23 04:43:00 +00:00
{
2023-07-21 21:56:14 +00:00
db.Add<IBotSharpTable>(record);
2023-07-25 02:22:18 +00:00
db.Add<IBotSharpTable>(userAgentRecord);
2023-06-23 04:43:00 +00:00
});
return record.ToAgent();
}
}