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-06-23 04:43:00 +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-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-06-23 04:43:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return record.ToAgent();
|
|
|
|
|
}
|
|
|
|
|
}
|