Skip to content

Commit

Permalink
Closes #60
Browse files Browse the repository at this point in the history
Modularize backend
  • Loading branch information
mtracewicz committed Sep 10, 2024
1 parent 3436b38 commit 08ff0b7
Show file tree
Hide file tree
Showing 19 changed files with 84 additions and 62 deletions.
1 change: 1 addition & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ WORKDIR /app
EXPOSE 80
EXPOSE 443

#Todo: clean it up for multiproject build
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY src/api/api.csproj src/api/
Expand Down
6 changes: 0 additions & 6 deletions backend/src/api/Constants/ErrorMessages.cs

This file was deleted.

2 changes: 1 addition & 1 deletion backend/src/api/Controllers/TodoController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using api.Services;
using core.Ports;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

Expand Down
14 changes: 9 additions & 5 deletions backend/src/api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using api.Data;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Microsoft.EntityFrameworkCore;
using Serilog;
using api;
using System.Security.Cryptography;
using api.Services;
using api;
using infrastructure.Data;
using infrastructure.Keycloak;
using core.Ports;

const string logFormat = "[{Timestamp:HH:mm:ss} {Level:u3}] {CorelationId} | {Message:lj}{NewLine}{Exception}";
Log.Logger = new LoggerConfiguration().Enrich.WithCorrelationId()
Expand All @@ -19,7 +20,10 @@
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpContextAccessor();
builder.Host.UseSerilog();
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseNpgsql(builder.Configuration.GetConnectionString("KSummarized")));
builder.Services.AddDbContext<ApplicationDbContext>(
options => options.UseNpgsql(builder.Configuration.GetConnectionString("KSummarized"),
x => x.MigrationsAssembly("infrastructure")
));
var keycloakJwtOptions = builder.Configuration.GetRequiredSection("KeycloakJwt").Get<KeycloakJwtOptions>()!;

// Create RSA key for offline validation of Keycloak token
Expand Down
13 changes: 0 additions & 13 deletions backend/src/api/Services/Todo/ITodoService.cs

This file was deleted.

10 changes: 7 additions & 3 deletions backend/src/api/api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.3">
<PrivateAssets>all</PrivateAssets>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2" />
Expand All @@ -27,4 +26,9 @@
<Folder Include="Migrations/" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\infrastructure\infrastructure.csproj" />
<ProjectReference Include="..\core\core.csproj" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions backend/src/core/Ports/ITodoService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace core.Ports;

public interface ITodoService
{
Task<TodoList> CreateList(string user, string name);
IEnumerable<TodoList> GetLists(string userId);
TodoList? GetList(string userId, int id);
bool DeleteList(string userId, int id);
Task<bool> RenameList(string user, int id, string name);
}
10 changes: 10 additions & 0 deletions backend/src/core/TodoList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace core;

public class TodoList
{
public int Id { get; set; }
public required string Name { get; set; }
public required Guid Owner { get; set; }

public bool IsAuthorized(Guid person) => Owner.Equals(person);
}
9 changes: 9 additions & 0 deletions backend/src/core/core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
6 changes: 0 additions & 6 deletions backend/src/infrastructure/Class1.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using api.Data.DAO;
using Microsoft.EntityFrameworkCore;

namespace api.Data;
namespace infrastructure.Data;

public class ApplicationDbContext : DbContext
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace api.Data.DAO;
namespace infrastructure.Data;

[Table("todo_lists")]
public class TodoListModel
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using api.Data;
using api.Data.DAO;
using api.Data.DTO;
using Microsoft.EntityFrameworkCore;
using core.Ports;
using core;

namespace api.Services;
namespace infrastructure.Data;

public class TodoService : ITodoService
{
Expand All @@ -15,29 +14,29 @@ public TodoService(ApplicationDbContext context)
_context = context;
}

public IEnumerable<TodoListDTO> GetLists(string userId)
public IEnumerable<TodoList> GetLists(string userId)
{
return _context.TodoLists.AsNoTracking()
.Where(list => list.Owner.Equals(Guid.Parse(userId)))
.Select(list => new TodoListDTO(list.Id, list.Name))
.Select(list => new TodoList { Id = list.Id, Name = list.Name, Owner = list.Owner })
.AsEnumerable();
}

public TodoListDTO? GetList(string userId, int id)
public TodoList? GetList(string userId, int id)
{
var list = _context.TodoLists.AsNoTracking()
.SingleOrDefault(l => l.Owner.Equals(Guid.Parse(userId)) && l.Id == id);
//TODO: Consider creating a mapper instead of this manual new
if (list is not null) { return new(list.Id, list.Name); }
if (list is not null) { return new() { Id = list.Id, Name = list.Name, Owner = list.Owner }; }
return null;
}

public async Task<TodoListModel> CreateList(string user, string name)
public async Task<TodoList> CreateList(string user, string name)
{
var newList = new TodoListModel() { Name = name, Owner = Guid.Parse(user) };
_context.TodoLists.Add(newList);
await _context.SaveChangesAsync();
return newList;
return new TodoList() { Id = newList.Id, Name = newList.Name, Owner = newList.Owner };
}

public bool DeleteList(string userId, int id)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Diagnostics.CodeAnalysis;

namespace api.Data;
namespace infrastructure.Keycloak;
public class KeycloakJwtOptions
{
public required string Issuer { get; init; }
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace api.Migrations
namespace infrastructure.Migrations
{
/// <inheritdoc />
public partial class TodoList : Migration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// <auto-generated />
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using api.Data;
using infrastructure.Data;

#nullable disable

namespace api.Migrations
namespace infrastructure.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
Expand Down
13 changes: 13 additions & 0 deletions backend/src/infrastructure/infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,17 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.8">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\core\core.csproj" />
</ItemGroup>

</Project>
6 changes: 3 additions & 3 deletions scripts/apply_migrations.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ get-content ../.env | foreach {
Write-Host "Starting DB"
docker compose up db -d 2>&1 > $null
Write-Output "Generateing migration script"
Set-Location ../backend/src/api
Set-Location ../backend
mkdir migration_scripts 2>&1 > $null
dotnet ef migrations script --idempotent -o ./migration_scripts/migration.sql
dotnet ef migrations script --idempotent -o ./migration_scripts/migration.sql -p ./src/infrastructure -s ./src/api

Write-Output "Applying.."
$passwd = "PGPASSWORD=" + $Env:POSTGRES_PASSWORD
Expand All @@ -24,7 +24,7 @@ docker run --network host -e $passwd -v ${src}:/migrations/ --rm postgres `

Write-Output "Cleanup"
Remove-Item -Recurse -Force migration_scripts
Set-Location ../../../scripts
Set-Location ../scripts
Write-Output "Migrations had been applyed!"
Write-Host "Stoping DB"
docker stop ks-database 2>&1 > $null
Expand Down

0 comments on commit 08ff0b7

Please sign in to comment.