This commit is contained in:
2025-02-07 15:21:00 +08:00
parent 0bc2c208f9
commit a9b125233d
8 changed files with 134 additions and 72 deletions

View File

@@ -8,12 +8,12 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" /> <PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.11" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.11"> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.2">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="MySql.EntityFrameworkCore" Version="8.0.11" /> <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup> </ItemGroup>

View File

@@ -1,5 +1,4 @@
using System.Diagnostics; using AceJobAgency.Data;
using AceJobAgency.Data;
using AceJobAgency.Entities; using AceJobAgency.Entities;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@@ -12,8 +11,12 @@ namespace AceJobAgency.Controllers
[HttpPost] [HttpPost]
public async Task<IActionResult> Register(User user) public async Task<IActionResult> Register(User user)
{ {
var userExists = context.Users.Any(u => u.Email == user.Email); var userEmailExists = context.Users.Any(u => u.Email == user.Email);
if (userExists) var userNationalRegistrationIdentityCardNumberExists = context.Users.Any(
u => u.NationalRegistrationIdentityCardNumber
== user.NationalRegistrationIdentityCardNumber
);
if (userEmailExists || userNationalRegistrationIdentityCardNumberExists)
{ {
return BadRequest("User with the same email already exists."); return BadRequest("User with the same email already exists.");
} }
@@ -23,6 +26,7 @@ namespace AceJobAgency.Controllers
user.Password = passwordHash; user.Password = passwordHash;
var userId = Guid.NewGuid().ToString(); var userId = Guid.NewGuid().ToString();
user.Id = userId; user.Id = userId;
user.IsActive = 1;
await context.Users.AddAsync(user); await context.Users.AddAsync(user);
await context.SaveChangesAsync(); await context.SaveChangesAsync();

View File

@@ -12,7 +12,7 @@ namespace AceJobAgency.Data
"DefaultConnection"); "DefaultConnection");
if (connectionString != null) if (connectionString != null)
{ {
optionsBuilder.UseMySQL(connectionString); optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
} }
} }
public DbSet<User> Users { get; set; } public DbSet<User> Users { get; set; }

View File

@@ -5,6 +5,7 @@ namespace AceJobAgency.Entities
public class User public class User
{ {
[Key] [Key]
[MaxLength(36)]
public required string Id { get; set; } public required string Id { get; set; }
[Required] [Required]
@@ -20,22 +21,34 @@ namespace AceJobAgency.Entities
[Required] [Required]
[StringLength(9, MinimumLength = 9)] [StringLength(9, MinimumLength = 9)]
public required string NRIC { get; set; } public required string NationalRegistrationIdentityCardNumber { get; set; }
[Required] [Required]
[EmailAddress] [EmailAddress]
[MaxLength(128)]
public required string Email { get; set; } public required string Email { get; set; }
[Required] [Required]
[DataType(DataType.Password)] [DataType(DataType.Password)]
[MaxLength(128)]
public required string Password { get; set; } public required string Password { get; set; }
[Required] [Required]
[DataType(DataType.Date)] [DataType(DataType.Date)]
public required DateTime DateOfBirth { get; set; } public required DateTime DateOfBirth { get; set; }
[MaxLength(128)]
public required string ResumeName { get; set; } public required string ResumeName { get; set; }
public required string WhoAmI { get; set; } [MaxLength(255)]
public string WhoAmI { get; set; } = string.Empty;
public int IsActive { get; set; } = 1;
[DataType(DataType.Date)]
public DateTime CreatedAt { get; init; } = DateTime.Now;
[DataType(DataType.Date)]
public DateTime UpdatedAt { get; set; } = DateTime.Now;
} }
} }

View File

@@ -1,46 +0,0 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace AceJobAgency.Migrations
{
/// <inheritdoc />
public partial class Added_User : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterDatabase()
.Annotation("MySQL:Charset", "utf8mb4");
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<string>(type: "varchar(255)", nullable: false),
FirstName = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false),
LastName = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false),
Gender = table.Column<int>(type: "int", nullable: false),
NRIC = table.Column<string>(type: "varchar(9)", maxLength: 9, nullable: false),
Email = table.Column<string>(type: "longtext", nullable: false),
Password = table.Column<string>(type: "longtext", nullable: false),
DateOfBirth = table.Column<DateTime>(type: "datetime(6)", nullable: false),
ResumeName = table.Column<string>(type: "longtext", nullable: false),
WhoAmI = table.Column<string>(type: "longtext", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
})
.Annotation("MySQL:Charset", "utf8mb4");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Users");
}
}
}

View File

@@ -3,6 +3,7 @@ using System;
using AceJobAgency.Data; using AceJobAgency.Data;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
@@ -11,28 +12,35 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace AceJobAgency.Migrations namespace AceJobAgency.Migrations
{ {
[DbContext(typeof(DataContext))] [DbContext(typeof(DataContext))]
[Migration("20250206173504_Added_User")] [Migration("20250207071719_AddedUser")]
partial class Added_User partial class AddedUser
{ {
/// <inheritdoc /> /// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
{ {
#pragma warning disable 612, 618 #pragma warning disable 612, 618
modelBuilder modelBuilder
.HasAnnotation("ProductVersion", "8.0.11") .HasAnnotation("ProductVersion", "8.0.2")
.HasAnnotation("Relational:MaxIdentifierLength", 64); .HasAnnotation("Relational:MaxIdentifierLength", 64);
MySqlModelBuilderExtensions.AutoIncrementColumns(modelBuilder);
modelBuilder.Entity("AceJobAgency.Entities.User", b => modelBuilder.Entity("AceJobAgency.Entities.User", b =>
{ {
b.Property<string>("Id") b.Property<string>("Id")
.HasColumnType("varchar(255)"); .HasMaxLength(36)
.HasColumnType("varchar(36)");
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime(6)");
b.Property<DateTime>("DateOfBirth") b.Property<DateTime>("DateOfBirth")
.HasColumnType("datetime(6)"); .HasColumnType("datetime(6)");
b.Property<string>("Email") b.Property<string>("Email")
.IsRequired() .IsRequired()
.HasColumnType("longtext"); .HasMaxLength(128)
.HasColumnType("varchar(128)");
b.Property<string>("FirstName") b.Property<string>("FirstName")
.IsRequired() .IsRequired()
@@ -42,27 +50,36 @@ namespace AceJobAgency.Migrations
b.Property<int>("Gender") b.Property<int>("Gender")
.HasColumnType("int"); .HasColumnType("int");
b.Property<int>("IsActive")
.HasColumnType("int");
b.Property<string>("LastName") b.Property<string>("LastName")
.IsRequired() .IsRequired()
.HasMaxLength(50) .HasMaxLength(50)
.HasColumnType("varchar(50)"); .HasColumnType("varchar(50)");
b.Property<string>("NRIC") b.Property<string>("NationalRegistrationIdentityCardNumber")
.IsRequired() .IsRequired()
.HasMaxLength(9) .HasMaxLength(9)
.HasColumnType("varchar(9)"); .HasColumnType("varchar(9)");
b.Property<string>("Password") b.Property<string>("Password")
.IsRequired() .IsRequired()
.HasColumnType("longtext"); .HasMaxLength(128)
.HasColumnType("varchar(128)");
b.Property<string>("ResumeName") b.Property<string>("ResumeName")
.IsRequired() .IsRequired()
.HasColumnType("longtext"); .HasMaxLength(128)
.HasColumnType("varchar(128)");
b.Property<DateTime>("UpdatedAt")
.HasColumnType("datetime(6)");
b.Property<string>("WhoAmI") b.Property<string>("WhoAmI")
.IsRequired() .IsRequired()
.HasColumnType("longtext"); .HasMaxLength(255)
.HasColumnType("varchar(255)");
b.HasKey("Id"); b.HasKey("Id");

View File

@@ -0,0 +1,57 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace AceJobAgency.Migrations
{
/// <inheritdoc />
public partial class AddedUser : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterDatabase()
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<string>(type: "varchar(36)", maxLength: 36, nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
FirstName = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
LastName = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
Gender = table.Column<int>(type: "int", nullable: false),
NationalRegistrationIdentityCardNumber = table.Column<string>(type: "varchar(9)", maxLength: 9, nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
Email = table.Column<string>(type: "varchar(128)", maxLength: 128, nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
Password = table.Column<string>(type: "varchar(128)", maxLength: 128, nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
DateOfBirth = table.Column<DateTime>(type: "datetime(6)", nullable: false),
ResumeName = table.Column<string>(type: "varchar(128)", maxLength: 128, nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
WhoAmI = table.Column<string>(type: "varchar(255)", maxLength: 255, nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
IsActive = table.Column<int>(type: "int", nullable: false),
CreatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false),
UpdatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
})
.Annotation("MySql:CharSet", "utf8mb4");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Users");
}
}
}

View File

@@ -3,6 +3,7 @@ using System;
using AceJobAgency.Data; using AceJobAgency.Data;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable #nullable disable
@@ -16,20 +17,27 @@ namespace AceJobAgency.Migrations
{ {
#pragma warning disable 612, 618 #pragma warning disable 612, 618
modelBuilder modelBuilder
.HasAnnotation("ProductVersion", "8.0.11") .HasAnnotation("ProductVersion", "8.0.2")
.HasAnnotation("Relational:MaxIdentifierLength", 64); .HasAnnotation("Relational:MaxIdentifierLength", 64);
MySqlModelBuilderExtensions.AutoIncrementColumns(modelBuilder);
modelBuilder.Entity("AceJobAgency.Entities.User", b => modelBuilder.Entity("AceJobAgency.Entities.User", b =>
{ {
b.Property<string>("Id") b.Property<string>("Id")
.HasColumnType("varchar(255)"); .HasMaxLength(36)
.HasColumnType("varchar(36)");
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime(6)");
b.Property<DateTime>("DateOfBirth") b.Property<DateTime>("DateOfBirth")
.HasColumnType("datetime(6)"); .HasColumnType("datetime(6)");
b.Property<string>("Email") b.Property<string>("Email")
.IsRequired() .IsRequired()
.HasColumnType("longtext"); .HasMaxLength(128)
.HasColumnType("varchar(128)");
b.Property<string>("FirstName") b.Property<string>("FirstName")
.IsRequired() .IsRequired()
@@ -39,27 +47,36 @@ namespace AceJobAgency.Migrations
b.Property<int>("Gender") b.Property<int>("Gender")
.HasColumnType("int"); .HasColumnType("int");
b.Property<int>("IsActive")
.HasColumnType("int");
b.Property<string>("LastName") b.Property<string>("LastName")
.IsRequired() .IsRequired()
.HasMaxLength(50) .HasMaxLength(50)
.HasColumnType("varchar(50)"); .HasColumnType("varchar(50)");
b.Property<string>("NRIC") b.Property<string>("NationalRegistrationIdentityCardNumber")
.IsRequired() .IsRequired()
.HasMaxLength(9) .HasMaxLength(9)
.HasColumnType("varchar(9)"); .HasColumnType("varchar(9)");
b.Property<string>("Password") b.Property<string>("Password")
.IsRequired() .IsRequired()
.HasColumnType("longtext"); .HasMaxLength(128)
.HasColumnType("varchar(128)");
b.Property<string>("ResumeName") b.Property<string>("ResumeName")
.IsRequired() .IsRequired()
.HasColumnType("longtext"); .HasMaxLength(128)
.HasColumnType("varchar(128)");
b.Property<DateTime>("UpdatedAt")
.HasColumnType("datetime(6)");
b.Property<string>("WhoAmI") b.Property<string>("WhoAmI")
.IsRequired() .IsRequired()
.HasColumnType("longtext"); .HasMaxLength(255)
.HasColumnType("varchar(255)");
b.HasKey("Id"); b.HasKey("Id");