Skip to content

Commit

Permalink
API Layer Added
Browse files Browse the repository at this point in the history
  • Loading branch information
rahiyansafin committed May 5, 2023
1 parent 4f533c1 commit 7442a88
Show file tree
Hide file tree
Showing 11 changed files with 310 additions and 2 deletions.
31 changes: 31 additions & 0 deletions HR.LeaveManagement.API/Controllers/WeatherForecastController.cs
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();
}
}
28 changes: 28 additions & 0 deletions HR.LeaveManagement.API/HR.LeaveManagement.API.csproj
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>
77 changes: 77 additions & 0 deletions HR.LeaveManagement.API/Middlewares/ExceptionMiddleware.cs
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);
}
}
8 changes: 8 additions & 0 deletions HR.LeaveManagement.API/Models/CustomProblemDetails.cs
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[]>();
}
55 changes: 55 additions & 0 deletions HR.LeaveManagement.API/Program.cs
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();
41 changes: 41 additions & 0 deletions HR.LeaveManagement.API/Properties/launchSettings.json
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"
}
}
}
}
12 changes: 12 additions & 0 deletions HR.LeaveManagement.API/WeatherForecast.cs
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; }
}
8 changes: 8 additions & 0 deletions HR.LeaveManagement.API/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
41 changes: 41 additions & 0 deletions HR.LeaveManagement.API/appsettings.json
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": "*"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace HR.LeaveManagement.Application;
public static class ApplicationServiceResigtration
{
public static IServiceCollection ApplicationServices(this IServiceCollection services)
public static IServiceCollection AddApplicationServices(this IServiceCollection services)
{
services.AddAutoMapper(Assembly.GetExecutingAssembly());
services.AddMediatR(Assembly.GetExecutingAssembly());
Expand Down
9 changes: 8 additions & 1 deletion HR.LeaveManagement.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HR.LeaveManagement.Applicat
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HR.LeaveManagement.Infrastructure", "HR.LeaveManagement.Infrastructure\HR.LeaveManagement.Infrastructure.csproj", "{FEF27D23-751D-4D0C-9FEB-740537F0CB9D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HR.LeaveManagement.Persistence", "HR.LeaveManagement.Persistence\HR.LeaveManagement.Persistence.csproj", "{0E79A659-1969-4BC8-9680-8F385381331F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HR.LeaveManagement.Persistence", "HR.LeaveManagement.Persistence\HR.LeaveManagement.Persistence.csproj", "{0E79A659-1969-4BC8-9680-8F385381331F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HR.LeaveManagement.API", "HR.LeaveManagement.API\HR.LeaveManagement.API.csproj", "{9783B2CC-7344-404E-B59B-5BF50269D611}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -45,6 +47,10 @@ Global
{0E79A659-1969-4BC8-9680-8F385381331F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0E79A659-1969-4BC8-9680-8F385381331F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0E79A659-1969-4BC8-9680-8F385381331F}.Release|Any CPU.Build.0 = Release|Any CPU
{9783B2CC-7344-404E-B59B-5BF50269D611}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9783B2CC-7344-404E-B59B-5BF50269D611}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9783B2CC-7344-404E-B59B-5BF50269D611}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9783B2CC-7344-404E-B59B-5BF50269D611}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -58,6 +64,7 @@ Global
{D63F4CEC-1751-4508-89BB-004EF5F2D655} = {79013FD3-7C1C-4AF0-AA5B-8F356DC00404}
{FEF27D23-751D-4D0C-9FEB-740537F0CB9D} = {806C1EC6-F10D-40B0-A859-1E6D915C2CFB}
{0E79A659-1969-4BC8-9680-8F385381331F} = {806C1EC6-F10D-40B0-A859-1E6D915C2CFB}
{9783B2CC-7344-404E-B59B-5BF50269D611} = {C3F8BCD4-2E58-47F2-A4AD-244F2ADCF632}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {20D6677C-2E5C-4FCC-9FFB-D6FE8214DA70}
Expand Down

0 comments on commit 7442a88

Please sign in to comment.