diff --git a/DrinkRateAPI/DbEntities/DbApplicationUser.cs b/DrinkRateAPI/DbEntities/DbApplicationUser.cs index 77f1b46..c9fc20d 100644 --- a/DrinkRateAPI/DbEntities/DbApplicationUser.cs +++ b/DrinkRateAPI/DbEntities/DbApplicationUser.cs @@ -1,8 +1,52 @@ +using DrinkRateAPI.Contexts; using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.Options; namespace DrinkRateAPI.DbEntities; public class DbApplicationUser : IdentityUser { public virtual DbUserProfile UserProfile { get; set; } +} + +public class UserWithProfileManager : UserManager +{ + private readonly ApplicationDbContext _context; + + public UserWithProfileManager( + IUserStore store, + IOptions optionsAccessor, + IPasswordHasher passwordHasher, + IEnumerable> userValidators, + IEnumerable> passwordValidators, + ILookupNormalizer keyNormalizer, + IdentityErrorDescriber errors, + IServiceProvider services, + ILogger> logger, + ApplicationDbContext context) + : base(store, optionsAccessor, passwordHasher, userValidators, + passwordValidators, keyNormalizer, errors, services, logger) + { + _context = context; + } + + public override async Task CreateAsync(DbApplicationUser user, string password) + { + var result = await base.CreateAsync(user, password); + if (!result.Succeeded) + return result; + + var newProfile = new DbUserProfile + { + ApplicationUser = user, + UserName = $"User_{user.Id}", + IsAdmin = false, + IsDeleted = false + }; + + await _context.UserProfiles.AddAsync(newProfile); + await _context.SaveChangesAsync(); + + return result; + } } \ No newline at end of file diff --git a/DrinkRateAPI/Migrations/20250809154816_ChangeApplicationUserIdType.Designer.cs b/DrinkRateAPI/Migrations/20250809165046_UserCreationDemo.Designer.cs similarity index 98% rename from DrinkRateAPI/Migrations/20250809154816_ChangeApplicationUserIdType.Designer.cs rename to DrinkRateAPI/Migrations/20250809165046_UserCreationDemo.Designer.cs index 4c567e6..60e2e62 100644 --- a/DrinkRateAPI/Migrations/20250809154816_ChangeApplicationUserIdType.Designer.cs +++ b/DrinkRateAPI/Migrations/20250809165046_UserCreationDemo.Designer.cs @@ -12,8 +12,8 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace DrinkRateAPI.Migrations { [DbContext(typeof(ApplicationDbContext))] - [Migration("20250809154816_ChangeApplicationUserIdType")] - partial class ChangeApplicationUserIdType + [Migration("20250809165046_UserCreationDemo")] + partial class UserCreationDemo { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -347,7 +347,7 @@ namespace DrinkRateAPI.Migrations modelBuilder.Entity("DrinkRateAPI.DbEntities.DbUserProfileCompanyTableStat", b => { - b.Property("UserId") + b.Property("UserProfileId") .HasColumnType("uuid"); b.Property("CompanyTableId") @@ -362,7 +362,7 @@ namespace DrinkRateAPI.Migrations b.Property("RatingCount") .HasColumnType("integer"); - b.HasKey("UserId", "CompanyTableId"); + b.HasKey("UserProfileId", "CompanyTableId"); b.HasIndex("CompanyTableId"); @@ -371,7 +371,7 @@ namespace DrinkRateAPI.Migrations modelBuilder.Entity("DrinkRateAPI.DbEntities.DbUserProfileProductTableStat", b => { - b.Property("UserId") + b.Property("UserProfileId") .HasColumnType("uuid"); b.Property("ProductTableId") @@ -386,7 +386,7 @@ namespace DrinkRateAPI.Migrations b.Property("RatingCount") .HasColumnType("integer"); - b.HasKey("UserId", "ProductTableId"); + b.HasKey("UserProfileId", "ProductTableId"); b.HasIndex("ProductTableId"); @@ -672,7 +672,7 @@ namespace DrinkRateAPI.Migrations b.HasOne("DrinkRateAPI.DbEntities.DbUserProfile", "UserProfile") .WithMany("UserProfileCompanyTableStats") - .HasForeignKey("UserId") + .HasForeignKey("UserProfileId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -691,7 +691,7 @@ namespace DrinkRateAPI.Migrations b.HasOne("DrinkRateAPI.DbEntities.DbUserProfile", "UserProfile") .WithMany("UserProfileProductTableStats") - .HasForeignKey("UserId") + .HasForeignKey("UserProfileId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); diff --git a/DrinkRateAPI/Migrations/20250809154816_ChangeApplicationUserIdType.cs b/DrinkRateAPI/Migrations/20250809165046_UserCreationDemo.cs similarity index 98% rename from DrinkRateAPI/Migrations/20250809154816_ChangeApplicationUserIdType.cs rename to DrinkRateAPI/Migrations/20250809165046_UserCreationDemo.cs index 348973d..978d5ea 100644 --- a/DrinkRateAPI/Migrations/20250809154816_ChangeApplicationUserIdType.cs +++ b/DrinkRateAPI/Migrations/20250809165046_UserCreationDemo.cs @@ -7,7 +7,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace DrinkRateAPI.Migrations { /// - public partial class ChangeApplicationUserIdType : Migration + public partial class UserCreationDemo : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -345,7 +345,7 @@ namespace DrinkRateAPI.Migrations name: "UserProfileCompanyTableStats", columns: table => new { - UserId = table.Column(type: "uuid", nullable: false), + UserProfileId = table.Column(type: "uuid", nullable: false), CompanyTableId = table.Column(type: "uuid", nullable: false), RatingCount = table.Column(type: "integer", nullable: false), HighestRatingCount = table.Column(type: "integer", nullable: false), @@ -353,7 +353,7 @@ namespace DrinkRateAPI.Migrations }, constraints: table => { - table.PrimaryKey("PK_UserProfileCompanyTableStats", x => new { x.UserId, x.CompanyTableId }); + table.PrimaryKey("PK_UserProfileCompanyTableStats", x => new { x.UserProfileId, x.CompanyTableId }); table.ForeignKey( name: "FK_UserProfileCompanyTableStats_CompanyTables_CompanyTableId", column: x => x.CompanyTableId, @@ -361,8 +361,8 @@ namespace DrinkRateAPI.Migrations principalColumn: "Id", onDelete: ReferentialAction.Cascade); table.ForeignKey( - name: "FK_UserProfileCompanyTableStats_UserProfiles_UserId", - column: x => x.UserId, + name: "FK_UserProfileCompanyTableStats_UserProfiles_UserProfileId", + column: x => x.UserProfileId, principalTable: "UserProfiles", principalColumn: "Id", onDelete: ReferentialAction.Cascade); @@ -372,7 +372,7 @@ namespace DrinkRateAPI.Migrations name: "UserProfileProductTableStats", columns: table => new { - UserId = table.Column(type: "uuid", nullable: false), + UserProfileId = table.Column(type: "uuid", nullable: false), ProductTableId = table.Column(type: "uuid", nullable: false), RatingCount = table.Column(type: "integer", nullable: false), HighestRatingCount = table.Column(type: "integer", nullable: false), @@ -380,7 +380,7 @@ namespace DrinkRateAPI.Migrations }, constraints: table => { - table.PrimaryKey("PK_UserProfileProductTableStats", x => new { x.UserId, x.ProductTableId }); + table.PrimaryKey("PK_UserProfileProductTableStats", x => new { x.UserProfileId, x.ProductTableId }); table.ForeignKey( name: "FK_UserProfileProductTableStats_ProductTable_ProductTableId", column: x => x.ProductTableId, @@ -388,8 +388,8 @@ namespace DrinkRateAPI.Migrations principalColumn: "Id", onDelete: ReferentialAction.Cascade); table.ForeignKey( - name: "FK_UserProfileProductTableStats_UserProfiles_UserId", - column: x => x.UserId, + name: "FK_UserProfileProductTableStats_UserProfiles_UserProfileId", + column: x => x.UserProfileId, principalTable: "UserProfiles", principalColumn: "Id", onDelete: ReferentialAction.Cascade); diff --git a/DrinkRateAPI/Migrations/ApplicationDbContextModelSnapshot.cs b/DrinkRateAPI/Migrations/ApplicationDbContextModelSnapshot.cs index fabb4ed..c25953b 100644 --- a/DrinkRateAPI/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/DrinkRateAPI/Migrations/ApplicationDbContextModelSnapshot.cs @@ -344,7 +344,7 @@ namespace DrinkRateAPI.Migrations modelBuilder.Entity("DrinkRateAPI.DbEntities.DbUserProfileCompanyTableStat", b => { - b.Property("UserId") + b.Property("UserProfileId") .HasColumnType("uuid"); b.Property("CompanyTableId") @@ -359,7 +359,7 @@ namespace DrinkRateAPI.Migrations b.Property("RatingCount") .HasColumnType("integer"); - b.HasKey("UserId", "CompanyTableId"); + b.HasKey("UserProfileId", "CompanyTableId"); b.HasIndex("CompanyTableId"); @@ -368,7 +368,7 @@ namespace DrinkRateAPI.Migrations modelBuilder.Entity("DrinkRateAPI.DbEntities.DbUserProfileProductTableStat", b => { - b.Property("UserId") + b.Property("UserProfileId") .HasColumnType("uuid"); b.Property("ProductTableId") @@ -383,7 +383,7 @@ namespace DrinkRateAPI.Migrations b.Property("RatingCount") .HasColumnType("integer"); - b.HasKey("UserId", "ProductTableId"); + b.HasKey("UserProfileId", "ProductTableId"); b.HasIndex("ProductTableId"); @@ -669,7 +669,7 @@ namespace DrinkRateAPI.Migrations b.HasOne("DrinkRateAPI.DbEntities.DbUserProfile", "UserProfile") .WithMany("UserProfileCompanyTableStats") - .HasForeignKey("UserId") + .HasForeignKey("UserProfileId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -688,7 +688,7 @@ namespace DrinkRateAPI.Migrations b.HasOne("DrinkRateAPI.DbEntities.DbUserProfile", "UserProfile") .WithMany("UserProfileProductTableStats") - .HasForeignKey("UserId") + .HasForeignKey("UserProfileId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); diff --git a/DrinkRateAPI/Program.cs b/DrinkRateAPI/Program.cs index 7d4ddbc..1faf6d8 100644 --- a/DrinkRateAPI/Program.cs +++ b/DrinkRateAPI/Program.cs @@ -1,5 +1,6 @@ using DrinkRateAPI.Contexts; using DrinkRateAPI.DbEntities; +using Microsoft.AspNetCore.Identity; using Microsoft.OpenApi.Models; var builder = WebApplication.CreateBuilder(args); @@ -12,6 +13,7 @@ builder.Services.AddEndpointsApiExplorer(); builder.Services.AddAuthorization(); builder.Services.AddIdentityApiEndpoints() .AddEntityFrameworkStores(); +builder.Services.AddScoped, UserWithProfileManager>(); builder.Services.AddSwaggerGen(c => {