Adding index and max length to user tabel + minor fixes

This commit is contained in:
2025-03-13 00:53:08 +01:00
parent 61b9c732bc
commit 962de4df9e
10 changed files with 409 additions and 34 deletions

View File

@@ -29,7 +29,7 @@ namespace BasicDotnetTemplate.MainProject.Tests;
[TestClass]
public class UserControllerTests
{
private IMapper _mapper;
private IMapper? _mapper;
[TestInitialize]
public void Setup()

View File

@@ -19,7 +19,7 @@ namespace BasicDotnetTemplate.MainProject.Tests;
[TestClass]
public class AutoMapperConfiguration_Tests
{
private IMapper _mapper;
private IMapper? _mapper;
[TestInitialize]
public void Setup()
@@ -53,13 +53,13 @@ public class AutoMapperConfiguration_Tests
},
IsTestUser = true
};
UserDto data = _mapper.Map<UserDto>(user);
UserDto? data = _mapper?.Map<UserDto>(user);
Assert.IsTrue(data.Guid == user.Guid);
Assert.IsTrue(data.Username == user.Username);
Assert.IsTrue(data.FirstName == user.FirstName);
Assert.IsTrue(data.LastName == user.LastName);
Assert.IsTrue(data.Email == user.Email);
Assert.IsTrue(data?.Guid == user.Guid);
Assert.IsTrue(data?.Username == user.Username);
Assert.IsTrue(data?.FirstName == user.FirstName);
Assert.IsTrue(data?.LastName == user.LastName);
Assert.IsTrue(data?.Email == user.Email);
}
catch (Exception ex)
{

View File

@@ -19,7 +19,7 @@ namespace BasicDotnetTemplate.MainProject.Tests;
[TestClass]
public class GetUserResponse_Tests
{
private IMapper _mapper;
private IMapper? _mapper;
[TestInitialize]
public void Setup()
@@ -97,7 +97,7 @@ public class GetUserResponse_Tests
},
IsTestUser = true
};
UserDto data = _mapper.Map<UserDto>(user);
UserDto? data = _mapper?.Map<UserDto>(user);
var getUserResponse = new GetUserResponse(200, "This is a test message", data);
Assert.IsTrue(getUserResponse.Status == 200 && getUserResponse.Message == "This is a test message" && getUserResponse.Data == data);
}

View File

@@ -13,6 +13,20 @@ namespace BasicDotnetTemplate.MainProject.Core.Database
}
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.HasIndex(x => x.Email, "IX_Email");
modelBuilder.Entity<User>()
.HasIndex(x => x.Username, "IX_Username");
modelBuilder.Entity<User>()
.HasIndex(x => new { x.IsDeleted, x.Guid }, "IX_IsDeleted_Guid")
.HasFilter("[IsDeleted] = 0");
}
}
}

View File

@@ -0,0 +1,170 @@
// <auto-generated />
using System;
using BasicDotnetTemplate.MainProject.Core.Database;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace MainProject.Migrations
{
[DbContext(typeof(SqlServerContext))]
[Migration("20250312234517_AlterTableUserMaxLengthIndexes")]
partial class AlterTableUserMaxLengthIndexes
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.2")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<DateTime>("CreationTime")
.HasColumnType("datetime2");
b.Property<int>("CreationUserId")
.HasColumnType("int");
b.Property<DateTime>("DeletionTime")
.HasColumnType("datetime2");
b.Property<int>("DeletionUserId")
.HasColumnType("int");
b.Property<string>("Guid")
.IsRequired()
.HasMaxLength(45)
.HasColumnType("nvarchar(45)");
b.Property<bool>("IsDeleted")
.HasColumnType("bit");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("UpdateTime")
.HasColumnType("datetime2");
b.Property<int>("UpdateUserId")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("Role");
});
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<DateTime>("CreationTime")
.HasColumnType("datetime2");
b.Property<int>("CreationUserId")
.HasColumnType("int");
b.Property<DateTime>("DeletionTime")
.HasColumnType("datetime2");
b.Property<int>("DeletionUserId")
.HasColumnType("int");
b.Property<string>("Email")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.Property<string>("FirstName")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.Property<string>("Guid")
.IsRequired()
.HasMaxLength(45)
.HasColumnType("nvarchar(45)");
b.Property<bool>("IsDeleted")
.HasColumnType("bit");
b.Property<bool>("IsTestUser")
.HasColumnType("bit");
b.Property<string>("LastName")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("PasswordHash")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("PasswordSalt")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("RoleId")
.HasColumnType("int");
b.Property<DateTime>("UpdateTime")
.HasColumnType("datetime2");
b.Property<int>("UpdateUserId")
.HasColumnType("int");
b.Property<string>("Username")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.HasKey("Id");
b.HasIndex("RoleId");
b.HasIndex(new[] { "Email" }, "IX_Email");
b.HasIndex(new[] { "IsDeleted", "Guid" }, "IX_IsDeleted_Guid")
.HasFilter("[IsDeleted] = 0");
b.HasIndex(new[] { "Username" }, "IX_Username");
b.ToTable("Users");
});
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b =>
{
b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", "Role")
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Role");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,165 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace MainProject.Migrations
{
/// <inheritdoc />
public partial class AlterTableUserMaxLengthIndexes : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string>(
name: "Username",
table: "Users",
type: "nvarchar(200)",
maxLength: 200,
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(max)");
migrationBuilder.AlterColumn<string>(
name: "LastName",
table: "Users",
type: "nvarchar(200)",
maxLength: 200,
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(max)");
migrationBuilder.AlterColumn<string>(
name: "Guid",
table: "Users",
type: "nvarchar(45)",
maxLength: 45,
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(max)");
migrationBuilder.AlterColumn<string>(
name: "FirstName",
table: "Users",
type: "nvarchar(200)",
maxLength: 200,
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(max)");
migrationBuilder.AlterColumn<string>(
name: "Email",
table: "Users",
type: "nvarchar(200)",
maxLength: 200,
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(max)");
migrationBuilder.AddColumn<bool>(
name: "IsTestUser",
table: "Users",
type: "bit",
nullable: false,
defaultValue: false);
migrationBuilder.AlterColumn<string>(
name: "Guid",
table: "Role",
type: "nvarchar(45)",
maxLength: 45,
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(max)");
migrationBuilder.CreateIndex(
name: "IX_Email",
table: "Users",
column: "Email");
migrationBuilder.CreateIndex(
name: "IX_IsDeleted_Guid",
table: "Users",
columns: new[] { "IsDeleted", "Guid" },
filter: "[IsDeleted] = 0");
migrationBuilder.CreateIndex(
name: "IX_Username",
table: "Users",
column: "Username");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "IX_Email",
table: "Users");
migrationBuilder.DropIndex(
name: "IX_IsDeleted_Guid",
table: "Users");
migrationBuilder.DropIndex(
name: "IX_Username",
table: "Users");
migrationBuilder.DropColumn(
name: "IsTestUser",
table: "Users");
migrationBuilder.AlterColumn<string>(
name: "Username",
table: "Users",
type: "nvarchar(max)",
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(200)",
oldMaxLength: 200);
migrationBuilder.AlterColumn<string>(
name: "LastName",
table: "Users",
type: "nvarchar(max)",
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(200)",
oldMaxLength: 200);
migrationBuilder.AlterColumn<string>(
name: "Guid",
table: "Users",
type: "nvarchar(max)",
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(45)",
oldMaxLength: 45);
migrationBuilder.AlterColumn<string>(
name: "FirstName",
table: "Users",
type: "nvarchar(max)",
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(200)",
oldMaxLength: 200);
migrationBuilder.AlterColumn<string>(
name: "Email",
table: "Users",
type: "nvarchar(max)",
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(200)",
oldMaxLength: 200);
migrationBuilder.AlterColumn<string>(
name: "Guid",
table: "Role",
type: "nvarchar(max)",
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(45)",
oldMaxLength: 45);
}
}
}

View File

@@ -44,7 +44,8 @@ namespace MainProject.Migrations
b.Property<string>("Guid")
.IsRequired()
.HasColumnType("nvarchar(max)");
.HasMaxLength(45)
.HasColumnType("nvarchar(45)");
b.Property<bool>("IsDeleted")
.HasColumnType("bit");
@@ -86,22 +87,29 @@ namespace MainProject.Migrations
b.Property<string>("Email")
.IsRequired()
.HasColumnType("nvarchar(max)");
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.Property<string>("FirstName")
.IsRequired()
.HasColumnType("nvarchar(max)");
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.Property<string>("Guid")
.IsRequired()
.HasColumnType("nvarchar(max)");
.HasMaxLength(45)
.HasColumnType("nvarchar(45)");
b.Property<bool>("IsDeleted")
.HasColumnType("bit");
b.Property<bool>("IsTestUser")
.HasColumnType("bit");
b.Property<string>("LastName")
.IsRequired()
.HasColumnType("nvarchar(max)");
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.Property<string>("Password")
.IsRequired()
@@ -126,12 +134,20 @@ namespace MainProject.Migrations
b.Property<string>("Username")
.IsRequired()
.HasColumnType("nvarchar(max)");
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.HasKey("Id");
b.HasIndex("RoleId");
b.HasIndex(new[] { "Email" }, "IX_Email");
b.HasIndex(new[] { "IsDeleted", "Guid" }, "IX_IsDeleted_Guid")
.HasFilter("[IsDeleted] = 0");
b.HasIndex(new[] { "Username" }, "IX_Username");
b.ToTable("Users");
});

View File

@@ -1,18 +1,22 @@
namespace BasicDotnetTemplate.MainProject.Models.Database.SqlServer
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;
namespace BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
public class Base
{
public class Base
{
public int Id { get; set; }
public string Guid { get; set; }
public bool IsDeleted { get; set; }
public DateTime CreationTime { get; set; }
public int CreationUserId { get; set; }
public DateTime UpdateTime { get; set; }
public int UpdateUserId { get; set; }
public DateTime DeletionTime { get; set; }
public int DeletionUserId { get; set; }
}
public int Id { get; set; }
[MaxLength(45)]
public string Guid { get; set; }
public bool IsDeleted { get; set; }
public DateTime CreationTime { get; set; }
public int CreationUserId { get; set; }
public DateTime UpdateTime { get; set; }
public int UpdateUserId { get; set; }
public DateTime DeletionTime { get; set; }
public int DeletionUserId { get; set; }
}

View File

@@ -1,12 +1,18 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using Microsoft.EntityFrameworkCore;
namespace BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
public class User : Base
{
[MaxLength(200)]
public required string Username { get; set; }
[MaxLength(200)]
public required string FirstName { get; set; }
[MaxLength(200)]
public required string LastName { get; set; }
[MaxLength(200)]
public required string Email { get; set; }
public required string PasswordSalt { get; set; }
public required string PasswordHash { get; set; }

View File

@@ -68,7 +68,7 @@ public class JwtTokenUtils
token = authorizations[1];
}
if(!String.IsNullOrEmpty(token))
if (!String.IsNullOrEmpty(token))
{
try
{
@@ -97,9 +97,9 @@ public class JwtTokenUtils
}
}
}
catch(Exception exception)
catch (Exception exception)
{
Logger.Error($"[JwtTokenUtils][ValidateToken] | {exception}");
Logger.Error(exception, $"[JwtTokenUtils][ValidateToken]");
return guid;
}
}