From 8d1d0b40d56f90248f948e474330258bf57cf0b6 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 17 Jan 2021 14:45:48 +0200 Subject: Fixed role implementation by bringing back and improving all role models --- src/DevHive.Services/Services/RoleService.cs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'src/DevHive.Services/Services/RoleService.cs') diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index c38ac74..0945624 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -1,14 +1,15 @@ using System; using System.Threading.Tasks; using AutoMapper; -using DevHive.Common.Models.Identity; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using DevHive.Services.Interfaces; +using DevHive.Services.Models.Identity.Role; +using DevHive.Services.Models.Language; namespace DevHive.Services.Services { - public class RoleService : IRoleService + public class RoleService : IRoleService { private readonly IRoleRepository _roleRepository; private readonly IMapper _roleMapper; @@ -19,25 +20,34 @@ namespace DevHive.Services.Services this._roleMapper = mapper; } - public async Task CreateRole(RoleModel roleServiceModel) + public async Task CreateRole(RoleServiceModel roleServiceModel) { if (await this._roleRepository.DoesNameExist(roleServiceModel.Name)) throw new ArgumentException("Role already exists!"); + Role role = this._roleMapper.Map(roleServiceModel); + bool success = await this._roleRepository.AddAsync(role); + + if(success) + { + Role newRole = await this._roleRepository.GetByNameAsync(roleServiceModel.Name); + return newRole.Id; + } + else + return Guid.Empty; - return await this._roleRepository.AddAsync(role); } - 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(RoleModel roleServiceModel) + public async Task UpdateRole(RoleServiceModel roleServiceModel) { if (!await this._roleRepository.DoesRoleExist(roleServiceModel.Id)) throw new ArgumentException("Role does not exist!"); -- cgit v1.2.3 From 163827d4a8dbc4b41bfa0f78edc7dd30626e95d3 Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 19 Jan 2021 19:02:37 +0200 Subject: Fixed role id creeping up the chain --- src/DevHive.Services/Interfaces/IRoleService.cs | 2 +- src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs | 3 --- .../Models/Identity/Role/UpdateRoleServiceModel.cs | 3 +++ src/DevHive.Services/Services/RoleService.cs | 8 ++++---- 4 files changed, 8 insertions(+), 8 deletions(-) (limited to 'src/DevHive.Services/Services/RoleService.cs') diff --git a/src/DevHive.Services/Interfaces/IRoleService.cs b/src/DevHive.Services/Interfaces/IRoleService.cs index a7a0e47..fd661be 100644 --- a/src/DevHive.Services/Interfaces/IRoleService.cs +++ b/src/DevHive.Services/Interfaces/IRoleService.cs @@ -10,7 +10,7 @@ namespace DevHive.Services.Interfaces Task GetRoleById(Guid id); - Task UpdateRole(RoleServiceModel roleServiceModel); + Task UpdateRole(UpdateRoleServiceModel roleServiceModel); Task DeleteRole(Guid id); } diff --git a/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs b/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs index 3f834ef..07249fe 100644 --- a/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs @@ -1,10 +1,7 @@ -using System; - namespace DevHive.Services.Models.Identity.Role { public class RoleServiceModel { - public Guid Id { get; set; } public string Name { get; set; } } } diff --git a/src/DevHive.Services/Models/Identity/Role/UpdateRoleServiceModel.cs b/src/DevHive.Services/Models/Identity/Role/UpdateRoleServiceModel.cs index be71771..e21e6b4 100644 --- a/src/DevHive.Services/Models/Identity/Role/UpdateRoleServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/Role/UpdateRoleServiceModel.cs @@ -1,7 +1,10 @@ +using System; + namespace DevHive.Services.Models.Identity.Role { public class UpdateRoleServiceModel { + 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 0945624..3ebb7c8 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -47,15 +47,15 @@ namespace DevHive.Services.Services return this._roleMapper.Map(role); } - public async Task UpdateRole(RoleServiceModel roleServiceModel) + public async Task UpdateRole(UpdateRoleServiceModel updateRoleServiceModel) { - if (!await this._roleRepository.DoesRoleExist(roleServiceModel.Id)) + if (!await this._roleRepository.DoesRoleExist(updateRoleServiceModel.Id)) throw new ArgumentException("Role does not exist!"); - if (await this._roleRepository.DoesNameExist(roleServiceModel.Name)) + if (await this._roleRepository.DoesNameExist(updateRoleServiceModel.Name)) throw new ArgumentException("Role name already exists!"); - Role role = this._roleMapper.Map(roleServiceModel); + Role role = this._roleMapper.Map(updateRoleServiceModel); return await this._roleRepository.EditAsync(role); } -- cgit v1.2.3