add: Сервисы пользователя

This commit is contained in:
mfnefd 2024-11-25 16:59:13 +04:00
parent 1e107f86ec
commit bc7d0ff156
12 changed files with 163 additions and 8 deletions

View File

@ -2,8 +2,8 @@ namespace Contracts.DTO;
public class UserDto
{
public Guid? Id { get; set; }
public string? Username { get; set; }
public string? Password { get; set; }
public decimal? Balance { get; set; }
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public decimal Balance { get; set; }
}

View File

@ -0,0 +1,7 @@
namespace Contracts.DTO;
public class UserLoginDTO
{
public string Name { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
}

View File

@ -0,0 +1,16 @@
using Contracts.DTO;
using Contracts.ViewModels;
namespace Contracts.Mappers;
public static class UserMapper
{
public static UserViewModel ToView(this UserDto user)
=> new()
{
Id = user.Id,
Name = user.Name,
Balance = user.Balance
};
}

View File

@ -5,7 +5,7 @@ namespace Contracts.Repositories;
public interface IUserRepo
{
public Task<UserDto> Get(UserSearch search);
public Task<UserDto?> Get(UserSearch search);
public Task<UserDto> Create(UserDto user);
public Task<UserDto> Update(UserDto user);
public Task<UserDto> Delete(UserSearch search);

View File

@ -3,4 +3,5 @@ namespace Contracts.SearchModels;
public class UserSearch
{
public Guid? Id { get; set; }
public string? Name { get; set; }
}

View File

@ -1,12 +1,11 @@
using Contracts.DTO;
using Contracts.SearchModels;
using Contracts.ViewModels;
namespace Contracts.Services;
public interface IAuthService
{
public Task<UserViewModel> Login();
public Task<UserViewModel> Login(UserLoginDTO loginData);
public Task<UserViewModel> Register(UserDto user);
public Task<UserViewModel> UpdateUserData(UserDto user);
public Task<UserViewModel> Delete(Guid id);
}

View File

@ -0,0 +1,12 @@
using Contracts.DTO;
using Contracts.SearchModels;
using Contracts.ViewModels;
namespace Contracts.Services;
public interface IUserService
{
public Task<UserViewModel> GetDetails(UserSearch search);
public Task<UserViewModel> UpdateUserData(UserDto user);
public Task<UserViewModel> Delete(UserSearch search);
}

View File

@ -0,0 +1,49 @@
using Contracts.DTO;
using Contracts.Mappers;
using Contracts.Repositories;
using Contracts.SearchModels;
using Contracts.Services;
using Contracts.ViewModels;
using Services.Support.Exceptions;
namespace Services.Domain;
public class AuthService : IAuthService
{
private readonly IUserRepo _userRepo;
public AuthService(IUserRepo userRepo)
{
_userRepo = userRepo;
}
public async Task<UserViewModel> Login(UserLoginDTO loginData)
{
if (loginData == null || string.IsNullOrWhiteSpace(loginData.Name)
|| string.IsNullOrWhiteSpace(loginData.Password))
{
throw new ArgumentException("Неверные данные для входа");
}
var user = await _userRepo.Get(new UserSearch() { Name = loginData.Name });
if (user == null)
{
throw new UserNotFoundException($"Пользователь {loginData.Name} не найден");
}
return user.ToView();
}
public async Task<UserViewModel> Register(UserDto user)
{
var existingUser = await _userRepo.Get(new UserSearch() { Name = user.Name });
if (existingUser != null)
{
throw new AlreadyExistsException("Такой пользователь уже существует");
}
var createdUser = await _userRepo.Create(user);
return createdUser.ToView();
}
}

View File

@ -0,0 +1,47 @@
using Contracts.DTO;
using Contracts.Mappers;
using Contracts.Repositories;
using Contracts.SearchModels;
using Contracts.Services;
using Contracts.ViewModels;
using Services.Support.Exceptions;
namespace Services.Domain;
public class UserService : IUserService
{
private readonly IUserRepo _userRepo;
public UserService(IUserRepo userRepo)
{
_userRepo = userRepo;
}
public async Task<UserViewModel> Delete(UserSearch search)
{
var user = await _userRepo.Delete(search);
return user.ToView();
}
public async Task<UserViewModel> GetDetails(UserSearch search)
{
var user = await _userRepo.Get(search);
if (user == null)
{
throw new UserNotFoundException($"Пользователь {search.Name} не найден");
}
return user.ToView();
}
public async Task<UserViewModel> UpdateUserData(UserDto user)
{
var existingUser = await _userRepo.Get(new UserSearch() { Name = user.Name });
if (existingUser == null)
{
throw new UserNotFoundException($"Пользователь {user.Name} не найден");
}
var updatedUser = await _userRepo.Update(user);
return updatedUser.ToView();
}
}

View File

@ -1,5 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\Contracts\Contracts.csproj" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>

View File

@ -0,0 +1,9 @@
namespace Services.Support.Exceptions;
public class AlreadyExistsException : Exception
{
public AlreadyExistsException(string message) : base(message) { }
public AlreadyExistsException(string message, Exception innerException)
: base(message, innerException) { }
}

View File

@ -0,0 +1,11 @@
using Contracts.SearchModels;
namespace Services.Support.Exceptions;
public class UserNotFoundException : Exception
{
public UserNotFoundException(string message)
: base(message) { }
public UserNotFoundException(string message, Exception innerException)
: base(message, innerException) { }
}