diff options
| author | transtrike <transtrike@gmail.com> | 2021-01-21 22:13:16 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2021-01-21 22:13:16 +0200 |
| commit | 13a2ceda912f961a232c87236f1b29aa29bb6160 (patch) | |
| tree | 59f8d2bf63b03bacc76f98114d2aed78e420ddcd /src/DevHive.Web | |
| parent | a47ea20ab91017da53437f750ed8e0f939f5cdba (diff) | |
| parent | bda98b96433d7a9952524fab4ec65f96998b55de (diff) | |
| download | DevHive-13a2ceda912f961a232c87236f1b29aa29bb6160.tar DevHive-13a2ceda912f961a232c87236f1b29aa29bb6160.tar.gz DevHive-13a2ceda912f961a232c87236f1b29aa29bb6160.zip | |
Merge branch 'refactor_user_updating' into dev
Diffstat (limited to 'src/DevHive.Web')
39 files changed, 411 insertions, 261 deletions
diff --git a/src/DevHive.Web/Models/Identity/Validation/GoodPasswordModelValidation.cs b/src/DevHive.Web/Attributes/GoodPasswordModelValidation.cs index f920c35..7d6a1ea 100644 --- a/src/DevHive.Web/Models/Identity/Validation/GoodPasswordModelValidation.cs +++ b/src/DevHive.Web/Attributes/GoodPasswordModelValidation.cs @@ -1,7 +1,7 @@ using System; using System.ComponentModel.DataAnnotations; -namespace DevHive.Web.Models.Identity.Validation +namespace DevHive.Web.Attributes { public class GoodPassword : ValidationAttribute { diff --git a/src/DevHive.Web/Models/Identity/Validation/OnlyAlphanumericsModelValidation.cs b/src/DevHive.Web/Attributes/OnlyAlphanumericsModelValidation.cs index 5c8c66c..26e0733 100644 --- a/src/DevHive.Web/Models/Identity/Validation/OnlyAlphanumericsModelValidation.cs +++ b/src/DevHive.Web/Attributes/OnlyAlphanumericsModelValidation.cs @@ -1,7 +1,7 @@ using System; using System.ComponentModel.DataAnnotations; -namespace DevHive.Web.Models.Identity.Validation +namespace DevHive.Web.Attributes { public class OnlyAlphanumerics : ValidationAttribute { diff --git a/src/DevHive.Web/Models/Identity/Validation/OnlyLettersModelValidation.cs b/src/DevHive.Web/Attributes/OnlyLettersModelValidation.cs index 29a995a..07afee9 100644 --- a/src/DevHive.Web/Models/Identity/Validation/OnlyLettersModelValidation.cs +++ b/src/DevHive.Web/Attributes/OnlyLettersModelValidation.cs @@ -1,7 +1,7 @@ using System; using System.ComponentModel.DataAnnotations; -namespace DevHive.Web.Models.Identity.Validation +namespace DevHive.Web.Attributes { public class OnlyLetters : ValidationAttribute { diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs new file mode 100644 index 0000000..286727f --- /dev/null +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs @@ -0,0 +1,16 @@ +using DevHive.Web.Middleware; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; + +namespace DevHive.Web.Configurations.Extensions +{ + public static class ConfigureExceptionHandlerMiddleware + { + public static void ExceptionHandlerMiddlewareConfiguration(this IServiceCollection services) { } + + public static void UseExceptionHandlerMiddlewareConfiguration(this IApplicationBuilder app) + { + app.UseMiddleware<ExceptionMiddleware>(); + } + } +} diff --git a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs index 3c2a4d0..eca0d1a 100644 --- a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs @@ -8,11 +8,14 @@ namespace DevHive.Web.Configurations.Mapping { public LanguageMappings() { - CreateMap<LanguageWebModel, LanguageServiceModel>(); CreateMap<CreateLanguageWebModel, CreateLanguageServiceModel>(); - CreateMap<UpdateLanguageWebModel, UpdateLanguageServiceModel>(); + CreateMap<ReadLanguageWebModel, ReadLanguageServiceModel>(); + CreateMap<UpdateLanguageWebModel, UpdateLanguageServiceModel>() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap<LanguageWebModel, LanguageServiceModel>(); CreateMap<LanguageServiceModel, LanguageWebModel>(); + CreateMap<ReadLanguageServiceModel, ReadLanguageWebModel>(); CreateMap<CreateLanguageServiceModel, CreateLanguageWebModel>(); CreateMap<UpdateLanguageServiceModel, UpdateLanguageWebModel>(); } diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index afa3a94..2ea2742 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -1,6 +1,6 @@ using AutoMapper; using DevHive.Web.Models.Identity.Role; -using DevHive.Common.Models.Identity; +using DevHive.Services.Models.Identity.Role; namespace DevHive.Web.Configurations.Mapping { @@ -8,11 +8,14 @@ namespace DevHive.Web.Configurations.Mapping { public RoleMappings() { - CreateMap<CreateRoleModel, RoleModel>(); - CreateMap<UpdateRoleModel, RoleModel>(); + CreateMap<CreateRoleWebModel, CreateRoleServiceModel>(); + CreateMap<UpdateRoleWebModel, UpdateRoleServiceModel>() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap<RoleWebModel, RoleServiceModel>(); - CreateMap<RoleModel, RoleWebModel>(); - CreateMap<RoleWebModel, RoleModel>(); + CreateMap<CreateRoleServiceModel, CreateRoleWebModel>(); + CreateMap<UpdateRoleServiceModel, UpdateRoleWebModel>(); + CreateMap<RoleServiceModel, RoleWebModel>(); } } } diff --git a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs index 828dac1..708b6ac 100644 --- a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs @@ -9,10 +9,13 @@ namespace DevHive.Web.Configurations.Mapping public TechnologyMappings() { CreateMap<CreateTechnologyWebModel, CreateTechnologyServiceModel>(); - CreateMap<UpdateTechnologyWebModel, UpdateTechnologyServiceModel>(); + CreateMap<ReadTechnologyWebModel, ReadTechnologyServiceModel>(); + CreateMap<UpdateTechnologyWebModel, UpdateTechnologyServiceModel>() + .ForMember(src => src.Id, dest => dest.Ignore()); CreateMap<TechnologyWebModel, TechnologyServiceModel>(); CreateMap<CreateTechnologyServiceModel, CreateTechnologyWebModel>(); + CreateMap<ReadTechnologyServiceModel, ReadTechnologyWebModel>(); CreateMap<UpdateTechnologyServiceModel, UpdateTechnologyWebModel>(); CreateMap<TechnologyServiceModel, TechnologyWebModel>(); } diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index 59003ea..9dbf613 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -2,6 +2,8 @@ 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 { @@ -17,6 +19,15 @@ namespace DevHive.Web.Configurations.Mapping CreateMap<UserServiceModel, UserWebModel>(); CreateMap<TokenModel, TokenWebModel>(); + + //Update + CreateMap<UpdateUserWebModel, UpdateUserServiceModel>() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap<FriendWebModel, FriendServiceModel>() + .ForMember(src => src.Id, dest => dest.Ignore()); + + CreateMap<UpdateUserServiceModel, UpdateUserWebModel>(); + CreateMap<FriendServiceModel, FriendWebModel>(); } } } diff --git a/src/DevHive.Web/Controllers/ErrorController.cs b/src/DevHive.Web/Controllers/ErrorController.cs deleted file mode 100644 index b187501..0000000 --- a/src/DevHive.Web/Controllers/ErrorController.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Diagnostics; -using AutoMapper; -using Microsoft.AspNetCore.Diagnostics; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Newtonsoft.Json; - -namespace DevHive.Web.Controllers -{ - public class ErrorController : ControllerBase - { - [HttpPost] - [Route("/api/Error")] - public IActionResult Error() - { - //Later for logging - string requestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; - - IExceptionHandlerFeature exception = - HttpContext.Features.Get<IExceptionHandlerFeature>(); - - object result = ProcessException(requestId, exception); - return new BadRequestObjectResult(JsonConvert.SerializeObject(result)); - } - - private object ProcessException(string requestId, IExceptionHandlerFeature exception) - { - switch (exception.Error) - { - case ArgumentException _: - case InvalidOperationException _: - case AutoMapperMappingException _: - case AutoMapperConfigurationException _: - return MessageToObject(exception.Error.Message); - default: - return MessageToObject(null); - } - } - - private object MessageToObject(string message) - { - return new - { - Error = message - }; - } - } -} - diff --git a/src/DevHive.Web/Controllers/LanguageController.cs b/src/DevHive.Web/Controllers/LanguageController.cs index 5202f16..e2d0dec 100644 --- a/src/DevHive.Web/Controllers/LanguageController.cs +++ b/src/DevHive.Web/Controllers/LanguageController.cs @@ -26,29 +26,29 @@ namespace DevHive.Web.Controllers { CreateLanguageServiceModel languageServiceModel = this._languageMapper.Map<CreateLanguageServiceModel>(createLanguageWebModel); - bool result = await this._languageService.CreateLanguage(languageServiceModel); + Guid id = await this._languageService.CreateLanguage(languageServiceModel); - if (!result) - return new BadRequestObjectResult("Could not create Language"); - - return new OkResult(); + return id == Guid.Empty ? + new BadRequestObjectResult($"Could not create language {createLanguageWebModel.Name}") : + new OkObjectResult(new { Id = id }); } [HttpGet] - public async Task<IActionResult> GetById(Guid languageId) + public async Task<IActionResult> GetById(Guid id) { - LanguageServiceModel languageServiceModel = await this._languageService.GetLanguageById(languageId); - LanguageWebModel languageWebModel = this._languageMapper.Map<LanguageWebModel>(languageServiceModel); + ReadLanguageServiceModel languageServiceModel = await this._languageService.GetLanguageById(id); + ReadLanguageWebModel languageWebModel = this._languageMapper.Map<ReadLanguageWebModel>(languageServiceModel); return new OkObjectResult(languageWebModel); } [HttpPut] - public async Task<IActionResult> Update(Guid languageId, [FromBody] UpdateLanguageWebModel updateModel) + public async Task<IActionResult> Update(Guid id, [FromBody] UpdateLanguageWebModel updateModel) { UpdateLanguageServiceModel updatelanguageServiceModel = this._languageMapper.Map<UpdateLanguageServiceModel>(updateModel); + updatelanguageServiceModel.Id = id; - bool result = await this._languageService.UpdateLanguage(languageId, updatelanguageServiceModel); + bool result = await this._languageService.UpdateLanguage(updatelanguageServiceModel); if (!result) return new BadRequestObjectResult("Could not update Language"); @@ -57,9 +57,9 @@ namespace DevHive.Web.Controllers } [HttpDelete] - public async Task<IActionResult> Delete(Guid languageId) + public async Task<IActionResult> Delete(Guid id) { - bool result = await this._languageService.DeleteLanguage(languageId); + bool result = await this._languageService.DeleteLanguage(id); if (!result) return new BadRequestObjectResult("Could not delete Language"); diff --git a/src/DevHive.Web/Controllers/PostController.cs b/src/DevHive.Web/Controllers/PostController.cs index 15adb1b..2a08605 100644 --- a/src/DevHive.Web/Controllers/PostController.cs +++ b/src/DevHive.Web/Controllers/PostController.cs @@ -32,12 +32,11 @@ namespace DevHive.Web.Controllers CreatePostServiceModel postServiceModel = this._postMapper.Map<CreatePostServiceModel>(createPostModel); - bool result = await this._postService.CreatePost(postServiceModel); + Guid id = await this._postService.CreatePost(postServiceModel); - if (!result) - return new BadRequestObjectResult("Could not create post!"); - - return new OkResult(); + return id == Guid.Empty ? + new BadRequestObjectResult("Could not create post") : + new OkObjectResult(new { Id = id }); } [HttpPost] @@ -46,12 +45,11 @@ namespace DevHive.Web.Controllers { CreateCommentServiceModel createCommentServiceModel = this._postMapper.Map<CreateCommentServiceModel>(commentWebModel); - bool result = await this._postService.AddComment(createCommentServiceModel); + Guid id = await this._postService.AddComment(createCommentServiceModel); - if (!result) - return new BadRequestObjectResult("Could not create the Comment"); - - return new OkResult(); + return id == Guid.Empty ? + new BadRequestObjectResult("Could not create language") : + new OkObjectResult(new { Id = id }); } //Read diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index 8ea2711..227b877 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -3,8 +3,8 @@ using Microsoft.AspNetCore.Mvc; using DevHive.Web.Models.Identity.Role; using AutoMapper; using System; -using DevHive.Common.Models.Identity; using DevHive.Services.Interfaces; +using DevHive.Services.Models.Identity.Role; namespace DevHive.Web.Controllers { @@ -23,36 +23,36 @@ namespace DevHive.Web.Controllers } [HttpPost] - public async Task<IActionResult> Create([FromBody] CreateRoleModel createRoleModel) + public async Task<IActionResult> Create([FromBody] CreateRoleWebModel createRoleWebModel) { - RoleModel roleServiceModel = - this._roleMapper.Map<RoleModel>(createRoleModel); + RoleServiceModel roleServiceModel = + this._roleMapper.Map<RoleServiceModel>(createRoleWebModel); - bool result = await this._roleService.CreateRole(roleServiceModel); + Guid id = await this._roleService.CreateRole(roleServiceModel); - if (!result) - return new BadRequestObjectResult("Could not create role!"); + return id == Guid.Empty ? + new BadRequestObjectResult($"Could not create role {createRoleWebModel.Name}") : + new OkObjectResult(new { Id = id }); - return new OkResult(); } [HttpGet] public async Task<IActionResult> GetById(Guid id) { - RoleModel roleServiceModel = await this._roleService.GetRoleById(id); - RoleModel roleWebModel = this._roleMapper.Map<RoleModel>(roleServiceModel); + RoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); + RoleWebModel roleWebModel = this._roleMapper.Map<RoleWebModel>(roleServiceModel); return new OkObjectResult(roleWebModel); } [HttpPut] - public async Task<IActionResult> Update(Guid id, [FromBody] UpdateRoleModel updateRoleModel) + public async Task<IActionResult> Update(Guid id, [FromBody] UpdateRoleWebModel updateRoleWebModel) { - RoleModel roleServiceModel = - this._roleMapper.Map<RoleModel>(updateRoleModel); - roleServiceModel.Id = id; + UpdateRoleServiceModel updateRoleServiceModel = + this._roleMapper.Map<UpdateRoleServiceModel>(updateRoleWebModel); + updateRoleServiceModel.Id = id; - bool result = await this._roleService.UpdateRole(roleServiceModel); + bool result = await this._roleService.UpdateRole(updateRoleServiceModel); if (!result) return new BadRequestObjectResult("Could not update role!"); diff --git a/src/DevHive.Web/Controllers/TechnologyController.cs b/src/DevHive.Web/Controllers/TechnologyController.cs index 3be3b8a..ba2ffdc 100644 --- a/src/DevHive.Web/Controllers/TechnologyController.cs +++ b/src/DevHive.Web/Controllers/TechnologyController.cs @@ -22,33 +22,33 @@ namespace DevHive.Web.Controllers } [HttpPost] - public async Task<IActionResult> Create([FromBody] CreateTechnologyWebModel technologyWebModel) + public async Task<IActionResult> Create([FromBody] CreateTechnologyWebModel createTechnologyWebModel) { - CreateTechnologyServiceModel technologyServiceModel = this._technologyMapper.Map<CreateTechnologyServiceModel>(technologyWebModel); + CreateTechnologyServiceModel technologyServiceModel = this._technologyMapper.Map<CreateTechnologyServiceModel>(createTechnologyWebModel); - bool result = await this._technologyService.Create(technologyServiceModel); + Guid id = await this._technologyService.Create(technologyServiceModel); - if (!result) - return new BadRequestObjectResult("Could not create the Technology"); - - return new OkResult(); + return id == Guid.Empty ? + new BadRequestObjectResult($"Could not create technology {createTechnologyWebModel.Name}") : + new OkObjectResult(new { Id = id }); } [HttpGet] - public async Task<IActionResult> GetById(Guid technologyId) + public async Task<IActionResult> GetById(Guid id) { - CreateTechnologyServiceModel createTechnologyServiceModel = await this._technologyService.GetTechnologyById(technologyId); + CreateTechnologyServiceModel createTechnologyServiceModel = await this._technologyService.GetTechnologyById(id); CreateTechnologyWebModel createTechnologyWebModel = this._technologyMapper.Map<CreateTechnologyWebModel>(createTechnologyServiceModel); return new OkObjectResult(createTechnologyWebModel); } [HttpPut] - public async Task<IActionResult> Update(Guid technologyId, [FromBody] UpdateTechnologyWebModel updateModel) + public async Task<IActionResult> Update(Guid id, [FromBody] UpdateTechnologyWebModel updateModel) { - UpdateTechnologyServiceModel updateTechnologyWebModel = this._technologyMapper.Map<UpdateTechnologyServiceModel>(updateModel); + UpdateTechnologyServiceModel updateTechnologyServiceModel = this._technologyMapper.Map<UpdateTechnologyServiceModel>(updateModel); + updateTechnologyServiceModel.Id = id; - bool result = await this._technologyService.UpdateTechnology(technologyId, updateTechnologyWebModel); + bool result = await this._technologyService.UpdateTechnology(updateTechnologyServiceModel); if (!result) return new BadRequestObjectResult("Could not update Technology"); @@ -57,9 +57,9 @@ namespace DevHive.Web.Controllers } [HttpDelete] - public async Task<IActionResult> Delete(Guid technologyId) + public async Task<IActionResult> Delete(Guid id) { - bool result = await this._technologyService.DeleteTechnology(technologyId); + bool result = await this._technologyService.DeleteTechnology(id); if (!result) return new BadRequestObjectResult("Could not delete Technology"); diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index b33c3b9..fbbbbff 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -6,12 +6,10 @@ using DevHive.Web.Models.Identity.User; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using DevHive.Common.Models.Identity; -using DevHive.Common.Models.Misc; -using DevHive.Web.Models.Language; -using DevHive.Services.Models.Language; -using DevHive.Web.Models.Technology; -using DevHive.Services.Models.Technology; using DevHive.Services.Interfaces; +using Microsoft.AspNetCore.JsonPatch; +using DevHive.Common.Models.Misc; +using System.Collections.Generic; namespace DevHive.Web.Controllers { @@ -57,40 +55,6 @@ namespace DevHive.Web.Controllers } #endregion - #region Create - - [HttpPost] - [Route("AddAFriend")] - public async Task<IActionResult> AddAFriend(Guid userId, [FromBody] IdModel friendIdModel) - { - return await this._userService.AddFriend(userId, friendIdModel.Id) ? - new OkResult() : - new BadRequestResult(); - } - - [HttpPost] - [Route("AddLanguageToUser")] - public async Task<IActionResult> AddLanguageToUser(Guid userId, [FromBody] LanguageWebModel languageWebModel) - { - LanguageServiceModel languageServiceModel = this._userMapper.Map<LanguageServiceModel>(languageWebModel); - - return await this._userService.AddLanguageToUser(userId, languageServiceModel) ? - new OkResult() : - new BadRequestResult(); - } - - [HttpPost] - [Route("AddTechnologyToUser")] - public async Task<IActionResult> AddTechnologyToUser(Guid userId, [FromBody] TechnologyWebModel technologyWebModel) - { - TechnologyServiceModel technologyServiceModel = this._userMapper.Map<TechnologyServiceModel>(technologyWebModel); - - return await this._userService.AddTechnologyToUser(userId, technologyServiceModel) ? - new OkResult() : - new BadRequestResult(); - } - #endregion - #region Read [HttpGet] @@ -106,10 +70,11 @@ namespace DevHive.Web.Controllers } [HttpGet] - [Route("GetAFriend")] - public async Task<IActionResult> GetAFriend(Guid friendId) + [Route("GetUser")] + [AllowAnonymous] + public async Task<IActionResult> GetUser(string username) { - UserServiceModel friendServiceModel = await this._userService.GetFriendById(friendId); + UserServiceModel friendServiceModel = await this._userService.GetUserByUsername(username); UserWebModel friend = this._userMapper.Map<UserWebModel>(friendServiceModel); return new OkObjectResult(friend); @@ -118,12 +83,12 @@ namespace DevHive.Web.Controllers #region Update [HttpPut] - public async Task<IActionResult> Update(Guid id, [FromBody] UpdateUserWebModel updateModel, [FromHeader] string authorization) + public async Task<IActionResult> Update(Guid id, [FromBody] UpdateUserWebModel updateUserWebModel, [FromHeader] string authorization) { if (!await this._userService.ValidJWT(id, authorization)) return new UnauthorizedResult(); - UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map<UpdateUserServiceModel>(updateModel); + UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map<UpdateUserServiceModel>(updateUserWebModel); updateUserServiceModel.Id = id; UserServiceModel userServiceModel = await this._userService.UpdateUser(updateUserServiceModel); @@ -134,7 +99,6 @@ namespace DevHive.Web.Controllers #endregion #region Delete - [HttpDelete] public async Task<IActionResult> Delete(Guid id, [FromHeader] string authorization) { @@ -144,36 +108,6 @@ namespace DevHive.Web.Controllers await this._userService.DeleteUser(id); return new OkResult(); } - - [HttpDelete] - [Route("RemoveAFriend")] - public async Task<IActionResult> RemoveAFriend(Guid userId, Guid friendId) - { - await this._userService.RemoveFriend(userId, friendId); - return new OkResult(); - } - - [HttpDelete] - [Route("RemoveLanguageFromUser")] - public async Task<IActionResult> RemoveLanguageFromUser(Guid userId, [FromBody] LanguageWebModel languageWebModel) - { - LanguageServiceModel languageServiceModel = this._userMapper.Map<LanguageServiceModel>(languageWebModel); - - return await this._userService.RemoveLanguageFromUser(userId, languageServiceModel) ? - new OkResult() : - new BadRequestResult(); - } - - [HttpDelete] - [Route("RemoveTechnologyFromUser")] - public async Task<IActionResult> RemoveTechnologyFromUser(Guid userId, [FromBody] TechnologyWebModel technologyWebModel) - { - TechnologyServiceModel technologyServiceModel = this._userMapper.Map<TechnologyServiceModel>(technologyWebModel); - - return await this._userService.RemoveTechnologyFromUser(userId, technologyServiceModel) ? - new OkResult() : - new BadRequestResult(); - } #endregion } } 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 becb3c9..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 CreateRoleModel + 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 9e97ffc..99b0f50 100644 --- a/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs @@ -1,9 +1,14 @@ -using System; +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 1eaad57..254affc 100644 --- a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs @@ -1,6 +1,13 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; + namespace DevHive.Web.Models.Identity.Role { - public class UpdateRoleModel : CreateRoleModel + 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 new file mode 100644 index 0000000..d59bff5 --- /dev/null +++ b/src/DevHive.Web/Models/Identity/User/FriendWebModel.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 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/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 9e41eb6..30c66fb 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -1,12 +1,34 @@ +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.Identity.Role; +using DevHive.Web.Models.Language; +using DevHive.Web.Models.Technology; namespace DevHive.Web.Models.Identity.User { public class UpdateUserWebModel : BaseUserWebModel { + [NotNull] [Required] [GoodPassword] public string Password { get; set; } + + [NotNull] + [Required] + public HashSet<FriendWebModel> Friends { get; set; } + + [NotNull] + [Required] + public HashSet<UpdateRoleWebModel> Roles { get; set; } + + [NotNull] + [Required] + public HashSet<UpdateLanguageWebModel> Languages { get; set; } + + [NotNull] + [Required] + public HashSet<UpdateTechnologyWebModel> 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..5b80ba3 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; -using DevHive.Common.Models.Identity; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; using DevHive.Web.Models.Identity.Role; using DevHive.Web.Models.Language; using DevHive.Web.Models.Technology; @@ -8,9 +9,20 @@ namespace DevHive.Web.Models.Identity.User { public class UserWebModel : BaseUserWebModel { - public IList<RoleWebModel> Roles { get; set; } = new List<RoleWebModel>(); - public IList<UserWebModel> Friends { get; set; } = new List<UserWebModel>(); - public IList<LanguageWebModel> Languages { get; set; } = new List<LanguageWebModel>(); - public IList<TechnologyWebModel> Technologies { get; set; } = new List<TechnologyWebModel>(); + [NotNull] + [Required] + public HashSet<RoleWebModel> Roles { get; set; } = new HashSet<RoleWebModel>(); + + [NotNull] + [Required] + public HashSet<FriendWebModel> Friends { get; set; } = new HashSet<FriendWebModel>(); + + [NotNull] + [Required] + public HashSet<LanguageWebModel> Languages { get; set; } = new HashSet<LanguageWebModel>(); + + [NotNull] + [Required] + public HashSet<TechnologyWebModel> Technologies { get; set; } = new HashSet<TechnologyWebModel>(); } } 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 new file mode 100644 index 0000000..ab4a089 --- /dev/null +++ b/src/DevHive.Web/Models/Language/ReadLanguageWebModel.cs @@ -0,0 +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 ed3b37c..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 : LanguageWebModel + public class UpdateLanguageWebModel { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] public string Name { get; set; } } } diff --git a/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs b/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs index 3cdc7c4..590851d 100644 --- a/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs +++ b/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs @@ -1,11 +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; } } -}
\ No newline at end of file +} 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/ReadTechnologyWebModel.cs b/src/DevHive.Web/Models/Technology/ReadTechnologyWebModel.cs new file mode 100644 index 0000000..edaaaef --- /dev/null +++ b/src/DevHive.Web/Models/Technology/ReadTechnologyWebModel.cs @@ -0,0 +1,14 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; + +namespace DevHive.Web.Models.Technology +{ + public class ReadTechnologyWebModel + { + [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 8bf48bf..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 : TechnologyWebModel + public class UpdateTechnologyWebModel { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] public string Name { get; set; } } } diff --git a/src/DevHive.Web/Properties/launchSettings.json b/src/DevHive.Web/Properties/launchSettings.json index 44d86fc..5deaadb 100644 --- a/src/DevHive.Web/Properties/launchSettings.json +++ b/src/DevHive.Web/Properties/launchSettings.json @@ -1,31 +1,28 @@ -{
- "$schema": "http://json.schemastore.org/launchsettings.json",
- "iisSettings": {
- "windowsAuthentication": false,
- "anonymousAuthentication": true,
- "iisExpress": {
- "applicationUrl": "http://localhost:1955",
- "sslPort": 44326
- }
- },
- "profiles": {
- "IIS Express": {
- "commandName": "IISExpress",
- "launchBrowser": true,
- "launchUrl": "swagger",
- "environmentVariables": {
- "ASPNETCORE_ENVIRONMENT": "Development"
- }
- },
- "DevHive.Web": {
- "commandName": "Project",
- "dotnetRunMessages": "true",
- "launchBrowser": true,
- "launchUrl": "swagger",
- "applicationUrl": "http://localhost:5000",
- "environmentVariables": {
- "ASPNETCORE_ENVIRONMENT": "Development"
- }
- }
- }
-}
+{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:1955", + "sslPort": 44326 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": false, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "DevHive.Web": { + "commandName": "Project", + "dotnetRunMessages": "true", + "launchBrowser": false, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/src/DevHive.Web/Startup.cs b/src/DevHive.Web/Startup.cs index 94aabe8..92d4359 100644 --- a/src/DevHive.Web/Startup.cs +++ b/src/DevHive.Web/Startup.cs @@ -33,6 +33,7 @@ namespace DevHive.Web services.JWTConfiguration(Configuration); services.AutoMapperConfiguration(); services.DependencyInjectionConfiguration(); + services.ExceptionHandlerMiddlewareConfiguration(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -46,14 +47,13 @@ namespace DevHive.Web if (env.IsDevelopment()) { - //app.UseDeveloperExceptionPage(); - app.UseExceptionHandler("/api/Error"); + app.UseDeveloperExceptionPage(); app.UseSwaggerConfiguration(); } else { - app.UseExceptionHandler("/api/Error"); app.UseHsts(); + app.UseExceptionHandlerMiddlewareConfiguration(); } app.UseDatabaseConfiguration(); diff --git a/src/DevHive.Web/appsettings.json b/src/DevHive.Web/appsettings.json index a460532..83932a7 100644 --- a/src/DevHive.Web/appsettings.json +++ b/src/DevHive.Web/appsettings.json @@ -1,15 +1,15 @@ -{
- "AppSettings": {
- "Secret": "gXfQlU6qpDleFWyimscjYcT3tgFsQg3yoFjcvSLxG56n1Vu2yptdIUq254wlJWjm"
- },
- "ConnectionStrings": {
- "DEV": "Server=localhost;Port=5432;Database=API;User Id=postgres;Password=;"
- },
- "Logging" : {
- "LogLevel" : {
- "Default" : "Information",
- "Microsoft" : "Warning",
- "Microsoft.Hosting.Lifetime" : "Information"
- }
- }
-}
+{ + "AppSettings": { + "Secret": "gXfQlU6qpDleFWyimscjYcT3tgFsQg3yoFjcvSLxG56n1Vu2yptdIUq254wlJWjm" + }, + "ConnectionStrings": { + "DEV": "Server=localhost;Port=5432;Database=API;User Id=postgres;Password=;" + }, + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} |
