From 86800d2c5bcee9aae5162eb71c846ca5f91df761 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 15 Dec 2020 10:07:38 +0200 Subject: Started work on refactoring of userservice --- src/DevHive.Data/Repositories/UserRepository.cs | 5 ++-- src/DevHive.Services/DevHive.Services.csproj | 2 ++ src/DevHive.Services/Services/UserService.cs | 36 ++++++++++++++++--------- 3 files changed, 29 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 5bd9d97..b884732 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -3,19 +3,20 @@ using System.Linq; using System.Threading.Tasks; using Data.Models.Interfaces.Database; using DevHive.Data.Models; +using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { public class UserRepository : IRepository { - /* private readonly UserRepository _dbRepository; + private readonly UserRepository _dbRepository; public UserRepository(DbContext context) : base (context) { this._dbRepository = new DbRepository(context); } - + /* public User FindByUsername(string username) { return this._dbRepository.DbSet diff --git a/src/DevHive.Services/DevHive.Services.csproj b/src/DevHive.Services/DevHive.Services.csproj index 277a342..4895e94 100644 --- a/src/DevHive.Services/DevHive.Services.csproj +++ b/src/DevHive.Services/DevHive.Services.csproj @@ -4,6 +4,8 @@ + + diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index f06198c..0c849b1 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -1,21 +1,33 @@ +using AutoMapper; +using DevHive.Data.Repositories; +using DevHive.Services.Options; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using System.IdentityModel.Tokens.Jwt; +using DevHive.Data.Models; +using System.Text; +using Microsoft.IdentityModel.Tokens; +using System.Security.Claims; +using System; + namespace DevHive.Services.Services { public class UserService { - /* private readonly UserRepository _userDbRepository; + private readonly UserRepository _userRepository; private readonly IMapper _userMapper; private readonly JWTOptions _jwtOptions; public UserService(DevHiveContext context, IMapper mapper, JWTOptions jwtOptions) { - this._userDbRepository = new UserRepository(context); + this._userRepository = new UserRepository(context); this._userMapper = mapper; this._jwtOptions = jwtOptions; } public async Task LoginUser(LoginDTO loginDTO) { - User user = this._userDbRepository.FindByUsername(loginDTO.UserName); + User user = this._userRepository.FindByUsername(loginDTO.UserName); if (user == null) return new NotFoundObjectResult("User does not exist!"); @@ -46,7 +58,7 @@ namespace DevHive.Services.Services public async Task RegisterUser(RegisterDTO registerDTO) { - if (this._userDbRepository.DoesUsernameExist(registerDTO.UserName)) + if (this._userRepository.DoesUsernameExist(registerDTO.UserName)) return new BadRequestObjectResult("Username already exists!"); User user = this._userMapper.Map(registerDTO); @@ -54,7 +66,7 @@ namespace DevHive.Services.Services user.Role = UserRoles.User; user.PasswordHash = GeneratePasswordHash(registerDTO.Password); - await this._userDbRepository.AddAsync(user); + await this._userRepository.AddAsync(user); return new CreatedResult("CreateUser", user); } @@ -67,7 +79,7 @@ namespace DevHive.Services.Services public async Task GetUserById(Guid id) { - User user = await this._userDbRepository.FindByIdAsync(id); + User user = await this._userRepository.FindByIdAsync(id); if (user == null) return new NotFoundObjectResult("User does not exist!"); @@ -77,27 +89,27 @@ namespace DevHive.Services.Services public async Task UpdateUser(Guid id, UserDTO userDTO) { - if (!this._userDbRepository.DoesUserExist(id)) + if (!this._userRepository.DoesUserExist(id)) return new NotFoundObjectResult("User does not exist!"); - if (!this._userDbRepository.HasThisUsername(id, userDTO.UserName) - && this._userDbRepository.DoesUsernameExist(userDTO.UserName)) + if (!this._userRepository.HasThisUsername(id, userDTO.UserName) + && this._userRepository.DoesUsernameExist(userDTO.UserName)) return new BadRequestObjectResult("Username already exists!"); User user = this._userMapper.Map(userDTO); - await this._userDbRepository.EditAsync(id, user); + await this._userRepository.EditAsync(id, user); return new AcceptedResult("UpdateUser", user); } public async Task DeleteUser(Guid id) { - if (!this._userDbRepository.DoesUserExist(id)) + if (!this._userRepository.DoesUserExist(id)) return new NotFoundObjectResult("User does not exist!"); await this._userDbRepository.DeleteAsync(id); return new OkResult(); - }*/ + } } } -- cgit v1.2.3