rename to Ac... improvements, fixes, etc...
This commit is contained in:
parent
4f1d42453c
commit
782d8ece5a
|
|
@ -4,10 +4,10 @@ using AyCode.Database.DbContexts;
|
|||
|
||||
namespace AyCode.Database.Tests;
|
||||
|
||||
public abstract class DatabaseTestModelBase<TDbContext> : TestModelBase where TDbContext : DbContextBase
|
||||
public abstract class DatabaseTestModelBase<TDbContext> : TestModelBase where TDbContext : AcDbContextBase
|
||||
{
|
||||
protected DatabaseTestModelBase()
|
||||
{
|
||||
//var dal = new dal
|
||||
//var acDal = new acDal
|
||||
}
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@ using AyCode.Database.DbContexts;
|
|||
namespace AyCode.Database.Tests
|
||||
{
|
||||
[TestClass]
|
||||
public abstract class DatabaseTests : DatabaseTestModelBase<DbContextBase>
|
||||
public abstract class DatabaseTests : DatabaseTestModelBase<AcDbContextBase>
|
||||
{
|
||||
[TestMethod]
|
||||
public void TestMethod1()
|
||||
|
|
|
|||
|
|
@ -11,12 +11,12 @@ namespace AyCode.Database.Tests.Users
|
|||
{
|
||||
[TestClass]
|
||||
public abstract class UserDbSetTest<TDbContext, TUser, TUserToken> : DatabaseTestModelBase<TDbContext>
|
||||
where TDbContext : DbContextBase, IUserDbContextBase<TUser, TUserToken>
|
||||
where TDbContext : AcDbContextBase, IAcUserDbContextBase<TUser, TUserToken>
|
||||
where TUser : class, IUserBase
|
||||
where TUserToken : class, IUserTokenBase
|
||||
|
||||
{
|
||||
//UserDalBase<UserDbContext, User, UserTokenBase>
|
||||
//AcUserDalBase<UserDbContext, User, UserTokenBase>
|
||||
//private UserDal _userDal;
|
||||
|
||||
[TestInitialize]
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore;
|
|||
|
||||
namespace AyCode.Database.DataLayers;
|
||||
|
||||
public abstract class DalBase<TDbContext> : IDalBase<TDbContext> where TDbContext : DbContextBase
|
||||
public abstract class AcDalBase<TDbContext> : IAcDalBase<TDbContext> where TDbContext : AcDbContextBase
|
||||
{
|
||||
public string Name { get; private set; }
|
||||
|
||||
|
|
@ -20,15 +20,15 @@ public abstract class DalBase<TDbContext> : IDalBase<TDbContext> where TDbContex
|
|||
|
||||
public TDbContext Context { get; private set; }
|
||||
|
||||
//protected DalBase()
|
||||
//protected AcDalBase()
|
||||
//{
|
||||
// Name = $"{GetType().Name}";
|
||||
//}
|
||||
|
||||
protected DalBase() : this(Activator.CreateInstance<TDbContext>())
|
||||
protected AcDalBase() : this(Activator.CreateInstance<TDbContext>())
|
||||
{ }
|
||||
|
||||
protected DalBase(TDbContext ctx)
|
||||
protected AcDalBase(TDbContext ctx)
|
||||
{
|
||||
Context = ctx;
|
||||
|
||||
|
|
@ -2,12 +2,12 @@
|
|||
|
||||
namespace AyCode.Database.DataLayers;
|
||||
|
||||
public interface IDalBase //: IDisposable
|
||||
public interface IAcDalBase //: IDisposable
|
||||
{
|
||||
public string Name { get; }
|
||||
}
|
||||
|
||||
public interface IDalBase<TDbContext> : IDalBase where TDbContext : DbContextBase
|
||||
public interface IAcDalBase<TDbContext> : IAcDalBase where TDbContext : AcDbContextBase
|
||||
{
|
||||
public bool AutoCloseSession { get; set; }
|
||||
public TDbContext Context { get; }
|
||||
|
|
@ -7,13 +7,13 @@ namespace AyCode.Database.DataLayers
|
|||
{
|
||||
private static readonly PooledDal Instance = new();
|
||||
|
||||
private readonly ConcurrentDictionary<Guid, ConcurrentDictionary<Type, IDalBase>> _dataLayersPoolById = new();
|
||||
private static ConcurrentDictionary<Guid, ConcurrentDictionary<Type, IDalBase>> DataLayersPoolById => Instance._dataLayersPoolById;
|
||||
private readonly ConcurrentDictionary<Guid, ConcurrentDictionary<Type, IAcDalBase>> _dataLayersPoolById = new();
|
||||
private static ConcurrentDictionary<Guid, ConcurrentDictionary<Type, IAcDalBase>> DataLayersPoolById => Instance._dataLayersPoolById;
|
||||
|
||||
public static int Count => DataLayersPoolById.Count;
|
||||
public static void Clear() => DataLayersPoolById.Clear();
|
||||
|
||||
public static TDal CreateDal<TDal>() where TDal : IDalBase => Activator.CreateInstance<TDal>();
|
||||
public static TDal CreateDal<TDal>() where TDal : IAcDalBase => Activator.CreateInstance<TDal>();
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
|
|
@ -21,20 +21,20 @@ namespace AyCode.Database.DataLayers
|
|||
/// <typeparam name="TDal"></typeparam>
|
||||
/// <param name="id">ONLY SessionId or logged in PlayerId!</param>
|
||||
/// <returns></returns>
|
||||
public static TDal GetDalById<TDal>(Guid id) where TDal : IDalBase
|
||||
public static TDal GetDalById<TDal>(Guid id) where TDal : IAcDalBase
|
||||
{
|
||||
if (id.IsNullOrEmpty()) return CreateDal<TDal>(); //TODO: A Guid.Empty id-t ÁTGONDOLNI! - J.
|
||||
|
||||
if (DataLayersPoolById.TryGetValue(id, out var dataLayersByType))
|
||||
return GetDalByType<TDal>(dataLayersByType);
|
||||
|
||||
dataLayersByType = new ConcurrentDictionary<Type, IDalBase>();
|
||||
dataLayersByType = new ConcurrentDictionary<Type, IAcDalBase>();
|
||||
DataLayersPoolById.TryAdd(id, dataLayersByType);
|
||||
|
||||
return GetDalByType<TDal>(dataLayersByType);
|
||||
}
|
||||
|
||||
private static TDal GetDalByType<TDal>(ConcurrentDictionary<Type, IDalBase> dataLayersByType) where TDal : IDalBase
|
||||
private static TDal GetDalByType<TDal>(ConcurrentDictionary<Type, IAcDalBase> dataLayersByType) where TDal : IAcDalBase
|
||||
{
|
||||
if (dataLayersByType.TryGetValue(typeof(TDal), out var dataLayer))
|
||||
return (TDal)dataLayer;
|
||||
|
|
|
|||
|
|
@ -10,11 +10,12 @@ using AyCode.Interfaces.Users;
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using AyCode.Database.Extensions;
|
||||
using AyCode.Core.Consts;
|
||||
using AyCode.Database.DbSets.Users;
|
||||
|
||||
namespace AyCode.Database.DataLayers.Users
|
||||
{
|
||||
public abstract class UserDalBase<TDbContext, TUser, TUserToken> : DalBase<TDbContext>, IUserDbContextBase<TUser, TUserToken>
|
||||
where TDbContext : DbContextBase, IUserDbContextBase<TUser, TUserToken>
|
||||
public abstract class AcUserDalBase<TDbContext, TUser, TUserToken> : AcDalBase<TDbContext>, IAcUserDbContextBase<TUser, TUserToken>
|
||||
where TDbContext : AcDbContextBase, IAcUserDbContextBase<TUser, TUserToken>
|
||||
where TUser : class, IUserBase
|
||||
where TUserToken : class, IUserTokenBase
|
||||
{
|
||||
|
|
@ -11,7 +11,7 @@ namespace AyCode.Database.DataLayers.Users
|
|||
{
|
||||
public static class AcUserDalExtension
|
||||
{
|
||||
//public static bool DeleteUser(this IUserDbContextBase ctx, IUserBase user)
|
||||
//public static bool DeleteUser(this IAcUserDbContextBase ctx, IUserBase user)
|
||||
//{
|
||||
// if (user == null) return false;
|
||||
|
||||
|
|
@ -36,30 +36,19 @@ namespace AyCode.Database.DataLayers.Users
|
|||
// return ctx.Remove(user).State == EntityState.Deleted;
|
||||
//}
|
||||
|
||||
public static TUser? GetUserById<TUser>(this IUserDbSet<TUser> ctx, Guid userId) where TUser : class, IUserBase
|
||||
{
|
||||
return ctx.Users.FirstOrDefault(u => u.Id == userId);
|
||||
}
|
||||
|
||||
public static TUser? GetUserByEmail<TUser>(this IUserDbSet<TUser> ctx, string email) where TUser : class, IUserBase
|
||||
{
|
||||
var emailLower = email.ToLower();
|
||||
return ctx.Users.FirstOrDefault(u => u.EmailAddress == emailLower);
|
||||
}
|
||||
|
||||
//public static IEnumerable<UserToUserConsent> GetUserConsentsByUserId(this UserDbContextBase ctx, Guid userId)
|
||||
//public static IEnumerable<UserToUserConsent> GetUserConsentsByUserId(this AcUserDbContextBase ctx, Guid userId)
|
||||
// => ctx.UserToUserConsents.Where(u => u.UserId == userId);
|
||||
|
||||
//public static bool IsUserConsentsAccepted(this UserDbContextBase ctx, Guid userId)
|
||||
//public static bool IsUserConsentsAccepted(this AcUserDbContextBase ctx, Guid userId)
|
||||
// => ctx.GetUserConsentsByUserId(userId).Count(x => x.IsAccepted) == AcConst.ConsentCount;
|
||||
|
||||
//public static bool SetUserConsentsToAccepted(this UserDbContextBase ctx, Guid userId)
|
||||
//public static bool SetUserConsentsToAccepted(this AcUserDbContextBase ctx, Guid userId)
|
||||
//{
|
||||
// var user = ctx.GetUserById(userId);
|
||||
// return ctx.SetUserConsentsToAccepted(user);
|
||||
//}
|
||||
|
||||
//public static bool SetUserConsentsToAccepted(this UserDbContextBase ctx, IUserBase? user)
|
||||
//public static bool SetUserConsentsToAccepted(this AcUserDbContextBase ctx, IUserBase? user)
|
||||
//{
|
||||
// if (!user.EmailConfirmed) return false; //Ez nem kéne ide... - J.
|
||||
|
||||
|
|
@ -80,73 +69,7 @@ namespace AyCode.Database.DataLayers.Users
|
|||
//}
|
||||
|
||||
|
||||
public static bool ChangePassword<TUser, TUserToken>(this IUserDbContextBase<TUser, TUserToken> ctx, TUser? user, string passwordHash, string verificationToken)
|
||||
where TUser : class, IUserBase
|
||||
where TUserToken : class, IUserTokenBase
|
||||
{
|
||||
if (user == null || !ctx.IsValidToken(user.Id, verificationToken)) return false;
|
||||
|
||||
user.Password = passwordHash;
|
||||
ctx.Users.Update(user);
|
||||
|
||||
ctx.DeactivateTokens(user.Id);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static TUserToken? UpdateUserTokenSent<TUserToken>(this IUserTokenDbSet<TUserToken> ctx, Guid userId, string verificationToken, DateTime tokenSentUtc) where TUserToken : class, IUserTokenBase
|
||||
{
|
||||
var userToken = ctx.GetUserToken(userId, verificationToken);
|
||||
|
||||
if (userToken == null) return null;
|
||||
|
||||
userToken.TokenSent = tokenSentUtc;
|
||||
userToken.TokenExpiration ??= tokenSentUtc.AddDays(1);
|
||||
|
||||
var utcNow = DateTime.UtcNow;
|
||||
userToken.IsActive = tokenSentUtc <= utcNow && userToken.TokenExpiration > utcNow;
|
||||
|
||||
ctx.UserTokens.Update(userToken);
|
||||
return userToken;
|
||||
}
|
||||
|
||||
public static TUserToken CreateUserToken<TUserToken>(this IUserTokenDbSet<TUserToken> ctx, Guid userId, string verificationToken) where TUserToken : class, IUserTokenBase
|
||||
{
|
||||
var userToken = Activator.CreateInstance<TUserToken>();
|
||||
userToken.UserId = userId;
|
||||
userToken.Token = verificationToken;
|
||||
|
||||
return ctx.CreateUserToken(userToken);
|
||||
}
|
||||
|
||||
public static TUserToken CreateUserToken<TUserToken>(this IUserTokenDbSet<TUserToken> ctx, TUserToken userToken) where TUserToken : class, IUserTokenBase
|
||||
{
|
||||
ctx.DeactivateTokens(userToken.UserId);
|
||||
|
||||
userToken.IsActive = true;
|
||||
ctx.UserTokens.Add(userToken);
|
||||
|
||||
return userToken;
|
||||
}
|
||||
|
||||
public static TUserToken? GetActiveUserToken<TUserToken>(this IUserTokenDbSet<TUserToken> ctx, Guid userId) where TUserToken : class, IUserTokenBase
|
||||
=> ctx.UserTokens.SingleOrDefault(x => x.UserId == userId && x.IsActive && (x.TokenExpiration == null || x.TokenExpiration > DateTime.UtcNow));
|
||||
|
||||
public static TUserToken? GetUserToken<TUserToken>(this IUserTokenDbSet<TUserToken> ctx, Guid userId, string verificationToken) where TUserToken : class?, IUserTokenBase?
|
||||
=> ctx.UserTokens.SingleOrDefault(x => x.UserId == userId && x.Token == verificationToken);
|
||||
|
||||
public static bool IsValidToken<TUserToken>(this IUserTokenDbSet<TUserToken> ctx, Guid userId, string verificationToken) where TUserToken : class, IUserTokenBase
|
||||
=> ctx.UserTokens.Any(x => x.UserId == userId && x.IsActive && x.Token == verificationToken && x.TokenExpiration > DateTime.UtcNow && x.TokenSent < DateTime.UtcNow);
|
||||
|
||||
public static void DeactivateTokens<TUserToken>(this IUserTokenDbSet<TUserToken> ctx, Guid userId) where TUserToken : class, IUserTokenBase
|
||||
{
|
||||
var activeUserTokens = ctx.UserTokens.Where(x => x.UserId == userId && x.IsActive);
|
||||
|
||||
foreach (var activeUserToken in activeUserTokens)
|
||||
activeUserToken.IsActive = false;
|
||||
|
||||
ctx.UserTokens.UpdateRange(activeUserTokens);
|
||||
}
|
||||
|
||||
////public static void GetUserDetails(this IUserDal userDal, Guid userId, Action<AspNetUser> callback)
|
||||
//// => callback(userDal.Get<AspNetUser>(userId));
|
||||
|
|
|
|||
|
|
@ -5,23 +5,23 @@ using AyCode.Interfaces.TimeStampInfo;
|
|||
|
||||
namespace AyCode.Database.DbContexts;
|
||||
|
||||
public class DbContextBase : DbContext
|
||||
public class AcDbContextBase : DbContext
|
||||
{
|
||||
public string Name { get; set; }
|
||||
|
||||
public Guid SessionId { get; protected set; } = Guid.NewGuid();
|
||||
|
||||
public DbContextBase()
|
||||
public AcDbContextBase()
|
||||
{
|
||||
//DbInterception.Add(new UtcDateTimeDbCommandInterceptor());
|
||||
}
|
||||
|
||||
public DbContextBase(string name) : this()
|
||||
public AcDbContextBase(string name) : this()
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public DbContextBase(DbContextOptions<DbContext> options, string name) : base(options)
|
||||
public AcDbContextBase(DbContextOptions<DbContext> options, string name) : base(options)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
|
|
@ -8,7 +8,7 @@ using Microsoft.EntityFrameworkCore;
|
|||
|
||||
namespace AyCode.Database.DbContexts.Users
|
||||
{
|
||||
public abstract class UserDbContextBase<TUser, TUserToken> : DbContextBase, IUserDbContextBase<TUser, TUserToken>
|
||||
public abstract class AcUserDbContextBase<TUser, TUserToken> : AcDbContextBase, IAcUserDbContextBase<TUser, TUserToken>
|
||||
where TUser : class, IUserBase
|
||||
where TUserToken : class, IUserTokenBase
|
||||
{
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
using AyCode.Database.DbSets.Users;
|
||||
using AyCode.Interfaces.Users;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace AyCode.Database.DbContexts.Users;
|
||||
|
||||
public interface IAcUserDbContextBase<TUser, TUserToken> : IAcUserDbSet<TUser>, IAcUserTokenDbSet<TUserToken> where TUser : class, IUserBase where TUserToken : class, IUserTokenBase
|
||||
{
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore;
|
|||
|
||||
namespace AyCode.Database.DbContexts.Users;
|
||||
|
||||
public interface IUserTokenDbContextBase<TUserToken> where TUserToken : class, IUserTokenBase
|
||||
public interface IAcUserTokenDbContextBase<TUserToken> where TUserToken : class, IUserTokenBase
|
||||
{
|
||||
DbSet<TUserToken> UserToken { get; set; }
|
||||
}
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
using AyCode.Database.DbSets.Users;
|
||||
using AyCode.Interfaces.Users;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace AyCode.Database.DbContexts.Users;
|
||||
|
||||
public interface IUserDbContextBase<TUser, TUserToken> : IUserDbSet<TUser>, IUserTokenDbSet<TUserToken> where TUser : class, IUserBase where TUserToken : class, IUserTokenBase
|
||||
{
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
using AyCode.Interfaces.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace AyCode.Database.DbSets.Base;
|
||||
|
||||
public interface IDbSetGuidBase<TEntity> where TEntity : class, IEntityGuid
|
||||
{
|
||||
DbSet<TEntity> Users { get; set; }
|
||||
}
|
||||
|
||||
public interface IDbSetIntBase<TEntity> where TEntity : class, IEntityInt
|
||||
{
|
||||
DbSet<TEntity> Users { get; set; }
|
||||
}
|
||||
|
||||
public interface IDbSetBase<TEntity, TPkey> where TEntity : class, IEntity<TPkey>
|
||||
{
|
||||
DbSet<TEntity> Users { get; set; }
|
||||
}
|
||||
|
||||
public interface IDbSetBase<TEntity> where TEntity : class, IEntity
|
||||
{
|
||||
DbSet<TEntity> Users { get; set; }
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
using AyCode.Interfaces.Users;
|
||||
|
||||
namespace AyCode.Database.DbSets.Users;
|
||||
|
||||
public static class AcUserDbSetExtensions
|
||||
{
|
||||
public static TUser? GetUserById<TUser>(this IAcUserDbSet<TUser> ctx, Guid userId) where TUser : class, IUserBase
|
||||
{
|
||||
return ctx.Users.FirstOrDefault(u => u.Id == userId);
|
||||
}
|
||||
|
||||
public static TUser? GetUserByEmail<TUser>(this IAcUserDbSet<TUser> ctx, string email) where TUser : class, IUserBase
|
||||
{
|
||||
var emailLower = email.ToLower();
|
||||
return ctx.Users.FirstOrDefault(u => u.EmailAddress == emailLower);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
using AyCode.Database.DbContexts.Users;
|
||||
using AyCode.Interfaces.Users;
|
||||
|
||||
namespace AyCode.Database.DbSets.Users;
|
||||
|
||||
public static class AcUserTokenDbSetExtensions
|
||||
{
|
||||
public static bool ChangePassword<TUser, TUserToken>(this IAcUserDbContextBase<TUser, TUserToken> ctx, TUser? user, string passwordHash, string verificationToken)
|
||||
where TUser : class, IUserBase
|
||||
where TUserToken : class, IUserTokenBase
|
||||
{
|
||||
if (user == null || !ctx.IsValidToken(user.Id, verificationToken)) return false;
|
||||
|
||||
user.Password = passwordHash;
|
||||
ctx.Users.Update(user);
|
||||
|
||||
ctx.DeactivateTokens(user.Id);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static TUserToken? UpdateUserTokenSent<TUserToken>(this IAcUserTokenDbSet<TUserToken> ctx, Guid userId, string verificationToken, DateTime tokenSentUtc) where TUserToken : class, IUserTokenBase
|
||||
{
|
||||
var userToken = ctx.GetUserToken(userId, verificationToken);
|
||||
|
||||
if (userToken == null) return null;
|
||||
|
||||
userToken.TokenSent = tokenSentUtc;
|
||||
userToken.TokenExpiration ??= tokenSentUtc.AddDays(1);
|
||||
|
||||
var utcNow = DateTime.UtcNow;
|
||||
userToken.IsActive = tokenSentUtc <= utcNow && userToken.TokenExpiration > utcNow;
|
||||
|
||||
ctx.UserTokens.Update(userToken);
|
||||
return userToken;
|
||||
}
|
||||
|
||||
public static TUserToken CreateUserToken<TUserToken>(this IAcUserTokenDbSet<TUserToken> ctx, Guid userId, string verificationToken) where TUserToken : class, IUserTokenBase
|
||||
{
|
||||
var userToken = Activator.CreateInstance<TUserToken>();
|
||||
userToken.UserId = userId;
|
||||
userToken.Token = verificationToken;
|
||||
|
||||
return ctx.CreateUserToken(userToken);
|
||||
}
|
||||
|
||||
public static TUserToken CreateUserToken<TUserToken>(this IAcUserTokenDbSet<TUserToken> ctx, TUserToken userToken) where TUserToken : class, IUserTokenBase
|
||||
{
|
||||
ctx.DeactivateTokens(userToken.UserId);
|
||||
|
||||
userToken.IsActive = true;
|
||||
ctx.UserTokens.Add(userToken);
|
||||
|
||||
return userToken;
|
||||
}
|
||||
|
||||
public static TUserToken? GetActiveUserToken<TUserToken>(this IAcUserTokenDbSet<TUserToken> ctx, Guid userId) where TUserToken : class, IUserTokenBase
|
||||
=> ctx.UserTokens.SingleOrDefault(x => x.UserId == userId && x.IsActive && (x.TokenExpiration == null || x.TokenExpiration > DateTime.UtcNow));
|
||||
|
||||
public static TUserToken? GetUserToken<TUserToken>(this IAcUserTokenDbSet<TUserToken> ctx, Guid userId, string verificationToken) where TUserToken : class?, IUserTokenBase?
|
||||
=> ctx.UserTokens.SingleOrDefault(x => x.UserId == userId && x.Token == verificationToken);
|
||||
|
||||
public static bool IsValidToken<TUserToken>(this IAcUserTokenDbSet<TUserToken> ctx, Guid userId, string verificationToken) where TUserToken : class, IUserTokenBase
|
||||
=> ctx.UserTokens.Any(x => x.UserId == userId && x.IsActive && x.Token == verificationToken && x.TokenExpiration > DateTime.UtcNow && x.TokenSent < DateTime.UtcNow);
|
||||
|
||||
public static void DeactivateTokens<TUserToken>(this IAcUserTokenDbSet<TUserToken> ctx, Guid userId) where TUserToken : class, IUserTokenBase
|
||||
{
|
||||
var activeUserTokens = ctx.UserTokens.Where(x => x.UserId == userId && x.IsActive);
|
||||
|
||||
foreach (var activeUserToken in activeUserTokens)
|
||||
activeUserToken.IsActive = false;
|
||||
|
||||
ctx.UserTokens.UpdateRange(activeUserTokens);
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore;
|
|||
|
||||
namespace AyCode.Database.DbSets.Users;
|
||||
|
||||
public interface IUserDbSet<TUser> where TUser : class, IUserBase
|
||||
public interface IAcUserDbSet<TUser> where TUser : class, IUserBase
|
||||
{
|
||||
DbSet<TUser> Users { get; set; }
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore;
|
|||
|
||||
namespace AyCode.Database.DbSets.Users;
|
||||
|
||||
public interface IUserTokenDbSet<TUserToken> where TUserToken : class, IUserTokenBase
|
||||
public interface IAcUserTokenDbSet<TUserToken> where TUserToken : class, IUserTokenBase
|
||||
{
|
||||
DbSet<TUserToken> UserTokens { get; set; }
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
using AyCode.Core.Helpers;
|
||||
using AyCode.Database.DataLayers;
|
||||
using AyCode.Database.DbContexts;
|
||||
using AyCode.Interfaces.Entities;
|
||||
|
||||
namespace AyCode.Database.Extensions;
|
||||
|
||||
public static class AcDalExtension
|
||||
{
|
||||
public static Task<TResultType> SessionAsync<TDbContext, TResultType>(this IAcDalBase<TDbContext> acDal, Func<TDbContext, TResultType> callback) where TDbContext : AcDbContextBase
|
||||
=> TaskHelper.ToThreadPoolTask(() => acDal.Session(callback));
|
||||
|
||||
public static Task<IEnumerable<TEntity>> SessionAsync<TDbContext, TEntity>(this IAcDalBase<TDbContext> acDal, Func<TDbContext, IQueryable<TEntity>> callback) where TEntity : IEntity where TDbContext : AcDbContextBase
|
||||
=> TaskHelper.ToThreadPoolTask(() => acDal.Session(callback));
|
||||
|
||||
public static TResultType Session<TDbContext, TResultType>(this IAcDalBase<TDbContext> acDal, Func<TDbContext, TResultType> callback) where TDbContext : AcDbContextBase
|
||||
{
|
||||
using var ctx = acDal.CreateDbContext();
|
||||
|
||||
return ctx.Session(callback);
|
||||
}
|
||||
|
||||
public static IEnumerable<TEntity> Session<TDbContext, TEntity>(this IAcDalBase<TDbContext> acDal, Func<TDbContext, IQueryable<TEntity>> callback) where TEntity : IEntity where TDbContext : AcDbContextBase
|
||||
{
|
||||
using var ctx = acDal.CreateDbContext();
|
||||
|
||||
return ctx.Session(callback);
|
||||
}
|
||||
|
||||
public static Task<bool> TransactionAsync<TDbContext>(this IAcDalBase<TDbContext> acDal, Func<TDbContext, bool> callbackTransactionBody, bool throwException = false) where TDbContext : AcDbContextBase
|
||||
=> TaskHelper.ToThreadPoolTask(() => acDal.Transaction(callbackTransactionBody, throwException));
|
||||
|
||||
public static bool Transaction<TDbContext>(this IAcDalBase<TDbContext> acDal, Func<TDbContext, bool> callbackTransactionBody, bool throwException = false) where TDbContext : AcDbContextBase
|
||||
{
|
||||
using var ctx = acDal.CreateDbContext();
|
||||
|
||||
return ctx.Transaction(callbackTransactionBody, throwException);
|
||||
}
|
||||
}
|
||||
|
|
@ -13,7 +13,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
|||
|
||||
namespace AyCode.Database.Extensions
|
||||
{
|
||||
public static class DbContextExtension
|
||||
public static class AcDbContextExtension
|
||||
{
|
||||
|
||||
public static IEntityType? GetEntityType<TEntity>(this DbContext ctx)
|
||||
|
|
@ -6,12 +6,12 @@ using AyCode.Interfaces.Entities;
|
|||
|
||||
namespace AyCode.Database.Extensions;
|
||||
|
||||
public static class DbSessionExtension
|
||||
public static class AcDbSessionExtension
|
||||
{
|
||||
public static Task<TResultType> SessionAsync<TDbContext, TResultType>(this TDbContext ctx, Func<TDbContext, TResultType> callback) where TDbContext : DbContextBase
|
||||
public static Task<TResultType> SessionAsync<TDbContext, TResultType>(this TDbContext ctx, Func<TDbContext, TResultType> callback) where TDbContext : AcDbContextBase
|
||||
=> TaskHelper.ToThreadPoolTask(() => ctx.Session(callback));
|
||||
|
||||
public static TResultType Session<TDbContext, TResultType>(this TDbContext ctx, Func<TDbContext, TResultType> callback) where TDbContext : DbContextBase
|
||||
public static TResultType Session<TDbContext, TResultType>(this TDbContext ctx, Func<TDbContext, TResultType> callback) where TDbContext : AcDbContextBase
|
||||
{
|
||||
TResultType result;
|
||||
|
||||
|
|
@ -33,7 +33,7 @@ public static class DbSessionExtension
|
|||
|
||||
public static IEnumerable<TEntity> Session<TDbContext, TEntity>(this TDbContext ctx, Func<TDbContext, IQueryable<TEntity>> callback)
|
||||
where TEntity : IEntity
|
||||
where TDbContext : DbContextBase
|
||||
where TDbContext : AcDbContextBase
|
||||
{
|
||||
foreach (var entity in callback(ctx))
|
||||
yield return entity;
|
||||
|
|
@ -8,13 +8,13 @@ using AyCode.Core.Consts;
|
|||
|
||||
namespace AyCode.Database.Extensions;
|
||||
|
||||
public static class DbTransactionExtension
|
||||
public static class AcDbTransactionExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// BeginTransaction
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private static IDbContextTransaction OpenTransaction<TDbContext>(this TDbContext ctx) where TDbContext : DbContextBase
|
||||
private static IDbContextTransaction OpenTransaction<TDbContext>(this TDbContext ctx) where TDbContext : AcDbContextBase
|
||||
{
|
||||
ctx.Database.AutoTransactionBehavior = AutoTransactionBehavior.Never;
|
||||
|
||||
|
|
@ -25,7 +25,7 @@ public static class DbTransactionExtension
|
|||
///
|
||||
/// </summary>
|
||||
/// <returns>True, ha sikeres volt a Commit.</returns>
|
||||
private static bool CommitTransaction<TDbContext>(this TDbContext ctx, bool throwException = false) where TDbContext : DbContextBase
|
||||
private static bool CommitTransaction<TDbContext>(this TDbContext ctx, bool throwException = false) where TDbContext : AcDbContextBase
|
||||
{
|
||||
var result = false;
|
||||
var transaction = ctx.Database?.CurrentTransaction;
|
||||
|
|
@ -51,10 +51,10 @@ public static class DbTransactionExtension
|
|||
return result;
|
||||
}
|
||||
|
||||
public static Task<bool> TransactionAsync<TDbContext>(this TDbContext ctx, Func<TDbContext, bool> callbackTransactionBody, bool throwException = false) where TDbContext : DbContextBase
|
||||
public static Task<bool> TransactionAsync<TDbContext>(this TDbContext ctx, Func<TDbContext, bool> callbackTransactionBody, bool throwException = false) where TDbContext : AcDbContextBase
|
||||
=> TaskHelper.ToThreadPoolTask(() => ctx.Transaction(callbackTransactionBody, throwException));
|
||||
|
||||
public static bool Transaction<TDbContext>(this TDbContext ctx, Func<TDbContext, bool> callbackTransactionBody, bool throwException = false) where TDbContext : DbContextBase
|
||||
public static bool Transaction<TDbContext>(this TDbContext ctx, Func<TDbContext, bool> callbackTransactionBody, bool throwException = false) where TDbContext : AcDbContextBase
|
||||
{
|
||||
var result = false;
|
||||
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
using AyCode.Core.Helpers;
|
||||
using AyCode.Database.DataLayers;
|
||||
using AyCode.Database.DbContexts;
|
||||
using AyCode.Interfaces.Entities;
|
||||
|
||||
namespace AyCode.Database.Extensions;
|
||||
|
||||
public static class DalExtension
|
||||
{
|
||||
public static Task<TResultType> SessionAsync<TDbContext, TResultType>(this IDalBase<TDbContext> dal, Func<TDbContext, TResultType> callback) where TDbContext : DbContextBase
|
||||
=> TaskHelper.ToThreadPoolTask(() => dal.Session(callback));
|
||||
|
||||
public static Task<IEnumerable<TEntity>> SessionAsync<TDbContext, TEntity>(this IDalBase<TDbContext> dal, Func<TDbContext, IQueryable<TEntity>> callback) where TEntity : IEntity where TDbContext : DbContextBase
|
||||
=> TaskHelper.ToThreadPoolTask(() => dal.Session(callback));
|
||||
|
||||
public static TResultType Session<TDbContext, TResultType>(this IDalBase<TDbContext> dal, Func<TDbContext, TResultType> callback) where TDbContext : DbContextBase
|
||||
{
|
||||
using var ctx = dal.CreateDbContext();
|
||||
|
||||
return ctx.Session(callback);
|
||||
}
|
||||
|
||||
public static IEnumerable<TEntity> Session<TDbContext, TEntity>(this IDalBase<TDbContext> dal, Func<TDbContext, IQueryable<TEntity>> callback) where TEntity : IEntity where TDbContext : DbContextBase
|
||||
{
|
||||
using var ctx = dal.CreateDbContext();
|
||||
|
||||
return ctx.Session(callback);
|
||||
}
|
||||
|
||||
public static Task<bool> TransactionAsync<TDbContext>(this IDalBase<TDbContext> dal, Func<TDbContext, bool> callbackTransactionBody, bool throwException = false) where TDbContext : DbContextBase
|
||||
=> TaskHelper.ToThreadPoolTask(() => dal.Transaction(callbackTransactionBody, throwException));
|
||||
|
||||
public static bool Transaction<TDbContext>(this IDalBase<TDbContext> dal, Func<TDbContext, bool> callbackTransactionBody, bool throwException = false) where TDbContext : DbContextBase
|
||||
{
|
||||
using var ctx = dal.CreateDbContext();
|
||||
|
||||
return ctx.Transaction(callbackTransactionBody, throwException);
|
||||
}
|
||||
}
|
||||
|
|
@ -10,10 +10,11 @@ using System.Threading.Tasks;
|
|||
namespace AyCode.Entities.Profiles
|
||||
{
|
||||
[Table("ProfileBase")]
|
||||
public class ProfileBase : IProfileBase
|
||||
public abstract class ProfileBase : IProfileBase
|
||||
{
|
||||
public ProfileBase() { }
|
||||
public ProfileBase(Guid ownerId) : this()
|
||||
protected ProfileBase() { }
|
||||
|
||||
protected ProfileBase(Guid ownerId) : this()
|
||||
{
|
||||
OwnerId = ownerId;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ using AyCode.Interfaces.Users;
|
|||
namespace AyCode.Entities.Users
|
||||
{
|
||||
[Table("Users")]
|
||||
public class UserBase : IUserBase
|
||||
public abstract class UserBase : IUserBase
|
||||
{
|
||||
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
|
||||
public Guid Id { get; set; }
|
||||
|
|
@ -27,24 +27,27 @@ namespace AyCode.Entities.Users
|
|||
public DateTime Modified { get; set; }
|
||||
|
||||
|
||||
public UserBase() { }
|
||||
protected UserBase() { }
|
||||
|
||||
public UserBase(string email, string password) : this(Guid.NewGuid(), email, password) { }
|
||||
public UserBase(Guid id, string email, string password) : this()
|
||||
protected UserBase(string email, string password) : this(Guid.NewGuid(), email, password) { }
|
||||
|
||||
protected UserBase(Guid id, string email, string password) : this()
|
||||
{
|
||||
Id = id;
|
||||
EmailAddress = email;
|
||||
Password = password;
|
||||
}
|
||||
|
||||
public UserBase(string email, string phoneNumber, string password) : this(Guid.NewGuid(), email, phoneNumber, password) { }
|
||||
public UserBase(Guid id, string email, string phoneNumber, string password) : this(id, email, password)
|
||||
protected UserBase(string email, string phoneNumber, string password) : this(Guid.NewGuid(), email, phoneNumber, password) { }
|
||||
|
||||
protected UserBase(Guid id, string email, string phoneNumber, string password) : this(id, email, password)
|
||||
{
|
||||
PhoneNumber = phoneNumber;
|
||||
}
|
||||
|
||||
public UserBase(string email, string phoneNumber, string password, string refreshToken) : this(Guid.NewGuid(), email, phoneNumber, password, refreshToken) { }
|
||||
public UserBase(Guid id, string email, string phoneNumber, string password, string refreshToken) : this(id, email, phoneNumber, password)
|
||||
protected UserBase(string email, string phoneNumber, string password, string refreshToken) : this(Guid.NewGuid(), email, phoneNumber, password, refreshToken) { }
|
||||
|
||||
protected UserBase(Guid id, string email, string phoneNumber, string password, string refreshToken) : this(id, email, phoneNumber, password)
|
||||
{
|
||||
RefreshToken = refreshToken;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ using System.ComponentModel.DataAnnotations;
|
|||
namespace AyCode.Entities.Users;
|
||||
|
||||
[Table("UserToken")]
|
||||
public class UserTokenBase : IUserTokenBase
|
||||
public abstract class UserTokenBase : IUserTokenBase
|
||||
{
|
||||
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public int Id { get; set; }
|
||||
|
|
|
|||
|
|
@ -8,9 +8,9 @@ namespace AyCode.Interfaces.Entities
|
|||
{
|
||||
}
|
||||
|
||||
public interface IEntity<T> : IEntity
|
||||
public interface IEntity<TPKey> : IEntity
|
||||
{
|
||||
[Key] T Id { get; set; }
|
||||
[Key] TPKey Id { get; set; }
|
||||
//T SetId(T id);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue