diff options
Diffstat (limited to 'src')
5 files changed, 60 insertions, 28 deletions
diff --git a/src/DevHive.Services/Models/Identity/User/TokenServiceModel.cs b/src/DevHive.Services/Models/Identity/User/TokenServiceModel.cs new file mode 100644 index 0000000..631808e --- /dev/null +++ b/src/DevHive.Services/Models/Identity/User/TokenServiceModel.cs @@ -0,0 +1,12 @@ +namespace DevHive.Services.Models.Identity.User +{ + public class TokenServiceModel + { + public TokenServiceModel(string token) + { + this.Token = token; + } + + public string Token { get; set; } + } +}
\ No newline at end of file diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 06f8b1b..1e81837 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -27,30 +27,26 @@ namespace DevHive.Services.Services this._jwtOptions = jwtOptions; } - public async Task<IActionResult> LoginUser(LoginServiceModel loginModel) + public async Task<TokenServiceModel> LoginUser(LoginServiceModel loginModel) { if (!await this._userRepository.IsUsernameValid(loginModel.UserName)) - return new BadRequestObjectResult("Invalid username!"); + throw new ArgumentException("Invalid username!"); - User user = await this._userRepository - .GetByUsername(loginModel.UserName); + User user = await this._userRepository.GetByUsername(loginModel.UserName); if (user.PasswordHash != GeneratePasswordHash(loginModel.Password)) - return new BadRequestObjectResult("Incorrect password!"); + throw new ArgumentException("Incorrect password!"); - return new OkObjectResult(new - { - Token = WriteJWTSecurityToken(user.Role) - }); + return new TokenServiceModel(WriteJWTSecurityToken(user.Role)); } - public async Task<IActionResult> RegisterUser(RegisterServiceModel registerModel) + public async Task<UserServiceModel> RegisterUser(RegisterServiceModel registerModel) { if (await this._userRepository.DoesUsernameExist(registerModel.UserName)) - return new BadRequestObjectResult("Username already exists!"); + throw new ArgumentException("Username already exists!"); if (await this._userRepository.DoesEmailExist(registerModel.Email)) - return new BadRequestObjectResult("Email already exists!"); + throw new ArgumentException("Email already exists!"); User user = this._userMapper.Map<User>(registerModel); user.Role = Role.DefaultRole; @@ -58,7 +54,7 @@ namespace DevHive.Services.Services await this._userRepository.AddAsync(user); - return new CreatedResult("CreateUser", user); + return this._userMapper.Map<UserServiceModel>(user); } public async Task<UserServiceModel> GetUserById(Guid id) @@ -69,30 +65,28 @@ namespace DevHive.Services.Services return this._userMapper.Map<UserServiceModel>(user); } - public async Task<IActionResult> UpdateUser(UpdateUserServiceModel updateModel) + public async Task<UserServiceModel> UpdateUser(UpdateUserServiceModel updateModel) { if (!this._userRepository.DoesUserExist(updateModel.Id)) - return new NotFoundObjectResult("User does not exist!"); + throw new ArgumentException("User does not exist!"); if (!this._userRepository.DoesUserHaveThisUsername(updateModel.Id, updateModel.UserName) && await this._userRepository.IsUsernameValid(updateModel.UserName)) - return new BadRequestObjectResult("Username already exists!"); + throw new ArgumentException("Username already exists!"); User user = this._userMapper.Map<User>(updateModel); await this._userRepository.EditAsync(user); - return new AcceptedResult("UpdateUser", user); + return this._userMapper.Map<UserServiceModel>(user);; } - public async Task<IActionResult> DeleteUser(Guid id) + public async Task DeleteUser(Guid id) { if (!this._userRepository.DoesUserExist(id)) - return new NotFoundObjectResult("User does not exist!"); + throw new ArgumentException("User does not exist!"); User user = await this._userRepository.GetByIdAsync(id); await this._userRepository.DeleteAsync(user); - - return new OkResult(); } private string GeneratePasswordHash(string password) diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index 06083de..1fdfc6a 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -15,6 +15,8 @@ namespace DevHive.Web.Configurations.Mapping CreateMap<UpdateUserWebModel, UpdateUserServiceModel>(); CreateMap<UserServiceModel, UserWebModel>(); + + CreateMap<TokenServiceModel, TokenWebModel>(); } } } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 74eccd4..44828e0 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -31,7 +31,10 @@ namespace DevHive.Web.Controllers { LoginServiceModel loginServiceModel = this._userMapper.Map<LoginServiceModel>(loginModel); - return await this._userService.LoginUser(loginServiceModel); + TokenServiceModel tokenServiceModel = await this._userService.LoginUser(loginServiceModel); + TokenWebModel tokenWebModel = this._userMapper.Map<TokenWebModel>(tokenServiceModel); + + return new OkObjectResult(tokenWebModel); } [HttpPost] @@ -40,27 +43,35 @@ namespace DevHive.Web.Controllers { RegisterServiceModel registerServiceModel = this._userMapper.Map<RegisterServiceModel>(registerModel); - return await this._userService.RegisterUser(registerServiceModel); + UserServiceModel userServiceModel = await this._userService.RegisterUser(registerServiceModel); + UserWebModel userWebModel = this._userMapper.Map<UserWebModel>(userServiceModel); + + return new CreatedResult("Register", userWebModel); } //Read [HttpGet] public async Task<IActionResult> GetById(Guid id) { - UserServiceModel serviceModel = await this._userService.GetUserById(id); + UserServiceModel userServiceModel = await this._userService.GetUserById(id); + UserWebModel userWebModel = this._userMapper.Map<UserWebModel>(userServiceModel); - return new OkObjectResult(this._userMapper.Map<UserWebModel>(serviceModel)); + return new OkObjectResult(userWebModel); } //Update [HttpPut] - [Authorize] + [Authorize(Roles = Role.DefaultRole)] public async Task<IActionResult> Update(Guid id, [FromBody] UpdateUserWebModel updateModel) { UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map<UpdateUserServiceModel>(updateModel); updateUserServiceModel.Id = id; - return await this._userService.UpdateUser(updateUserServiceModel); + UserServiceModel userServiceModel = await this._userService.UpdateUser(updateUserServiceModel); + UserWebModel userWebModel = this._userMapper.Map<UserWebModel>(userServiceModel); + + return new AcceptedResult("UpdateUser", userWebModel); + } //Delete @@ -68,7 +79,8 @@ namespace DevHive.Web.Controllers [Authorize(Roles = Role.DefaultRole)] public async Task<IActionResult> Delete(Guid id) { - return await this._userService.DeleteUser(id); + await this._userService.DeleteUser(id); + return new OkResult(); } } } diff --git a/src/DevHive.Web/Models/Identity/User/TokenWebModel.cs b/src/DevHive.Web/Models/Identity/User/TokenWebModel.cs new file mode 100644 index 0000000..154b64b --- /dev/null +++ b/src/DevHive.Web/Models/Identity/User/TokenWebModel.cs @@ -0,0 +1,12 @@ +namespace DevHive.Web.Models.Identity.User +{ + public class TokenWebModel + { + public TokenWebModel(string token) + { + this.Token = token; + } + + public string Token { get; set; } + } +}
\ No newline at end of file |
