Added EF entity, Migrations, Whiteboard repository service

This commit is contained in:
Veljko Tosic
2026-02-09 00:18:02 +01:00
parent e9ea07b58a
commit e526d25116
7 changed files with 223 additions and 1 deletions

View File

@@ -1,8 +1,10 @@
using AipsCore.Domain.Abstract; using AipsCore.Domain.Abstract;
using AipsCore.Domain.Models.User.External; using AipsCore.Domain.Models.User.External;
using AipsCore.Domain.Models.Whiteboard.External;
using AipsCore.Infrastructure.DI.Configuration; using AipsCore.Infrastructure.DI.Configuration;
using AipsCore.Infrastructure.Persistence.Db; using AipsCore.Infrastructure.Persistence.Db;
using AipsCore.Infrastructure.Persistence.User; using AipsCore.Infrastructure.Persistence.User;
using AipsCore.Infrastructure.Persistence.Whiteboard;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@@ -23,6 +25,7 @@ public static class PersistenceRegistrationExtensions
services.AddTransient<IUnitOfWork, EfUnitOfWork>(); services.AddTransient<IUnitOfWork, EfUnitOfWork>();
services.AddTransient<IUserRepository, UserRepository>(); services.AddTransient<IUserRepository, UserRepository>();
services.AddTransient<IWhiteboardRepository, WhiteboardRepository>();
return services; return services;
} }

View File

@@ -5,8 +5,9 @@ namespace AipsCore.Infrastructure.Persistence.Db;
public class AipsDbContext : DbContext public class AipsDbContext : DbContext
{ {
public DbSet<User.User> Users { get; set; } public DbSet<User.User> Users { get; set; }
public DbSet<Whiteboard.Whiteboard> Whiteboards { get; set; }
public AipsDbContext(DbContextOptions options) public AipsDbContext(DbContextOptions<AipsDbContext> options)
: base(options) : base(options)
{ {

View File

@@ -0,0 +1,75 @@
// <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("20260208220239_AddedBasicWhiteboard")]
partial class AddedBasicWhiteboard
{
/// <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");
});
modelBuilder.Entity("AipsCore.Infrastructure.Persistence.Whiteboard.Whiteboard", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("Code")
.IsRequired()
.HasMaxLength(8)
.HasColumnType("character varying(8)");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(32)
.HasColumnType("character varying(32)");
b.Property<Guid>("WhiteboardOwnerId")
.HasColumnType("uuid");
b.HasKey("Id");
b.ToTable("Whiteboards");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,36 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace AipsCore.Infrastructure.Persistence.Db.Migrations
{
/// <inheritdoc />
public partial class AddedBasicWhiteboard : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Whiteboards",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
WhiteboardOwnerId = table.Column<Guid>(type: "uuid", nullable: false),
Code = table.Column<string>(type: "character varying(8)", maxLength: 8, nullable: false),
Title = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Whiteboards", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Whiteboards");
}
}
}

View File

@@ -42,6 +42,30 @@ namespace AipsCore.Infrastructure.Persistence.Db.Migrations
b.ToTable("Users"); b.ToTable("Users");
}); });
modelBuilder.Entity("AipsCore.Infrastructure.Persistence.Whiteboard.Whiteboard", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("Code")
.IsRequired()
.HasMaxLength(8)
.HasColumnType("character varying(8)");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(32)
.HasColumnType("character varying(32)");
b.Property<Guid>("WhiteboardOwnerId")
.HasColumnType("uuid");
b.HasKey("Id");
b.ToTable("Whiteboards");
});
#pragma warning restore 612, 618 #pragma warning restore 612, 618
} }
} }

View File

@@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations;
namespace AipsCore.Infrastructure.Persistence.Whiteboard;
public class Whiteboard
{
[Key]
public Guid Id { get; set; }
[Required]
public Guid WhiteboardOwnerId { get; set; }
[Required]
[MaxLength(8)]
[MinLength(8)]
public string Code { get; set; } = null!;
[Required]
[MaxLength(32)]
[MinLength(3)]
public string Title { get; set; } = null!;
}

View File

@@ -0,0 +1,61 @@
using AipsCore.Domain.Models.Whiteboard.External;
using AipsCore.Domain.Models.Whiteboard.ValueObjects;
using AipsCore.Infrastructure.Persistence.Db;
using Microsoft.EntityFrameworkCore;
namespace AipsCore.Infrastructure.Persistence.Whiteboard;
public class WhiteboardRepository : IWhiteboardRepository
{
private readonly AipsDbContext _context;
public WhiteboardRepository(AipsDbContext context)
{
_context = context;
}
public async Task<Domain.Models.Whiteboard.Whiteboard?> Get(WhiteboardId whiteboardId, CancellationToken cancellationToken = default)
{
var whiteboardEntity = await _context.Whiteboards.FindAsync([new Guid(whiteboardId.IdValue), cancellationToken], cancellationToken: cancellationToken);
if (whiteboardEntity is null) return null;
return Domain.Models.Whiteboard.Whiteboard.Create(
whiteboardEntity.Id.ToString(),
whiteboardEntity.WhiteboardOwnerId.ToString(),
whiteboardEntity.Code,
whiteboardEntity.Title);
}
public async Task Save(Domain.Models.Whiteboard.Whiteboard whiteboard, CancellationToken cancellationToken = default)
{
var whiteboardEntity = await _context.Whiteboards.FindAsync(new Guid(whiteboard.Id.IdValue));
if (whiteboardEntity is not null)
{
whiteboardEntity.WhiteboardOwnerId = new Guid(whiteboard.WhiteboardOwnerId.IdValue);
whiteboardEntity.Code = whiteboard.Code.CodeValue;
whiteboardEntity.Title = whiteboard.Title.TitleValue;
_context.Whiteboards.Update(whiteboardEntity);
}
else
{
whiteboardEntity = new Whiteboard()
{
Id = new Guid(whiteboard.Id.IdValue),
WhiteboardOwnerId = new Guid(whiteboard.WhiteboardOwnerId.IdValue),
Code = whiteboard.Code.CodeValue,
Title = whiteboard.Title.TitleValue,
};
_context.Whiteboards.Add(whiteboardEntity);
}
}
public async Task<bool> WhiteboardCodeExists(WhiteboardCode whiteboardCode)
{
var codeExists = await _context.Whiteboards.AnyAsync(w => w.Code == whiteboardCode.CodeValue);
return codeExists;
}
}