Sprint 5 (#28)

This commit was merged in pull request #28.
This commit is contained in:
Caterina Simona Pastore
2025-05-28 00:10:38 +02:00
committed by GitHub
parent ac20664446
commit 79549bea05
53 changed files with 5781 additions and 63 deletions

View File

@@ -0,0 +1,32 @@
using System;
using System.Security.Cryptography;
using System.Text;
using BasicDotnetTemplate.MainProject.Core.Database;
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
namespace BasicDotnetTemplate.MainProject.Utils;
public class CommonDbMethodsUtils
{
private readonly SqlServerContext _sqlServerContext;
public CommonDbMethodsUtils(SqlServerContext sqlServerContext)
{
_sqlServerContext = sqlServerContext;
}
public IQueryable<Role> GetRolesQueryable()
{
return this._sqlServerContext.Roles.Where(x => !x.IsDeleted);
}
public IQueryable<Role> GetRoleByNameQueryable(string name)
{
return this.GetRolesQueryable().Where(x =>
x.Name.ToString() == name.ToString()
);
}
}

View File

@@ -0,0 +1,42 @@
using System.Text.Json;
using NLog;
namespace BasicDotnetTemplate.MainProject.Utils;
public static class FileUtils
{
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
private static readonly JsonSerializerOptions jsonSerializerOptions = new()
{
PropertyNameCaseInsensitive = true
};
public static T? ConvertFileToObject<T>(string? filePath = "")
{
Logger.Info("[FileUtils][ReadJson] Reading file");
if (string.IsNullOrWhiteSpace(filePath))
{
throw new ArgumentException("filePath cannot be null or empty", nameof(filePath));
}
if (!File.Exists(filePath))
{
throw new FileNotFoundException("The specified file does not exists", filePath);
}
try
{
string fileContent = File.ReadAllText(filePath);
return JsonSerializer.Deserialize<T>(fileContent, jsonSerializerOptions);
}
catch (JsonException ex)
{
throw new InvalidOperationException("Error during file deserialization", ex);
}
}
}

View File

@@ -218,6 +218,7 @@ public static class ProgramUtils
builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped<IAuthService, AuthService>();
builder.Services.AddScoped<IJwtService, JwtService>();
builder.Services.AddScoped<IPermissionService, PermissionService>();
builder.Services.AddScoped<IRoleService, RoleService>();
builder.Services.AddScoped<IUserService, UserService>();
Logger.Info("[ProgramUtils][AddScopes] Done scopes");
@@ -271,4 +272,24 @@ public static class ProgramUtils
}
public static void CreatePermissions(ref WebApplication app)
{
Logger.Info("[ProgramUtils][CreatePermissions] Adding permissions...");
using var scope = app.Services.CreateScope();
Func<IPermissionService?> permissionService = scope.ServiceProvider.GetRequiredService<IPermissionService>;
var isValidThread = Task.Run(() => permissionService!.Invoke()?.CreatePermissionsOnStartupAsync());
if (isValidThread.Result != null)
{
foreach (var result in isValidThread.Result)
{
var currentResult = String.IsNullOrEmpty(result) ? "No permission tracked" : result;
Logger.Info($"[ProgramUtils][CreatePermissions] => {currentResult}");
}
}
else
{
Logger.Error("[ProgramUtils][CreatePermissions] Something went wrong");
}
}
}