diff options
Diffstat (limited to 'src/DevHive.Web/Controllers')
| -rw-r--r-- | src/DevHive.Web/Controllers/ErrorController.cs | 50 | ||||
| -rw-r--r-- | src/DevHive.Web/Controllers/LanguageController.cs | 24 | ||||
| -rw-r--r-- | src/DevHive.Web/Controllers/PostController.cs | 18 | ||||
| -rw-r--r-- | src/DevHive.Web/Controllers/RoleController.cs | 30 | ||||
| -rw-r--r-- | src/DevHive.Web/Controllers/TechnologyController.cs | 28 | ||||
| -rw-r--r-- | src/DevHive.Web/Controllers/UserController.cs | 84 |
6 files changed, 58 insertions, 176 deletions
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 } } |
