Presentation 2
This commit is contained in:
77
dotnet/.gitignore
vendored
77
dotnet/.gitignore
vendored
@@ -1,77 +0,0 @@
|
||||
# Environment variables with secrets
|
||||
.env
|
||||
|
||||
# User-specific files
|
||||
*.suo
|
||||
*.user
|
||||
*.userosscache
|
||||
*.sln.docstates
|
||||
|
||||
# Build results
|
||||
[Dd]ebug/
|
||||
[Dd]ebugPublic/
|
||||
[Rr]elease/
|
||||
[Rr]eleases/
|
||||
x64/
|
||||
x86/
|
||||
[Aa][Rr][Mm]/
|
||||
[Aa][Rr][Mm]64/
|
||||
bld/
|
||||
[Bb]in/
|
||||
[Oo]bj/
|
||||
[Ll]og/
|
||||
|
||||
# Visual Studio cache/options directory
|
||||
.vs/
|
||||
|
||||
# JetBrains Rider
|
||||
.idea/
|
||||
*.sln.iml
|
||||
|
||||
# .NET Core
|
||||
project.lock.json
|
||||
project.fragment.lock.json
|
||||
artifacts/
|
||||
|
||||
# ASP.NET Scaffolding
|
||||
ScaffoldingReadMe.txt
|
||||
|
||||
# Files built by Visual Studio
|
||||
*_i.c
|
||||
*_p.c
|
||||
*_h.h
|
||||
*.ilk
|
||||
*.meta
|
||||
*.obj
|
||||
*.iobj
|
||||
*.pch
|
||||
*.pdb
|
||||
*.ipdb
|
||||
*.pgc
|
||||
*.pgd
|
||||
*.rsp
|
||||
*.sbr
|
||||
*.tlb
|
||||
*.tli
|
||||
*.tlh
|
||||
*.tmp
|
||||
*.tmp_proj
|
||||
*_wpftmp.csproj
|
||||
*.log
|
||||
*.vspscc
|
||||
*.vssscc
|
||||
.builds
|
||||
*.pidb
|
||||
*.svclog
|
||||
*.scc
|
||||
|
||||
# NuGet Packages
|
||||
*.nupkg
|
||||
*.snupkg
|
||||
**/packages/*
|
||||
!**/packages/build/
|
||||
*.nuget.props
|
||||
*.nuget.targets
|
||||
|
||||
# Docker
|
||||
**/.dockerignore
|
||||
@@ -8,6 +8,10 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.2" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.2">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="10.0.2" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -3,4 +3,4 @@ using AipsCore.Domain.Models.User.ValueObjects;
|
||||
|
||||
namespace AipsCore.Application.Models.User.Command.CreateUser;
|
||||
|
||||
public record CreateUserCommand(string Username, string Email) : ICommand<UserId>, ICommand;
|
||||
public record CreateUserCommand(string Username, string Email) : ICommand<UserId>;
|
||||
@@ -1,10 +1,11 @@
|
||||
using AipsCore.Application.Abstract.Command;
|
||||
using AipsCore.Domain.Abstract;
|
||||
using AipsCore.Domain.Models.User.External;
|
||||
using AipsCore.Domain.Models.User.ValueObjects;
|
||||
|
||||
namespace AipsCore.Application.Models.User.Command.CreateUser;
|
||||
|
||||
public class CreateUserCommandHandler : ICommandHandler<CreateUserCommand>
|
||||
public class CreateUserCommandHandler : ICommandHandler<CreateUserCommand, UserId>
|
||||
{
|
||||
private readonly IUserRepository _userRepository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
@@ -15,11 +16,13 @@ public class CreateUserCommandHandler : ICommandHandler<CreateUserCommand>
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task Handle(CreateUserCommand command, CancellationToken cancellationToken = default)
|
||||
public async Task<UserId> Handle(CreateUserCommand command, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var user = Domain.Models.User.User.Create(command.Email, command.Username);
|
||||
|
||||
await _userRepository.Save(user, cancellationToken);
|
||||
await _unitOfWork.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return user.Id;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
using AipsCore.Application.Abstract;
|
||||
using AipsCore.Application.Abstract.Command;
|
||||
using AipsCore.Application.Common.Dispatcher;
|
||||
using AipsCore.Application.Models.User.Command.CreateUser;
|
||||
using AipsCore.Domain.Models.User.ValueObjects;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
@@ -10,7 +13,7 @@ public static class AipsRegistrationExtensions
|
||||
public static IServiceCollection AddAips(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddHandlersFromAssembly(typeof(Dispatcher).Assembly);
|
||||
services.AddSingleton<IDispatcher, Dispatcher>();
|
||||
services.AddTransient<IDispatcher, Dispatcher>();
|
||||
|
||||
services.AddPersistence(configuration);
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ public static class HandlerRegistrationExtensions
|
||||
if (!handlerInterfaces.Contains(genericDef))
|
||||
continue;
|
||||
|
||||
services.AddScoped(@interface, type);
|
||||
services.AddTransient(@interface, type);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
using AipsCore.Domain.Abstract;
|
||||
using AipsCore.Domain.Models.User.External;
|
||||
using AipsCore.Infrastructure.DI.Configuration;
|
||||
using AipsCore.Infrastructure.Persistence.Db;
|
||||
using AipsCore.Infrastructure.Persistence.User;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@@ -17,6 +20,9 @@ public static class PersistenceRegistrationExtensions
|
||||
npgsql.MigrationsAssembly(typeof(AipsDbContext).Assembly.FullName);
|
||||
});
|
||||
});
|
||||
|
||||
services.AddTransient<IUnitOfWork, EfUnitOfWork>();
|
||||
services.AddTransient<IUserRepository, UserRepository>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
@@ -5,4 +5,10 @@ namespace AipsCore.Infrastructure.Persistence.Db;
|
||||
public class AipsDbContext : DbContext
|
||||
{
|
||||
public DbSet<User.User> Users { get; set; }
|
||||
|
||||
public AipsDbContext(DbContextOptions options)
|
||||
: base(options)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
51
dotnet/AipsCore/Infrastructure/Persistence/Db/Migrations/20260205205236_Init.Designer.cs
generated
Normal file
51
dotnet/AipsCore/Infrastructure/Persistence/Db/Migrations/20260205205236_Init.Designer.cs
generated
Normal file
@@ -0,0 +1,51 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using AipsCore.Infrastructure.Persistence.Db;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace AipsCore.Infrastructure.Persistence.Db.Migrations
|
||||
{
|
||||
[DbContext(typeof(AipsDbContext))]
|
||||
[Migration("20260205205236_Init")]
|
||||
partial class Init
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "10.0.2")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("AipsCore.Infrastructure.Persistence.User.User", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("Email")
|
||||
.IsRequired()
|
||||
.HasMaxLength(255)
|
||||
.HasColumnType("character varying(255)");
|
||||
|
||||
b.Property<string>("Username")
|
||||
.IsRequired()
|
||||
.HasMaxLength(255)
|
||||
.HasColumnType("character varying(255)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace AipsCore.Infrastructure.Persistence.Db.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class Init : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Users",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
Username = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: false),
|
||||
Email = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Users", x => x.Id);
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "Users");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using AipsCore.Infrastructure.Persistence.Db;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace AipsCore.Infrastructure.Persistence.Db.Migrations
|
||||
{
|
||||
[DbContext(typeof(AipsDbContext))]
|
||||
partial class AipsDbContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "10.0.2")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("AipsCore.Infrastructure.Persistence.User.User", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("Email")
|
||||
.IsRequired()
|
||||
.HasMaxLength(255)
|
||||
.HasColumnType("character varying(255)");
|
||||
|
||||
b.Property<string>("Username")
|
||||
.IsRequired()
|
||||
.HasMaxLength(255)
|
||||
.HasColumnType("character varying(255)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,7 +27,8 @@ public class UserRepository : IUserRepository
|
||||
|
||||
public async Task Save(Domain.Models.User.User user, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var userEntity = await _context.Users.FindAsync([new Guid(user.Id.IdValue), cancellationToken], cancellationToken: cancellationToken);
|
||||
// ReSharper disable once MethodSupportsCancellation
|
||||
var userEntity = await _context.Users.FindAsync(new Guid(user.Id.IdValue));
|
||||
|
||||
if (userEntity is not null)
|
||||
{
|
||||
|
||||
@@ -7,7 +7,14 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="DotNetEnv" Version="3.1.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.1"/>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.2">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="10.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using AipsCore.Application.Abstract;
|
||||
using AipsCore.Application.Models.User.Command.CreateUser;
|
||||
using AipsCore.Domain.Common.Validation;
|
||||
using AipsCore.Domain.Models.User.ValueObjects;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
@@ -12,8 +13,14 @@ public class UserController : ControllerBase
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<int>> CreateUser(CreateUserCommand command, IDispatcher dispatcher, CancellationToken cancellationToken)
|
||||
{
|
||||
var userId = await dispatcher.Execute<UserId>(command, cancellationToken);
|
||||
|
||||
return Ok(userId.IdValue);
|
||||
try
|
||||
{
|
||||
var userId = await dispatcher.Execute(command, cancellationToken);
|
||||
return Ok(userId.IdValue);
|
||||
}
|
||||
catch (ValidationException validationException)
|
||||
{
|
||||
return BadRequest(validationException.ValidationErrors);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,16 @@
|
||||
using AipsCore.Infrastructure.DI;
|
||||
using DotNetEnv;
|
||||
|
||||
Env.Load();
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
var configuration = builder.Configuration;
|
||||
builder.Configuration.AddEnvironmentVariables();
|
||||
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
builder.Services.AddAips(configuration);
|
||||
builder.Services.AddAips(builder.Configuration);
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
@@ -17,7 +20,6 @@ if (app.Environment.IsDevelopment())
|
||||
app.MapOpenApi();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user