add: Сервисы пользователя
This commit is contained in:
parent
1e107f86ec
commit
bc7d0ff156
@ -2,8 +2,8 @@ namespace Contracts.DTO;
|
|||||||
|
|
||||||
public class UserDto
|
public class UserDto
|
||||||
{
|
{
|
||||||
public Guid? Id { get; set; }
|
public Guid Id { get; set; }
|
||||||
public string? Username { get; set; }
|
public string Name { get; set; } = string.Empty;
|
||||||
public string? Password { get; set; }
|
public string Password { get; set; } = string.Empty;
|
||||||
public decimal? Balance { get; set; }
|
public decimal Balance { get; set; }
|
||||||
}
|
}
|
7
back/Contracts/DTOs/UserLoginDTO.cs
Normal file
7
back/Contracts/DTOs/UserLoginDTO.cs
Normal 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;
|
||||||
|
}
|
16
back/Contracts/Mappers/UserMapper.cs
Normal file
16
back/Contracts/Mappers/UserMapper.cs
Normal 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
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
@ -5,7 +5,7 @@ namespace Contracts.Repositories;
|
|||||||
|
|
||||||
public interface IUserRepo
|
public interface IUserRepo
|
||||||
{
|
{
|
||||||
public Task<UserDto> Get(UserSearch search);
|
public Task<UserDto?> Get(UserSearch search);
|
||||||
public Task<UserDto> Create(UserDto user);
|
public Task<UserDto> Create(UserDto user);
|
||||||
public Task<UserDto> Update(UserDto user);
|
public Task<UserDto> Update(UserDto user);
|
||||||
public Task<UserDto> Delete(UserSearch search);
|
public Task<UserDto> Delete(UserSearch search);
|
||||||
|
@ -3,4 +3,5 @@ namespace Contracts.SearchModels;
|
|||||||
public class UserSearch
|
public class UserSearch
|
||||||
{
|
{
|
||||||
public Guid? Id { get; set; }
|
public Guid? Id { get; set; }
|
||||||
|
public string? Name { get; set; }
|
||||||
}
|
}
|
@ -1,12 +1,11 @@
|
|||||||
using Contracts.DTO;
|
using Contracts.DTO;
|
||||||
|
using Contracts.SearchModels;
|
||||||
using Contracts.ViewModels;
|
using Contracts.ViewModels;
|
||||||
|
|
||||||
namespace Contracts.Services;
|
namespace Contracts.Services;
|
||||||
|
|
||||||
public interface IAuthService
|
public interface IAuthService
|
||||||
{
|
{
|
||||||
public Task<UserViewModel> Login();
|
public Task<UserViewModel> Login(UserLoginDTO loginData);
|
||||||
public Task<UserViewModel> Register(UserDto user);
|
public Task<UserViewModel> Register(UserDto user);
|
||||||
public Task<UserViewModel> UpdateUserData(UserDto user);
|
|
||||||
public Task<UserViewModel> Delete(Guid id);
|
|
||||||
}
|
}
|
12
back/Contracts/Services/IUserService.cs
Normal file
12
back/Contracts/Services/IUserService.cs
Normal 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);
|
||||||
|
}
|
49
back/Services/Domain/AuthService.cs
Normal file
49
back/Services/Domain/AuthService.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
47
back/Services/Domain/UserService.cs
Normal file
47
back/Services/Domain/UserService.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,9 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Contracts\Contracts.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
@ -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) { }
|
||||||
|
}
|
11
back/Services/Support/Exceptions/UserNotFoundException.cs
Normal file
11
back/Services/Support/Exceptions/UserNotFoundException.cs
Normal 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) { }
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user