AuthController work

This commit is contained in:
Artemy
2025-06-18 16:55:19 +07:00
parent 2c2c2b805c
commit 7d518c0882
26 changed files with 397 additions and 63 deletions
@@ -0,0 +1,129 @@
// <auto-generated />
using System;
using System.Collections.Generic;
using Govor.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace Govor.Data.Migrations
{
[DbContext(typeof(GovorDbContext))]
[Migration("20250618082839_AddPasswordHashColumn")]
partial class AddPasswordHashColumn
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.6")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("Govor.Core.Models.ChatGroup", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.PrimitiveCollection<List<Guid>>("Admins")
.IsRequired()
.HasColumnType("uuid[]");
b.PrimitiveCollection<List<string>>("InviteCode")
.IsRequired()
.HasColumnType("text[]");
b.Property<bool>("IsChannel")
.HasColumnType("boolean");
b.Property<bool>("IsPrivate")
.HasColumnType("boolean");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("ChatGroups");
});
modelBuilder.Entity("Govor.Core.Models.GroupAdmins", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<Guid>("GroupId")
.HasColumnType("uuid");
b.Property<Guid>("UserId")
.HasColumnType("uuid");
b.HasKey("Id");
b.ToTable("GroupAdmins");
});
modelBuilder.Entity("Govor.Core.Models.GroupMembership", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<Guid>("GroupId")
.HasColumnType("uuid");
b.Property<bool>("IsBanned")
.HasColumnType("boolean");
b.Property<Guid>("UserId")
.HasColumnType("uuid");
b.HasKey("Id");
b.ToTable("GroupMemberships");
});
modelBuilder.Entity("Govor.Core.Models.User", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateOnly>("CreatedOn")
.HasColumnType("date");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("text");
b.Property<Guid>("IconId")
.HasColumnType("uuid");
b.Property<string>("PasswordHash")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Username")
.IsRequired()
.HasColumnType("text");
b.Property<DateTime>("WasOnline")
.HasColumnType("timestamp with time zone");
b.HasKey("Id");
b.ToTable("Users");
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,45 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Govor.Data.Migrations
{
/// <inheritdoc />
public partial class AddPasswordHashColumn : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameColumn(
name: "HashPassword",
table: "Users",
newName: "PasswordHash");
migrationBuilder.AlterColumn<DateOnly>(
name: "CreatedOn",
table: "Users",
type: "date",
nullable: false,
oldClrType: typeof(DateTime),
oldType: "timestamp with time zone");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameColumn(
name: "PasswordHash",
table: "Users",
newName: "HashPassword");
migrationBuilder.AlterColumn<DateTime>(
name: "CreatedOn",
table: "Users",
type: "timestamp with time zone",
nullable: false,
oldClrType: typeof(DateOnly),
oldType: "date");
}
}
}
@@ -95,20 +95,20 @@ namespace Govor.Data.Migrations
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateTime>("CreatedOn")
.HasColumnType("timestamp with time zone");
b.Property<DateOnly>("CreatedOn")
.HasColumnType("date");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("text");
b.Property<string>("HashPassword")
.IsRequired()
.HasColumnType("text");
b.Property<Guid>("IconId")
.HasColumnType("uuid");
b.Property<string>("PasswordHash")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Username")
.IsRequired()
.HasColumnType("text");
@@ -0,0 +1,6 @@
namespace Govor.Data.Repositories;
public class GroupRepository
{
}
+6 -5
View File
@@ -2,6 +2,7 @@ using Govor.Core.Infrastructure.Extensions;
using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models;
using Govor.Core.Repositories;
using Govor.Core.Repositories.Users;
using Govor.Data.Repositories.Exceptions;
using Microsoft.EntityFrameworkCore;
@@ -54,7 +55,7 @@ public class UsersRepository : IUsersRepository
return await _context.Users
.AsNoTracking()
.FirstOrDefaultAsync(x => x.Name == username)
.FirstOrDefaultAsync(x => x.Username == username)
?? throw new NotFoundByKeyException<string>(username, "User with given username does not exist");
}
@@ -65,7 +66,7 @@ public class UsersRepository : IUsersRepository
return await _context.Users
.AsNoTracking()
.Where(x => usernames.Contains(x.Name))
.Where(x => usernames.Contains(x.Username))
.ToListOrThrowIfEmpty(new NotFoundByKeyException<IEnumerable<string>>(usernames, "Users with given usernames not found"));
}
@@ -109,7 +110,7 @@ public class UsersRepository : IUsersRepository
var rowsAffected = await _context.Users
.Where(u => u.Id == user.Id)
.ExecuteUpdateAsync(u => u
.SetProperty(a => a.Name, user.Name)
.SetProperty(a => a.Username, user.Username)
.SetProperty(u => u.IconId, user.IconId)
.SetProperty(u => u.Description, user.Description)
.SetProperty(u => u.CreatedOn, user.CreatedOn)
@@ -168,7 +169,7 @@ public class UsersRepository : IUsersRepository
return _context.Users.AnyAsync(u =>
u.Id == user.Id &&
u.Name == user.Name &&
u.Username == user.Username &&
u.PasswordHash == user.PasswordHash
);
}
@@ -180,7 +181,7 @@ public class UsersRepository : IUsersRepository
public Task<bool> ExistsUsernameAsync(string username)
{
return _context.Users.AnyAsync(u => u.Name == username);
return _context.Users.AnyAsync(u => u.Username == username);
}
}