diff --git a/Database/Repositories/GenericRepository.cs b/Database/Repositories/GenericRepository.cs index 30a8c94..1e94db0 100755 --- a/Database/Repositories/GenericRepository.cs +++ b/Database/Repositories/GenericRepository.cs @@ -1,6 +1,5 @@ using System.Linq.Expressions; using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Internal; namespace GamificationService.Database.Repositories; @@ -16,8 +15,8 @@ public class GenericRepository where TEntity : class } public virtual IQueryable Get( - Expression> filter = null, - Func, IOrderedQueryable> orderBy = null, + Expression>? filter = null, + Func, IOrderedQueryable>? orderBy = null, string includeProperties = "") { IQueryable query = dbSet; diff --git a/Database/Repositories/UnitOfWork.cs b/Database/Repositories/UnitOfWork.cs index 6232423..9629621 100755 --- a/Database/Repositories/UnitOfWork.cs +++ b/Database/Repositories/UnitOfWork.cs @@ -4,76 +4,69 @@ namespace GamificationService.Database.Repositories; public class UnitOfWork : IDisposable { - #region fields - - private ApplicationContext _context; - - #endregion - - - private IDbContextTransaction _transaction; - - public UnitOfWork(ApplicationContext context) - { - _context = context; - } +#region fields + protected ApplicationContext _context; + private IDbContextTransaction? _transaction; - #region Properties + #endregion - #endregion + public UnitOfWork(ApplicationContext context) + { + _context = context; + } - public bool Save() - { - return _context.SaveChanges() > 0; - } + public bool Save() + { + return _context.SaveChanges() > 0; + } - public async Task SaveAsync() - { - return await _context.SaveChangesAsync() > 0; - } + public async Task SaveAsync() + { + return await _context.SaveChangesAsync() > 0; + } - - private bool disposed = false; + public async Task BeginTransactionAsync() + { + if (_transaction is not null) + throw new InvalidOperationException("A transaction has already been started."); + _transaction = await _context.Database.BeginTransactionAsync(); + } - protected virtual void Dispose(bool disposing) + public async Task CommitAsync() + { + if (_transaction is null) + throw new InvalidOperationException("A transaction has not been started."); + + try { - if (!this.disposed) - { - if (disposing) - { - _context.Dispose(); - } - } - this.disposed = true; + await _transaction.CommitAsync(); } - public async Task BeginTransactionAsync() + finally { - if (_transaction is not null) - throw new InvalidOperationException("A transaction has already been started."); - _transaction = await _context.Database.BeginTransactionAsync(); + await _transaction.DisposeAsync(); + _transaction = null; } - public async Task CommitAsync() + } + + private bool disposed = false; + + protected virtual void Dispose(bool disposing) + { + if (!disposed) { - if (_transaction is null) - throw new InvalidOperationException("A transaction has not been started."); - - try - { - await _transaction.CommitAsync(); - _transaction.Dispose(); - _transaction = null; - } - catch (Exception) - { - if (_transaction is not null) - await _transaction.RollbackAsync(); - throw; - } - } - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); + if (disposing) + { + _transaction?.Dispose(); // Dispose transaction if it exists + _context.Dispose(); + } + disposed = true; } + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } } diff --git a/Exceptions/UtilServices/Cookies/CookiesException.cs b/Exceptions/UtilServices/Cookies/CookiesException.cs deleted file mode 100755 index 84bce9f..0000000 --- a/Exceptions/UtilServices/Cookies/CookiesException.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace GamificationService.Exceptions.UtilServices.Cookies; - -/// -/// Represents an exception related to cookie operations. -/// -public class CookiesException : Exception -{ - /// - /// Initializes a new instance of the class. - /// - public CookiesException() : base() { } - - /// - /// Initializes a new instance of the class with a specified error message. - /// - /// The message that describes the error. - public CookiesException(string message) : base(message) { } - - /// - /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. - /// - /// The error message that explains the reason for the exception. - /// The exception that is the cause of the current exception, or a null reference if no inner exception is specified. - public CookiesException(string message, Exception innerException) : base(message, innerException) { } - -} diff --git a/Exceptions/UtilServices/Cookies/DeleteCookiesException.cs b/Exceptions/UtilServices/Cookies/DeleteCookiesException.cs deleted file mode 100755 index f75ee53..0000000 --- a/Exceptions/UtilServices/Cookies/DeleteCookiesException.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace GamificationService.Exceptions.UtilServices.Cookies; - -/// -/// Represents an exception related to deleting cookies. -/// -public class DeleteCookiesException : CookiesException -{ - /// - /// Initializes a new instance of the class. - /// - public DeleteCookiesException() : base() { } - - /// - /// Initializes a new instance of the class with a specified error message. - /// - /// The message that describes the error. - public DeleteCookiesException(string message) : base(message) { } - - /// - /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. - /// - /// The error message that explains the reason for the exception. - /// The exception that is the cause of the current exception, or a null reference if no inner exception is specified. - public DeleteCookiesException(string message, Exception innerException) : base(message, innerException) { } - -} diff --git a/Exceptions/UtilServices/Cookies/SetCookiesException.cs b/Exceptions/UtilServices/Cookies/SetCookiesException.cs deleted file mode 100755 index 6f8f460..0000000 --- a/Exceptions/UtilServices/Cookies/SetCookiesException.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace GamificationService.Exceptions.UtilServices.Cookies; - -/// -/// Represents an exception related to setting cookies. -/// -public class SetCookiesException : CookiesException -{ - /// - /// Initializes a new instance of the class. - /// - public SetCookiesException() : base() { } - - /// - /// Initializes a new instance of the class with a specified error message. - /// - /// The message that describes the error. - public SetCookiesException(string message) : base(message) { } - - /// - /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. - /// - /// The error message that explains the reason for the exception. - /// The exception that is the cause of the current exception, or a null reference if no inner exception is specified. - public SetCookiesException(string message, Exception innerException) : base(message, innerException) { } - -} diff --git a/Exceptions/UtilServices/Email/EmailException.cs b/Exceptions/UtilServices/Email/EmailException.cs deleted file mode 100755 index 8e252a5..0000000 --- a/Exceptions/UtilServices/Email/EmailException.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace GamificationService.Exceptions.UtilServices.Email; - -/// -/// Represents an exception related to email operations. -/// -public class EmailException : Exception -{ - /// - /// Initializes a new instance of the class. - /// - public EmailException() : base() { } - - /// - /// Initializes a new instance of the class with a specified error message. - /// - /// The message that describes the error. - public EmailException(string message) : base(message) { } - - /// - /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. - /// - /// The error message that explains the reason for the exception. - /// The exception that is the cause of the current exception, or a null reference if no inner exception is specified. - public EmailException(string message, Exception innerException) : base(message, innerException) { } -} diff --git a/Exceptions/UtilServices/Email/SendEmailException.cs b/Exceptions/UtilServices/Email/SendEmailException.cs deleted file mode 100755 index bc7a97a..0000000 --- a/Exceptions/UtilServices/Email/SendEmailException.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace GamificationService.Exceptions.UtilServices.Email; - -/// -/// Represents an exception that occurs during the process of sending an email. -/// -public class SendEmailException : EmailException -{ - /// - /// Initializes a new instance of the class. - /// - public SendEmailException() : base("An error occurred while sending the email.") { } - - /// - /// Initializes a new instance of the class with a specified error message. - /// - /// The message that describes the error. - public SendEmailException(string message) : base(message) { } - - /// - /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. - /// - /// The error message that explains the reason for the exception. - /// The exception that is the cause of the current exception, or a null reference if no inner exception is specified. - public SendEmailException(string message, Exception innerException) : base(message, innerException) { } -} diff --git a/Exceptions/UtilServices/JWT/GenerateJWTTokenException.cs b/Exceptions/UtilServices/JWT/GenerateJWTTokenException.cs deleted file mode 100755 index 3d0e380..0000000 --- a/Exceptions/UtilServices/JWT/GenerateJWTTokenException.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace GamificationService.Exceptions.UtilServices.JWT; - -/// -/// Represents an exception that occurs while generating a JWT token. -/// -public class GenerateJWTTokenException : JWTException -{ - /// - /// Initializes a new instance of the class. - /// - public GenerateJWTTokenException() : base() { } - - /// - /// Initializes a new instance of the class with a specified error message. - /// - /// The message that describes the error. - public GenerateJWTTokenException(string message) : base(message) { } - - /// - /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. - /// - /// The error message that explains the reason for the exception. - /// The exception that is the cause of the current exception, or a null reference if no inner exception is specified. - public GenerateJWTTokenException(string message, Exception innerException) : base(message, innerException) { } -} diff --git a/Exceptions/UtilServices/JWT/JWTException.cs b/Exceptions/UtilServices/JWT/JWTException.cs deleted file mode 100755 index 671f60b..0000000 --- a/Exceptions/UtilServices/JWT/JWTException.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace GamificationService.Exceptions.UtilServices.JWT; - -/// -/// Represents an exception related to JWT (JSON Web Token) operations. -/// -public class JWTException : Exception -{ - /// - /// Initializes a new instance of the class. - /// - public JWTException() : base() { } - - /// - /// Initializes a new instance of the class with a specified error message. - /// - /// The message that describes the error. - public JWTException(string message) : base(message) { } - - /// - /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. - /// - /// The error message that explains the reason for the exception. - /// The exception that is the cause of the current exception, or a null reference if no inner exception is specified. - public JWTException(string message, Exception innerException) : base(message, innerException) { } -} diff --git a/Extensions/DependencyInjectionExtensions.cs b/Extensions/DependencyInjectionExtensions.cs index 343aa64..6ef3c1c 100755 --- a/Extensions/DependencyInjectionExtensions.cs +++ b/Extensions/DependencyInjectionExtensions.cs @@ -1,10 +1,8 @@ -using System.Net; -using System.Net.Mail; using System.Reflection; using System.Text; using GamificationService.Database; using GamificationService.Database.Repositories; -using GamificationService.Logs; +using GamificationService.Loggging; using GamificationService.Mapper; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.EntityFrameworkCore; @@ -69,7 +67,7 @@ public static class SwaggerExtensions { public static IServiceCollection AddSwagger(this IServiceCollection services) { - string projectName = Assembly.GetExecutingAssembly().GetName().Name; + string projectName = Assembly.GetExecutingAssembly().GetName().Name!; services.AddOpenApi(); services.AddEndpointsApiExplorer(); services.AddSwaggerGen(c => diff --git a/Logging/LoggingConfigurator.cs b/Logging/LoggingConfigurator.cs new file mode 100755 index 0000000..4da4ff2 --- /dev/null +++ b/Logging/LoggingConfigurator.cs @@ -0,0 +1,24 @@ +using System.Reflection; +using Serilog; +using Serilog.Exceptions; + +namespace GamificationService.Loggging; + +public static class LoggingConfigurator +{ + public static void ConfigureLogging(){ + var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"; + var configuration = new ConfigurationBuilder() + .AddJsonFile("appsettings.json",optional:false,reloadOnChange:true).Build(); + Console.WriteLine(environment); + Console.WriteLine(configuration); + Log.Logger = new LoggerConfiguration() + .Enrich.FromLogContext() + .Enrich.WithExceptionDetails() + .WriteTo.Debug() + .WriteTo.Console() + .Enrich.WithProperty("Environment",environment) + .ReadFrom.Configuration(configuration) + .CreateLogger(); + } +} diff --git a/Models/BasicResponses/BasicResponse.cs b/Models/BasicResponses/BasicResponse.cs index b8f20b5..8e2e7d6 100755 --- a/Models/BasicResponses/BasicResponse.cs +++ b/Models/BasicResponses/BasicResponse.cs @@ -3,5 +3,5 @@ namespace GamificationService.Models.BasicResponses; public class BasicResponse { public short Code { get; set; } - public string Message { get; set; } + public string Message { get; set; } = ""; }