diff --git a/back/Contracts/DTOs/UserDTO.cs b/back/Contracts/DTOs/UserDTO.cs index abf5f99..e312313 100644 --- a/back/Contracts/DTOs/UserDTO.cs +++ b/back/Contracts/DTOs/UserDTO.cs @@ -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; } } \ No newline at end of file diff --git a/back/Contracts/DTOs/UserLoginDTO.cs b/back/Contracts/DTOs/UserLoginDTO.cs new file mode 100644 index 0000000..73210e9 --- /dev/null +++ b/back/Contracts/DTOs/UserLoginDTO.cs @@ -0,0 +1,7 @@ +namespace Contracts.DTO; + +public class UserLoginDTO +{ + public string Name { get; set; } = string.Empty; + public string Password { get; set; } = string.Empty; +} \ No newline at end of file diff --git a/back/Contracts/Mappers/UserMapper.cs b/back/Contracts/Mappers/UserMapper.cs new file mode 100644 index 0000000..a32afed --- /dev/null +++ b/back/Contracts/Mappers/UserMapper.cs @@ -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 + }; + +} \ No newline at end of file diff --git a/back/Contracts/Repositories/IUserRepo.cs b/back/Contracts/Repositories/IUserRepo.cs index 3785e5e..926450c 100644 --- a/back/Contracts/Repositories/IUserRepo.cs +++ b/back/Contracts/Repositories/IUserRepo.cs @@ -5,7 +5,7 @@ namespace Contracts.Repositories; public interface IUserRepo { - public Task Get(UserSearch search); + public Task Get(UserSearch search); public Task Create(UserDto user); public Task Update(UserDto user); public Task Delete(UserSearch search); diff --git a/back/Contracts/SearchModels/UserSearch.cs b/back/Contracts/SearchModels/UserSearch.cs index 7165750..7ea4178 100644 --- a/back/Contracts/SearchModels/UserSearch.cs +++ b/back/Contracts/SearchModels/UserSearch.cs @@ -3,4 +3,5 @@ namespace Contracts.SearchModels; public class UserSearch { public Guid? Id { get; set; } + public string? Name { get; set; } } \ No newline at end of file diff --git a/back/Contracts/Services/IAuthService.cs b/back/Contracts/Services/IAuthService.cs index d6e5983..03548f7 100644 --- a/back/Contracts/Services/IAuthService.cs +++ b/back/Contracts/Services/IAuthService.cs @@ -1,12 +1,11 @@ using Contracts.DTO; +using Contracts.SearchModels; using Contracts.ViewModels; namespace Contracts.Services; public interface IAuthService { - public Task Login(); + public Task Login(UserLoginDTO loginData); public Task Register(UserDto user); - public Task UpdateUserData(UserDto user); - public Task Delete(Guid id); } \ No newline at end of file diff --git a/back/Contracts/Services/IUserService.cs b/back/Contracts/Services/IUserService.cs new file mode 100644 index 0000000..736d3bf --- /dev/null +++ b/back/Contracts/Services/IUserService.cs @@ -0,0 +1,12 @@ +using Contracts.DTO; +using Contracts.SearchModels; +using Contracts.ViewModels; + +namespace Contracts.Services; + +public interface IUserService +{ + public Task GetDetails(UserSearch search); + public Task UpdateUserData(UserDto user); + public Task Delete(UserSearch search); +} \ No newline at end of file diff --git a/back/Services/Domain/AuthService.cs b/back/Services/Domain/AuthService.cs new file mode 100644 index 0000000..4d59a79 --- /dev/null +++ b/back/Services/Domain/AuthService.cs @@ -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 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 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(); + } +} diff --git a/back/Services/Domain/UserService.cs b/back/Services/Domain/UserService.cs new file mode 100644 index 0000000..e54904d --- /dev/null +++ b/back/Services/Domain/UserService.cs @@ -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 Delete(UserSearch search) + { + var user = await _userRepo.Delete(search); + return user.ToView(); + } + + public async Task GetDetails(UserSearch search) + { + var user = await _userRepo.Get(search); + if (user == null) + { + throw new UserNotFoundException($"Пользователь {search.Name} не найден"); + } + return user.ToView(); + } + + public async Task 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(); + } +} diff --git a/back/Services/Services.csproj b/back/Services/Services.csproj index fa71b7a..3c22415 100644 --- a/back/Services/Services.csproj +++ b/back/Services/Services.csproj @@ -1,5 +1,9 @@  + + + + net8.0 enable diff --git a/back/Services/Support/Exceptions/AlreadyExistsException.cs b/back/Services/Support/Exceptions/AlreadyExistsException.cs new file mode 100644 index 0000000..8577218 --- /dev/null +++ b/back/Services/Support/Exceptions/AlreadyExistsException.cs @@ -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) { } +} \ No newline at end of file diff --git a/back/Services/Support/Exceptions/UserNotFoundException.cs b/back/Services/Support/Exceptions/UserNotFoundException.cs new file mode 100644 index 0000000..df152a1 --- /dev/null +++ b/back/Services/Support/Exceptions/UserNotFoundException.cs @@ -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) { } +} \ No newline at end of file