diff --git a/src/Infrastructure/BotSharp.Abstraction/Users/ICurrentUser.cs b/src/Infrastructure/BotSharp.Abstraction/Users/ICurrentUser.cs deleted file mode 100644 index f4498a22..00000000 --- a/src/Infrastructure/BotSharp.Abstraction/Users/ICurrentUser.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace BotSharp.Abstraction.Users; - -public interface ICurrentUser -{ - string Id { get; } - string Email { get; } - string FirstName { get; } - string LastName { get; } -} diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs index 0d9c21db..8b8b8975 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs @@ -7,7 +7,12 @@ public partial class AgentService public async Task CreateAgent(Agent agent) { var db = _services.GetRequiredService(); - var record = db.Agent.FirstOrDefault(x => x.OwnerId == _user.Id && x.Name == agent.Name); + + 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(); + if (record != null) { return record.ToAgent(); @@ -15,13 +20,21 @@ public partial class AgentService record = AgentRecord.FromAgent(agent); record.Id = Guid.NewGuid().ToString(); - record.OwnerId = _user.Id; record.CreatedDateTime = DateTime.UtcNow; record.UpdatedDateTime = DateTime.UtcNow; + var userAgentRecord = new UserAgentRecord + { + UserId = _user.Id, + AgentId = record.Id, + CreatedTime = DateTime.UtcNow, + UpdatedTime = DateTime.UtcNow + }; + db.Transaction(delegate { db.Add(record); + db.Add(userAgentRecord); }); return record.ToAgent(); diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs index 27b5a2cf..583bfc27 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs @@ -8,9 +8,10 @@ public partial class AgentService public async Task> GetAgents() { var db = _services.GetRequiredService(); - var query = from agent in db.Agent - where agent.OwnerId == _user.Id - select agent.ToAgent(); + var query = from a in db.Agent + join ua in db.UserAgent on a.Id equals ua.AgentId + where ua.UserId == _user.Id + select a.ToAgent(); return query.ToList(); } diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs index 25723fcc..572b795e 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs @@ -11,7 +11,10 @@ public partial class AgentService db.Transaction(delegate { - var record = db.Agent.FirstOrDefault(x => x.OwnerId == agent.OwerId && x.Id == agent.Id); + var record = (from a in db.Agent + join ua in db.UserAgent on a.Id equals ua.AgentId + where ua.UserId == agent.OwerId && a.Id == agent.Id + select a).First(); record.Name = agent.Name; record.Description = agent.Description; diff --git a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs index cb8a3545..35649f6d 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs +++ b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs @@ -7,7 +7,6 @@ public static class BotSharpServiceCollectionExtensions { public static IServiceCollection AddBotSharp(this IServiceCollection services, IConfiguration config) { - services.AddScoped(); services.AddScoped(); services.AddScoped(); diff --git a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs index 98fa8eeb..9c449b90 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs @@ -4,5 +4,6 @@ public class BotSharpDbContext : Database { public IQueryable User => Table(); public IQueryable Agent => Table(); + public IQueryable UserAgent => Table(); public IQueryable Conversation => Table(); } diff --git a/src/Infrastructure/BotSharp.Core/Repository/DbTables/AgentRecord.cs b/src/Infrastructure/BotSharp.Core/Repository/DbTables/AgentRecord.cs index 99fd31b3..31754c8e 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/DbTables/AgentRecord.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/DbTables/AgentRecord.cs @@ -11,12 +11,8 @@ public class AgentRecord : DbRecord, IBotSharpTable [MaxLength(64)] public string Name { get; set; } = string.Empty; - [Required] - [MaxLength(36)] - public string OwnerId { get; set; } = string.Empty; - [MaxLength(512)] - public string Description { get; set; } + public string? Description { get; set; } [Required] public DateTime CreatedDateTime { get; set; } @@ -30,8 +26,7 @@ public class AgentRecord : DbRecord, IBotSharpTable { Id = agent.Id, Name = agent.Name, - Description = agent.Description, - OwnerId = agent.OwerId + Description = agent.Description }; } @@ -42,7 +37,6 @@ public class AgentRecord : DbRecord, IBotSharpTable Id = Id, Name = Name, Description = Description, - OwerId = OwnerId, CreatedDateTime = CreatedDateTime, UpdatedDateTime = UpdatedDateTime }; diff --git a/src/Infrastructure/BotSharp.Core/Repository/DbTables/UserAgentRecord.cs b/src/Infrastructure/BotSharp.Core/Repository/DbTables/UserAgentRecord.cs new file mode 100644 index 00000000..4d855d32 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Repository/DbTables/UserAgentRecord.cs @@ -0,0 +1,45 @@ +using BotSharp.Abstraction.Users.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace BotSharp.Core.Repository.DbTables; + +[Table("UserAgent")] +public class UserAgentRecord : DbRecord, IBotSharpTable +{ + [Required] + [StringLength(36)] + public string UserId { get; set; } = string.Empty; + + [Required] + [MaxLength(36)] + public string AgentId { get; set; } = string.Empty; + + [Required] + public DateTime UpdatedTime { get; set; } = DateTime.UtcNow; + + [Required] + public DateTime CreatedTime { get; set; } = DateTime.UtcNow; + + public static UserRecord FromUser(User user) + { + return new UserRecord + { + Id = user.Id, + FirstName = user.FirstName, + LastName = user.LastName, + Email = user.Email, + Password = user.Password + }; + } + + public User ToUser() + { + return new User + { + Id = Id, + CreatedTime = CreatedTime, + UpdatedTime = UpdatedTime + }; + } +} diff --git a/src/Infrastructure/BotSharp.Core/Repository/DbTables/UserRecord.cs b/src/Infrastructure/BotSharp.Core/Repository/DbTables/UserRecord.cs index 88d8b51c..159ce96a 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/DbTables/UserRecord.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/DbTables/UserRecord.cs @@ -27,6 +27,9 @@ public class UserRecord : DbRecord, IBotSharpTable [MaxLength(256)] public string Password { get; set; } = string.Empty; + [MaxLength(36)] + public string? ExternalId { get; set; } + [Required] public DateTime UpdatedTime { get; set; } = DateTime.UtcNow; diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/CurrentUser.cs b/src/Infrastructure/BotSharp.Core/Users/Services/CurrentUser.cs deleted file mode 100644 index f0ef926f..00000000 --- a/src/Infrastructure/BotSharp.Core/Users/Services/CurrentUser.cs +++ /dev/null @@ -1,25 +0,0 @@ -using BotSharp.Abstraction.Users; -using Microsoft.AspNetCore.Http; -using System.Security.Claims; - -namespace BotSharp.Core.Users.Services; - -public class CurrentUser : ICurrentUser -{ - private readonly IHttpContextAccessor _contextAccessor; - private IEnumerable _claims => _contextAccessor.HttpContext.User.Claims; - - public CurrentUser(IHttpContextAccessor contextAccessor) - { - _contextAccessor = contextAccessor; - } - - public string Id => _claims.First(x => x.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").Value; - - - public string Email => _claims.First(x => x.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress").Value; - - public string FirstName => _claims.First(x => x.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname").Value; - - public string LastName => _claims.First(x => x.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname").Value; -} diff --git a/src/WebStarter/Program.cs b/src/WebStarter/Program.cs index 695a6121..5d9db003 100644 --- a/src/WebStarter/Program.cs +++ b/src/WebStarter/Program.cs @@ -1,4 +1,7 @@ +using BotSharp.Abstraction.Users; using BotSharp.Core; +using BotSharp.Core.Users.Services; +using EntityFrameworkCore.BootKit; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.IdentityModel.Tokens; using System.Text; @@ -34,8 +37,11 @@ builder.Services.AddAuthentication(options => }; }); +builder.Services.AddScoped(); + // Add BotSharp -builder.Services.AddBotSharp(builder.Configuration); +builder.Services.AddBotSharp(builder.Configuration) + .ConfigureBotSharpRepository(builder.Configuration); builder.Services.AddCors(options => {