From 0f35d898c2d0bbc84196ad254dd94a26852e3e4e Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 15 Dec 2020 20:13:24 +0200 Subject: Implemented RoleService and updated RoleRepository structure --- src/DevHive.Data/Repositories/RoleRepository.cs | 21 ++++++++-- src/DevHive.Services/Services/RoleService.cs | 53 ++++++++++++++++++++----- 2 files changed, 60 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs index 1c40b04..b83a2e6 100644 --- a/src/DevHive.Data/Repositories/RoleRepository.cs +++ b/src/DevHive.Data/Repositories/RoleRepository.cs @@ -2,32 +2,47 @@ using System; using System.Threading.Tasks; using Data.Models.Interfaces.Database; using DevHive.Data.Models; +using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { public class RoleRepository : IRepository { + private readonly DbContext _context; + + public RoleRepository(DbContext context) + { + this._context = context; + } + public async Task AddAsync(Role entity) { throw new NotImplementedException(); } - //Find entity by id public async Task GetByIdAsync(Guid id) { throw new NotImplementedException(); } - //Modify Entity from database public async Task EditAsync(Role newEntity) { throw new NotImplementedException(); } - //Delete Entity from database public async Task DeleteAsync(Role entity) { throw new NotImplementedException(); } + + public async Task DoesNameExist(string name) + { + throw new NotImplementedException(); + } + + public async Task DoesRoleExist(Guid id) + { + throw new NotImplementedException(); + } } } diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index 9077a47..b3e734d 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -1,5 +1,7 @@ using System; using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Models; using DevHive.Data.Repositories; using DevHive.Services.Models.Identity.Role; using Microsoft.AspNetCore.Mvc; @@ -8,31 +10,60 @@ namespace DevHive.Services.Services { public class RoleService { - private readonly DevHiveContext _context; + private readonly RoleRepository _roleRepository; + private readonly IMapper _roleMapper; - public RoleService(DevHiveContext context) + public RoleService(DevHiveContext context, IMapper mapper) { - this._context = context; + this._roleRepository = new RoleRepository(context); + this._roleMapper = mapper; } - public Task CreateRole(RoleServiceModel roleServiceModel) + public async Task CreateRole(RoleServiceModel roleServiceModel) { - throw new NotImplementedException(); + if (!await this._roleRepository.DoesNameExist(roleServiceModel.Name)) + return new BadRequestObjectResult("Invalid role name!"); + + Role role = this._roleMapper.Map(roleServiceModel); + + await this._roleRepository.AddAsync(role); + + return new CreatedResult("CreateRole", role); } - public Task GetRoleById(Guid id) + public async Task GetRoleById(Guid id) { - throw new NotImplementedException(); + Role role = await this._roleRepository.GetByIdAsync(id); + + if (role == null) + return new NotFoundObjectResult("Role does not exist!"); + + return new ObjectResult(role); } - public Task UpdateRole(RoleServiceModel roleServiceModel) + public async Task UpdateRole(RoleServiceModel roleServiceModel) { - throw new NotImplementedException(); + if (!await this._roleRepository.DoesRoleExist(roleServiceModel.Id)) + return new NotFoundObjectResult("Role does not exist!"); + + if (!await this._roleRepository.DoesNameExist(roleServiceModel.Name)) + return new BadRequestObjectResult("Role name already exists!"); + + Role role = this._roleMapper.Map(roleServiceModel); + await this._roleRepository.EditAsync(role); + + return new AcceptedResult("UpdateRole", role); } - public Task DeleteRole(Guid id) + public async Task DeleteRole(Guid id) { - throw new NotImplementedException(); + if (!await this._roleRepository.DoesRoleExist(id)) + return new NotFoundObjectResult("Role does not exist!"); + + Role role = await this._roleRepository.GetByIdAsync(id); + await this._roleRepository.DeleteAsync(role); + + return new OkResult(); } } } -- cgit v1.2.3 From b4f102b15879fd2a8415fd42373c990e17a86baf Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 15 Dec 2020 20:27:13 +0200 Subject: Implemented RoleRepository --- src/DevHive.Data/Repositories/RoleRepository.cs | 39 ++++++++++++++++++++----- 1 file changed, 32 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs index b83a2e6..9b6cf14 100644 --- a/src/DevHive.Data/Repositories/RoleRepository.cs +++ b/src/DevHive.Data/Repositories/RoleRepository.cs @@ -15,34 +15,59 @@ namespace DevHive.Data.Repositories this._context = context; } + //Create public async Task AddAsync(Role entity) { - throw new NotImplementedException(); + await this._context + .Set() + .AddAsync(entity); + + await this._context.SaveChangesAsync(); } + //Read public async Task GetByIdAsync(Guid id) { - throw new NotImplementedException(); + return await this._context + .Set() + .FindAsync(id); + } + //Update public async Task EditAsync(Role newEntity) { - throw new NotImplementedException(); - } + this._context + .Set() + .Update(newEntity); + await this._context.SaveChangesAsync(); + } + + //Delete public async Task DeleteAsync(Role entity) { - throw new NotImplementedException(); + this._context + .Set() + .Remove(entity); + + await this._context.SaveChangesAsync(); } public async Task DoesNameExist(string name) { - throw new NotImplementedException(); + return await this._context + .Set() + .AsNoTracking() + .AnyAsync(r => r.Name == name); } public async Task DoesRoleExist(Guid id) { - throw new NotImplementedException(); + return await this._context + .Set() + .AsNoTracking() + .AnyAsync(r => r.Id == id); } } } -- cgit v1.2.3