aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Services/Services/UserService.cs
diff options
context:
space:
mode:
authortranstrike <transtrike@gmail.com>2020-12-16 22:38:25 +0200
committertranstrike <transtrike@gmail.com>2020-12-16 22:38:25 +0200
commitcd02428e748e691a0005b2687edf69a99766aac6 (patch)
treeefd9b1a5dcba1c42a9fbc65a6bc45d45e499179d /src/DevHive.Services/Services/UserService.cs
parent1d696036bb0afa88a1a88da8dc8e5cfa9d2944cd (diff)
downloadDevHive-cd02428e748e691a0005b2687edf69a99766aac6.tar
DevHive-cd02428e748e691a0005b2687edf69a99766aac6.tar.gz
DevHive-cd02428e748e691a0005b2687edf69a99766aac6.zip
Added DevHive.Common; Changed repositories behavior; Abstracted some common logic
Diffstat (limited to 'src/DevHive.Services/Services/UserService.cs')
-rw-r--r--src/DevHive.Services/Services/UserService.cs20
1 files changed, 13 insertions, 7 deletions
diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs
index 24f74f5..63b8389 100644
--- a/src/DevHive.Services/Services/UserService.cs
+++ b/src/DevHive.Services/Services/UserService.cs
@@ -3,7 +3,6 @@ using DevHive.Data.Repositories;
using DevHive.Services.Options;
using DevHive.Services.Models.Identity.User;
using System.Threading.Tasks;
-using Microsoft.AspNetCore.Mvc;
using DevHive.Data.Models;
using System;
using System.IdentityModel.Tokens.Jwt;
@@ -12,6 +11,7 @@ using Microsoft.IdentityModel.Tokens;
using System.Security.Cryptography;
using System.Text;
using System.Collections.Generic;
+using DevHive.Common.Models.Identity;
namespace DevHive.Services.Services
{
@@ -28,7 +28,7 @@ namespace DevHive.Services.Services
this._jwtOptions = jwtOptions;
}
- public async Task<TokenServiceModel> LoginUser(LoginServiceModel loginModel)
+ public async Task<TokenModel> LoginUser(LoginServiceModel loginModel)
{
if (!await this._userRepository.IsUsernameValid(loginModel.UserName))
throw new ArgumentException("Invalid username!");
@@ -38,10 +38,10 @@ namespace DevHive.Services.Services
if (user.PasswordHash != GeneratePasswordHash(loginModel.Password))
throw new ArgumentException("Incorrect password!");
- return new TokenServiceModel(WriteJWTSecurityToken(user.Role));
+ return new TokenModel(WriteJWTSecurityToken(user.Role));
}
- public async Task<TokenServiceModel> RegisterUser(RegisterServiceModel registerModel)
+ public async Task<TokenModel> RegisterUser(RegisterServiceModel registerModel)
{
if (await this._userRepository.DoesUsernameExist(registerModel.UserName))
throw new ArgumentException("Username already exists!");
@@ -55,7 +55,7 @@ namespace DevHive.Services.Services
await this._userRepository.AddAsync(user);
- return new TokenServiceModel(WriteJWTSecurityToken(user.Role));
+ return new TokenModel(WriteJWTSecurityToken(user.Role));
}
public async Task<UserServiceModel> GetUserById(Guid id)
@@ -76,7 +76,10 @@ namespace DevHive.Services.Services
throw new ArgumentException("Username already exists!");
User user = this._userMapper.Map<User>(updateModel);
- await this._userRepository.EditAsync(user);
+ bool result = await this._userRepository.EditAsync(user);
+
+ if (!result)
+ throw new InvalidOperationException("Unable to edit user!");
return this._userMapper.Map<UserServiceModel>(user);;
}
@@ -87,7 +90,10 @@ namespace DevHive.Services.Services
throw new ArgumentException("User does not exist!");
User user = await this._userRepository.GetByIdAsync(id);
- await this._userRepository.DeleteAsync(user);
+ bool result = await this._userRepository.DeleteAsync(user);
+
+ if (!result)
+ throw new InvalidOperationException("Unable to delete user!");
}
private string GeneratePasswordHash(string password)