Seperate UserAgent.

This commit is contained in:
hchen2020 2023-07-24 21:22:18 -05:00
parent 9ec85acf7a
commit f84534ec1a
11 changed files with 81 additions and 50 deletions

View file

@ -1,9 +0,0 @@
namespace BotSharp.Abstraction.Users;
public interface ICurrentUser
{
string Id { get; }
string Email { get; }
string FirstName { get; }
string LastName { get; }
}

View file

@ -7,7 +7,12 @@ public partial class AgentService
public async Task<Agent> CreateAgent(Agent agent)
{
var db = _services.GetRequiredService<BotSharpDbContext>();
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<IBotSharpTable>(delegate
{
db.Add<IBotSharpTable>(record);
db.Add<IBotSharpTable>(userAgentRecord);
});
return record.ToAgent();

View file

@ -8,9 +8,10 @@ public partial class AgentService
public async Task<List<Agent>> GetAgents()
{
var db = _services.GetRequiredService<BotSharpDbContext>();
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();
}

View file

@ -11,7 +11,10 @@ public partial class AgentService
db.Transaction<IBotSharpTable>(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;

View file

@ -7,7 +7,6 @@ public static class BotSharpServiceCollectionExtensions
{
public static IServiceCollection AddBotSharp(this IServiceCollection services, IConfiguration config)
{
services.AddScoped<IUserIdentity, UserIdentity>();
services.AddScoped<IUserService, UserService>();
services.AddScoped<IAgentService, AgentService>();

View file

@ -4,5 +4,6 @@ public class BotSharpDbContext : Database
{
public IQueryable<UserRecord> User => Table<UserRecord>();
public IQueryable<AgentRecord> Agent => Table<AgentRecord>();
public IQueryable<UserAgentRecord> UserAgent => Table<UserAgentRecord>();
public IQueryable<ConversationRecord> Conversation => Table<ConversationRecord>();
}

View file

@ -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
};

View file

@ -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
};
}
}

View file

@ -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;

View file

@ -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<Claim> _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;
}

View file

@ -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<IUserIdentity, UserIdentity>();
// Add BotSharp
builder.Services.AddBotSharp(builder.Configuration);
builder.Services.AddBotSharp(builder.Configuration)
.ConfigureBotSharpRepository<DbContext4SqlServer>(builder.Configuration);
builder.Services.AddCors(options =>
{