From 1d813cd8e099306ba0cabf90031ea4c4b24827bb Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 15 Dec 2020 09:49:39 +0200 Subject: Implemented web models and updated user controller --- src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs (limited to 'src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs') diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs new file mode 100644 index 0000000..3d96189 --- /dev/null +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -0,0 +1,11 @@ +namespace DevHive.Web.Models.Identity.User +{ + public class UpdateUserWebModel + { + public string UserName { get; set; } + public string Email { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public string Password { get; set; } + } +} -- cgit v1.2.3 From d80b44003ca03cd09bf28278bf2e243581c00332 Mon Sep 17 00:00:00 2001 From: transtrike Date: Wed, 16 Dec 2020 10:23:15 +0200 Subject: Fixed GetById to return only public info --- src/DevHive.Services/Configurations/Mapping/UserMappings.cs | 2 ++ src/DevHive.Services/Services/UserService.cs | 11 ++++------- .../Configurations/Extensions/ConfigureDatabase.cs | 6 ++++++ src/DevHive.Web/Configurations/Mapping/UserMappings.cs | 3 +++ src/DevHive.Web/Controllers/UserController.cs | 7 +++++-- src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs | 6 +----- src/DevHive.Web/Models/Identity/User/UserWebModel.cs | 11 +++++++++++ 7 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 src/DevHive.Web/Models/Identity/User/UserWebModel.cs (limited to 'src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs') diff --git a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs index 9a35e43..ca8fa20 100644 --- a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs @@ -11,6 +11,8 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); CreateMap(); + + CreateMap(); } } } diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index c71209e..06f8b1b 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -11,7 +11,6 @@ using System.Security.Claims; using Microsoft.IdentityModel.Tokens; using System.Security.Cryptography; using System.Text; -using System.Collections.Immutable; namespace DevHive.Services.Services { @@ -62,14 +61,12 @@ namespace DevHive.Services.Services return new CreatedResult("CreateUser", user); } - public async Task GetUserById(Guid id) + public async Task GetUserById(Guid id) { - User user = await this._userRepository.GetByIdAsync(id); - - if (user == null) - return new NotFoundObjectResult("User does not exist!"); + User user = await this._userRepository.GetByIdAsync(id) + ?? throw new ArgumentException("User does not exist!"); - return new OkObjectResult(user); + return this._userMapper.Map(user); } public async Task UpdateUser(UpdateUserServiceModel updateModel) diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs index f308957..0fe32de 100644 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs @@ -37,6 +37,12 @@ namespace DevHive.Web.Configurations.Extensions options.Stores.MaxLengthForKeys = 20; }); + + services.AddAuthorization(options => + { + options.AddPolicy($"{Role.DefaultRole}", + policy => policy.RequireRole($"{Role.DefaultRole}")); + }); } public static void UseDatabaseConfiguration(this IApplicationBuilder app) diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index 49f0348..06083de 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -11,7 +11,10 @@ namespace DevHive.Web.Configurations.Mapping { CreateMap(); CreateMap(); + CreateMap(); CreateMap(); + + CreateMap(); } } } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index f241409..74eccd4 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -1,6 +1,7 @@ using System; using System.Threading.Tasks; using AutoMapper; +using DevHive.Data.Models; using DevHive.Data.Repositories; using DevHive.Services.Models.Identity.User; using DevHive.Services.Options; @@ -46,7 +47,9 @@ namespace DevHive.Web.Controllers [HttpGet] public async Task GetById(Guid id) { - return await this._userService.GetUserById(id); + UserServiceModel serviceModel = await this._userService.GetUserById(id); + + return new OkObjectResult(this._userMapper.Map(serviceModel)); } //Update @@ -62,7 +65,7 @@ namespace DevHive.Web.Controllers //Delete [HttpDelete] - [Authorize] + [Authorize(Roles = Role.DefaultRole)] public async Task Delete(Guid id) { return await this._userService.DeleteUser(id); diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index 3d96189..e04e7da 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -1,11 +1,7 @@ namespace DevHive.Web.Models.Identity.User { - public class UpdateUserWebModel + public class UpdateUserWebModel : UserWebModel { - public string UserName { get; set; } - public string Email { get; set; } - public string FirstName { get; set; } - public string LastName { get; set; } public string Password { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs new file mode 100644 index 0000000..e070d44 --- /dev/null +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -0,0 +1,11 @@ +namespace DevHive.Web.Models.Identity.User +{ + public class UserWebModel + { + public string UserName { get; set; } + public string Email { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public string Role { get; set; } + } +} -- cgit v1.2.3 From 47c2a91612b54329ba0353b1296846bafc0e9775 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 16 Dec 2020 13:04:07 +0200 Subject: Changed models in Web to use inheritance of most repeated properties --- .../Models/Identity/User/BaseUserWebModel.cs | 30 ++++++++++++++++++++++ .../Models/Identity/User/RegisterWebModel.cs | 26 ++----------------- .../Models/Identity/User/UpdateUserWebModel.cs | 9 +++++-- .../Models/Identity/User/UserWebModel.cs | 6 +---- 4 files changed, 40 insertions(+), 31 deletions(-) create mode 100644 src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs (limited to 'src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs') diff --git a/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs new file mode 100644 index 0000000..ff9fac5 --- /dev/null +++ b/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs @@ -0,0 +1,30 @@ +using System.ComponentModel.DataAnnotations; +using DevHive.Web.Models.Identity.Validation; + +namespace DevHive.Web.Models.Identity.User +{ + public class BaseUserWebModel + { + [Required] + [MinLength(3)] + [MaxLength(50)] + [OnlyAlphanumerics(ErrorMessage = "Username can only contain letters and digits!")] + public string UserName { get; set; } + + [Required] + [EmailAddress] + public string Email { get; set; } + + [Required] + [MinLength(3)] + [MaxLength(30)] + [OnlyLetters(ErrorMessage = "First name can only contain letters!")] + public string FirstName { get; set; } + + [Required] + [MinLength(3)] + [MaxLength(30)] + [OnlyLetters(ErrorMessage = "Last name can only contain letters!")] + public string LastName { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs b/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs index 60e860a..22b178b 100644 --- a/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs @@ -3,32 +3,10 @@ using DevHive.Web.Models.Identity.Validation; namespace DevHive.Web.Models.Identity.User { - public class RegisterWebModel + public class RegisterWebModel : BaseUserWebModel { - [Required] - [MinLength(3)] - [MaxLength(50)] - [OnlyAlphanumerics(ErrorMessage = "Username can only contain letters and digits!")] - public string UserName { get; set; } - - [Required] - [EmailAddress] - public string Email { get; set; } - - [Required] - [MinLength(3)] - [MaxLength(30)] - [OnlyLetters(ErrorMessage = "First name can only contain letters!")] - public string FirstName { get; set; } - - [Required] - [MinLength(3)] - [MaxLength(30)] - [OnlyLetters(ErrorMessage = "Last name can only contain letters!")] - public string LastName { get; set; } - [Required] [GoodPassword] - public string Password { get; set; } + public virtual string Password { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index e04e7da..fbe02a5 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -1,7 +1,12 @@ +using System.ComponentModel.DataAnnotations; +using DevHive.Web.Models.Identity.Validation; + namespace DevHive.Web.Models.Identity.User { - public class UpdateUserWebModel : UserWebModel + public class UpdateUserWebModel : BaseUserWebModel { - public string Password { get; set; } + [Required] + [GoodPassword] + public virtual string Password { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index e070d44..88ee1b7 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -1,11 +1,7 @@ namespace DevHive.Web.Models.Identity.User { - public class UserWebModel + public class UserWebModel : BaseUserWebModel { - public string UserName { get; set; } - public string Email { get; set; } - public string FirstName { get; set; } - public string LastName { get; set; } public string Role { get; set; } } } -- cgit v1.2.3 From 61c51944844ed404cd4f174440d6e81b2a8591ba Mon Sep 17 00:00:00 2001 From: transtrike Date: Wed, 13 Jan 2021 23:06:18 +0200 Subject: Fixed sln-wide code formatting --- .../Configurations/Extensions/ConfigureDatabase.cs | 4 +- .../Configurations/Mapping/CommentMappings.cs | 4 +- .../Configurations/Mapping/LanguageMappings.cs | 6 +- .../Configurations/Mapping/RoleMappings.cs | 6 +- .../Configurations/Mapping/TechnologyMappings.cs | 6 +- .../Configurations/Mapping/UserMappings.cs | 2 +- src/DevHive.Web/Controllers/ErrorController.cs | 2 +- src/DevHive.Web/Controllers/LanguageController.cs | 12 +- src/DevHive.Web/Controllers/PostController.cs | 12 +- src/DevHive.Web/Controllers/RoleController.cs | 8 +- .../Controllers/TechnologyController.cs | 12 +- src/DevHive.Web/Controllers/UserController.cs | 6 +- .../Models/Identity/User/BaseUserWebModel.cs | 4 +- .../Models/Identity/User/LoginWebModel.cs | 4 +- .../Models/Identity/User/RegisterWebModel.cs | 4 +- .../Models/Identity/User/UpdateUserWebModel.cs | 2 +- .../Models/Identity/User/UserWebModel.cs | 2 +- .../Validation/GoodPasswordModelValidation.cs | 2 +- .../Models/Language/UpdateLanguageWebModel.cs | 4 +- src/DevHive.Web/Startup.cs | 142 ++++++++++----------- 20 files changed, 122 insertions(+), 122 deletions(-) (limited to 'src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs') diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs index b42ae05..4831435 100644 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs @@ -20,7 +20,7 @@ namespace DevHive.Web.Configurations.Extensions services.AddIdentity() .AddEntityFrameworkStores(); - + services.Configure(options => { options.User.RequireUniqueEmail = true; @@ -41,7 +41,7 @@ namespace DevHive.Web.Configurations.Extensions services.AddAuthorization(options => { - options.AddPolicy("User", options => + options.AddPolicy("User", options => { options.RequireAuthenticatedUser(); options.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme); diff --git a/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs b/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs index 394490e..5998e7a 100644 --- a/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs @@ -13,5 +13,5 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap(); } - } -} \ No newline at end of file + } +} diff --git a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs index bae8562..3c2a4d0 100644 --- a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs @@ -4,7 +4,7 @@ using DevHive.Services.Models.Language; namespace DevHive.Web.Configurations.Mapping { - public class LanguageMappings : Profile + public class LanguageMappings : Profile { public LanguageMappings() { @@ -16,5 +16,5 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap(); } - } -} \ No newline at end of file + } +} diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index 5d33c56..afa3a94 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -4,15 +4,15 @@ using DevHive.Common.Models.Identity; namespace DevHive.Web.Configurations.Mapping { - public class RoleMappings : Profile + public class RoleMappings : Profile { public RoleMappings() { CreateMap(); CreateMap(); - + CreateMap(); CreateMap(); } - } + } } diff --git a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs index 849e47f..8523897 100644 --- a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs @@ -4,7 +4,7 @@ using DevHive.Services.Models.Technology; namespace DevHive.Web.Configurations.Mapping { - public class TechnologyMappings : Profile + public class TechnologyMappings : Profile { public TechnologyMappings() { @@ -13,5 +13,5 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap(); } - } -} \ No newline at end of file + } +} diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index 4420368..59003ea 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -18,5 +18,5 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); } - } + } } diff --git a/src/DevHive.Web/Controllers/ErrorController.cs b/src/DevHive.Web/Controllers/ErrorController.cs index c3f1e55..b187501 100644 --- a/src/DevHive.Web/Controllers/ErrorController.cs +++ b/src/DevHive.Web/Controllers/ErrorController.cs @@ -19,7 +19,7 @@ namespace DevHive.Web.Controllers IExceptionHandlerFeature exception = HttpContext.Features.Get(); - + object result = ProcessException(requestId, exception); return new BadRequestObjectResult(JsonConvert.SerializeObject(result)); } diff --git a/src/DevHive.Web/Controllers/LanguageController.cs b/src/DevHive.Web/Controllers/LanguageController.cs index 29c1e99..486e16e 100644 --- a/src/DevHive.Web/Controllers/LanguageController.cs +++ b/src/DevHive.Web/Controllers/LanguageController.cs @@ -29,7 +29,7 @@ namespace DevHive.Web.Controllers bool result = await this._languageService.CreateLanguage(languageServiceModel); - if(!result) + if (!result) return new BadRequestObjectResult("Could not create Language"); return new OkResult(); @@ -52,21 +52,21 @@ namespace DevHive.Web.Controllers bool result = await this._languageService.UpdateLanguage(updatelanguageServiceModel); - if(!result) + if (!result) return new BadRequestObjectResult("Could not update Language"); return new OkResult(); } - + [HttpDelete] public async Task Delete(Guid id) { bool result = await this._languageService.DeleteLanguage(id); - - if(!result) + + if (!result) return new BadRequestObjectResult("Could not delete Language"); return new OkResult(); } } -} \ No newline at end of file +} diff --git a/src/DevHive.Web/Controllers/PostController.cs b/src/DevHive.Web/Controllers/PostController.cs index 753897c..a906e47 100644 --- a/src/DevHive.Web/Controllers/PostController.cs +++ b/src/DevHive.Web/Controllers/PostController.cs @@ -30,9 +30,9 @@ namespace DevHive.Web.Controllers [HttpPost] public async Task Create([FromBody] CreatePostWebModel createPostModel) { - CreatePostServiceModel postServiceModel = - this._postMapper.Map(createPostModel); - + CreatePostServiceModel postServiceModel = + this._postMapper.Map(createPostModel); + bool result = await this._postService.CreatePost(postServiceModel); if (!result) @@ -49,7 +49,7 @@ namespace DevHive.Web.Controllers bool result = await this._postService.AddComment(createCommentServiceModel); - if(!result) + if (!result) return new BadRequestObjectResult("Could not create the Comment"); return new OkResult(); @@ -81,7 +81,7 @@ namespace DevHive.Web.Controllers [HttpPut] public async Task Update(Guid id, [FromBody] UpdatePostWebModel updatePostModel) { - UpdatePostServiceModel postServiceModel = + UpdatePostServiceModel postServiceModel = this._postMapper.Map(updatePostModel); postServiceModel.IssuerId = id; @@ -129,7 +129,7 @@ namespace DevHive.Web.Controllers { if (!await this._postService.ValidateJwtForComment(id, authorization)) return new UnauthorizedResult(); - + bool result = await this._postService.DeleteComment(id); if (!result) diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index d710f5a..0a8f7a1 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -26,9 +26,9 @@ namespace DevHive.Web.Controllers [HttpPost] public async Task Create([FromBody] CreateRoleModel createRoleModel) { - RoleModel roleServiceModel = - this._roleMapper.Map(createRoleModel); - + RoleModel roleServiceModel = + this._roleMapper.Map(createRoleModel); + bool result = await this._roleService.CreateRole(roleServiceModel); if (!result) @@ -49,7 +49,7 @@ namespace DevHive.Web.Controllers [HttpPut] public async Task Update(Guid id, [FromBody] UpdateRoleModel updateRoleModel) { - RoleModel roleServiceModel = + RoleModel roleServiceModel = this._roleMapper.Map(updateRoleModel); roleServiceModel.Id = id; diff --git a/src/DevHive.Web/Controllers/TechnologyController.cs b/src/DevHive.Web/Controllers/TechnologyController.cs index e02ca3d..905a71d 100644 --- a/src/DevHive.Web/Controllers/TechnologyController.cs +++ b/src/DevHive.Web/Controllers/TechnologyController.cs @@ -29,7 +29,7 @@ namespace DevHive.Web.Controllers bool result = await this._technologyService.Create(technologyServiceModel); - if(!result) + if (!result) return new BadRequestObjectResult("Could not create the Technology"); return new OkResult(); @@ -51,21 +51,21 @@ namespace DevHive.Web.Controllers bool result = await this._technologyService.UpdateTechnology(updateTechnologyWebModel); - if(!result) + if (!result) return new BadRequestObjectResult("Could not update Technology"); return new OkResult(); } - + [HttpDelete] public async Task Delete(Guid id) { bool result = await this._technologyService.DeleteTechnology(id); - - if(!result) + + if (!result) return new BadRequestObjectResult("Could not delete Technology"); return new OkResult(); } } -} \ No newline at end of file +} diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 0960915..26271b2 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -20,7 +20,7 @@ namespace DevHive.Web.Controllers [ApiController] [Route("/api/[controller]")] [Authorize(Roles = "User")] - public class UserController: ControllerBase + public class UserController : ControllerBase { private readonly UserService _userService; private readonly IMapper _userMapper; @@ -154,7 +154,7 @@ namespace DevHive.Web.Controllers await this._userService.RemoveFriend(userId, friendId); return new OkResult(); } - + [HttpDelete] [Route("RemoveLanguageFromUser")] public async Task RemoveLanguageFromUser(Guid userId, [FromBody] LanguageWebModel languageWebModel) @@ -176,7 +176,7 @@ namespace DevHive.Web.Controllers new OkResult() : new BadRequestResult(); } - + #endregion } } diff --git a/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs index ff9fac5..2d99786 100644 --- a/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs @@ -1,9 +1,9 @@ using System.ComponentModel.DataAnnotations; using DevHive.Web.Models.Identity.Validation; -namespace DevHive.Web.Models.Identity.User +namespace DevHive.Web.Models.Identity.User { - public class BaseUserWebModel + public class BaseUserWebModel { [Required] [MinLength(3)] diff --git a/src/DevHive.Web/Models/Identity/User/LoginWebModel.cs b/src/DevHive.Web/Models/Identity/User/LoginWebModel.cs index 3bd7428..87c7416 100644 --- a/src/DevHive.Web/Models/Identity/User/LoginWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/LoginWebModel.cs @@ -1,6 +1,6 @@ -namespace DevHive.Web.Models.Identity.User +namespace DevHive.Web.Models.Identity.User { - public class LoginWebModel + public class LoginWebModel { public string UserName { get; set; } public string Password { get; set; } diff --git a/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs b/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs index 22b178b..273c2d3 100644 --- a/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs @@ -1,9 +1,9 @@ using System.ComponentModel.DataAnnotations; using DevHive.Web.Models.Identity.Validation; -namespace DevHive.Web.Models.Identity.User +namespace DevHive.Web.Models.Identity.User { - public class RegisterWebModel : BaseUserWebModel + public class RegisterWebModel : BaseUserWebModel { [Required] [GoodPassword] diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index fbe02a5..91fbc64 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -1,7 +1,7 @@ using System.ComponentModel.DataAnnotations; using DevHive.Web.Models.Identity.Validation; -namespace DevHive.Web.Models.Identity.User +namespace DevHive.Web.Models.Identity.User { public class UpdateUserWebModel : BaseUserWebModel { diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 260d34c..8f7995c 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -4,7 +4,7 @@ using DevHive.Web.Models.Identity.Role; using DevHive.Web.Models.Language; using DevHive.Web.Models.Technology; -namespace DevHive.Web.Models.Identity.User +namespace DevHive.Web.Models.Identity.User { public class UserWebModel : BaseUserWebModel { diff --git a/src/DevHive.Web/Models/Identity/Validation/GoodPasswordModelValidation.cs b/src/DevHive.Web/Models/Identity/Validation/GoodPasswordModelValidation.cs index f69121a..f920c35 100644 --- a/src/DevHive.Web/Models/Identity/Validation/GoodPasswordModelValidation.cs +++ b/src/DevHive.Web/Models/Identity/Validation/GoodPasswordModelValidation.cs @@ -3,7 +3,7 @@ using System.ComponentModel.DataAnnotations; namespace DevHive.Web.Models.Identity.Validation { - public class GoodPassword : ValidationAttribute + public class GoodPassword : ValidationAttribute { public override bool IsValid(object value) { diff --git a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs index 2da8217..deca0fc 100644 --- a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs +++ b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs @@ -2,5 +2,5 @@ using System; namespace DevHive.Web.Models.Language { - public class UpdateLanguageWebModel : CreateLanguageWebModel {} -} \ No newline at end of file + public class UpdateLanguageWebModel : CreateLanguageWebModel { } +} diff --git a/src/DevHive.Web/Startup.cs b/src/DevHive.Web/Startup.cs index 96ab318..94aabe8 100644 --- a/src/DevHive.Web/Startup.cs +++ b/src/DevHive.Web/Startup.cs @@ -1,71 +1,71 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using DevHive.Web.Configurations.Extensions; -using Newtonsoft.Json; - -namespace DevHive.Web -{ - public class Startup - { - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } - - public IConfiguration Configuration { get; } - - // This method gets called by the runtime. Use this method to add services to the container. - public void ConfigureServices(IServiceCollection services) - { - services.AddCors(); - - services.AddControllers() - .AddNewtonsoftJson(x => - { - x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; - }); - - services.DatabaseConfiguration(Configuration); - services.SwaggerConfiguration(); - services.JWTConfiguration(Configuration); - services.AutoMapperConfiguration(); - services.DependencyInjectionConfiguration(); - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { - app.UseCors(x => x - .AllowAnyMethod() - .AllowAnyHeader() - .SetIsOriginAllowed(origin => true) // allow any origin - .AllowCredentials()); // allow credentials - - if (env.IsDevelopment()) - { - //app.UseDeveloperExceptionPage(); - app.UseExceptionHandler("/api/Error"); - app.UseSwaggerConfiguration(); - } - else - { - app.UseExceptionHandler("/api/Error"); - app.UseHsts(); - } - - app.UseDatabaseConfiguration(); - app.UseAutoMapperConfiguration(); - - app.UseEndpoints(endpoints => - { - endpoints.MapControllerRoute( - name: "default", - pattern: "api/{controller}/{action}" - ); - }); - } - } -} +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using DevHive.Web.Configurations.Extensions; +using Newtonsoft.Json; + +namespace DevHive.Web +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddCors(); + + services.AddControllers() + .AddNewtonsoftJson(x => + { + x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; + }); + + services.DatabaseConfiguration(Configuration); + services.SwaggerConfiguration(); + services.JWTConfiguration(Configuration); + services.AutoMapperConfiguration(); + services.DependencyInjectionConfiguration(); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + app.UseCors(x => x + .AllowAnyMethod() + .AllowAnyHeader() + .SetIsOriginAllowed(origin => true) // allow any origin + .AllowCredentials()); // allow credentials + + if (env.IsDevelopment()) + { + //app.UseDeveloperExceptionPage(); + app.UseExceptionHandler("/api/Error"); + app.UseSwaggerConfiguration(); + } + else + { + app.UseExceptionHandler("/api/Error"); + app.UseHsts(); + } + + app.UseDatabaseConfiguration(); + app.UseAutoMapperConfiguration(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllerRoute( + name: "default", + pattern: "api/{controller}/{action}" + ); + }); + } + } +} -- cgit v1.2.3 From e0c6b1a631fb50b2bb985a85ddf6af57cb76e09b Mon Sep 17 00:00:00 2001 From: transtrike Date: Fri, 15 Jan 2021 16:22:01 +0200 Subject: Removed virtual from methods --- src/DevHive.Data/Models/Role.cs | 2 +- src/DevHive.Data/Models/User.cs | 2 +- src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs | 2 +- src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs') diff --git a/src/DevHive.Data/Models/Role.cs b/src/DevHive.Data/Models/Role.cs index 0e7dd9f..63e6c7c 100644 --- a/src/DevHive.Data/Models/Role.cs +++ b/src/DevHive.Data/Models/Role.cs @@ -11,6 +11,6 @@ namespace DevHive.Data.Models public const string DefaultRole = "User"; public const string AdminRole = "Admin"; - public virtual List Users { get; set; } + public List Users { get; set; } } } diff --git a/src/DevHive.Data/Models/User.cs b/src/DevHive.Data/Models/User.cs index fda4651..7a213c7 100644 --- a/src/DevHive.Data/Models/User.cs +++ b/src/DevHive.Data/Models/User.cs @@ -24,7 +24,7 @@ namespace DevHive.Data.Models /// public IList Technologies { get; set; } - public virtual IList Roles { get; set; } = new List(); + public IList Roles { get; set; } = new List(); public IList Friends { get; set; } = new List(); } diff --git a/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs b/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs index 273c2d3..04fd6bd 100644 --- a/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs @@ -7,6 +7,6 @@ namespace DevHive.Web.Models.Identity.User { [Required] [GoodPassword] - public virtual string Password { get; set; } + public string Password { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index 91fbc64..9e41eb6 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -7,6 +7,6 @@ namespace DevHive.Web.Models.Identity.User { [Required] [GoodPassword] - public virtual string Password { get; set; } + public string Password { get; set; } } } -- cgit v1.2.3 From c43a7665b9febe890eb6dac143b62c36464cc94d Mon Sep 17 00:00:00 2001 From: transtrike Date: Sun, 17 Jan 2021 12:24:17 +0200 Subject: Moved Lang&Tech CRUD to Update in User Layer --- .../Interfaces/ILanguageService.cs | 2 +- .../Interfaces/ITechnologyService.cs | 2 +- src/DevHive.Services/Interfaces/IUserService.cs | 2 +- .../Models/Identity/User/UserServiceModel.cs | 5 +- src/DevHive.Services/Services/LanguageService.cs | 5 +- src/DevHive.Services/Services/TechnologyService.cs | 5 +- src/DevHive.Services/Services/UserService.cs | 8 +-- src/DevHive.Web/Controllers/LanguageController.cs | 3 +- .../Controllers/TechnologyController.cs | 5 +- src/DevHive.Web/Controllers/UserController.cs | 70 +--------------------- .../Models/Identity/User/FriendWebModel.cs | 7 +++ .../Models/Identity/User/UpdateUserWebModel.cs | 9 +++ .../Models/Identity/User/UserWebModel.cs | 4 +- .../Models/Language/UpdateLanguageWebModel.cs | 2 +- .../Models/Technology/UpdateTechnologyWebModel.cs | 2 +- 15 files changed, 44 insertions(+), 87 deletions(-) create mode 100644 src/DevHive.Web/Models/Identity/User/FriendWebModel.cs (limited to 'src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs') diff --git a/src/DevHive.Services/Interfaces/ILanguageService.cs b/src/DevHive.Services/Interfaces/ILanguageService.cs index eb45a8d..08b3812 100644 --- a/src/DevHive.Services/Interfaces/ILanguageService.cs +++ b/src/DevHive.Services/Interfaces/ILanguageService.cs @@ -10,7 +10,7 @@ namespace DevHive.Services.Interfaces Task GetLanguageById(Guid languageId); - Task UpdateLanguage(Guid languageId, UpdateLanguageServiceModel languageServiceModel); + Task UpdateLanguage(UpdateLanguageServiceModel languageServiceModel); Task DeleteLanguage(Guid languageId); } diff --git a/src/DevHive.Services/Interfaces/ITechnologyService.cs b/src/DevHive.Services/Interfaces/ITechnologyService.cs index 0797078..9e1e955 100644 --- a/src/DevHive.Services/Interfaces/ITechnologyService.cs +++ b/src/DevHive.Services/Interfaces/ITechnologyService.cs @@ -10,7 +10,7 @@ namespace DevHive.Services.Interfaces Task GetTechnologyById(Guid id); - Task UpdateTechnology(Guid technologyId, UpdateTechnologyServiceModel updateTechnologyServiceModel); + Task UpdateTechnology(UpdateTechnologyServiceModel updateTechnologyServiceModel); Task DeleteTechnology(Guid id); } diff --git a/src/DevHive.Services/Interfaces/IUserService.cs b/src/DevHive.Services/Interfaces/IUserService.cs index 5ef141f..0f834e9 100644 --- a/src/DevHive.Services/Interfaces/IUserService.cs +++ b/src/DevHive.Services/Interfaces/IUserService.cs @@ -16,7 +16,7 @@ namespace DevHive.Services.Interfaces Task AddLanguageToUser(Guid userId, LanguageServiceModel languageServiceModel); Task AddTechnologyToUser(Guid userId, TechnologyServiceModel technologyServiceModel); - Task GetFriendById(Guid friendId); + Task GetFriend(string username); Task GetUserById(Guid id); Task UpdateUser(UpdateUserServiceModel updateModel); diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index 8825f50..ecf8c42 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -8,8 +8,11 @@ namespace DevHive.Services.Models.Identity.User public class UserServiceModel : BaseUserServiceModel { public IList Roles { get; set; } = new List(); + public IList Friends { get; set; } = new List(); + public IList Languages { get; set; } = new List(); - public IList TechnologyServiceModels { get; set; } = new List(); + + public IList Technologies { get; set; } = new List(); } } diff --git a/src/DevHive.Services/Services/LanguageService.cs b/src/DevHive.Services/Services/LanguageService.cs index be035c2..5b697cd 100644 --- a/src/DevHive.Services/Services/LanguageService.cs +++ b/src/DevHive.Services/Services/LanguageService.cs @@ -48,9 +48,9 @@ namespace DevHive.Services.Services #region Update - public async Task UpdateLanguage(Guid languageId, UpdateLanguageServiceModel languageServiceModel) + public async Task UpdateLanguage(UpdateLanguageServiceModel languageServiceModel) { - bool langExists = await this._languageRepository.DoesLanguageExistAsync(languageId); + bool langExists = await this._languageRepository.DoesLanguageExistAsync(languageServiceModel.Id); bool newLangNameExists = await this._languageRepository.DoesLanguageNameExistAsync(languageServiceModel.Name); if (!langExists) @@ -59,7 +59,6 @@ namespace DevHive.Services.Services if (newLangNameExists) throw new ArgumentException("This name is already in our datbase!"); - languageServiceModel.Id = languageId; Language lang = this._languageMapper.Map(languageServiceModel); return await this._languageRepository.EditAsync(lang); } diff --git a/src/DevHive.Services/Services/TechnologyService.cs b/src/DevHive.Services/Services/TechnologyService.cs index d8b7262..c088281 100644 --- a/src/DevHive.Services/Services/TechnologyService.cs +++ b/src/DevHive.Services/Services/TechnologyService.cs @@ -48,15 +48,14 @@ namespace DevHive.Services.Services #region Update - public async Task UpdateTechnology(Guid technologyId, UpdateTechnologyServiceModel updateTechnologyServiceModel) + public async Task UpdateTechnology(UpdateTechnologyServiceModel updateTechnologyServiceModel) { - if (!await this._technologyRepository.DoesTechnologyExistAsync(technologyId)) + if (!await this._technologyRepository.DoesTechnologyExistAsync(updateTechnologyServiceModel.Id)) throw new ArgumentException("Technology does not exist!"); if (await this._technologyRepository.DoesTechnologyNameExistAsync(updateTechnologyServiceModel.Name)) throw new ArgumentException("Technology name already exists!"); - updateTechnologyServiceModel.Id = technologyId; Technology technology = this._technologyMapper.Map(updateTechnologyServiceModel); bool result = await this._technologyRepository.EditAsync(technology); diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 37dbc7b..d9e87e0 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -160,12 +160,12 @@ namespace DevHive.Services.Services return this._userMapper.Map(user); } - public async Task GetFriendById(Guid friendId) + public async Task GetFriend(string username) { - if (!await _userRepository.DoesUserExistAsync(friendId)) - throw new ArgumentException("User does not exist!"); + User friend = await this._userRepository.GetByUsernameAsync(username); - User friend = await this._userRepository.GetByIdAsync(friendId); + if (default(User) == friend) + throw new ArgumentException("User does not exist!"); return this._userMapper.Map(friend); } diff --git a/src/DevHive.Web/Controllers/LanguageController.cs b/src/DevHive.Web/Controllers/LanguageController.cs index 5202f16..c503a47 100644 --- a/src/DevHive.Web/Controllers/LanguageController.cs +++ b/src/DevHive.Web/Controllers/LanguageController.cs @@ -47,8 +47,9 @@ namespace DevHive.Web.Controllers public async Task Update(Guid languageId, [FromBody] UpdateLanguageWebModel updateModel) { UpdateLanguageServiceModel updatelanguageServiceModel = this._languageMapper.Map(updateModel); + updatelanguageServiceModel.Id = languageId; - bool result = await this._languageService.UpdateLanguage(languageId, updatelanguageServiceModel); + bool result = await this._languageService.UpdateLanguage(updatelanguageServiceModel); if (!result) return new BadRequestObjectResult("Could not update Language"); diff --git a/src/DevHive.Web/Controllers/TechnologyController.cs b/src/DevHive.Web/Controllers/TechnologyController.cs index 3be3b8a..7f2f400 100644 --- a/src/DevHive.Web/Controllers/TechnologyController.cs +++ b/src/DevHive.Web/Controllers/TechnologyController.cs @@ -46,9 +46,10 @@ namespace DevHive.Web.Controllers [HttpPut] public async Task Update(Guid technologyId, [FromBody] UpdateTechnologyWebModel updateModel) { - UpdateTechnologyServiceModel updateTechnologyWebModel = this._technologyMapper.Map(updateModel); + UpdateTechnologyServiceModel updateTechnologyServiceModel = this._technologyMapper.Map(updateModel); + updateTechnologyServiceModel.Id = technologyId; - bool result = await this._technologyService.UpdateTechnology(technologyId, updateTechnologyWebModel); + bool result = await this._technologyService.UpdateTechnology(updateTechnologyServiceModel); if (!result) return new BadRequestObjectResult("Could not update Technology"); diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index b33c3b9..8d48705 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -57,40 +57,6 @@ namespace DevHive.Web.Controllers } #endregion - #region Create - - [HttpPost] - [Route("AddAFriend")] - public async Task AddAFriend(Guid userId, [FromBody] IdModel friendIdModel) - { - return await this._userService.AddFriend(userId, friendIdModel.Id) ? - new OkResult() : - new BadRequestResult(); - } - - [HttpPost] - [Route("AddLanguageToUser")] - public async Task AddLanguageToUser(Guid userId, [FromBody] LanguageWebModel languageWebModel) - { - LanguageServiceModel languageServiceModel = this._userMapper.Map(languageWebModel); - - return await this._userService.AddLanguageToUser(userId, languageServiceModel) ? - new OkResult() : - new BadRequestResult(); - } - - [HttpPost] - [Route("AddTechnologyToUser")] - public async Task AddTechnologyToUser(Guid userId, [FromBody] TechnologyWebModel technologyWebModel) - { - TechnologyServiceModel technologyServiceModel = this._userMapper.Map(technologyWebModel); - - return await this._userService.AddTechnologyToUser(userId, technologyServiceModel) ? - new OkResult() : - new BadRequestResult(); - } - #endregion - #region Read [HttpGet] @@ -107,9 +73,10 @@ namespace DevHive.Web.Controllers [HttpGet] [Route("GetAFriend")] - public async Task GetAFriend(Guid friendId) + [AllowAnonymous] + public async Task GetAFriend(string username) { - UserServiceModel friendServiceModel = await this._userService.GetFriendById(friendId); + UserServiceModel friendServiceModel = await this._userService.GetFriend(username); UserWebModel friend = this._userMapper.Map(friendServiceModel); return new OkObjectResult(friend); @@ -134,7 +101,6 @@ namespace DevHive.Web.Controllers #endregion #region Delete - [HttpDelete] public async Task Delete(Guid id, [FromHeader] string authorization) { @@ -144,36 +110,6 @@ namespace DevHive.Web.Controllers await this._userService.DeleteUser(id); return new OkResult(); } - - [HttpDelete] - [Route("RemoveAFriend")] - public async Task RemoveAFriend(Guid userId, Guid friendId) - { - await this._userService.RemoveFriend(userId, friendId); - return new OkResult(); - } - - [HttpDelete] - [Route("RemoveLanguageFromUser")] - public async Task RemoveLanguageFromUser(Guid userId, [FromBody] LanguageWebModel languageWebModel) - { - LanguageServiceModel languageServiceModel = this._userMapper.Map(languageWebModel); - - return await this._userService.RemoveLanguageFromUser(userId, languageServiceModel) ? - new OkResult() : - new BadRequestResult(); - } - - [HttpDelete] - [Route("RemoveTechnologyFromUser")] - public async Task RemoveTechnologyFromUser(Guid userId, [FromBody] TechnologyWebModel technologyWebModel) - { - TechnologyServiceModel technologyServiceModel = this._userMapper.Map(technologyWebModel); - - return await this._userService.RemoveTechnologyFromUser(userId, technologyServiceModel) ? - new OkResult() : - new BadRequestResult(); - } #endregion } } diff --git a/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs b/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs new file mode 100644 index 0000000..681529a --- /dev/null +++ b/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Web.Models.Identity.User +{ + public class FriendWebModel + { + public string Username { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index 9e41eb6..782c558 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -1,5 +1,8 @@ +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using DevHive.Web.Models.Identity.Validation; +using DevHive.Web.Models.Language; +using DevHive.Web.Models.Technology; namespace DevHive.Web.Models.Identity.User { @@ -8,5 +11,11 @@ namespace DevHive.Web.Models.Identity.User [Required] [GoodPassword] public string Password { get; set; } + + public IList Friends { get; set; } + + public IList Languages { get; set; } + + public IList Technologies { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 8f7995c..3b243e7 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using DevHive.Common.Models.Identity; using DevHive.Web.Models.Identity.Role; using DevHive.Web.Models.Language; using DevHive.Web.Models.Technology; @@ -9,8 +8,11 @@ namespace DevHive.Web.Models.Identity.User public class UserWebModel : BaseUserWebModel { public IList Roles { get; set; } = new List(); + public IList Friends { get; set; } = new List(); + public IList Languages { get; set; } = new List(); + public IList Technologies { get; set; } = new List(); } } diff --git a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs index ed3b37c..18d945f 100644 --- a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs +++ b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs @@ -2,7 +2,7 @@ using System; namespace DevHive.Web.Models.Language { - public class UpdateLanguageWebModel : LanguageWebModel + public class UpdateLanguageWebModel { public string Name { get; set; } } diff --git a/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs b/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs index 8bf48bf..4651b9e 100644 --- a/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs +++ b/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs @@ -2,7 +2,7 @@ using System; namespace DevHive.Web.Models.Technology { - public class UpdateTechnologyWebModel : TechnologyWebModel + public class UpdateTechnologyWebModel { public string Name { get; set; } } -- cgit v1.2.3 From 8560caa13b7f7d0e80ee712c77efc59a80e8935c Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 19 Jan 2021 21:46:06 +0200 Subject: Added attributes to the Web models for validations --- .../Attributes/GoodPasswordModelValidation.cs | 24 +++++++++++ .../Attributes/OnlyAlphanumericsModelValidation.cs | 20 +++++++++ .../Attributes/OnlyLettersModelValidation.cs | 20 +++++++++ .../Extensions/ConfigureCustomMiddleware.cs | 2 +- src/DevHive.Web/Middleware/ExceptionMiddleware.cs | 50 ++++++++++++++++++++++ .../Models/Identity/Role/CreateRoleWebModel.cs | 7 +++ .../Models/Identity/Role/RoleWebModel.cs | 7 +++ .../Models/Identity/Role/UpdateRoleWebModel.cs | 4 ++ .../Models/Identity/User/BaseUserWebModel.cs | 7 ++- .../Models/Identity/User/FriendWebModel.cs | 11 ++++- .../Models/Identity/User/LoginWebModel.cs | 13 ++++++ .../Models/Identity/User/RegisterWebModel.cs | 4 +- .../Models/Identity/User/UpdateUserWebModel.cs | 10 ++++- .../Models/Identity/User/UserWebModel.cs | 10 +++++ .../Validation/GoodPasswordModelValidation.cs | 24 ----------- .../Validation/OnlyAlphanumericsModelValidation.cs | 20 --------- .../Validation/OnlyLettersModelValidation.cs | 20 --------- .../Models/Language/CreateLanguageWebModel.cs | 9 +++- .../Models/Language/LanguageWebModel.cs | 6 ++- .../Models/Language/ReadLanguageWebModel.cs | 7 +++ .../Models/Language/UpdateLanguageWebModel.cs | 7 ++- .../Models/Middleware/ExceptionMiddleware.cs | 50 ---------------------- .../Models/Post/Comment/CommentWebModel.cs | 13 ++++++ .../Models/Post/Post/BasePostWebModel.cs | 9 +++- .../Models/Post/Post/CreatePostWebModel.cs | 6 +-- src/DevHive.Web/Models/Post/Post/PostWebModel.cs | 20 ++++++++- .../Models/Technology/CreateTechnologyWebModel.cs | 7 +++ .../Models/Technology/TechnologyWebModel.cs | 6 ++- .../Models/Technology/UpdateTechnologyWebModel.cs | 6 +++ 29 files changed, 269 insertions(+), 130 deletions(-) create mode 100644 src/DevHive.Web/Attributes/GoodPasswordModelValidation.cs create mode 100644 src/DevHive.Web/Attributes/OnlyAlphanumericsModelValidation.cs create mode 100644 src/DevHive.Web/Attributes/OnlyLettersModelValidation.cs create mode 100644 src/DevHive.Web/Middleware/ExceptionMiddleware.cs delete mode 100644 src/DevHive.Web/Models/Identity/Validation/GoodPasswordModelValidation.cs delete mode 100644 src/DevHive.Web/Models/Identity/Validation/OnlyAlphanumericsModelValidation.cs delete mode 100644 src/DevHive.Web/Models/Identity/Validation/OnlyLettersModelValidation.cs delete mode 100644 src/DevHive.Web/Models/Middleware/ExceptionMiddleware.cs (limited to 'src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs') diff --git a/src/DevHive.Web/Attributes/GoodPasswordModelValidation.cs b/src/DevHive.Web/Attributes/GoodPasswordModelValidation.cs new file mode 100644 index 0000000..7d6a1ea --- /dev/null +++ b/src/DevHive.Web/Attributes/GoodPasswordModelValidation.cs @@ -0,0 +1,24 @@ +using System; +using System.ComponentModel.DataAnnotations; + +namespace DevHive.Web.Attributes +{ + public class GoodPassword : ValidationAttribute + { + public override bool IsValid(object value) + { + var stringValue = (string)value; + + for (int i = 0; i < stringValue.Length; i++) + { + if (Char.IsDigit(stringValue[i])) + { + base.ErrorMessage = "Password must be atleast 5 characters long!"; + return stringValue.Length >= 5; + } + } + base.ErrorMessage = "Password must contain atleast 1 digit!"; + return false; + } + } +} diff --git a/src/DevHive.Web/Attributes/OnlyAlphanumericsModelValidation.cs b/src/DevHive.Web/Attributes/OnlyAlphanumericsModelValidation.cs new file mode 100644 index 0000000..26e0733 --- /dev/null +++ b/src/DevHive.Web/Attributes/OnlyAlphanumericsModelValidation.cs @@ -0,0 +1,20 @@ +using System; +using System.ComponentModel.DataAnnotations; + +namespace DevHive.Web.Attributes +{ + public class OnlyAlphanumerics : ValidationAttribute + { + public override bool IsValid(object value) + { + var stringValue = (string)value; + + foreach (char ch in stringValue) + { + if (!Char.IsLetterOrDigit(ch)) + return false; + } + return true; + } + } +} diff --git a/src/DevHive.Web/Attributes/OnlyLettersModelValidation.cs b/src/DevHive.Web/Attributes/OnlyLettersModelValidation.cs new file mode 100644 index 0000000..07afee9 --- /dev/null +++ b/src/DevHive.Web/Attributes/OnlyLettersModelValidation.cs @@ -0,0 +1,20 @@ +using System; +using System.ComponentModel.DataAnnotations; + +namespace DevHive.Web.Attributes +{ + public class OnlyLetters : ValidationAttribute + { + public override bool IsValid(object value) + { + var stringValue = (string)value; + + foreach (char ch in stringValue) + { + if (!Char.IsLetter(ch)) + return false; + } + return true; + } + } +} diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureCustomMiddleware.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureCustomMiddleware.cs index 24c2adf..efcb8e1 100644 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureCustomMiddleware.cs +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureCustomMiddleware.cs @@ -1,4 +1,4 @@ -using DevHive.Web.Models.Middleware; +using DevHive.Web.Middleware; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; diff --git a/src/DevHive.Web/Middleware/ExceptionMiddleware.cs b/src/DevHive.Web/Middleware/ExceptionMiddleware.cs new file mode 100644 index 0000000..cb6d4ca --- /dev/null +++ b/src/DevHive.Web/Middleware/ExceptionMiddleware.cs @@ -0,0 +1,50 @@ +using System; +using System.Net; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; + +namespace DevHive.Web.Middleware +{ + public class ExceptionMiddleware + { + private readonly RequestDelegate _next; + // private readonly ILogger _logger; + + public ExceptionMiddleware(RequestDelegate next) + { + this._next = next; + // this._logger = logger; + } + // public ExceptionMiddleware(RequestDelegate next, ILogger logger) + // { + // this._logger = logger; + // this._next = next; + // } + + public async Task InvokeAsync(HttpContext httpContext) + { + try + { + await this._next(httpContext); + } + catch (Exception ex) + { + // this._logger.LogError($"Something went wrong: {ex}"); + await HandleExceptionAsync(httpContext, ex); + } + } + + private Task HandleExceptionAsync(HttpContext context, Exception exception) + { + context.Response.ContentType = "application/json"; + context.Response.StatusCode = (int)HttpStatusCode.BadRequest; + + return context.Response.WriteAsync(new + { + StatusCode = context.Response.StatusCode, + Message = exception.Message + }.ToString()); + } + } +} diff --git a/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs index e872428..859cdd9 100644 --- a/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs @@ -1,7 +1,14 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; + namespace DevHive.Web.Models.Identity.Role { public class CreateRoleWebModel { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] public string Name { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs index 41ade23..99b0f50 100644 --- a/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs @@ -1,7 +1,14 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; + namespace DevHive.Web.Models.Identity.Role { public class RoleWebModel { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] public string Name { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs index aaf0270..254affc 100644 --- a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs @@ -1,9 +1,13 @@ using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; namespace DevHive.Web.Models.Identity.Role { public class UpdateRoleWebModel : RoleWebModel { + [NotNull] + [Required] public Guid Id { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs index 2d99786..d7d8d29 100644 --- a/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs @@ -1,26 +1,31 @@ using System.ComponentModel.DataAnnotations; -using DevHive.Web.Models.Identity.Validation; +using System.Diagnostics.CodeAnalysis; +using DevHive.Web.Attributes; namespace DevHive.Web.Models.Identity.User { public class BaseUserWebModel { + [NotNull] [Required] [MinLength(3)] [MaxLength(50)] [OnlyAlphanumerics(ErrorMessage = "Username can only contain letters and digits!")] public string UserName { get; set; } + [NotNull] [Required] [EmailAddress] public string Email { get; set; } + [NotNull] [Required] [MinLength(3)] [MaxLength(30)] [OnlyLetters(ErrorMessage = "First name can only contain letters!")] public string FirstName { get; set; } + [NotNull] [Required] [MinLength(3)] [MaxLength(30)] diff --git a/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs b/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs index 681529a..d59bff5 100644 --- a/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs @@ -1,7 +1,16 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; +using DevHive.Web.Attributes; + namespace DevHive.Web.Models.Identity.User { public class FriendWebModel { - public string Username { get; set; } + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] + [OnlyAlphanumerics(ErrorMessage = "Username can only contain letters and digits!")] + public string UserName { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/LoginWebModel.cs b/src/DevHive.Web/Models/Identity/User/LoginWebModel.cs index 87c7416..0395274 100644 --- a/src/DevHive.Web/Models/Identity/User/LoginWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/LoginWebModel.cs @@ -1,8 +1,21 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; +using DevHive.Web.Attributes; + namespace DevHive.Web.Models.Identity.User { public class LoginWebModel { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] + [OnlyAlphanumerics(ErrorMessage = "Username can only contain letters and digits!")] public string UserName { get; set; } + + [NotNull] + [Required] + [GoodPassword] public string Password { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs b/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs index 04fd6bd..0fc7ec6 100644 --- a/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs @@ -1,10 +1,12 @@ using System.ComponentModel.DataAnnotations; -using DevHive.Web.Models.Identity.Validation; +using System.Diagnostics.CodeAnalysis; +using DevHive.Web.Attributes; namespace DevHive.Web.Models.Identity.User { public class RegisterWebModel : BaseUserWebModel { + [NotNull] [Required] [GoodPassword] public string Password { get; set; } diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index 782c558..724930c 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; -using DevHive.Web.Models.Identity.Validation; +using System.Diagnostics.CodeAnalysis; +using DevHive.Web.Attributes; using DevHive.Web.Models.Language; using DevHive.Web.Models.Technology; @@ -8,14 +9,21 @@ namespace DevHive.Web.Models.Identity.User { public class UpdateUserWebModel : BaseUserWebModel { + [NotNull] [Required] [GoodPassword] public string Password { get; set; } + [NotNull] + [Required] public IList Friends { get; set; } + [NotNull] + [Required] public IList Languages { get; set; } + [NotNull] + [Required] public IList Technologies { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 3b243e7..88f199d 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -1,4 +1,6 @@ using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; using DevHive.Web.Models.Identity.Role; using DevHive.Web.Models.Language; using DevHive.Web.Models.Technology; @@ -7,12 +9,20 @@ namespace DevHive.Web.Models.Identity.User { public class UserWebModel : BaseUserWebModel { + [NotNull] + [Required] public IList Roles { get; set; } = new List(); + [NotNull] + [Required] public IList Friends { get; set; } = new List(); + [NotNull] + [Required] public IList Languages { get; set; } = new List(); + [NotNull] + [Required] public IList Technologies { get; set; } = new List(); } } diff --git a/src/DevHive.Web/Models/Identity/Validation/GoodPasswordModelValidation.cs b/src/DevHive.Web/Models/Identity/Validation/GoodPasswordModelValidation.cs deleted file mode 100644 index f920c35..0000000 --- a/src/DevHive.Web/Models/Identity/Validation/GoodPasswordModelValidation.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; - -namespace DevHive.Web.Models.Identity.Validation -{ - public class GoodPassword : ValidationAttribute - { - public override bool IsValid(object value) - { - var stringValue = (string)value; - - for (int i = 0; i < stringValue.Length; i++) - { - if (Char.IsDigit(stringValue[i])) - { - base.ErrorMessage = "Password must be atleast 5 characters long!"; - return stringValue.Length >= 5; - } - } - base.ErrorMessage = "Password must contain atleast 1 digit!"; - return false; - } - } -} diff --git a/src/DevHive.Web/Models/Identity/Validation/OnlyAlphanumericsModelValidation.cs b/src/DevHive.Web/Models/Identity/Validation/OnlyAlphanumericsModelValidation.cs deleted file mode 100644 index 5c8c66c..0000000 --- a/src/DevHive.Web/Models/Identity/Validation/OnlyAlphanumericsModelValidation.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; - -namespace DevHive.Web.Models.Identity.Validation -{ - public class OnlyAlphanumerics : ValidationAttribute - { - public override bool IsValid(object value) - { - var stringValue = (string)value; - - foreach (char ch in stringValue) - { - if (!Char.IsLetterOrDigit(ch)) - return false; - } - return true; - } - } -} diff --git a/src/DevHive.Web/Models/Identity/Validation/OnlyLettersModelValidation.cs b/src/DevHive.Web/Models/Identity/Validation/OnlyLettersModelValidation.cs deleted file mode 100644 index 29a995a..0000000 --- a/src/DevHive.Web/Models/Identity/Validation/OnlyLettersModelValidation.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; - -namespace DevHive.Web.Models.Identity.Validation -{ - public class OnlyLetters : ValidationAttribute - { - public override bool IsValid(object value) - { - var stringValue = (string)value; - - foreach (char ch in stringValue) - { - if (!Char.IsLetter(ch)) - return false; - } - return true; - } - } -} diff --git a/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs b/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs index d261500..a739e7f 100644 --- a/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs +++ b/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs @@ -1,9 +1,14 @@ -using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; namespace DevHive.Web.Models.Language { public class CreateLanguageWebModel { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] public string Name { get; set; } } -} \ No newline at end of file +} diff --git a/src/DevHive.Web/Models/Language/LanguageWebModel.cs b/src/DevHive.Web/Models/Language/LanguageWebModel.cs index 455b559..7515e70 100644 --- a/src/DevHive.Web/Models/Language/LanguageWebModel.cs +++ b/src/DevHive.Web/Models/Language/LanguageWebModel.cs @@ -1,9 +1,13 @@ using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; namespace DevHive.Web.Models.Language { public class LanguageWebModel { + [NotNull] + [Required] public Guid Id { get; set; } } -} \ No newline at end of file +} diff --git a/src/DevHive.Web/Models/Language/ReadLanguageWebModel.cs b/src/DevHive.Web/Models/Language/ReadLanguageWebModel.cs index f1e0ecc..ab4a089 100644 --- a/src/DevHive.Web/Models/Language/ReadLanguageWebModel.cs +++ b/src/DevHive.Web/Models/Language/ReadLanguageWebModel.cs @@ -1,7 +1,14 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; + namespace DevHive.Web.Models.Language { public class ReadLanguageWebModel { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] public string Name { get; set; } } } diff --git a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs index 18d945f..128d534 100644 --- a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs +++ b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs @@ -1,9 +1,14 @@ -using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; namespace DevHive.Web.Models.Language { public class UpdateLanguageWebModel { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] public string Name { get; set; } } } diff --git a/src/DevHive.Web/Models/Middleware/ExceptionMiddleware.cs b/src/DevHive.Web/Models/Middleware/ExceptionMiddleware.cs deleted file mode 100644 index c57452e..0000000 --- a/src/DevHive.Web/Models/Middleware/ExceptionMiddleware.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Net; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Logging; - -namespace DevHive.Web.Models.Middleware -{ - public class ExceptionMiddleware - { - private readonly RequestDelegate _next; - // private readonly ILogger _logger; - - public ExceptionMiddleware(RequestDelegate next) - { - this._next = next; - // this._logger = logger; - } - // public ExceptionMiddleware(RequestDelegate next, ILogger logger) - // { - // this._logger = logger; - // this._next = next; - // } - - public async Task InvokeAsync(HttpContext httpContext) - { - try - { - await this._next(httpContext); - } - catch (Exception ex) - { - // this._logger.LogError($"Something went wrong: {ex}"); - await HandleExceptionAsync(httpContext, ex); - } - } - - private Task HandleExceptionAsync(HttpContext context, Exception exception) - { - context.Response.ContentType = "application/json"; - context.Response.StatusCode = (int)HttpStatusCode.BadRequest; - - return context.Response.WriteAsync(new - { - StatusCode = context.Response.StatusCode, - Message = exception.Message - }.ToString()); - } - } -} diff --git a/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs b/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs index d66e5c9..590851d 100644 --- a/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs +++ b/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs @@ -1,12 +1,25 @@ using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; namespace DevHive.Web.Models.Post.Comment { public class CommentWebModel { + [NotNull] + [Required] public Guid IssuerId { get; set; } + + [NotNull] + [Required] public Guid PostId { get; set; } + + [NotNull] + [Required] public string Message { get; set; } + + [NotNull] + [Required] public DateTime TimeCreated { get; set; } } } diff --git a/src/DevHive.Web/Models/Post/Post/BasePostWebModel.cs b/src/DevHive.Web/Models/Post/Post/BasePostWebModel.cs index caa9925..35ddd34 100644 --- a/src/DevHive.Web/Models/Post/Post/BasePostWebModel.cs +++ b/src/DevHive.Web/Models/Post/Post/BasePostWebModel.cs @@ -1,10 +1,17 @@ using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; namespace DevHive.Web.Models.Post.Post { public class BasePostWebModel { + [NotNull] + [Required] public Guid IssuerId { get; set; } + + [NotNull] + [Required] public string Message { get; set; } } -} \ No newline at end of file +} diff --git a/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs b/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs index 7558b2c..389ff9e 100644 --- a/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs +++ b/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs @@ -2,7 +2,5 @@ using System; namespace DevHive.Web.Models.Post.Post { - public class CreatePostWebModel : BasePostWebModel - { - } -} \ No newline at end of file + public class CreatePostWebModel : BasePostWebModel { } +} diff --git a/src/DevHive.Web/Models/Post/Post/PostWebModel.cs b/src/DevHive.Web/Models/Post/Post/PostWebModel.cs index fa18c3a..fe35cee 100644 --- a/src/DevHive.Web/Models/Post/Post/PostWebModel.cs +++ b/src/DevHive.Web/Models/Post/Post/PostWebModel.cs @@ -1,3 +1,6 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; +using DevHive.Web.Attributes; using DevHive.Web.Models.Post.Comment; namespace DevHive.Web.Models.Post.Post @@ -6,16 +9,31 @@ namespace DevHive.Web.Models.Post.Post { //public string Picture { get; set; } + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] public string IssuerFirstName { get; set; } + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] public string IssuerLastName { get; set; } + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] + [OnlyAlphanumerics(ErrorMessage = "Username can only contain letters and digits!")] public string IssuerUsername { get; set; } + [NotNull] + [Required] public string Message { get; set; } //public Files[] Files { get; set; } public CommentWebModel[] Comments { get; set; } } -} \ No newline at end of file +} diff --git a/src/DevHive.Web/Models/Technology/CreateTechnologyWebModel.cs b/src/DevHive.Web/Models/Technology/CreateTechnologyWebModel.cs index 13029ee..ec9ec15 100644 --- a/src/DevHive.Web/Models/Technology/CreateTechnologyWebModel.cs +++ b/src/DevHive.Web/Models/Technology/CreateTechnologyWebModel.cs @@ -1,7 +1,14 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; + namespace DevHive.Web.Models.Technology { public class CreateTechnologyWebModel { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] public string Name { get; set; } } } diff --git a/src/DevHive.Web/Models/Technology/TechnologyWebModel.cs b/src/DevHive.Web/Models/Technology/TechnologyWebModel.cs index 05f7af8..6e8273b 100644 --- a/src/DevHive.Web/Models/Technology/TechnologyWebModel.cs +++ b/src/DevHive.Web/Models/Technology/TechnologyWebModel.cs @@ -1,9 +1,13 @@ using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; namespace DevHive.Web.Models.Technology { public class TechnologyWebModel { + [NotNull] + [Required] public Guid Id { get; set; } } -} \ No newline at end of file +} diff --git a/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs b/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs index 4651b9e..3e9fe2a 100644 --- a/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs +++ b/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs @@ -1,9 +1,15 @@ using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; namespace DevHive.Web.Models.Technology { public class UpdateTechnologyWebModel { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] public string Name { get; set; } } } -- cgit v1.2.3 From 8179af787a7bf375753a178b89111a91d84a8bb8 Mon Sep 17 00:00:00 2001 From: transtrike Date: Wed, 20 Jan 2021 18:07:58 +0200 Subject: Changed List to HashSet for "no duplicates" scenario --- src/DevHive.Data/Interfaces/Models/ILanguage.cs | 2 +- src/DevHive.Data/Interfaces/Models/IRole.cs | 2 +- src/DevHive.Data/Interfaces/Models/ITechnology.cs | 2 +- src/DevHive.Data/Interfaces/Models/IUser.cs | 8 ++++---- .../Interfaces/Repositories/IUserRepository.cs | 4 ++-- src/DevHive.Data/Models/Language.cs | 2 +- src/DevHive.Data/Models/Role.cs | 2 +- src/DevHive.Data/Models/Technology.cs | 2 +- src/DevHive.Data/Models/User.cs | 8 ++++---- src/DevHive.Data/Repositories/UserRepository.cs | 4 ++-- .../Models/Identity/User/RegisterServiceModel.cs | 4 ++-- .../Models/Identity/User/UpdateUserServiceModel.cs | 8 ++++---- .../Models/Identity/User/UserServiceModel.cs | 8 ++++---- src/DevHive.Services/Services/PostService.cs | 5 +++-- src/DevHive.Services/Services/UserService.cs | 12 ++++++------ .../DevHive.Data.Tests/UserRepositoryTests.cs | 18 +++++++++--------- .../Models/Identity/User/UpdateUserWebModel.cs | 6 +++--- src/DevHive.Web/Models/Identity/User/UserWebModel.cs | 8 ++++---- 18 files changed, 53 insertions(+), 52 deletions(-) (limited to 'src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs') diff --git a/src/DevHive.Data/Interfaces/Models/ILanguage.cs b/src/DevHive.Data/Interfaces/Models/ILanguage.cs index 9549777..b77d5ae 100644 --- a/src/DevHive.Data/Interfaces/Models/ILanguage.cs +++ b/src/DevHive.Data/Interfaces/Models/ILanguage.cs @@ -6,7 +6,7 @@ namespace DevHive.Data.Interfaces.Models public interface ILanguage : IModel { string Name { get; set; } - List Users { get; set; } + HashSet Users { get; set; } } } diff --git a/src/DevHive.Data/Interfaces/Models/IRole.cs b/src/DevHive.Data/Interfaces/Models/IRole.cs index 0623f07..c8b7068 100644 --- a/src/DevHive.Data/Interfaces/Models/IRole.cs +++ b/src/DevHive.Data/Interfaces/Models/IRole.cs @@ -5,6 +5,6 @@ namespace DevHive.Data.Interfaces.Models { public interface IRole { - List Users { get; set; } + HashSet Users { get; set; } } } diff --git a/src/DevHive.Data/Interfaces/Models/ITechnology.cs b/src/DevHive.Data/Interfaces/Models/ITechnology.cs index 159a21a..153f75f 100644 --- a/src/DevHive.Data/Interfaces/Models/ITechnology.cs +++ b/src/DevHive.Data/Interfaces/Models/ITechnology.cs @@ -6,6 +6,6 @@ namespace DevHive.Data.Interfaces.Models public interface ITechnology : IModel { string Name { get; set; } - List Users { get; set; } + HashSet Users { get; set; } } } diff --git a/src/DevHive.Data/Interfaces/Models/IUser.cs b/src/DevHive.Data/Interfaces/Models/IUser.cs index ef8c927..08ce385 100644 --- a/src/DevHive.Data/Interfaces/Models/IUser.cs +++ b/src/DevHive.Data/Interfaces/Models/IUser.cs @@ -8,9 +8,9 @@ namespace DevHive.Data.Interfaces.Models string FirstName { get; set; } string LastName { get; set; } string ProfilePictureUrl { get; set; } - IList Languages { get; set; } - IList Technologies { get; set; } - IList Roles { get; set; } - IList Friends { get; set; } + HashSet Languages { get; set; } + HashSet Technologies { get; set; } + HashSet Roles { get; set; } + HashSet Friends { get; set; } } } diff --git a/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs index eca6adb..456eb94 100644 --- a/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs +++ b/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs @@ -14,8 +14,8 @@ namespace DevHive.Data.Interfaces.Repositories Task GetByUsernameAsync(string username); Language GetUserLanguage(User user, Language language); - IList GetUserLanguages(User user); - IList GetUserTechnologies(User user); + HashSet GetUserLanguages(User user); + HashSet GetUserTechnologies(User user); Technology GetUserTechnology(User user, Technology technology); IEnumerable QueryAll(); diff --git a/src/DevHive.Data/Models/Language.cs b/src/DevHive.Data/Models/Language.cs index 0db8cb6..f2b2786 100644 --- a/src/DevHive.Data/Models/Language.cs +++ b/src/DevHive.Data/Models/Language.cs @@ -8,6 +8,6 @@ namespace DevHive.Data.Models { public Guid Id { get; set; } public string Name { get; set; } - public List Users { get; set; } + public HashSet Users { get; set; } } } diff --git a/src/DevHive.Data/Models/Role.cs b/src/DevHive.Data/Models/Role.cs index e63f007..e0855aa 100644 --- a/src/DevHive.Data/Models/Role.cs +++ b/src/DevHive.Data/Models/Role.cs @@ -12,6 +12,6 @@ namespace DevHive.Data.Models public const string DefaultRole = "User"; public const string AdminRole = "Admin"; - public List Users { get; set; } + public HashSet Users { get; set; } } } diff --git a/src/DevHive.Data/Models/Technology.cs b/src/DevHive.Data/Models/Technology.cs index 9096cbe..a0728d5 100644 --- a/src/DevHive.Data/Models/Technology.cs +++ b/src/DevHive.Data/Models/Technology.cs @@ -8,6 +8,6 @@ namespace DevHive.Data.Models { public Guid Id { get; set; } public string Name { get; set; } - public List Users { get; set; } + public HashSet Users { get; set; } } } diff --git a/src/DevHive.Data/Models/User.cs b/src/DevHive.Data/Models/User.cs index cf779f5..2ac7adf 100644 --- a/src/DevHive.Data/Models/User.cs +++ b/src/DevHive.Data/Models/User.cs @@ -19,15 +19,15 @@ namespace DevHive.Data.Models /// Languages that the user uses or is familiar with /// // [Unique] - public IList Languages { get; set; } + public HashSet Languages { get; set; } /// /// Technologies that the user uses or is familiar with /// - public IList Technologies { get; set; } = new List(); + public HashSet Technologies { get; set; } = new HashSet(); - public IList Roles { get; set; } = new List(); + public HashSet Roles { get; set; } = new HashSet(); - public IList Friends { get; set; } = new List(); + public HashSet Friends { get; set; } = new HashSet(); } } diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 2ca8099..492d46b 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -82,7 +82,7 @@ namespace DevHive.Data.Repositories .FirstOrDefaultAsync(x => x.UserName == username); } - public IList GetUserLanguages(User user) + public HashSet GetUserLanguages(User user) { return user.Languages; } @@ -93,7 +93,7 @@ namespace DevHive.Data.Repositories .FirstOrDefault(x => x.Id == language.Id); } - public IList GetUserTechnologies(User user) + public HashSet GetUserTechnologies(User user) { return user.Technologies; } diff --git a/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs b/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs index 74f66b4..3171ea6 100644 --- a/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs @@ -6,8 +6,8 @@ namespace DevHive.Services.Models.Identity.User { public class RegisterServiceModel : BaseUserServiceModel { - public IList Languages { get; set; } - public IList Technologies { get; set; } + public HashSet Languages { get; set; } + public HashSet Technologies { get; set; } public string Password { get; set; } } } diff --git a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs index 5b5a178..87af43a 100644 --- a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs @@ -7,13 +7,13 @@ namespace DevHive.Services.Models.Identity.User { public Guid Id { get; set; } - public IList Roles { get; set; } = new List(); + public HashSet Roles { get; set; } = new HashSet(); - public IList Friends { get; set; } = new List(); + public HashSet Friends { get; set; } = new HashSet(); - public IList Languages { get; set; } = new List(); + public HashSet Languages { get; set; } = new HashSet(); - public IList Technologies { get; set; } = new List(); + public HashSet Technologies { get; set; } = new HashSet(); } } diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index 913b5c0..5fcd494 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -7,12 +7,12 @@ namespace DevHive.Services.Models.Identity.User { public class UserServiceModel : BaseUserServiceModel { - public IList Roles { get; set; } = new List(); + public HashSet Roles { get; set; } = new HashSet(); - public IList Friends { get; set; } = new List(); + public HashSet Friends { get; set; } = new HashSet(); - public IList Languages { get; set; } = new List(); + public HashSet Languages { get; set; } = new HashSet(); - public IList Technologies { get; set; } = new List(); + public HashSet Technologies { get; set; } = new HashSet(); } } diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index f2f60d1..9503b8a 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -9,6 +9,7 @@ using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using DevHive.Services.Interfaces; using DevHive.Data.Interfaces.Repositories; +using System.Linq; namespace DevHive.Services.Services { @@ -131,8 +132,8 @@ namespace DevHive.Services.Services { var jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7)); - string jwtUserName = this.GetClaimTypeValues("unique_name", jwt.Claims)[0]; - //List jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims); + string jwtUserName = this.GetClaimTypeValues("unique_name", jwt.Claims).First(); + //HashSet jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims); User user = await this._userRepository.GetByUsernameAsync(jwtUserName) ?? throw new ArgumentException("User does not exist!"); diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index ee4b24d..51c4432 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -75,7 +75,7 @@ namespace DevHive.Services.Services // Set the default role to the user Role defaultRole = await this._roleRepository.GetByNameAsync(Role.DefaultRole); - user.Roles = new List() { defaultRole }; + user.Roles = new HashSet() { defaultRole }; await this._userRepository.AddAsync(user); @@ -144,12 +144,12 @@ namespace DevHive.Services.Services await this.ValidateUserCollections(updateUserServiceModel); - List languages = new(); + HashSet languages = new(); foreach (UpdateUserCollectionServiceModel lang in updateUserServiceModel.Languages) languages.Add(await this._languageRepository.GetByNameAsync(lang.Name) ?? throw new ArgumentException("Invalid language name!")); - List technologies = new(); + HashSet technologies = new(); foreach (UpdateUserCollectionServiceModel tech in updateUserServiceModel.Technologies) technologies.Add(await this._technologyRepository.GetByNameAsync(tech.Name) ?? throw new ArgumentException("Invalid technology name!")); @@ -257,7 +257,7 @@ namespace DevHive.Services.Services // There is authorization name in the beginning, i.e. "Bearer eyJh..." var jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7)); - Guid jwtUserID = new Guid(this.GetClaimTypeValues("ID", jwt.Claims)[0]); + Guid jwtUserID = new Guid(this.GetClaimTypeValues("ID", jwt.Claims).First()); List jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims); User user = await this._userRepository.GetByIdAsync(jwtUserID) @@ -326,11 +326,11 @@ namespace DevHive.Services.Services } } - private string WriteJWTSecurityToken(Guid userId, IList roles) + private string WriteJWTSecurityToken(Guid userId, HashSet roles) { byte[] signingKey = Encoding.ASCII.GetBytes(_jwtOptions.Secret); - List claims = new() + HashSet claims = new() { new Claim("ID", $"{userId}"), }; diff --git a/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs b/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs index b0a5b93..be116b0 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs +++ b/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs @@ -165,10 +165,10 @@ namespace DevHive.Data.Tests //Arrange User dummyUser = CreateDummyUser(); await this._userRepository.AddAsync(dummyUser); - IList dummyUserLanguages = dummyUser.Languages; + HashSet dummyUserLanguages = dummyUser.Languages; //Act - IList languages = this._userRepository.GetUserLanguages(dummyUser); + HashSet languages = this._userRepository.GetUserLanguages(dummyUser); //Assert Assert.AreEqual(dummyUserLanguages, languages, "Method doesn't query languages properly"); @@ -185,7 +185,7 @@ namespace DevHive.Data.Tests // Language dummyLang = await this._languageRepository.GetByNameAsync("csharp"); // //Act - // IList languages = this._userRepository.GetUserLanguage(dummyUser, dummyLang); + // HashSet languages = this._userRepository.GetUserLanguage(dummyUser, dummyLang); // //Assert // Assert.AreEqual(dummyUserLanguages, languages, "Method doesn't query languages properly"); @@ -195,7 +195,7 @@ namespace DevHive.Data.Tests #region HelperMethods private User CreateDummyUser() { - List languages = new() + HashSet languages = new() { new Language() { @@ -204,7 +204,7 @@ namespace DevHive.Data.Tests }, }; - List technologies = new() + HashSet technologies = new() { new Technology() { @@ -213,7 +213,7 @@ namespace DevHive.Data.Tests }, }; - List roles = new() + HashSet roles = new() { new Role() { @@ -237,7 +237,7 @@ namespace DevHive.Data.Tests private User CreateAnotherDummyUser() { - List languages = new() + HashSet languages = new() { new Language() { @@ -246,7 +246,7 @@ namespace DevHive.Data.Tests }, }; - List technologies = new() + HashSet technologies = new() { new Technology() { @@ -255,7 +255,7 @@ namespace DevHive.Data.Tests }, }; - List roles = new() + HashSet roles = new() { new Role() { diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index 724930c..3c38ab6 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -16,14 +16,14 @@ namespace DevHive.Web.Models.Identity.User [NotNull] [Required] - public IList Friends { get; set; } + public HashSet Friends { get; set; } [NotNull] [Required] - public IList Languages { get; set; } + public HashSet Languages { get; set; } [NotNull] [Required] - public IList Technologies { get; set; } + public HashSet Technologies { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 1d2d17b..5b80ba3 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -11,18 +11,18 @@ namespace DevHive.Web.Models.Identity.User { [NotNull] [Required] - public IList Roles { get; set; } = new List(); + public HashSet Roles { get; set; } = new HashSet(); [NotNull] [Required] - public IList Friends { get; set; } = new List(); + public HashSet Friends { get; set; } = new HashSet(); [NotNull] [Required] - public IList Languages { get; set; } = new List(); + public HashSet Languages { get; set; } = new HashSet(); [NotNull] [Required] - public IList Technologies { get; set; } = new List(); + public HashSet Technologies { get; set; } = new HashSet(); } } -- cgit v1.2.3 From 9e86699c9b3aff17e0c4d19850b41b792a9625ef Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 21 Jan 2021 19:12:04 +0200 Subject: Removed HTTP Patch; Refactored HTTP Put; Fixed Update bug --- .../Repositories/LanguageRepository.cs | 7 +- .../Repositories/TechnologyRepository.cs | 1 + src/DevHive.Data/Repositories/UserRepository.cs | 15 ++- .../Configurations/Mapping/RoleMapings.cs | 2 +- .../Configurations/Mapping/TechnologyMappings.cs | 2 + .../Mapping/UserCollectionMappings.cs | 22 ---- src/DevHive.Services/Interfaces/IUserService.cs | 1 - .../Models/Identity/Role/CreateRoleServiceModel.cs | 14 +++ .../Models/Identity/User/FriendServiceModel.cs | 3 + .../Identity/User/UpdateFriendServiceModel.cs | 10 ++ .../User/UpdateUserCollectionServiceModel.cs | 7 -- .../Models/Identity/User/UpdateUserServiceModel.cs | 11 +- src/DevHive.Services/Services/UserService.cs | 135 +++++++++------------ .../Configurations/Mapping/LanguageMappings.cs | 7 +- .../Configurations/Mapping/RoleMappings.cs | 9 +- .../Configurations/Mapping/TechnologyMappings.cs | 3 +- .../Configurations/Mapping/UserMappings.cs | 13 +- src/DevHive.Web/Controllers/UserController.cs | 14 --- .../Models/Identity/User/UpdateUserWebModel.cs | 5 + 19 files changed, 132 insertions(+), 149 deletions(-) delete mode 100644 src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs create mode 100644 src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs create mode 100644 src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs (limited to 'src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs') diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index 108b307..4c51cf3 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -20,8 +20,7 @@ namespace DevHive.Data.Repositories public async Task AddAsync(Language entity) { - await this._context - .Set() + await this._context.Languages .AddAsync(entity); return await this.SaveChangesAsync(this._context); @@ -32,14 +31,14 @@ namespace DevHive.Data.Repositories public async Task GetByIdAsync(Guid id) { - return await this._context - .Set() + return await this._context.Languages .FindAsync(id); } public async Task GetByNameAsync(string languageName) { return await this._context.Languages + .AsNoTracking() .FirstOrDefaultAsync(x => x.Name == languageName); } #endregion diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index 390ad3f..a41d4fb 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -40,6 +40,7 @@ namespace DevHive.Data.Repositories public async Task GetByNameAsync(string technologyName) { return await this._context.Technologies + .AsNoTracking() .FirstOrDefaultAsync(x => x.Name == technologyName); } #endregion diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 3f9af70..c769f7e 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -6,6 +6,7 @@ using DevHive.Common.Models.Misc; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.ChangeTracking; namespace DevHive.Data.Repositories { @@ -78,7 +79,9 @@ namespace DevHive.Data.Repositories public async Task GetByUsernameAsync(string username) { return await this._context.Users - .Include(u => u.Roles) + .AsNoTracking() + .Include(x => x.Languages) + .Include(x => x.Technologies) .FirstOrDefaultAsync(x => x.UserName == username); } @@ -107,9 +110,13 @@ namespace DevHive.Data.Repositories #region Update - public async Task EditAsync(User newEntity) + public async Task EditAsync(User entity) { - this._context.Update(newEntity); + User user = await this._context.Users + .FirstOrDefaultAsync(x => x.Id == entity.Id); + + this._context.Update(user); + this._context.Entry(entity).CurrentValues.SetValues(entity); return await this.SaveChangesAsync(this._context); } @@ -177,6 +184,7 @@ namespace DevHive.Data.Repositories public async Task DoesUserExistAsync(Guid id) { return await this._context.Users + .AsNoTracking() .AnyAsync(x => x.Id == id); } @@ -208,6 +216,7 @@ namespace DevHive.Data.Repositories public bool DoesUserHaveThisUsername(Guid id, string username) { return this._context.Users + .AsNoTracking() .Any(x => x.Id == id && x.UserName == username); } diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index b5541f9..d6c8511 100644 --- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs +++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs @@ -12,7 +12,7 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs index 079ec3e..0103ccf 100644 --- a/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs @@ -11,8 +11,10 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); CreateMap(); + CreateMap(); CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs deleted file mode 100644 index 7a773e8..0000000 --- a/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs +++ /dev/null @@ -1,22 +0,0 @@ -using AutoMapper; -using DevHive.Data.Models; -using DevHive.Services.Models.Identity.User; - -namespace DevHive.Services.Configurations.Mapping -{ - public class UserCollectionMappings : Profile - { - public UserCollectionMappings() - { - CreateMap(); - CreateMap(); - CreateMap(); - CreateMap(); - - CreateMap(); - CreateMap(); - CreateMap(); - CreateMap(); - } - } -} diff --git a/src/DevHive.Services/Interfaces/IUserService.cs b/src/DevHive.Services/Interfaces/IUserService.cs index 88be0c8..923e9bb 100644 --- a/src/DevHive.Services/Interfaces/IUserService.cs +++ b/src/DevHive.Services/Interfaces/IUserService.cs @@ -18,7 +18,6 @@ namespace DevHive.Services.Interfaces Task GetUserById(Guid id); Task UpdateUser(UpdateUserServiceModel updateModel); - Task PatchUser(Guid id, List patch); Task DeleteUser(Guid id); Task RemoveFriend(Guid userId, Guid friendId); diff --git a/src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs b/src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs new file mode 100644 index 0000000..53bea9e --- /dev/null +++ b/src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs @@ -0,0 +1,14 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; + +namespace DevHive.Services.Models.Identity.Role +{ + public class CreateRoleServiceModel + { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs b/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs index 63d57f7..a784f5c 100644 --- a/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs @@ -1,7 +1,10 @@ +using System; + namespace DevHive.Services.Models.Identity.User { public class FriendServiceModel { + public Guid Id { get; set; } public string UserName { get; set; } } } diff --git a/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs new file mode 100644 index 0000000..83fcc34 --- /dev/null +++ b/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace DevHive.Services.Models.Identity.User +{ + public class UpdateFriendServiceModel + { + public Guid Id { get; set; } + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs deleted file mode 100644 index c40a980..0000000 --- a/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace DevHive.Services.Models.Identity.User -{ - public class UpdateUserCollectionServiceModel - { - public string Name { get; set; } - } -} diff --git a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs index 835bf54..9277e3e 100644 --- a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs @@ -1,5 +1,8 @@ using System; using System.Collections.Generic; +using DevHive.Services.Models.Identity.Role; +using DevHive.Services.Models.Language; +using DevHive.Services.Models.Technology; namespace DevHive.Services.Models.Identity.User { @@ -9,13 +12,13 @@ namespace DevHive.Services.Models.Identity.User public string Password { get; set; } - public HashSet Roles { get; set; } = new HashSet(); + public HashSet Roles { get; set; } = new HashSet(); - public HashSet Friends { get; set; } = new HashSet(); + public HashSet Friends { get; set; } = new HashSet(); - public HashSet Languages { get; set; } = new HashSet(); + public HashSet Languages { get; set; } = new HashSet(); - public HashSet Technologies { get; set; } = new HashSet(); + public HashSet Technologies { get; set; } = new HashSet(); } } diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index dca00fa..a57fd23 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -14,8 +14,9 @@ using DevHive.Services.Interfaces; using DevHive.Data.Interfaces.Repositories; using System.Linq; using DevHive.Common.Models.Misc; -using System.Reflection; -using Microsoft.CodeAnalysis.CSharp.Syntax; +using DevHive.Services.Models.Language; +using DevHive.Services.Models.Technology; +using DevHive.Services.Models.Identity.Role; namespace DevHive.Services.Services { @@ -135,69 +136,57 @@ namespace DevHive.Services.Services public async Task UpdateUser(UpdateUserServiceModel updateUserServiceModel) { - //Method: ValidateUserOnUpdate - if (!await this._userRepository.DoesUserExistAsync(updateUserServiceModel.Id)) - throw new ArgumentException("User does not exist!"); - - if (!this._userRepository.DoesUserHaveThisUsername(updateUserServiceModel.Id, updateUserServiceModel.UserName) - && await this._userRepository.DoesUsernameExistAsync(updateUserServiceModel.UserName)) - throw new ArgumentException("Username already exists!"); + await this.ValidateUserOnUpdate(updateUserServiceModel); await this.ValidateUserCollections(updateUserServiceModel); - //Method: Insert collections to user - HashSet languages = new(); - foreach (UpdateUserCollectionServiceModel lang in updateUserServiceModel.Languages) - languages.Add(await this._languageRepository.GetByNameAsync(lang.Name) ?? - throw new ArgumentException("Invalid language name!")); + //Preserve roles + int roleCount = updateUserServiceModel.Roles.Count; + for (int i = 0; i < roleCount; i++) + { + Role role = await this._roleRepository.GetByNameAsync(updateUserServiceModel.Roles.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid role name!"); - HashSet technologies = new(); - foreach (UpdateUserCollectionServiceModel tech in updateUserServiceModel.Technologies) - technologies.Add(await this._technologyRepository.GetByNameAsync(tech.Name) ?? - throw new ArgumentException("Invalid technology name!")); + UpdateRoleServiceModel updateRoleServiceModel = this._userMapper.Map(role); - User user = this._userMapper.Map(updateUserServiceModel); + updateUserServiceModel.Roles.Add(updateRoleServiceModel); + } - user.Languages = languages; - user.Technologies = technologies; + int langCount = updateUserServiceModel.Languages.Count; + for (int i = 0; i < langCount; i++) + { + Language language = await this._languageRepository.GetByNameAsync(updateUserServiceModel.Languages.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid language name!"); - bool successful = await this._userRepository.EditAsync(user); + UpdateLanguageServiceModel updateLanguageServiceModel = this._userMapper.Map(language); - if (!successful) - throw new InvalidOperationException("Unable to edit user!"); + updateUserServiceModel.Languages.Add(updateLanguageServiceModel); + } - return this._userMapper.Map(user); ; - } + //Clean the already replaced languages + updateUserServiceModel.Languages.RemoveWhere(x => x.Id == Guid.Empty); - public async Task PatchUser(Guid id, List patchList) - { - User user = await this._userRepository.GetByIdAsync(id) ?? - throw new ArgumentException("User does not exist!"); + int techCount = updateUserServiceModel.Technologies.Count; + for (int i = 0; i < techCount; i++) + { + Technology technology = await this._technologyRepository.GetByNameAsync(updateUserServiceModel.Technologies.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid technology name!"); - UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map(user); + UpdateTechnologyServiceModel updateTechnologyServiceModel = this._userMapper.Map(technology); - foreach (Patch patch in patchList) - { - bool successful = patch.Action switch - { - "replace" => ReplacePatch(updateUserServiceModel, patch), - "add" => AddPatch(updateUserServiceModel, patch), - "remove" => RemovePatch(updateUserServiceModel, patch), - _ => throw new ArgumentException("Invalid patch operation!"), - }; - - if (!successful) - throw new ArgumentException("A problem occurred while applying patch"); + updateUserServiceModel.Technologies.Add(updateTechnologyServiceModel); } - bool success = await this._userRepository.EditAsync(user); - if (success) - { - user = await this._userRepository.GetByIdAsync(id); - return this._userMapper.Map(user); - } - else - return null; + //Clean the already replaced technologies + updateUserServiceModel.Technologies.RemoveWhere(x => x.Id == Guid.Empty); + + User user = this._userMapper.Map(updateUserServiceModel); + bool successful = await this._userRepository.EditAsync(user); + + if (!successful) + throw new InvalidOperationException("Unable to edit user!"); + + return this._userMapper.Map(user); } #endregion @@ -282,10 +271,20 @@ namespace DevHive.Services.Services return toReturn; } + private async Task ValidateUserOnUpdate(UpdateUserServiceModel updateUserServiceModel) + { + if (!await this._userRepository.DoesUserExistAsync(updateUserServiceModel.Id)) + throw new ArgumentException("User does not exist!"); + + if (!this._userRepository.DoesUserHaveThisUsername(updateUserServiceModel.Id, updateUserServiceModel.UserName) + && await this._userRepository.DoesUsernameExistAsync(updateUserServiceModel.UserName)) + throw new ArgumentException("Username already exists!"); + } + private async Task ValidateUserCollections(UpdateUserServiceModel updateUserServiceModel) { // Friends - foreach (UpdateUserCollectionServiceModel friend in updateUserServiceModel.Friends) + foreach (var friend in updateUserServiceModel.Friends) { User returnedFriend = await this._userRepository.GetByUsernameAsync(friend.Name); @@ -294,29 +293,24 @@ namespace DevHive.Services.Services } // Languages - foreach (UpdateUserCollectionServiceModel language in updateUserServiceModel.Languages) + foreach (var language in updateUserServiceModel.Languages) { Language returnedLanguage = await this._languageRepository.GetByNameAsync(language.Name); - if (default(Language) == returnedLanguage) + if (returnedLanguage == null) throw new ArgumentException($"Language {language.Name} does not exist!"); } // Technology - foreach (UpdateUserCollectionServiceModel technology in updateUserServiceModel.Technologies) + foreach (var technology in updateUserServiceModel.Technologies) { Technology returnedTechnology = await this._technologyRepository.GetByNameAsync(technology.Name); - if (default(Technology) == returnedTechnology) + if (returnedTechnology == null) throw new ArgumentException($"Technology {technology.Name} does not exist!"); } } - private async Task ValidateUserOnUpdate(UpdateUserServiceModel updateUserServiceModel) - { - throw new NotImplementedException(); - } - private string WriteJWTSecurityToken(Guid userId, HashSet roles) { byte[] signingKey = Encoding.ASCII.GetBytes(_jwtOptions.Secret); @@ -344,25 +338,6 @@ namespace DevHive.Services.Services SecurityToken token = tokenHandler.CreateToken(tokenDescriptor); return tokenHandler.WriteToken(token); } - - private bool AddPatch(UpdateUserServiceModel updateUserServiceModel, Patch patch) - { - // Type type = typeof(UpdateUserServiceModel); - // PropertyInfo property = type.GetProperty(patch.Name); - - // property.SetValue(updateUserServiceModel, patch.Value); - throw new NotImplementedException(); - } - - private bool RemovePatch(UpdateUserServiceModel updateUserServiceModel, Patch patch) - { - throw new NotImplementedException(); - } - - private bool ReplacePatch(UpdateUserServiceModel updateUserServiceModel, Patch patch) - { - throw new NotImplementedException(); - } #endregion } } diff --git a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs index 8cac3ca..eca0d1a 100644 --- a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs @@ -8,10 +8,11 @@ namespace DevHive.Web.Configurations.Mapping { public LanguageMappings() { - CreateMap(); - CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap(); CreateMap(); CreateMap(); diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index 66ae8e3..2ea2742 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -8,11 +8,14 @@ namespace DevHive.Web.Configurations.Mapping { public RoleMappings() { - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap(); + CreateMap(); + CreateMap(); CreateMap(); - CreateMap(); } } } diff --git a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs index 4ecd5f3..708b6ac 100644 --- a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs @@ -10,7 +10,8 @@ namespace DevHive.Web.Configurations.Mapping { CreateMap(); CreateMap(); - CreateMap(); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); CreateMap(); CreateMap(); diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index 5faf4b5..9dbf613 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -20,13 +20,14 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); - CreateMap(); - CreateMap(); + //Update + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); - CreateMap() - .ForMember(f => f.Name, u => u.MapFrom(src => src.UserName)); - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 7121ac8..fbbbbff 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -96,20 +96,6 @@ namespace DevHive.Web.Controllers return new AcceptedResult("UpdateUser", userWebModel); } - - [HttpPatch] - public async Task Patch(Guid id, [FromBody] List patch, [FromHeader] string authorization) - { - if (!await this._userService.ValidJWT(id, authorization)) - return new UnauthorizedResult(); - - UserServiceModel userServiceModel = await this._userService.PatchUser(id, patch); - - if (userServiceModel == null) - return new BadRequestObjectResult("Wrong patch properties"); - else - return new OkObjectResult(this._userMapper.Map(userServiceModel)); - } #endregion #region Delete diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index 3c38ab6..30c66fb 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Diagnostics.CodeAnalysis; using DevHive.Web.Attributes; +using DevHive.Web.Models.Identity.Role; using DevHive.Web.Models.Language; using DevHive.Web.Models.Technology; @@ -18,6 +19,10 @@ namespace DevHive.Web.Models.Identity.User [Required] public HashSet Friends { get; set; } + [NotNull] + [Required] + public HashSet Roles { get; set; } + [NotNull] [Required] public HashSet Languages { get; set; } -- cgit v1.2.3 From df52b1068e16adc50ffd365e2e8b8ea19b59fac3 Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 26 Jan 2021 11:44:41 +0200 Subject: UserUpdate does not allow updating roles if not admin; HTTP Put now works properly; UserUpdate validates properly --- .../Configurations/Mapping/RoleMapings.cs | 4 +- .../Configurations/Mapping/UserMappings.cs | 5 +- src/DevHive.Services/Interfaces/IRoleService.cs | 2 +- .../Identity/User/UpdateFriendServiceModel.cs | 2 +- .../Models/Identity/User/UserServiceModel.cs | 8 +- .../Models/Language/UpdateLanguageServiceModel.cs | 4 +- .../Technology/UpdateTechnologyServiceModel.cs | 4 +- src/DevHive.Services/Services/RoleService.cs | 4 +- src/DevHive.Services/Services/UserService.cs | 142 +++++++++++---------- .../Configurations/Mapping/RoleMappings.cs | 4 +- .../Configurations/Mapping/UserMappings.cs | 14 +- src/DevHive.Web/Controllers/RoleController.cs | 2 +- .../Models/Identity/Role/UpdateRoleWebModel.cs | 6 +- .../Models/Identity/User/FriendWebModel.cs | 16 --- .../Models/Identity/User/UpdateUserWebModel.cs | 2 +- .../Models/Identity/User/UserWebModel.cs | 2 +- .../Models/Identity/User/UsernameWebModel.cs | 16 +++ 17 files changed, 125 insertions(+), 112 deletions(-) delete mode 100644 src/DevHive.Web/Models/Identity/User/FriendWebModel.cs create mode 100644 src/DevHive.Web/Models/Identity/User/UsernameWebModel.cs (limited to 'src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs') diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index 23bd46f..e61a107 100644 --- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs +++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs @@ -9,10 +9,10 @@ namespace DevHive.Services.Configurations.Mapping public RoleMappings() { CreateMap(); - CreateMap(); + CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); CreateMap(); } } diff --git a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs index 6797ce1..096af38 100644 --- a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs @@ -11,11 +11,10 @@ namespace DevHive.Services.Configurations.Mapping { CreateMap(); CreateMap(); + CreateMap(); CreateMap() .AfterMap((src, dest) => dest.PasswordHash = PasswordModifications.GeneratePasswordHash(src.Password)); - CreateMap(); - CreateMap() - .ForMember(dest => dest.UserName, src => src.MapFrom(p => p.Name)); + CreateMap(); CreateMap(); CreateMap() diff --git a/src/DevHive.Services/Interfaces/IRoleService.cs b/src/DevHive.Services/Interfaces/IRoleService.cs index d3a45e5..d47728c 100644 --- a/src/DevHive.Services/Interfaces/IRoleService.cs +++ b/src/DevHive.Services/Interfaces/IRoleService.cs @@ -8,7 +8,7 @@ namespace DevHive.Services.Interfaces { Task CreateRole(CreateRoleServiceModel roleServiceModel); - Task GetRoleById(Guid id); + Task GetRoleById(Guid id); Task UpdateRole(UpdateRoleServiceModel roleServiceModel); diff --git a/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs index 83fcc34..b0efe10 100644 --- a/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs @@ -5,6 +5,6 @@ namespace DevHive.Services.Models.Identity.User public class UpdateFriendServiceModel { public Guid Id { get; set; } - public string Name { get; set; } + public string UserName { get; set; } } } diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index 3e41057..7da54b8 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -7,12 +7,12 @@ namespace DevHive.Services.Models.Identity.User { public class UserServiceModel : BaseUserServiceModel { - public HashSet Roles { get; set; } = new HashSet(); + public HashSet Roles { get; set; } = new(); - public HashSet Friends { get; set; } = new HashSet(); + public HashSet Friends { get; set; } = new(); - public HashSet Languages { get; set; } = new HashSet(); + public HashSet Languages { get; set; } = new(); - public HashSet Technologies { get; set; } = new HashSet(); + public HashSet Technologies { get; set; } = new(); } } diff --git a/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs b/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs index 8536693..84b7f27 100644 --- a/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs +++ b/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs @@ -2,8 +2,10 @@ using System; namespace DevHive.Services.Models.Language { - public class UpdateLanguageServiceModel : LanguageServiceModel + public class UpdateLanguageServiceModel { + public Guid Id { get; set; } + public string Name { get; set; } } } diff --git a/src/DevHive.Services/Models/Technology/UpdateTechnologyServiceModel.cs b/src/DevHive.Services/Models/Technology/UpdateTechnologyServiceModel.cs index a18e286..f4c7921 100644 --- a/src/DevHive.Services/Models/Technology/UpdateTechnologyServiceModel.cs +++ b/src/DevHive.Services/Models/Technology/UpdateTechnologyServiceModel.cs @@ -2,8 +2,10 @@ using System; namespace DevHive.Services.Models.Technology { - public class UpdateTechnologyServiceModel : TechnologyServiceModel + public class UpdateTechnologyServiceModel { + public Guid Id { get; set; } + public string Name { get; set; } } } diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index 9f7a5ac..a8b8e17 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -38,12 +38,12 @@ namespace DevHive.Services.Services } - public async Task GetRoleById(Guid id) + public async Task GetRoleById(Guid id) { Role role = await this._roleRepository.GetByIdAsync(id) ?? throw new ArgumentException("Role does not exist!"); - return this._roleMapper.Map(role); + return this._roleMapper.Map(role); } public async Task UpdateRole(UpdateRoleServiceModel updateRoleServiceModel) diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 1beb07f..960630e 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -111,61 +111,9 @@ namespace DevHive.Services.Services await this.ValidateUserCollections(updateUserServiceModel); - /* Roles */ - int roleCount = updateUserServiceModel.Roles.Count; - for (int i = 0; i < roleCount; i++) - { - Role role = await this._roleRepository.GetByNameAsync(updateUserServiceModel.Roles.ElementAt(i).Name) ?? - throw new ArgumentException("Invalid role name!"); - - UpdateRoleServiceModel updateRoleServiceModel = this._userMapper.Map(role); - - updateUserServiceModel.Roles.Add(updateRoleServiceModel); - } - - /* Languages */ - int langCount = updateUserServiceModel.Languages.Count; - for (int i = 0; i < langCount; i++) - { - Language language = await this._languageRepository.GetByNameAsync(updateUserServiceModel.Languages.ElementAt(i).Name) ?? - throw new ArgumentException("Invalid language name!"); - - UpdateLanguageServiceModel updateLanguageServiceModel = this._userMapper.Map(language); - - updateUserServiceModel.Languages.Add(updateLanguageServiceModel); - } - //Clean the already replaced languages - updateUserServiceModel.Languages.RemoveWhere(x => x.Id == Guid.Empty); - - /* Technologies */ - int techCount = updateUserServiceModel.Technologies.Count; - for (int i = 0; i < techCount; i++) - { - Technology technology = await this._technologyRepository.GetByNameAsync(updateUserServiceModel.Technologies.ElementAt(i).Name) ?? - throw new ArgumentException("Invalid technology name!"); - - UpdateTechnologyServiceModel updateTechnologyServiceModel = this._userMapper.Map(technology); - - updateUserServiceModel.Technologies.Add(updateTechnologyServiceModel); - } - //Clean the already replaced technologies - updateUserServiceModel.Technologies.RemoveWhere(x => x.Id == Guid.Empty); - - /* Friends */ - HashSet friends = new(); - int friendsCount = updateUserServiceModel.Friends.Count; - for (int i = 0; i < friendsCount; i++) - { - User friend = await this._userRepository.GetByUsernameAsync(updateUserServiceModel.Friends.ElementAt(i).Name) ?? - throw new ArgumentException("Invalid friend's username!"); - - friends.Add(friend); - } - //Clean the already replaced technologies - updateUserServiceModel.Friends.RemoveWhere(x => x.Id == Guid.Empty); + updateUserServiceModel = await this.PopulateUpdateModelWithIds(updateUserServiceModel); User user = this._userMapper.Map(updateUserServiceModel); - user.Friends = friends; bool successful = await this._userRepository.EditAsync(updateUserServiceModel.Id, user); @@ -249,30 +197,49 @@ namespace DevHive.Services.Services private async Task ValidateUserCollections(UpdateUserServiceModel updateUserServiceModel) { + //Do NOT allow a user to change his roles, unless he is an Admin + bool isAdmin = (await this._userRepository.GetByIdAsync(updateUserServiceModel.Id)) + .Roles.Any(r => r.Name == Role.AdminRole); + + if (isAdmin) + { + // Roles + foreach (var role in updateUserServiceModel.Roles) + { + Role returnedRole = await this._roleRepository.GetByNameAsync(role.Name) ?? + throw new ArgumentException($"Role {role.Name} does not exist!"); + } + } + //Preserve original user roles + else + { + HashSet roles = (await this._userRepository.GetByIdAsync(updateUserServiceModel.Id)).Roles; + + foreach (var role in roles) + { + Role returnedRole = await this._roleRepository.GetByNameAsync(role.Name) ?? + throw new ArgumentException($"Role {role.Name} does not exist!"); + } + } + // Friends foreach (var friend in updateUserServiceModel.Friends) { - User returnedFriend = await this._userRepository.GetByUsernameAsync(friend.Name); - - if (returnedFriend == null) - throw new ArgumentException($"User {friend.Name} does not exist!"); + User returnedFriend = await this._userRepository.GetByUsernameAsync(friend.UserName) ?? + throw new ArgumentException($"User {friend.UserName} does not exist!"); } // Languages foreach (var language in updateUserServiceModel.Languages) { - Language returnedLanguage = await this._languageRepository.GetByNameAsync(language.Name); - - if (returnedLanguage == null) + Language returnedLanguage = await this._languageRepository.GetByNameAsync(language.Name) ?? throw new ArgumentException($"Language {language.Name} does not exist!"); } // Technology foreach (var technology in updateUserServiceModel.Technologies) { - Technology returnedTechnology = await this._technologyRepository.GetByNameAsync(technology.Name); - - if (returnedTechnology == null) + Technology returnedTechnology = await this._technologyRepository.GetByNameAsync(technology.Name) ?? throw new ArgumentException($"Technology {technology.Name} does not exist!"); } } @@ -306,12 +273,13 @@ namespace DevHive.Services.Services } #endregion + #region Misc public async Task SuperSecretPromotionToAdmin(Guid userId) { User user = await this._userRepository.GetByIdAsync(userId) ?? throw new ArgumentException("User does not exist! Can't promote shit in this country..."); - if(!await this._roleRepository.DoesNameExist("Admin")) + if (!await this._roleRepository.DoesNameExist("Admin")) { Role adminRole = new() { @@ -329,5 +297,51 @@ namespace DevHive.Services.Services return admin.Id; } + + private async Task PopulateUpdateModelWithIds(UpdateUserServiceModel updateUserServiceModel) + { + /* Roles */ + int roleCount = updateUserServiceModel.Roles.Count; + for (int i = 0; i < roleCount; i++) + { + Role role = await this._roleRepository.GetByNameAsync(updateUserServiceModel.Roles.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid role name!"); + + updateUserServiceModel.Roles.ElementAt(i).Id = role.Id; + } + + /* Languages */ + int langCount = updateUserServiceModel.Languages.Count; + for (int i = 0; i < langCount; i++) + { + Language language = await this._languageRepository.GetByNameAsync(updateUserServiceModel.Languages.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid language name!"); + + updateUserServiceModel.Languages.ElementAt(i).Id = language.Id; + } + + /* Technologies */ + int techCount = updateUserServiceModel.Technologies.Count; + for (int i = 0; i < techCount; i++) + { + Technology technology = await this._technologyRepository.GetByNameAsync(updateUserServiceModel.Technologies.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid technology name!"); + + updateUserServiceModel.Technologies.ElementAt(i).Id = technology.Id; + } + + /* Friends */ + int friendsCount = updateUserServiceModel.Friends.Count; + for (int i = 0; i < friendsCount; i++) + { + User friend = await this._userRepository.GetByUsernameAsync(updateUserServiceModel.Friends.ElementAt(i).UserName) ?? + throw new ArgumentException("Invalid friend's username!"); + + updateUserServiceModel.Friends.ElementAt(i).Id = friend.Id; + } + + return updateUserServiceModel; + } + #endregion } } diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index 2f01f77..2ea2742 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -11,11 +11,11 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap() .ForMember(src => src.Id, dest => dest.Ignore()); - CreateMap(); + CreateMap(); CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index e80a69a..1b26cc9 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -2,8 +2,6 @@ using AutoMapper; using DevHive.Services.Models.Identity.User; using DevHive.Web.Models.Identity.User; using DevHive.Common.Models.Identity; -using DevHive.Web.Models.Language; -using DevHive.Web.Models.Technology; namespace DevHive.Web.Configurations.Mapping { @@ -21,16 +19,12 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); //Update - CreateMap() - .ForMember(src => src.Id, dest => dest.Ignore()); - CreateMap() - .ForMember(src => src.Id, dest => dest.Ignore()); - CreateMap() - .ForMember(src => src.Id, dest => dest.Ignore()) - .ForMember(src => src.Name, dest => dest.MapFrom(p => p.UserName)); + CreateMap(); + CreateMap(); + CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index d8bb60c..c68a32b 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -40,7 +40,7 @@ namespace DevHive.Web.Controllers [Authorize(Policy = "User")] public async Task GetById(Guid id) { - ReadRoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); + RoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); RoleWebModel roleWebModel = this._roleMapper.Map(roleServiceModel); return new OkObjectResult(roleWebModel); diff --git a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs index 254affc..3870481 100644 --- a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs @@ -4,10 +4,12 @@ using System.Diagnostics.CodeAnalysis; namespace DevHive.Web.Models.Identity.Role { - public class UpdateRoleWebModel : RoleWebModel + public class UpdateRoleWebModel { [NotNull] [Required] - public Guid Id { get; set; } + [MinLength(3)] + [MaxLength(50)] + public string Name { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs b/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs deleted file mode 100644 index d59bff5..0000000 --- a/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.ComponentModel.DataAnnotations; -using System.Diagnostics.CodeAnalysis; -using DevHive.Web.Attributes; - -namespace DevHive.Web.Models.Identity.User -{ - public class FriendWebModel - { - [NotNull] - [Required] - [MinLength(3)] - [MaxLength(50)] - [OnlyAlphanumerics(ErrorMessage = "Username can only contain letters and digits!")] - public string UserName { get; set; } - } -} diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index 30c66fb..62901f6 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -17,7 +17,7 @@ namespace DevHive.Web.Models.Identity.User [NotNull] [Required] - public HashSet Friends { get; set; } + public HashSet Friends { get; set; } [NotNull] [Required] diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 5b80ba3..4097901 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -15,7 +15,7 @@ namespace DevHive.Web.Models.Identity.User [NotNull] [Required] - public HashSet Friends { get; set; } = new HashSet(); + public HashSet Friends { get; set; } = new HashSet(); [NotNull] [Required] diff --git a/src/DevHive.Web/Models/Identity/User/UsernameWebModel.cs b/src/DevHive.Web/Models/Identity/User/UsernameWebModel.cs new file mode 100644 index 0000000..a20c1bf --- /dev/null +++ b/src/DevHive.Web/Models/Identity/User/UsernameWebModel.cs @@ -0,0 +1,16 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; +using DevHive.Web.Attributes; + +namespace DevHive.Web.Models.Identity.User +{ + public class UsernameWebModel + { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] + [OnlyAlphanumerics(ErrorMessage = "Username can only contain letters and digits!")] + public string UserName { get; set; } + } +} -- cgit v1.2.3