aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Services/Services
diff options
context:
space:
mode:
authortranstrike <transtrike@gmail.com>2020-12-14 23:29:14 +0200
committertranstrike <transtrike@gmail.com>2020-12-14 23:29:14 +0200
commitdee2e37a4a8759108390c664e06bf147b8385cbf (patch)
treebd65fe5649731a55aa6f1d8b48d53d89032fb8be /src/DevHive.Services/Services
parent1ccdefdac025b1b986ad2bd0bc3eda7505d6e7c3 (diff)
downloadDevHive-dee2e37a4a8759108390c664e06bf147b8385cbf.tar
DevHive-dee2e37a4a8759108390c664e06bf147b8385cbf.tar.gz
DevHive-dee2e37a4a8759108390c664e06bf147b8385cbf.zip
Stabalized project for compilation. Next step after init architecture
Diffstat (limited to 'src/DevHive.Services/Services')
-rw-r--r--src/DevHive.Services/Services/RoleService.cs26
-rw-r--r--src/DevHive.Services/Services/UserService.cs103
2 files changed, 129 insertions, 0 deletions
diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs
new file mode 100644
index 0000000..202c611
--- /dev/null
+++ b/src/DevHive.Services/Services/RoleService.cs
@@ -0,0 +1,26 @@
+using System;
+using System.Threading.Tasks;
+using DevHive.Data.Repositories;
+
+namespace DevHive.Services.Services
+{
+ public class RoleService
+ {
+ /* private readonly DevHiveContext _context;
+
+ public RoleService(DevHiveContext context)
+ {
+ this._context = context;
+ }
+
+ public Task<IActionResult> CreatePost(string name)
+ {
+ throw new NotImplementedException();
+ }
+
+ public Task<IActionResult> GetPostById(uint postId)
+ {
+ throw new NotImplementedException();
+ }*/
+ }
+}
diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs
new file mode 100644
index 0000000..f06198c
--- /dev/null
+++ b/src/DevHive.Services/Services/UserService.cs
@@ -0,0 +1,103 @@
+namespace DevHive.Services.Services
+{
+ public class UserService
+ {
+ /* private readonly UserRepository _userDbRepository;
+ private readonly IMapper _userMapper;
+ private readonly JWTOptions _jwtOptions;
+
+ public UserService(DevHiveContext context, IMapper mapper, JWTOptions jwtOptions)
+ {
+ this._userDbRepository = new UserRepository(context);
+ this._userMapper = mapper;
+ this._jwtOptions = jwtOptions;
+ }
+
+ public async Task<IActionResult> LoginUser(LoginDTO loginDTO)
+ {
+ User user = this._userDbRepository.FindByUsername(loginDTO.UserName);
+
+ if (user == null)
+ return new NotFoundObjectResult("User does not exist!");
+
+ byte[] key = Encoding.ASCII.GetBytes(_jwtOptions.Secret);
+
+ if (user.PasswordHash != GeneratePasswordHash(loginDTO.Password))
+ return new BadRequestObjectResult("Incorrect password!");
+
+ // Create Jwt Token configuration
+ var tokenDescriptor = new SecurityTokenDescriptor
+ {
+ Subject = new ClaimsIdentity(new Claim[]
+ {
+ new Claim(ClaimTypes.Role, user.Role) // Authorize user by role
+ }),
+ Expires = DateTime.UtcNow.AddDays(7),
+ SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha512Signature)
+ };
+
+ var tokenHandler = new JwtSecurityTokenHandler();
+ var token = tokenHandler.CreateToken(tokenDescriptor);
+ var tokenString = tokenHandler.WriteToken(token);
+
+ return new OkObjectResult(new { Token = tokenString });
+ }
+
+ public async Task<IActionResult> RegisterUser(RegisterDTO registerDTO)
+ {
+
+ if (this._userDbRepository.DoesUsernameExist(registerDTO.UserName))
+ return new BadRequestObjectResult("Username already exists!");
+
+ User user = this._userMapper.Map<User>(registerDTO);
+
+ user.Role = UserRoles.User;
+ user.PasswordHash = GeneratePasswordHash(registerDTO.Password);
+
+ await this._userDbRepository.AddAsync(user);
+
+ return new CreatedResult("CreateUser", user);
+ }
+
+ private string GeneratePasswordHash(string password)
+ {
+ //TODO: Implement
+ return password;
+ }
+
+ public async Task<IActionResult> GetUserById(Guid id)
+ {
+ User user = await this._userDbRepository.FindByIdAsync(id);
+
+ if (user == null)
+ return new NotFoundObjectResult("User does not exist!");
+
+ return new OkObjectResult(user);
+ }
+
+ public async Task<IActionResult> UpdateUser(Guid id, UserDTO userDTO)
+ {
+ if (!this._userDbRepository.DoesUserExist(id))
+ return new NotFoundObjectResult("User does not exist!");
+
+ if (!this._userDbRepository.HasThisUsername(id, userDTO.UserName)
+ && this._userDbRepository.DoesUsernameExist(userDTO.UserName))
+ return new BadRequestObjectResult("Username already exists!");
+
+ User user = this._userMapper.Map<User>(userDTO);
+ await this._userDbRepository.EditAsync(id, user);
+
+ return new AcceptedResult("UpdateUser", user);
+ }
+
+ public async Task<IActionResult> DeleteUser(Guid id)
+ {
+ if (!this._userDbRepository.DoesUserExist(id))
+ return new NotFoundObjectResult("User does not exist!");
+
+ await this._userDbRepository.DeleteAsync(id);
+
+ return new OkResult();
+ }*/
+ }
+}