45 lines
1.6 KiB
C#
45 lines
1.6 KiB
C#
using FictionArchive.Service.UserService.Models.Database;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace FictionArchive.Service.UserService.Services;
|
|
|
|
public class UserManagementService
|
|
{
|
|
private readonly ILogger<UserManagementService> _logger;
|
|
private readonly UserServiceDbContext _dbContext;
|
|
|
|
public UserManagementService(UserServiceDbContext dbContext, ILogger<UserManagementService> logger)
|
|
{
|
|
_dbContext = dbContext;
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task<User> RegisterUser(string username, string email, string oAuthProviderId,
|
|
string? inviterOAuthProviderId)
|
|
{
|
|
var newUser = new User();
|
|
User? inviter =
|
|
await _dbContext.Users.FirstOrDefaultAsync(user => user.OAuthProviderId == inviterOAuthProviderId);
|
|
if (inviter == null && inviterOAuthProviderId != null)
|
|
{
|
|
_logger.LogCritical(
|
|
"A user with OAuthProviderId {OAuthProviderId} was marked as having inviter with OAuthProviderId {inviterOAuthProviderId}, but no user was found with that value.",
|
|
inviterOAuthProviderId, inviterOAuthProviderId);
|
|
newUser.Disabled = true;
|
|
}
|
|
|
|
newUser.Username = username;
|
|
newUser.Email = email;
|
|
newUser.OAuthProviderId = oAuthProviderId;
|
|
|
|
_dbContext.Users.Add(newUser); // Add the new user to the DbContext
|
|
await _dbContext.SaveChangesAsync(); // Save changes to the database
|
|
|
|
return newUser;
|
|
}
|
|
|
|
public IQueryable<User> GetUsers()
|
|
{
|
|
return _dbContext.Users.AsQueryable();
|
|
}
|
|
} |