initial commit
This commit is contained in:
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