Starting updating mooncore dependency usage

This commit is contained in:
2025-02-04 17:09:07 +01:00
parent 1a4864ba00
commit bf5a744499
38 changed files with 1099 additions and 748 deletions

View File

@@ -1,65 +0,0 @@
using Microsoft.EntityFrameworkCore;
using MoonCore.Exceptions;
using MoonCore.Extended.Abstractions;
using MoonCore.Extended.Helpers;
using MoonCore.Extended.OAuth2.LocalProvider;
using Moonlight.ApiServer.Database.Entities;
namespace Moonlight.ApiServer.Implementations.OAuth2;
public class LocalOAuth2Provider : ILocalProviderImplementation<User>
{
private readonly DatabaseRepository<User> UserRepository;
public LocalOAuth2Provider(DatabaseRepository<User> userRepository)
{
UserRepository = userRepository;
}
public async Task SaveChanges(User model)
{
await UserRepository.Update(model);
}
public async Task<User?> LoadById(int id)
{
var res = await UserRepository
.Get()
.FirstOrDefaultAsync(x => x.Id == id);
return res;
}
public Task<User> Login(string email, string password)
{
var user = UserRepository.Get().FirstOrDefault(x => x.Email == email);
if (user == null)
throw new HttpApiException("Invalid email or password", 400);
if(!HashHelper.Verify(password, user.Password))
throw new HttpApiException("Invalid email or password", 400);
return Task.FromResult(user);
}
public async Task<User> Register(string username, string email, string password)
{
if (UserRepository.Get().Any(x => x.Username == username))
throw new HttpApiException("A user with that username already exists", 400);
if (UserRepository.Get().Any(x => x.Email == email))
throw new HttpApiException("A user with that email address already exists", 400);
var user = new User()
{
Username = username,
Email = email,
Password = HashHelper.Hash(password)
};
var finalUser = await UserRepository.Add(user);
return finalUser;
}
}