aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDanail Dimitrov <danaildimitrov321@gmail.com>2020-12-15 20:39:59 +0200
committerDanail Dimitrov <danaildimitrov321@gmail.com>2020-12-15 20:39:59 +0200
commit98ae2dde03781166518fb586b81753ac2f923831 (patch)
tree35fcb6905fa19acb73af9982223648f47305a83d /src
parentb6855296340e4b70ee7a83d3118780eb23e46c83 (diff)
parentb4f102b15879fd2a8415fd42373c990e17a86baf (diff)
downloadDevHive-98ae2dde03781166518fb586b81753ac2f923831.tar
DevHive-98ae2dde03781166518fb586b81753ac2f923831.tar.gz
DevHive-98ae2dde03781166518fb586b81753ac2f923831.zip
Merge branch 'dev' of github.com:Team-Kaleidoscope/DevHive into dev
Diffstat (limited to 'src')
-rw-r--r--src/DevHive.Data/Repositories/RoleRepository.cs56
-rw-r--r--src/DevHive.Services/Services/RoleService.cs53
2 files changed, 90 insertions, 19 deletions
diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs
index 1c40b04..9b6cf14 100644
--- a/src/DevHive.Data/Repositories/RoleRepository.cs
+++ b/src/DevHive.Data/Repositories/RoleRepository.cs
@@ -2,32 +2,72 @@ 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<Role>
{
+ private readonly DbContext _context;
+
+ public RoleRepository(DbContext context)
+ {
+ this._context = context;
+ }
+
+ //Create
public async Task AddAsync(Role entity)
{
- throw new NotImplementedException();
+ await this._context
+ .Set<Role>()
+ .AddAsync(entity);
+
+ await this._context.SaveChangesAsync();
}
- //Find entity by id
+ //Read
public async Task<Role> GetByIdAsync(Guid id)
{
- throw new NotImplementedException();
+ return await this._context
+ .Set<Role>()
+ .FindAsync(id);
+
}
- //Modify Entity from database
+ //Update
public async Task EditAsync(Role newEntity)
{
- throw new NotImplementedException();
- }
+ this._context
+ .Set<Role>()
+ .Update(newEntity);
- //Delete Entity from database
+ await this._context.SaveChangesAsync();
+ }
+
+ //Delete
public async Task DeleteAsync(Role entity)
{
- throw new NotImplementedException();
+ this._context
+ .Set<Role>()
+ .Remove(entity);
+
+ await this._context.SaveChangesAsync();
+ }
+
+ public async Task<bool> DoesNameExist(string name)
+ {
+ return await this._context
+ .Set<Role>()
+ .AsNoTracking()
+ .AnyAsync(r => r.Name == name);
+ }
+
+ public async Task<bool> DoesRoleExist(Guid id)
+ {
+ return await this._context
+ .Set<Role>()
+ .AsNoTracking()
+ .AnyAsync(r => r.Id == id);
}
}
}
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<IActionResult> CreateRole(RoleServiceModel roleServiceModel)
+ public async Task<IActionResult> CreateRole(RoleServiceModel roleServiceModel)
{
- throw new NotImplementedException();
+ if (!await this._roleRepository.DoesNameExist(roleServiceModel.Name))
+ return new BadRequestObjectResult("Invalid role name!");
+
+ Role role = this._roleMapper.Map<Role>(roleServiceModel);
+
+ await this._roleRepository.AddAsync(role);
+
+ return new CreatedResult("CreateRole", role);
}
- public Task<IActionResult> GetRoleById(Guid id)
+ public async Task<IActionResult> 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<IActionResult> UpdateRole(RoleServiceModel roleServiceModel)
+ public async Task<IActionResult> 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<Role>(roleServiceModel);
+ await this._roleRepository.EditAsync(role);
+
+ return new AcceptedResult("UpdateRole", role);
}
- public Task<IActionResult> DeleteRole(Guid id)
+ public async Task<IActionResult> 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();
}
}
}