aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSyndamia <kamen.d.mladenov@protonmail.com>2020-12-17 11:59:52 +0200
committerSyndamia <kamen.d.mladenov@protonmail.com>2020-12-17 11:59:52 +0200
commitfadb0fd1b7a13d1c6210e11f2db2add7c8fd45a9 (patch)
tree58ac9249044012c5754cc5bfbb7ee0c51740ccc6 /src
parent9346dc469481c7ec07a36700180c7008a2d9cb45 (diff)
downloadDevHive-fadb0fd1b7a13d1c6210e11f2db2add7c8fd45a9.tar
DevHive-fadb0fd1b7a13d1c6210e11f2db2add7c8fd45a9.tar.gz
DevHive-fadb0fd1b7a13d1c6210e11f2db2add7c8fd45a9.zip
Reworked RoleController and RoleService to be consistent with the app structure
Diffstat (limited to 'src')
-rw-r--r--src/DevHive.Services/Configurations/Mapping/RoleMapings.cs1
-rw-r--r--src/DevHive.Services/Services/RoleService.cs39
-rw-r--r--src/DevHive.Web/Configurations/Mapping/RoleMappings.cs2
-rw-r--r--src/DevHive.Web/Controllers/RoleController.cs35
-rw-r--r--src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs10
-rw-r--r--src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs6
6 files changed, 56 insertions, 37 deletions
diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs
index 0e06523..25b314a 100644
--- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs
+++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs
@@ -9,6 +9,7 @@ namespace DevHive.Services.Configurations.Mapping
public RoleMappings()
{
CreateMap<RoleServiceModel, Role>();
+ CreateMap<Role, RoleServiceModel>();
}
}
}
diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs
index c04bf31..d5c1848 100644
--- a/src/DevHive.Services/Services/RoleService.cs
+++ b/src/DevHive.Services/Services/RoleService.cs
@@ -4,7 +4,6 @@ using AutoMapper;
using DevHive.Data.Models;
using DevHive.Data.Repositories;
using DevHive.Services.Models.Identity.Role;
-using Microsoft.AspNetCore.Mvc;
namespace DevHive.Services.Services
{
@@ -19,51 +18,43 @@ namespace DevHive.Services.Services
this._roleMapper = mapper;
}
- public async Task<IActionResult> CreateRole(RoleServiceModel roleServiceModel)
+ public async Task<bool> CreateRole(RoleServiceModel roleServiceModel)
{
if (await this._roleRepository.DoesNameExist(roleServiceModel.Name))
- return new BadRequestObjectResult("Role already exists!");
+ throw new ArgumentException("Role already exists!");
Role role = this._roleMapper.Map<Role>(roleServiceModel);
- await this._roleRepository.AddAsync(role);
-
- return new CreatedResult("CreateRole", role);
+ return await this._roleRepository.AddAsync(role);
}
- public async Task<IActionResult> GetRoleById(Guid id)
+ public async Task<RoleServiceModel> GetRoleById(Guid id)
{
- Role role = await this._roleRepository.GetByIdAsync(id);
-
- if (role == null)
- return new NotFoundObjectResult("Role does not exist!");
+ Role role = await this._roleRepository.GetByIdAsync(id)
+ ?? throw new ArgumentException("Role does not exist!");
- return new ObjectResult(role);
+ return this._roleMapper.Map<RoleServiceModel>(role);
}
- public async Task<IActionResult> UpdateRole(RoleServiceModel roleServiceModel)
+ public async Task<bool> UpdateRole(RoleServiceModel roleServiceModel)
{
if (!await this._roleRepository.DoesRoleExist(roleServiceModel.Id))
- return new NotFoundObjectResult("Role does not exist!");
+ throw new ArgumentException("Role does not exist!");
- if (!await this._roleRepository.DoesNameExist(roleServiceModel.Name))
- return new BadRequestObjectResult("Role name already exists!");
+ if (await this._roleRepository.DoesNameExist(roleServiceModel.Name))
+ throw new ArgumentException("Role name already exists!");
Role role = this._roleMapper.Map<Role>(roleServiceModel);
- await this._roleRepository.EditAsync(role);
-
- return new AcceptedResult("UpdateRole", role);
+ return await this._roleRepository.EditAsync(role);
}
- public async Task<IActionResult> DeleteRole(Guid id)
+ public async Task<bool> DeleteRole(Guid id)
{
if (!await this._roleRepository.DoesRoleExist(id))
- return new NotFoundObjectResult("Role does not exist!");
+ throw new ArgumentException("Role does not exist!");
Role role = await this._roleRepository.GetByIdAsync(id);
- await this._roleRepository.DeleteAsync(role);
-
- return new OkResult();
+ return await this._roleRepository.DeleteAsync(role);
}
}
}
diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs
index 9ccb7d7..2743df3 100644
--- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs
+++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs
@@ -10,6 +10,8 @@ namespace DevHive.Web.Configurations.Mapping
{
CreateMap<CreateRoleWebModel, RoleServiceModel>();
CreateMap<UpdateRoleWebModel, RoleServiceModel>();
+ CreateMap<RoleServiceModel, RoleWebModel>();
+ CreateMap<RoleWebModel, RoleServiceModel>();
}
}
}
diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs
index 610d370..1664f4f 100644
--- a/src/DevHive.Web/Controllers/RoleController.cs
+++ b/src/DevHive.Web/Controllers/RoleController.cs
@@ -25,33 +25,52 @@ namespace DevHive.Web.Controllers
}
[HttpPost]
- public Task<IActionResult> Create(CreateRoleWebModel createRoleWebModel)
+ public async Task<IActionResult> Create([FromBody] CreateRoleWebModel createRoleWebModel)
{
RoleServiceModel roleServiceModel =
this._roleMapper.Map<RoleServiceModel>(createRoleWebModel);
- return this._roleService.CreateRole(roleServiceModel);
+ bool result = await this._roleService.CreateRole(roleServiceModel);
+
+ if (!result)
+ return new BadRequestObjectResult("Could not create role!");
+
+ return new OkResult();
}
[HttpGet]
- public Task<IActionResult> GetById(Guid id)
+ public async Task<IActionResult> GetById(Guid id)
{
- return this._roleService.GetRoleById(id);
+ RoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id);
+ RoleWebModel roleWebModel = this._roleMapper.Map<RoleWebModel>(roleServiceModel);
+
+ return new OkObjectResult(roleWebModel);
}
[HttpPut]
- public Task<IActionResult> Update(UpdateRoleWebModel updateRoleWebModel)
+ public async Task<IActionResult> Update(Guid id, [FromBody] UpdateRoleWebModel updateRoleWebModel)
{
RoleServiceModel roleServiceModel =
this._roleMapper.Map<RoleServiceModel>(updateRoleWebModel);
+ roleServiceModel.Id = id;
+
+ bool result = await this._roleService.UpdateRole(roleServiceModel);
- return this._roleService.UpdateRole(roleServiceModel);
+ if (!result)
+ return new BadRequestObjectResult("Could not update role!");
+
+ return new OkResult();
}
[HttpDelete]
- public Task<IActionResult> Delete(Guid id)
+ public async Task<IActionResult> Delete(Guid id)
{
- return this._roleService.DeleteRole(id);
+ bool result = await this._roleService.DeleteRole(id);
+
+ if (!result)
+ return new BadRequestObjectResult("Could nor delete role!");
+
+ return new OkResult();
}
}
}
diff --git a/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs
new file mode 100644
index 0000000..d8ae465
--- /dev/null
+++ b/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs
@@ -0,0 +1,10 @@
+using System;
+
+namespace DevHive.Web.Models.Identity.Role
+{
+ public class RoleWebModel
+ {
+ public Guid Id { get; set; }
+ 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 b970dd5..213ec55 100644
--- a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs
+++ b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs
@@ -1,10 +1,6 @@
-using System;
-
namespace DevHive.Web.Models.Identity.Role
{
- public class UpdateRoleWebModel
+ public class UpdateRoleWebModel : CreateRoleWebModel
{
- public Guid Id { get; set; }
- public string NewName { get; set; }
}
}