Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes templates path, adds docker integration & resolves code smells #54

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
**/appsettings.Development.json
**/.classpath
**/.dockerignore
**/.env
Expand All @@ -22,4 +23,4 @@
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
README.md
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,7 @@ healthchecksdb
MigrationBackup/

# Ionide (cross platform F# VS Code tools) working folder
.ionide/
.ionide/
.template.config/

.idea/
28 changes: 28 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src

COPY ["./src/N-Tier.API/N-Tier.API.csproj", "src/N-Tier.API/"]
COPY ["./src/N-Tier.Application/N-Tier.Application.csproj", "src/N-Tier.Application/"]
COPY ["./src/N-Tier.Core/N-Tier.Core.csproj", "src/N-Tier.Core/"]
COPY ["./src/N-Tier.DataAccess/N-Tier.DataAccess.csproj", "src/N-Tier.DataAccess/"]
COPY ["./src/N-Tier.Shared/N-Tier.Shared.csproj", "src/N-Tier.Shared/"]

RUN dotnet restore "src/N-Tier.API/N-Tier.API.csproj"

COPY . .

WORKDIR "src/N-Tier.API/"
RUN dotnet build -c Release -o /app/build

FROM build AS publish
RUN dotnet publish -c Release -o /app/publish

FROM base AS runtime
WORKDIR /app

COPY --from=publish /app/publish .

ENTRYPOINT [ "dotnet", "N-Tier.API.dll" ]
7 changes: 7 additions & 0 deletions N-Tier.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "N-Tier.Application.UnitTest
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "N-Tier.Api.IntegrationTests", "tests\N-Tier.Api.IntegrationTests\N-Tier.Api.IntegrationTests.csproj", "{FD31F369-27D3-48B3-9FF7-F4A357F73B1E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docker", "docker", "{4C079880-AEA7-40E6-BA1A-F669D09BBC9D}"
ProjectSection(SolutionItems) = preProject
Dockerfile = Dockerfile
docker-compose.yml = docker-compose.yml
.dockerignore = .dockerignore
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
16 changes: 16 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: "3.9"
services:
db:
image: "mcr.microsoft.com/mssql/server"
environment:
SA_PASSWORD: "Admin123.?"
ACCEPT_EULA: "Y"
web:
build: .
ports:
- "5000:80"
depends_on:
- db
environment:
ASPNETCORE_ENVIRONMENT: "Production"
CONNECTION_STRING: "Server=db;Database=NTier;User=sa;Password=Admin123.?;"
12 changes: 12 additions & 0 deletions src/N-Tier.API/Controllers/HealthcheckController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Mvc;

namespace N_Tier.API.Controllers;

public class HealthcheckController : ApiController
{
[HttpGet]
public IActionResult Get()
{
return Ok();
}
}
25 changes: 0 additions & 25 deletions src/N-Tier.API/Dockerfile

This file was deleted.

1 change: 1 addition & 0 deletions src/N-Tier.API/Filters/ValidateModelAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace N_Tier.API.Filters;

[AttributeUsage(AttributeTargets.Class)]
public class ValidateModelAttribute : Attribute, IAsyncResultFilter
{
public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
Expand Down
2 changes: 1 addition & 1 deletion src/N-Tier.API/Middleware/ExceptionHandlingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task Invoke(HttpContext context)

private Task HandleException(HttpContext context, Exception ex)
{
_logger.LogError(ex.Message);
_logger.LogError("{Exception}", ex.Message);

var code = StatusCodes.Status500InternalServerError;
var errors = new List<string> { ex.Message };
Expand Down
5 changes: 1 addition & 4 deletions src/N-Tier.API/Middleware/TransactionMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ namespace N_Tier.API.Middleware;

public class TransactionMiddleware
{
private readonly ILogger<TransactionMiddleware> _logger;

private readonly RequestDelegate _next;

public TransactionMiddleware(RequestDelegate next, ILogger<TransactionMiddleware> logger)
public TransactionMiddleware(RequestDelegate next)
{
_next = next;
_logger = logger;
}

public async Task Invoke(HttpContext context, DatabaseContext databaseContext)
Expand Down
16 changes: 16 additions & 0 deletions src/N-Tier.API/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,21 @@
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"Database": {
"UseInMemoryDatabase": true,
"ConnectionString": "Server=db;Database=NTier;User=sa;Password=Admin123.?;"
},
"JwtConfiguration": {
"SecretKey": "Super secret token key"
},
"SmtpSettings": {
"Server": "smtp.gmail.com",
"Port": 587,
"SenderName": "N-Tier",
"SenderEmail": "<account>@gmail.com",
"Username": "<account>@gmail.com",
"Password": "<account-password>"
}
}
2 changes: 1 addition & 1 deletion src/N-Tier.API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"AllowedHosts": "*",
"Database": {
"UseInMemoryDatabase": true,
"ConnectionString": "Data Source=(LocalDb)\\MSSQLLocalDB;Initial Catalog=NTier;Integrated Security=True"
"ConnectionString": "Server=db;Database=NTier;User=sa;Password=Admin123.?;"
},
"JwtConfiguration": {
"SecretKey": "Super secret token key"
Expand Down
5 changes: 4 additions & 1 deletion src/N-Tier.Application/Exceptions/BadRequestException.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
namespace N_Tier.Application.Exceptions;
using System.Runtime.Serialization;

namespace N_Tier.Application.Exceptions;

[Serializable]
public class BadRequestException : Exception
{
public BadRequestException(string message) : base(message) { }
protected BadRequestException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
6 changes: 5 additions & 1 deletion src/N-Tier.Application/Exceptions/NotFoundException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
namespace N_Tier.Application.Exceptions;
using System.Runtime.Serialization;

namespace N_Tier.Application.Exceptions;

[Serializable]
public class NotFoundException : Exception
{
public NotFoundException(string message) : base(message) { }
protected NotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
namespace N_Tier.Application.Exceptions;
using System.Runtime.Serialization;

namespace N_Tier.Application.Exceptions;

[Serializable]
public class UnprocessableRequestException : Exception
{
public UnprocessableRequestException(string message) : base(message) { }
protected UnprocessableRequestException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
21 changes: 14 additions & 7 deletions src/N-Tier.Application/N-Tier.Application.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
Expand All @@ -7,15 +7,22 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="10.1.1"/>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1"/>
<PackageReference Include="FluentValidation.AspNetCore" Version="10.3.4"/>
<PackageReference Include="MailKit" Version="2.15.0"/>
<PackageReference Include="MimeKit" Version="2.15.1"/>
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
<PackageReference Include="FluentValidation.AspNetCore" Version="10.3.4" />
<PackageReference Include="MailKit" Version="2.15.0" />
<PackageReference Include="MimeKit" Version="2.15.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\N-Tier.DataAccess\N-Tier.DataAccess.csproj"/>
<ProjectReference Include="..\N-Tier.DataAccess\N-Tier.DataAccess.csproj" />
</ItemGroup>

<ItemGroup>
<None Remove="Templates\confirmation_email.html" />
<Content Include="Templates\confirmation_email.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/N-Tier.Application/Services/DevImpl/DevEmailService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public async Task SendEmailAsync(EmailMessage emailMessage)
{
await Task.Delay(100);

_logger.LogInformation($"Email was sent to: [{emailMessage.ToAddress}]. Body: {emailMessage.Body}");
_logger.LogInformation("Email was sent to: [{ToAddress}]. Body: {Body}", emailMessage.ToAddress, emailMessage.Body);
}
}
1 change: 0 additions & 1 deletion src/N-Tier.Application/Services/Impl/EmailService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ private async Task SendAsync(MimeMessage message)
catch
{
await client.DisconnectAsync(true);
client.Dispose();

throw;
}
Expand Down
9 changes: 2 additions & 7 deletions src/N-Tier.Application/Services/Impl/TemplateService.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
using System.Reflection;

namespace N_Tier.Application.Services.Impl;
namespace N_Tier.Application.Services.Impl;

public class TemplateService : ITemplateService
{
private readonly string _templatesPath;

public TemplateService()
{
var projectPath = Directory.GetParent(Directory.GetCurrentDirectory()).FullName;
var templateProject = Assembly.GetExecutingAssembly().GetName().Name;

_templatesPath = Path.Combine(projectPath, templateProject, "Templates");
_templatesPath = Path.Combine(AppContext.BaseDirectory, "Templates");
}

public async Task<string> GetTemplateAsync(string templateName)
Expand Down
3 changes: 2 additions & 1 deletion src/N-Tier.Core/Exceptions/ResourceNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.Serialization;
#nullable enable
using System.Runtime.Serialization;

namespace N_Tier.Core.Exceptions
{
Expand Down
11 changes: 2 additions & 9 deletions src/N-Tier.DataAccess/DataAccessDependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ private static void AddRepositories(this IServiceCollection services)
private static void AddDatabase(this IServiceCollection services, IConfiguration configuration)
{
var databaseConfig = configuration.GetSection("Database").Get<DatabaseConfiguration>();
var connectionString = string.IsNullOrEmpty(databaseConfig.ConnectionString) ? configuration["CONNECTION_STRING"] : databaseConfig.ConnectionString;

if (databaseConfig.UseInMemoryDatabase)
services.AddDbContext<DatabaseContext>(options =>
Expand All @@ -41,7 +42,7 @@ private static void AddDatabase(this IServiceCollection services, IConfiguration
});
else
services.AddDbContext<DatabaseContext>(options =>
options.UseSqlServer(databaseConfig.ConnectionString,
options.UseSqlServer(connectionString,
opt => opt.MigrationsAssembly(typeof(DatabaseContext).Assembly.FullName)));
}

Expand Down Expand Up @@ -69,11 +70,3 @@ private static void AddIdentity(this IServiceCollection services)
});
}
}

// TODO move outside?
public class DatabaseConfiguration
{
public bool UseInMemoryDatabase { get; set; }

public string ConnectionString { get; set; }
}
8 changes: 8 additions & 0 deletions src/N-Tier.DataAccess/DatabaseConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace N_Tier.DataAccess;

public class DatabaseConfiguration
{
public bool UseInMemoryDatabase { get; set; }

public string ConnectionString { get; set; }
}