refactoring, improvements...

This commit is contained in:
jozsef.b@aycode.com 2024-04-24 06:50:45 +02:00
parent 2cbc51b143
commit 7360291242
12 changed files with 125 additions and 17 deletions

View File

@ -10,6 +10,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.3.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.3.1" />

View File

@ -1,9 +1,24 @@
namespace AyCode.Core.Tests;
using Microsoft.Extensions.Configuration;
namespace AyCode.Core.Tests;
public abstract class TestModelBase
{
protected IConfiguration AppSettingsConfiguration;
protected TestModelBase()
{
//if (IsProductVersion) throw new Exception("IsProductVersion!!!!!");
AppSettingsConfiguration = InitAppSettingsConfiguration();
}
protected IConfiguration InitAppSettingsConfiguration(string appSettingsFileName = "appsettings.json")
{
var config = new ConfigurationBuilder()
.AddJsonFile(appSettingsFileName)
.AddEnvironmentVariables()
.Build();
return config;
}
}

View File

@ -6,18 +6,10 @@ namespace AyCode.Database.Tests;
public abstract class AcDatabaseTestModelBase<TDal, TDbContext> : TestModelBase where TDal : IAcDalBase<TDbContext> where TDbContext : AcDbContextBase
{
protected TDal Dal;
protected AcDatabaseTestModelBase()
{
Dal = PooledDal.CreateDal<TDal>();
}
protected readonly TDal Dal = PooledDal.CreateDal<TDal>();
}
public abstract class AcDatabaseTestModelBase<TDbContext> : TestModelBase where TDbContext : AcDbContextBase
{
protected TDbContext Context;
protected AcDatabaseTestModelBase()
{
Context = Activator.CreateInstance<TDbContext>();
}
protected readonly TDbContext Context = Activator.CreateInstance<TDbContext>();
}

View File

@ -10,6 +10,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.3.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.3.1" />

View File

@ -75,8 +75,29 @@ namespace AyCode.Database.DataLayers.Users
return profile;
}
public Task<bool> RemoveUserAsync(TUser user) => TransactionAsync(ctx => ctx.RemoveUser(user));
public Task<bool> RemoveUserAsync(Guid userId) => TransactionAsync(ctx => ctx.RemoveUser(userId));
public Task<bool> RemoveUserAsync(TUser? user) => TransactionAsync(ctx => RemoveUserTransactionBody(user, ctx));
public Task<bool> RemoveUserAsync(Guid userId) => TransactionAsync(ctx =>
{
var user = ctx.GetUserById(userId);
return RemoveUserTransactionBody(user, ctx);
});
protected bool RemoveUserTransactionBody(TUser? user, TDbContext ctx)
{
if (user == null) return false;
var profile = ctx.Profiles.FirstOrDefault(x => x.Id == user.ProfileId);
if (profile != null)
{
var address = ctx.Addresses.FirstOrDefault(x => x.Id == profile.AddressId);
if (address != null) ctx.Addresses.Remove(address);
ctx.Profiles.Remove(profile);
}
return ctx.RemoveUser(user);
}
public TUser? AuthenticateUser(string? email, string? password, string refreshToken, out AcErrorCode errorCode)
{

View File

@ -16,6 +16,9 @@ namespace AyCode.Database.DbContexts.Users
{
public required DbSet<TUser> Users { get; set; }
public required DbSet<TUserToken> UserTokens { get; set; }
public DbSet<TProfileAddress> Addresses { get; set; }
public DbSet<TProfile> Profiles { get; set; }
protected AcUserDbContextBase() : this(string.Empty)
{ }

View File

@ -1,4 +1,6 @@
using AyCode.Database.DbSets.Users;
using AyCode.Database.DbSets.Addresses;
using AyCode.Database.DbSets.Profiles;
using AyCode.Database.DbSets.Users;
using AyCode.Interfaces.Addresses;
using AyCode.Interfaces.Profiles;
using AyCode.Interfaces.ServiceProviders;
@ -6,7 +8,9 @@ using AyCode.Interfaces.Users;
namespace AyCode.Database.DbContexts.Users;
public interface IAcUserDbContextBase<TUser, TProfile, TUserToken, TServiceProvider, TUserToServiceProvider, TProfileAddress> : IAcUserDbSet<TUser, TProfile, TServiceProvider, TUserToServiceProvider, TProfileAddress>, IAcUserTokenDbSet<TUserToken>
public interface IAcUserDbContextBase<TUser, TProfile, TUserToken, TServiceProvider, TUserToServiceProvider, TProfileAddress>
: IAcUserDbSet<TUser, TProfile, TServiceProvider, TUserToServiceProvider, TProfileAddress>, IAcUserTokenDbSet<TUserToken>, IAcAddressDbSetBase<TProfileAddress>, IAcProfileDbSetBase<TProfile>
where TUser : class, IAcUser<TProfile, TServiceProvider, TUserToServiceProvider, TProfileAddress>
where TProfile : class, IAcProfile<TProfileAddress>
where TUserToken : class, IAcUserTokenBase

View File

@ -0,0 +1,10 @@
using AyCode.Interfaces.Profiles.Dtos;
using AyCode.Interfaces.Users;
using Microsoft.EntityFrameworkCore;
namespace AyCode.Database.DbSets.Profiles;
public interface IAcProfileDbSetBase<TProfile> where TProfile : class, IAcProfileDtoBase
{
DbSet<TProfile> Profiles { get; set; }
}

View File

@ -14,6 +14,8 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.3.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.3.1" />

View File

@ -26,6 +26,7 @@
<ItemGroup>
<Folder Include="Messages\" />
<Folder Include="Emails\" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,50 @@
using AyCode.Core.Consts;
using Microsoft.Extensions.Configuration;
using SendGrid.Helpers.Mail;
using SendGrid;
namespace AyCode.Services.Server.Emails;
public interface IAcEmailServiceServer
{
}
public class AcEmailServiceServer() : IAcEmailServiceServer
{
private IConfiguration _configuration;
private readonly SendGridClient _sendGridClient;
private readonly EmailAddress _fromEmailAddress;
public AcEmailServiceServer(IConfiguration configuration) : this()
{
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
//_sendGridClient = new SendGridClient(_configuration.ServerUserName);
//_fromEmailAddress = new EmailAddress(_configuration.FromEmail, _configuration.FromName);
//Console.WriteLine($"{config.ServerUserName}; {config.FromEmail}; {config.FromName}");
}
public async Task<Response> SendLostPasswordEmailAsync(string addressEmail, string verificationToken)
{
const string subject = "Lost password";
var toEmailAddress = new EmailAddress(addressEmail);
var plainTextContent = $"{verificationToken}";
var htmlContent = $"<strong>{verificationToken}</strong>";
var msg = MailHelper.CreateSingleEmail(_fromEmailAddress, toEmailAddress, subject, plainTextContent, htmlContent);
return await _sendGridClient.SendEmailAsync(msg).ConfigureAwait(false);
}
public async Task<Response> SendRegistrationEmailAsync(string addressEmail, string verificationToken, string userName)
{
const string subject = "Registration";
var toEmailAddress = new EmailAddress(addressEmail);
var plainTextContent = $"User name: {userName}{AcEnv.NL}Confirmation token: {verificationToken}";
var htmlContent = $"User name: <strong>{userName}</strong></br>Confirmation token: <strong>{verificationToken}</strong>";
var msg = MailHelper.CreateSingleEmail(_fromEmailAddress, toEmailAddress, subject, plainTextContent, htmlContent);
return await _sendGridClient.SendEmailAsync(msg).ConfigureAwait(false);
}
}

View File

@ -108,11 +108,17 @@ public class AcLoginServiceServer<TResultLoggedInModel, TDal, TDbContext, TUser,
=> RegistrationAsync(Guid.NewGuid(), email, password, phoneNumber);
public virtual Task<AcErrorCode> RegistrationAsync(Guid userId, string email, string password, string? phoneNumber = null)
=> TaskHelper.ToThreadPoolTask(() => Registration(userId, email, password, phoneNumber));
public virtual bool SendConfirmationToken(string? email, string confirmationToken)
{
return TaskHelper.ToThreadPoolTask(() => Registration(userId, email, password, phoneNumber));
//var sendGrid = SendGrid.SendGridClient();
return true;
}
//public Task SendConfirmationToken(string email)
public virtual Task<bool> SendConfirmationTokenAsync(string email, string confirmationToken)
=> TaskHelper.ToThreadPoolTask(() => SendConfirmationToken(email, confirmationToken));
public string GenerateAccessToken(TUser user)
{
var tokenHandler = new JwtSecurityTokenHandler();