-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f533c1
commit 7442a88
Showing
11 changed files
with
310 additions
and
2 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
HR.LeaveManagement.API/Controllers/WeatherForecastController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace HR.LeaveManagement.API.Controllers; | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class WeatherForecastController : ControllerBase | ||
{ | ||
private static readonly string[] Summaries = new[] | ||
{ | ||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | ||
}; | ||
|
||
private readonly ILogger<WeatherForecastController> _logger; | ||
|
||
public WeatherForecastController(ILogger<WeatherForecastController> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
[HttpGet(Name = "GetWeatherForecast")] | ||
public IEnumerable<WeatherForecast> Get() | ||
{ | ||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast | ||
{ | ||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), | ||
TemperatureC = Random.Shared.Next(-20, 55), | ||
Summary = Summaries[Random.Shared.Next(Summaries.Length)] | ||
}) | ||
.ToArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.5" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.5"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> | ||
<PackageReference Include="Serilog" Version="2.12.0" /> | ||
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" /> | ||
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" /> | ||
<PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" /> | ||
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\HR.LeaveManagement.Application\HR.LeaveManagement.Application.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System.Net; | ||
|
||
using HR.LeaveManagement.API.Models; | ||
using HR.LeaveManagement.Application.Exceptions; | ||
|
||
using Newtonsoft.Json; | ||
|
||
namespace HR.LeaveManagement.API.Middlewares; | ||
|
||
public class ExceptionMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
private readonly ILogger<ExceptionMiddleware> _logger; | ||
|
||
public ExceptionMiddleware(RequestDelegate next, ILogger<ExceptionMiddleware> logger) | ||
{ | ||
_next = next; | ||
_logger = logger; | ||
} | ||
|
||
public async Task InvokeAsync(HttpContext httpContext) | ||
{ | ||
try | ||
{ | ||
await _next(httpContext); | ||
} | ||
catch (Exception ex) | ||
{ | ||
await HandleExceptionAsync(httpContext, ex); | ||
} | ||
} | ||
|
||
private async Task HandleExceptionAsync(HttpContext httpContext, Exception ex) | ||
{ | ||
HttpStatusCode statusCode = HttpStatusCode.InternalServerError; | ||
CustomProblemDetails problem = new(); | ||
|
||
switch (ex) | ||
{ | ||
case BadRequestException badRequestException: | ||
statusCode = HttpStatusCode.BadRequest; | ||
problem = new CustomProblemDetails | ||
{ | ||
Title = badRequestException.Message, | ||
Status = (int)statusCode, | ||
Detail = badRequestException.InnerException?.Message, | ||
Type = nameof(BadRequestException), | ||
Errors = badRequestException.ValidationErrors | ||
}; | ||
break; | ||
case NotFoundException NotFound: | ||
statusCode = HttpStatusCode.NotFound; | ||
problem = new CustomProblemDetails | ||
{ | ||
Title = NotFound.Message, | ||
Status = (int)statusCode, | ||
Type = nameof(NotFoundException), | ||
Detail = NotFound.InnerException?.Message, | ||
}; | ||
break; | ||
default: | ||
problem = new CustomProblemDetails | ||
{ | ||
Title = ex.Message, | ||
Status = (int)statusCode, | ||
Type = nameof(HttpStatusCode.InternalServerError), | ||
Detail = ex.StackTrace, | ||
}; | ||
break; | ||
} | ||
|
||
httpContext.Response.StatusCode = (int)statusCode; | ||
var logMessage = JsonConvert.SerializeObject(problem); | ||
_logger.LogError(logMessage); | ||
await httpContext.Response.WriteAsJsonAsync(problem); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace HR.LeaveManagement.API.Models; | ||
|
||
public class CustomProblemDetails : ProblemDetails | ||
{ | ||
public IDictionary<string, string[]> Errors { get; set; } = new Dictionary<string, string[]>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using HR.LeaveManagement.API.Middlewares; | ||
|
||
using Serilog; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add services to the container. | ||
|
||
builder.Host.UseSerilog((context, loggerConfig) => loggerConfig | ||
.WriteTo.Console() | ||
.ReadFrom.Configuration(context.Configuration)); | ||
|
||
//builder.Services.AddApplicationServices(); | ||
//builder.Services.AddInfrastructureServices(builder.Configuration); | ||
//builder.Services.AddPersistenceServices(builder.Configuration); | ||
//builder.Services.AddIdentityServices(builder.Configuration); | ||
|
||
builder.Services.AddControllers(); | ||
|
||
builder.Services.AddCors(options => | ||
{ | ||
options.AddPolicy("all", builder => builder.AllowAnyOrigin() | ||
.AllowAnyHeader() | ||
.AllowAnyMethod()); | ||
}); | ||
|
||
builder.Services.AddHttpContextAccessor(); | ||
|
||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
|
||
var app = builder.Build(); | ||
|
||
app.UseMiddleware<ExceptionMiddleware>(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
app.UseSerilogRequestLogging(); | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.UseCors("all"); | ||
|
||
app.UseAuthentication(); | ||
app.UseAuthorization(); | ||
|
||
app.MapControllers(); | ||
|
||
app.Run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:12308", | ||
"sslPort": 44339 | ||
} | ||
}, | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "http://localhost:5145", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "https://localhost:7208;http://localhost:5145", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace HR.LeaveManagement.API; | ||
|
||
public class WeatherForecast | ||
{ | ||
public DateOnly Date { get; set; } | ||
|
||
public int TemperatureC { get; set; } | ||
|
||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | ||
|
||
public string? Summary { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"ConnectionStrings": { | ||
"HrDatabaseConnectionString": "Server=(localdb)\\mssqllocaldb;Database=db_hr_leavemanagement;Trusted_Connection=True;MultipleActiveResultSets=true" | ||
}, | ||
"EmailSettings": { | ||
"ApiKey": "SendGrid-Key", | ||
"FromAddress": "[email protected]", | ||
"FromName": "HR Management System" | ||
}, | ||
"JwtSettings": { | ||
"Key": "SECRET_JWT_KEY_HERE", | ||
"Issuer": "HRLeavemanagement.Api", | ||
"Audience": "HRLeavemanagementUser", | ||
"DurationInMinutes": 15 | ||
}, | ||
"Serilog": { | ||
"MinimumLevel": { | ||
"Default": "Information", | ||
"Override": { | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
}, | ||
"WriteTo": [ | ||
{ | ||
"Name": "File", | ||
"Args": { | ||
"path": "./logs/log-.txt", | ||
"rollingInterval": "Day" | ||
} | ||
} | ||
] | ||
}, | ||
"AllowedHosts": "*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters