BotSharp/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs

78 lines
2.2 KiB
C#
Raw Normal View History

2023-08-26 04:41:01 +00:00
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Repositories.Records;
2023-08-10 04:53:22 +00:00
using Microsoft.EntityFrameworkCore.Infrastructure;
2023-06-11 23:46:02 +00:00
namespace BotSharp.Core.Repository;
2023-08-10 04:53:22 +00:00
public class BotSharpDbContext : Database, IBotSharpRepository
2023-06-11 23:46:02 +00:00
{
public IQueryable<UserRecord> User => Table<UserRecord>();
public IQueryable<AgentRecord> Agent => Table<AgentRecord>();
2023-07-25 02:22:18 +00:00
public IQueryable<UserAgentRecord> UserAgent => Table<UserAgentRecord>();
2023-06-27 18:31:13 +00:00
public IQueryable<ConversationRecord> Conversation => Table<ConversationRecord>();
2023-08-10 04:53:22 +00:00
2023-09-02 05:10:46 +00:00
public void UpdateAgent(AgentRecord agent)
{
throw new NotImplementedException();
}
public void CreateUser(UserRecord user)
{
throw new NotImplementedException();
}
public UserRecord GetUserByEmail(string email)
{
throw new NotImplementedException();
}
2023-08-10 04:53:22 +00:00
public int Transaction<TTableInterface>(Action action)
{
DatabaseFacade database = base.GetMaster(typeof(TTableInterface)).Database;
int num = 0;
if (database.CurrentTransaction == null)
{
using (Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction dbContextTransaction = database.BeginTransaction())
{
try
{
action();
num = base.SaveChanges();
dbContextTransaction.Commit();
return num;
}
catch (Exception ex)
{
dbContextTransaction.Rollback();
if (ex.Message.Contains("See the inner exception for details"))
{
throw ex.InnerException;
}
throw ex;
}
}
}
try
{
action();
return base.SaveChanges();
}
catch (Exception ex2)
{
if (database.CurrentTransaction != null)
{
database.CurrentTransaction.Rollback();
}
if (ex2.Message.Contains("See the inner exception for details"))
{
throw ex2.InnerException;
}
throw ex2;
}
}
2023-06-11 23:46:02 +00:00
}