Adding role creation during startup + minor fixes in tests
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
|
||||
using System.Collections;
|
||||
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Data.User;
|
||||
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
@@ -10,65 +11,109 @@ public interface IUserService
|
||||
{
|
||||
Task<User?> GetUserByIdAsync(int id);
|
||||
Task<User?> GetUserByGuidAsync(string guid);
|
||||
Task<User?> GetUserByUsernameAndPassword(string username, string password);
|
||||
Task<User?> GetUserByUsernameAndPassword(string email, string password);
|
||||
Task<bool> CheckIfEmailIsValid(string email, string? guid = "");
|
||||
Task<User?> CreateUser(CreateUserRequestData data, Role role);
|
||||
}
|
||||
|
||||
public class UserService : BaseService, IUserService
|
||||
{
|
||||
|
||||
public UserService(
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
IConfiguration configuration,
|
||||
SqlServerContext sqlServerContext
|
||||
) : base(configuration, sqlServerContext)
|
||||
) : base(httpContextAccessor, configuration, sqlServerContext)
|
||||
{ }
|
||||
|
||||
private IQueryable<User> GetUsers()
|
||||
private IQueryable<User> GetUsersQueryable()
|
||||
{
|
||||
return this._sqlServerContext.Users.Where(x => !x.IsDeleted);
|
||||
}
|
||||
|
||||
private IQueryable<User> GetUserByUsername(string username)
|
||||
private IQueryable<User> GetUserByEmailQueryable(string email)
|
||||
{
|
||||
return this.GetUsers().Where(x =>
|
||||
x.Username.ToString() == username.ToString()
|
||||
return this.GetUsersQueryable().Where(x =>
|
||||
x.Email.ToString() == email.ToString()
|
||||
);
|
||||
}
|
||||
|
||||
public async Task<User?> GetUserByIdAsync(int id)
|
||||
private User CreateUserData(CreateUserRequestData data, Role role)
|
||||
{
|
||||
return await this.GetUsers().Where(x => x.Id == id).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<User?> GetUserByGuidAsync(string guid)
|
||||
{
|
||||
return await this.GetUsers().Where(x => x.Guid == guid).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<User?> GetUserByUsernameAndPassword(string username, string password)
|
||||
{
|
||||
User? user = null;
|
||||
|
||||
try
|
||||
User user = new()
|
||||
{
|
||||
user = await this.GetUserByUsername(username).FirstOrDefaultAsync();
|
||||
if (user != null)
|
||||
{
|
||||
var encryptedPassword = user.PasswordHash;
|
||||
Console.WriteLine(encryptedPassword);
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Console.WriteLine(exception.Message);
|
||||
}
|
||||
CreationTime = DateTime.UtcNow,
|
||||
CreationUserId = this.GetCurrentUserId(),
|
||||
IsDeleted = false,
|
||||
Guid = Guid.NewGuid().ToString(),
|
||||
FirstName = data.FirstName,
|
||||
LastName = data.LastName,
|
||||
Email = data.Email,
|
||||
PasswordSalt = "",
|
||||
PasswordHash = "",
|
||||
Password = "",
|
||||
Role = role,
|
||||
IsTestUser = false
|
||||
};
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
// public async Task<User?> CreateUser(CreateUserRequestData data)
|
||||
// {
|
||||
|
||||
// }
|
||||
public async Task<User?> GetUserByIdAsync(int id)
|
||||
{
|
||||
return await this.GetUsersQueryable().Where(x => x.Id == id).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<User?> GetUserByGuidAsync(string guid)
|
||||
{
|
||||
return await this.GetUsersQueryable().Where(x => x.Guid == guid).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<User?> GetUserByUsernameAndPassword(string email, string password)
|
||||
{
|
||||
User? user = await this.GetUserByEmailQueryable(email).FirstOrDefaultAsync();
|
||||
if (user != null)
|
||||
{
|
||||
var encryptedPassword = user.PasswordHash;
|
||||
Console.WriteLine(encryptedPassword);
|
||||
}
|
||||
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
public async Task<bool> CheckIfEmailIsValid(string email, string? guid = "")
|
||||
{
|
||||
var valid = false;
|
||||
|
||||
User? user = await this.GetUserByEmailQueryable(email).FirstOrDefaultAsync();
|
||||
if (user != null)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(guid))
|
||||
{
|
||||
valid = user.Guid == guid && user.Email == email;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
valid = true;
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
|
||||
public async Task<User?> CreateUser(CreateUserRequestData data, Role role)
|
||||
{
|
||||
User? user = null;
|
||||
|
||||
using (var transaction = _sqlServerContext.Database.BeginTransactionAsync())
|
||||
{
|
||||
var tempUser = this.CreateUserData(data, role);
|
||||
}
|
||||
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user