Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
[*.cs]
# command to run:
# dotnet format --exclude "**/Migrations/**" --verbosity detailed

[*.cs]

#### Core EditorConfig Options ####
charset = utf-8

# Indentation and spacing
indent_size = 4
indent_size = 2
indent_style = space
tab_width = 4
tab_width = 2

# New line preferences
end_of_line = lf
insert_final_newline = false
insert_final_newline = true

# Wrapping preferences
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = true

# error unnecessary usings
dotnet_diagnostic.IDE0005.severity = error

#### .NET Coding Conventions ####

Expand Down
26 changes: 15 additions & 11 deletions Api/Configuration/AuthenticationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
using DataAccess;
using TourmalineCore.AspNetCore.JwtAuthentication.Core;
using TourmalineCore.AspNetCore.JwtAuthentication.Core.Options;
using DataAccess;

namespace Api.Configuration;

public static class AuthenticationExtensions
{
public static void AddAppAuthentication(this WebApplicationBuilder builder)
{
var configuration = builder.Configuration;
var authenticationOptions = configuration.GetSection(nameof(AuthenticationOptions)).Get<AuthenticationOptions>();
builder.Services.Configure<AuthenticationOptions>(configuration.GetSection(nameof(AuthenticationOptions)));
builder.Services.AddJwtAuthentication(authenticationOptions)
.WithUserClaimsProvider<UserClaimsProvider>(UserClaimsProvider.PermissionClaimType);
builder.Services.AddPersistence(configuration);
}
}
public static void AddAppAuthentication(this WebApplicationBuilder builder)
{
var configuration = builder.Configuration;
var authenticationOptions = configuration
.GetSection(nameof(AuthenticationOptions))
.Get<AuthenticationOptions>();

builder.Services.Configure<AuthenticationOptions>(configuration.GetSection(nameof(AuthenticationOptions)));
builder.Services
.AddJwtAuthentication(authenticationOptions)
.WithUserClaimsProvider<UserClaimsProvider>(UserClaimsProvider.PermissionClaimType);
builder.Services.AddPersistence(configuration);
}
}
39 changes: 20 additions & 19 deletions Api/Configuration/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@ namespace Api.Configuration;

public static class ConfigurationExtensions
{
public static ConfigurationManager AddAppConfiguration(this WebApplicationBuilder builder, string[] args)
{
var env = builder.Environment;
var configuration = builder.Configuration;
var reloadOnChange = configuration.GetValue("hostBuilder:reloadConfigOnChange", true);
public static ConfigurationManager AddAppConfiguration(this WebApplicationBuilder builder, string[] args)
{
var env = builder.Environment;
var configuration = builder.Configuration;
var reloadOnChange = configuration.GetValue("hostBuilder:reloadConfigOnChange", true);

configuration.AddJsonFile("appsettings.json", true, reloadOnChange)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true, reloadOnChange)
.AddJsonFile("appsettings.Active.json", true, reloadOnChange)
.AddJsonFile("swagger.json", true, reloadOnChange);
configuration
.AddJsonFile("appsettings.json", true, reloadOnChange)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true, reloadOnChange)
.AddJsonFile("appsettings.Active.json", true, reloadOnChange)
.AddJsonFile("swagger.json", true, reloadOnChange);

if (env.IsDevelopment() && !string.IsNullOrEmpty(env.ApplicationName))
{
var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
configuration.AddUserSecrets(appAssembly, true);
}
if (env.IsDevelopment() && !string.IsNullOrEmpty(env.ApplicationName))
{
var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
configuration.AddUserSecrets(appAssembly, true);
}

configuration.AddEnvironmentVariables();
configuration.AddCommandLine(args);
configuration.AddEnvironmentVariables();
configuration.AddCommandLine(args);

return configuration;
}
}
return configuration;
}
}
54 changes: 27 additions & 27 deletions Api/Configuration/LoggingExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
using Microsoft.Extensions.Logging.EventLog;
using System.Runtime.InteropServices;
using Microsoft.Extensions.Logging.EventLog;

namespace Api.Configuration;

public static class LoggingExtensions
{
public static void AddAppLogging(this WebApplicationBuilder builder)
{
const string LoggingSectionKey = "Logging";
var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
var logging = builder.Logging;
public static void AddAppLogging(this WebApplicationBuilder builder)
{
const string LoggingSectionKey = "Logging";
var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
var logging = builder.Logging;

if (isWindows)
{
logging.AddFilter<EventLogLoggerProvider>(level => level >= LogLevel.Warning);
logging.AddEventLog();
}
if (isWindows)
{
logging.AddFilter<EventLogLoggerProvider>(level => level >= LogLevel.Warning);
logging.AddEventLog();
}

logging.AddConfiguration(builder.Configuration.GetSection(LoggingSectionKey));
logging.AddSimpleConsole(opts =>
{
opts.SingleLine = true;
opts.TimestampFormat = "[yyyy-MM-dd HH:mm:ss] ";
opts.UseUtcTimestamp = true;
});
logging.AddConfiguration(builder.Configuration.GetSection(LoggingSectionKey));
logging.AddSimpleConsole(opts =>
{
opts.SingleLine = true;
opts.TimestampFormat = "[yyyy-MM-dd HH:mm:ss] ";
opts.UseUtcTimestamp = true;
});

logging.AddDebug();
logging.AddEventSourceLogger();
logging.AddDebug();
logging.AddEventSourceLogger();

logging.Configure(options =>
{
options.ActivityTrackingOptions = ActivityTrackingOptions.SpanId
| ActivityTrackingOptions.TraceId
| ActivityTrackingOptions.ParentId;
});
}
logging.Configure(options =>
{
options.ActivityTrackingOptions = ActivityTrackingOptions.SpanId
| ActivityTrackingOptions.TraceId
| ActivityTrackingOptions.ParentId;
});
}
}
79 changes: 40 additions & 39 deletions Api/Configuration/SwaggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,49 @@ namespace Api.Configuration;

public static class SwaggerExtensions
{
public static void AddAppSwagger(this WebApplicationBuilder builder)
public static void AddAppSwagger(this WebApplicationBuilder builder)
{
builder.Services.AddSwaggerGen(c =>
{
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "My API",
Version = "v1"
});
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "My API",
Version = "v1"
});

c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "Please insert JWT with Bearer into field",
Name = "Authorization",
Type = SecuritySchemeType.ApiKey
});
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "Please insert JWT with Bearer into field",
Name = "Authorization",
Type = SecuritySchemeType.ApiKey
});

c.AddSecurityRequirement(new OpenApiSecurityRequirement {
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[] { }
}
});
});
}
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[] { }
}
});
});
}

public static void UseAppSwagger(this WebApplication app)
public static void UseAppSwagger(this WebApplication app)
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseSwaggerUI(options =>
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
options.RoutePrefix = string.Empty;
});
}
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
options.RoutePrefix = string.Empty;
});
}
}
103 changes: 53 additions & 50 deletions Api/Controllers/DocumentsController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Application;
using Application;
using Application.Services;
using Core;
using Microsoft.AspNetCore.Authorization;
Expand All @@ -13,64 +13,67 @@ namespace Api.Controllers;
[Consumes("multipart/form-data")]
public class DocumentsController : Controller
{
private readonly IInnerCircleHttpClient _client;
private readonly IPayslipsValidator _payslipsValidator;
private readonly ILogger<DocumentsController> _logger;
private readonly IInnerCircleHttpClient _client;
private readonly IPayslipsValidator _payslipsValidator;
private readonly ILogger<DocumentsController> _logger;

public DocumentsController(IInnerCircleHttpClient client, IPayslipsValidator payslipsValidator, ILogger<DocumentsController> logger)
public DocumentsController(
IInnerCircleHttpClient client,
IPayslipsValidator payslipsValidator,
ILogger<DocumentsController> logger
)
{
_client = client;
_payslipsValidator = payslipsValidator;
_logger = logger;
}

[RequiresPermission(UserClaimsProvider.CanManageDocuments)]
[HttpPost("sendMailingPayslips")]
public async Task SendMailingPayslips([FromForm] List<PayslipsItem> payslips)
{
var employees = await GetEmployeesAsyncFromEmployeeService();

await _payslipsValidator.ValidateAsync(payslips, employees);

try
{
_client = client;
_payslipsValidator = payslipsValidator;
_logger = logger;
await _client.SendMailingPayslips(payslips, employees);
}

[RequiresPermission(UserClaimsProvider.CanManageDocuments)]
[HttpPost("sendMailingPayslips")]
public async Task SendMailingPayslips([FromForm] List<PayslipsItem> payslips)
catch (Exception ex)
{
var employees = await GetEmployeesAsyncFromEmployeeService();
_logger.LogError(ex.Message);
throw new Exception("Email sender service is not available");
}
}

await _payslipsValidator.ValidateAsync(payslips, employees);
[RequiresPermission(UserClaimsProvider.CanManageDocuments)]
[HttpGet("getEmployees")]
public async Task<EmployeesDto> GetEmployees()
{
var employees = await GetEmployeesAsyncFromEmployeeService();

try
{
await _client.SendMailingPayslips(payslips, employees);
}
catch(Exception ex)
return new EmployeesDto
{
Employees = employees
.Select(employee => new EmployeeDto
{
_logger.LogError(ex.Message);
throw new Exception("Email sender service is not available");
}
}
LastName = employee.LastName
})
.ToList()
};
}

[RequiresPermission(UserClaimsProvider.CanManageDocuments)]
[HttpGet("getEmployees")]
public async Task<EmployeesDto> GetEmployees()
private async Task<List<Employee>> GetEmployeesAsyncFromEmployeeService()
{
try
{
var employees = await GetEmployeesAsyncFromEmployeeService();

return new EmployeesDto
{
Employees = employees
.Select(employee => new EmployeeDto
{
LastName = employee.LastName
})
.ToList()
};
return await _client.GetEmployeesAsync();
}

private async Task<List<Employee>> GetEmployeesAsyncFromEmployeeService()
catch (Exception ex)
{
try
{
return await _client.GetEmployeesAsync();
}
catch(Exception ex)
{
_logger.LogError(ex.Message);
throw new Exception("Employees service is not available");
}
_logger.LogError(ex.Message);
throw new Exception("Employees service is not available");
}
}
}
}
Loading