initial commit

This commit is contained in:
Yui
2025-11-26 16:50:06 -03:00
commit 5644aa0ebf
47 changed files with 800 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,12 @@
using Application.Interfaces.Services;
using Application.Services;
using Microsoft.Extensions.DependencyInjection;
namespace Application;
public static class DependencyInjection
{
public static IServiceCollection AddApplication(this IServiceCollection services) =>
services.AddScoped<IUserService, UserService>()
.AddScoped<IKillService, KillService>();
}

View File

@@ -0,0 +1,8 @@
using Domain.Enums;
namespace Application.Exceptions;
public class BaseException(EErrorCategory category, string message) : Exception(message)
{
public readonly EErrorCategory Category = category;
}

View File

@@ -0,0 +1,8 @@
using Domain.Enums;
namespace Application.Exceptions;
public class DuplicateEmailException(string email) : BaseException(EErrorCategory.UserValidationError, $"Email {email} already registered")
{
}

View File

@@ -0,0 +1,8 @@
using Domain.Enums;
namespace Application.Exceptions;
public class DuplicateUsernameException(string? username) : BaseException(EErrorCategory.UserValidationError, $"Username {username} is taken")
{
}

View File

@@ -0,0 +1,8 @@
using Domain.Enums;
namespace Application.Exceptions;
public class InsufficientParametersException(List<string> missingParameters) : BaseException(EErrorCategory.RequestError, "Insufficient Parameters: " + string.Join(", ", missingParameters))
{
}

View File

@@ -0,0 +1,8 @@
using Domain.Enums;
namespace Application.Exceptions;
public class UserNotFoundException(int id) : BaseException(EErrorCategory.DataNotFoundError,$"User with ID {id} not found.")
{
}

View File

@@ -0,0 +1,12 @@
using Domain.Entities;
namespace Application.Interfaces.Repositories;
public interface IKillRepository
{
public IEnumerable<Kill> GetAllKills();
public bool TryGetKillById(int id, out Kill? kill);
public void AddKill(Kill kill);
public bool RemoveKill(Kill kill);
}

View File

@@ -0,0 +1,11 @@
using Domain.Entities;
namespace Application.Interfaces.Repositories;
public interface IUserRepository
{
public IEnumerable<User> GetAllUsers();
public bool TryGetUserById(int id, out User? user);
public bool AddUser(User user);
public bool UpdateUser(User user);
}

View File

@@ -0,0 +1,14 @@
using Application.Interfaces.Repositories;
using Domain.Entities;
namespace Application.Interfaces.Services;
public interface IKillService
{
public IEnumerable<Kill> GetAllKills();
public Kill GetKillById(int id);
public IEnumerable<Kill> GetKillsByUserId(int userId);
public IEnumerable<Kill> GetKillsByUserId(IEnumerable<int> userIds);
public bool AddKill(Kill kill);
public bool RemoveKill(Kill kill);
}

View File

@@ -0,0 +1,12 @@
using Application.Interfaces.Repositories;
using Domain.Entities;
namespace Application.Interfaces.Services;
public interface IUserService
{
public IEnumerable<User> GetAllUsers();
public User GetUserById(int id);
public bool CreateUser(string username, string password, string email);
public bool UpdateUser(int id, string username, string password, string email);
}

View File

@@ -0,0 +1,48 @@
using Application.Interfaces.Repositories;
using Application.Interfaces.Services;
using Domain.Entities;
namespace Application.Services;
public class KillService(IKillRepository repository, IUserService userService) : IKillService
{
private IKillRepository Repository { get; } = repository;
public IEnumerable<Kill> GetAllKills()
{
return Repository.GetAllKills();
}
public Kill GetKillById(int id)
{
if (!Repository.TryGetKillById(id, out var kill))
{
throw new KeyNotFoundException($"Failed to find kill with id: {id}");
}
return kill;
}
public IEnumerable<Kill> GetKillsByUserId(int userId)
{
return Repository.GetAllKills().Where(x => x.UserId == userId);
}
public IEnumerable<Kill> GetKillsByUserId(IEnumerable<int> userIds)
{
return Repository.GetAllKills().Where(x => userIds.Contains(x.UserId));
}
public bool AddKill(Kill kill)
{
Repository.AddKill(kill);
var user = userService.GetUserById(kill.UserId);
user.Kills.Append(kill);
return true;
}
public bool RemoveKill(Kill kill)
{
return Repository.RemoveKill(kill);
}
}

View File

@@ -0,0 +1,71 @@
using Application.Exceptions;
using Application.Interfaces.Repositories;
using Application.Interfaces.Services;
using Domain.Entities;
namespace Application.Services;
public class UserService(IUserRepository repository) : IUserService
{
private IUserRepository Repository { get; } = repository;
public IEnumerable<User> GetAllUsers()
{
return Repository.GetAllUsers();
}
public User GetUserById(int id)
{
if (!Repository.TryGetUserById(id, out var user))
{
throw new ArgumentException($"User with id {id} not found");
}
return user;
}
public bool CreateUser(string username, string password, string email)
{
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(email))
{
throw new InsufficientParametersException(["username", "password", "email"]);
}
if (Repository.GetAllUsers().Any(x => x.Username == username))
{
throw new DuplicateUsernameException(username);
}
if (Repository.GetAllUsers().Any(x => x.Email == email))
{
throw new DuplicateEmailException(email);
}
var user = new User
{
Id = Repository.GetAllUsers().Count() + 1,
Username = username,
Password = password,
Email = email
};
return Repository.AddUser(user);
}
public bool UpdateUser(int id, string username, string password, string email)
{
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(email))
{
throw new InsufficientParametersException(["username", "password", "email"]);
}
if (!Repository.TryGetUserById(id, out var user))
{
throw new UserNotFoundException(id);
}
user.Email = email;
user.Password = password;
user.Username = username;
return Repository.UpdateUser(user);
}
}