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,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);
}
}