initial commit
This commit is contained in:
18
Application/Application.csproj
Normal file
18
Application/Application.csproj
Normal 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>
|
||||
12
Application/DependencyInjection.cs
Normal file
12
Application/DependencyInjection.cs
Normal 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>();
|
||||
}
|
||||
8
Application/Exceptions/BaseException.cs
Normal file
8
Application/Exceptions/BaseException.cs
Normal 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;
|
||||
}
|
||||
8
Application/Exceptions/DuplicateEmailException.cs
Normal file
8
Application/Exceptions/DuplicateEmailException.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using Domain.Enums;
|
||||
|
||||
namespace Application.Exceptions;
|
||||
|
||||
public class DuplicateEmailException(string email) : BaseException(EErrorCategory.UserValidationError, $"Email {email} already registered")
|
||||
{
|
||||
|
||||
}
|
||||
8
Application/Exceptions/DuplicateUsernameException.cs
Normal file
8
Application/Exceptions/DuplicateUsernameException.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using Domain.Enums;
|
||||
|
||||
namespace Application.Exceptions;
|
||||
|
||||
public class DuplicateUsernameException(string? username) : BaseException(EErrorCategory.UserValidationError, $"Username {username} is taken")
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using Domain.Enums;
|
||||
|
||||
namespace Application.Exceptions;
|
||||
|
||||
public class InsufficientParametersException(List<string> missingParameters) : BaseException(EErrorCategory.RequestError, "Insufficient Parameters: " + string.Join(", ", missingParameters))
|
||||
{
|
||||
|
||||
}
|
||||
8
Application/Exceptions/UserNotFoundException.cs
Normal file
8
Application/Exceptions/UserNotFoundException.cs
Normal 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.")
|
||||
{
|
||||
|
||||
}
|
||||
12
Application/Interfaces/Repositories/IKillRepository.cs
Normal file
12
Application/Interfaces/Repositories/IKillRepository.cs
Normal 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);
|
||||
}
|
||||
11
Application/Interfaces/Repositories/IUserRepository.cs
Normal file
11
Application/Interfaces/Repositories/IUserRepository.cs
Normal 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);
|
||||
}
|
||||
14
Application/Interfaces/Services/IKillService.cs
Normal file
14
Application/Interfaces/Services/IKillService.cs
Normal 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);
|
||||
}
|
||||
12
Application/Interfaces/Services/IUserService.cs
Normal file
12
Application/Interfaces/Services/IUserService.cs
Normal 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);
|
||||
}
|
||||
48
Application/Services/KillService.cs
Normal file
48
Application/Services/KillService.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
71
Application/Services/UserService.cs
Normal file
71
Application/Services/UserService.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user