Minor fixes for CreateUser

This commit is contained in:
2025-03-16 23:26:15 +01:00
parent 18e713153b
commit f6cd629fe2
4 changed files with 53 additions and 11 deletions

View File

@@ -13,6 +13,7 @@ public interface IRoleService
Task<Role?> GetRoleByGuidAsync(string guid);
Task<bool> CheckIfNameIsValid(string name, string? guid = "");
Task<Role?> CreateRole(CreateRoleRequestData data);
Task<Role?> GetRoleForUser(string? guid);
}
public class RoleService : BaseService, IRoleService
@@ -36,6 +37,8 @@ public class RoleService : BaseService, IRoleService
);
}
private Role CreateRoleData(CreateRoleRequestData data)
{
Role role = new()
@@ -52,6 +55,9 @@ public class RoleService : BaseService, IRoleService
}
public async Task<Role?> GetRoleByIdAsync(int id)
{
return await this.GetRolesQueryable().Where(x => x.Id == id).FirstOrDefaultAsync();
@@ -98,6 +104,20 @@ public class RoleService : BaseService, IRoleService
return role;
}
public async Task<Role?> GetRoleForUser(string? guid)
{
Role? role = null;
if (String.IsNullOrEmpty(guid))
{
role = await this.GetRoleByNameQueryable("Default").FirstOrDefaultAsync();
}
else
{
role = await this.GetRoleByGuidAsync(guid);
}
return role;
}
}

View File

@@ -13,7 +13,7 @@ public interface IUserService
Task<User?> GetUserByGuidAsync(string guid);
Task<User?> GetUserByUsernameAndPassword(string email, string password);
Task<bool> CheckIfEmailIsValid(string email, string? guid = "");
Task<User?> CreateUser(CreateUserRequestData data, Role role);
Task<User?> CreateUserAsync(CreateUserRequestData data, Role role);
}
public class UserService : BaseService, IUserService
@@ -102,16 +102,19 @@ public class UserService : BaseService, IUserService
return valid;
}
public async Task<User?> CreateUser(CreateUserRequestData data, Role role)
public async Task<User?> CreateUserAsync(CreateUserRequestData data, Role role)
{
User? user = null;
using (var transaction = _sqlServerContext.Database.BeginTransactionAsync())
{
var tempUser = this.CreateUserData(data, role);
await _sqlServerContext.Users.AddAsync(tempUser);
await _sqlServerContext.SaveChangesAsync();
await (await transaction).CommitAsync();
user = tempUser;
}
return user;
}