Skip to content

Commit ca9f583

Browse files
committed
✨ Implement user role assignment and enhance user status mngmt
1 parent 4a2fa20 commit ca9f583

File tree

7 files changed

+118
-112
lines changed

7 files changed

+118
-112
lines changed

TaleEngine/TaleEngine.Application.Contracts/IUserService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ public interface IUserService
99
UserEntity GetById(int id);
1010
List<UserEntity> GetAllUsers();
1111
void ChangeUserStatus(int id, User user);
12+
void AssignRoleToUser(int id, int roleId);
1213
}
1314
}

TaleEngine/TaleEngine.Application/Backoffice/UserService.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ public UserService(IUnitOfWork unitOfWork)
1616
_unitOfWork = unitOfWork ?? throw new ArgumentNullException();
1717
}
1818

19-
public void ChangeUserStatus(int id, User user)
20-
{
21-
throw new NotImplementedException();
22-
}
23-
2419
public List<UserEntity> GetAllUsers()
2520
{
2621
var result = _unitOfWork.UserRepository.GetAll();
@@ -32,5 +27,28 @@ public UserEntity GetById(int id)
3227
var result = _unitOfWork.UserRepository.GetById(id);
3328
return result;
3429
}
30+
31+
public void ChangeUserStatus(int id, User user)
32+
{
33+
var userEntity = _unitOfWork.UserRepository.GetById(id);
34+
if (userEntity == null) return;
35+
36+
userEntity.StatusId = user.Status;
37+
_unitOfWork.UserRepository.Update(userEntity);
38+
_unitOfWork.UserRepository.Save();
39+
}
40+
41+
public void AssignRoleToUser(int id, int roleId)
42+
{
43+
var userEntity = _unitOfWork.UserRepository.GetById(id);
44+
if (userEntity == null) return;
45+
46+
var role = _unitOfWork.RoleRepository.GetById(roleId);
47+
if (role == null) return;
48+
49+
userEntity.Roles.Add(role);
50+
_unitOfWork.UserRepository.Update(userEntity);
51+
_unitOfWork.UserRepository.Save();
52+
}
3553
}
3654
}
Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using TaleEngine.Aggregates.UserAggregate;
23
using TaleEngine.CQRS.Contracts;
34
using TaleEngine.Services.Contracts;
45

@@ -9,57 +10,66 @@ public class UserCommands : IUserCommands
910
private readonly IUserStatusQueries _userStatusCommands;
1011
private readonly IUserService _service;
1112

12-
public UserCommands(IUserStatusQueries userStatusCommands)
13+
public UserCommands(IUserStatusQueries userStatusCommands, IUserService service)
1314
{
1415
_userStatusCommands = userStatusCommands ?? throw new ArgumentNullException(nameof(userStatusCommands));
16+
_service = service ?? throw new ArgumentNullException(nameof(service));
1517
}
1618

1719
public void ActivateCommand(int userId)
1820
{
1921
if (userId == 0) return;
2022
var status = _userStatusCommands.ActiveQuery();
2123
if (status == 0) return;
22-
//ChangeUserStatusTo(userId, status);
24+
ChangeUserStatusTo(userId, status);
2325
}
2426

2527
public void BanCommand(int userId)
2628
{
2729
if (userId == 0) return;
2830
var status = _userStatusCommands.BanQuery();
2931
if (status == 0) return;
30-
//ChangeUserStatusTo(userId, status);
32+
ChangeUserStatusTo(userId, status);
3133
}
3234

3335
public void DeactivateCommand(int userId)
3436
{
3537
if (userId == 0) return;
3638
var status = _userStatusCommands.InactiveQuery();
3739
if (status == 0) return;
38-
//ChangeUserStatusTo(userId, status);
40+
ChangeUserStatusTo(userId, status);
3941
}
4042

4143
public void MarkAsPendingCommand(int userId)
4244
{
4345
if (userId == 0) return;
4446
var status = _userStatusCommands.PendingQuery();
4547
if (status == 0) return;
46-
//ChangeUserStatusTo(userId, status);
48+
ChangeUserStatusTo(userId, status);
4749
}
4850

4951
public void ReviewCommand(int userId)
5052
{
5153
if (userId == 0) return;
5254
var status = _userStatusCommands.RevisionQuery();
5355
if (status == 0) return;
54-
//ChangeUserStatusTo(userId, status);
56+
ChangeUserStatusTo(userId, status);
57+
}
58+
59+
public void AssignRoleCommand(int userId, int roleId)
60+
{
61+
if (userId == 0 || roleId == 0) return;
62+
63+
_service.AssignRoleToUser(userId, roleId);
5564
}
5665

5766
private void ChangeUserStatusTo(int userId, int status)
5867
{
5968
var user = _service.GetById(userId);
6069
if (user == null) return;
6170

62-
throw new NotImplementedException();
71+
var userAggregate = new User { Status = status };
72+
_service.ChangeUserStatus(userId, userAggregate);
6373
}
6474
}
6575
}

TaleEngine/TaleEngine.Bussiness/Contracts/IUserCommands.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ public interface IUserCommands
77
void BanCommand(int userId);
88
void ReviewCommand(int userId);
99
void MarkAsPendingCommand(int userId);
10+
void AssignRoleCommand(int userId, int roleId);
1011
}
1112
}

TaleEngine/TaleEngine/Controllers/V2/RolesController.cs renamed to TaleEngine/TaleEngine/Controllers/Backoffice/RolesController.cs

Lines changed: 67 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,67 @@
1-
using Microsoft.AspNetCore.Http;
2-
using Microsoft.AspNetCore.Mvc;
3-
using System;
4-
using TaleEngine.CQRS.Contracts;
5-
6-
namespace TaleEngine.API.Controllers.V2
7-
{
8-
/// <summary>
9-
/// Roles management
10-
/// </summary>
11-
[ApiController]
12-
[ProducesResponseType(StatusCodes.Status400BadRequest)]
13-
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
14-
[ProducesResponseType(StatusCodes.Status403Forbidden)]
15-
[ProducesResponseType(StatusCodes.Status404NotFound)]
16-
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
17-
[ControllerName("Management roles and permissions associated")]
18-
[Route("api/v2/[controller]")]
19-
public class RolesController : Controller
20-
{
21-
private IRoleQueries query;
22-
23-
/// <summary>
24-
/// Constructor for roles controller
25-
/// </summary>
26-
/// <param name="command">Role service</param>
27-
public RolesController(IRoleQueries command)
28-
{
29-
query = command ?? throw new ArgumentNullException(nameof(command));
30-
}
31-
32-
/// <summary>
33-
/// Gets all the roles in the app
34-
/// </summary>
35-
/// <returns>All the roles</returns>
36-
[HttpGet("[action]")]
37-
[ProducesResponseType(StatusCodes.Status200OK)]
38-
[ProducesResponseType(StatusCodes.Status204NoContent)]
39-
public IActionResult GetAllRoles()
40-
{
41-
var result = query.AllRolesQuery();
42-
43-
if (result == null || result.Count == 0)
44-
{
45-
return NoContent();
46-
}
47-
48-
return Ok(result);
49-
}
50-
51-
/// <summary>
52-
/// Gets a single role with permissions
53-
/// </summary>
54-
/// <param name="roleId">Role identifier</param>
55-
/// <returns>The selected role with permissions</returns>
56-
[HttpGet("[action]/{roleId:int}")]
57-
[ProducesResponseType(StatusCodes.Status200OK)]
58-
[ProducesResponseType(StatusCodes.Status204NoContent)]
59-
public IActionResult GetRole(int roleId)
60-
{
61-
var result = query.GetRoleQuery(roleId);
62-
return Ok(result);
63-
}
64-
65-
}
66-
}
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
3+
using System;
4+
using TaleEngine.CQRS.Contracts;
5+
6+
namespace TaleEngine.API.Controllers.Backoffice
7+
{
8+
/// <summary>
9+
/// Roles management
10+
/// </summary>
11+
[ApiController]
12+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
13+
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
14+
[ProducesResponseType(StatusCodes.Status403Forbidden)]
15+
[ProducesResponseType(StatusCodes.Status404NotFound)]
16+
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
17+
[ControllerName("Management roles and permissions associated")]
18+
[Route("api/[controller]")]
19+
public class RolesController : Controller
20+
{
21+
private IRoleQueries query;
22+
23+
/// <summary>
24+
/// Constructor for roles controller
25+
/// </summary>
26+
/// <param name="command">Role service</param>
27+
public RolesController(IRoleQueries command)
28+
{
29+
query = command ?? throw new ArgumentNullException(nameof(command));
30+
}
31+
32+
/// <summary>
33+
/// Gets all the roles in the app
34+
/// </summary>
35+
/// <returns>All the roles</returns>
36+
[HttpGet("[action]")]
37+
[ProducesResponseType(StatusCodes.Status200OK)]
38+
[ProducesResponseType(StatusCodes.Status204NoContent)]
39+
public IActionResult GetAllRoles()
40+
{
41+
var result = query.AllRolesQuery();
42+
43+
if (result == null || result.Count == 0)
44+
{
45+
return NoContent();
46+
}
47+
48+
return Ok(result);
49+
}
50+
51+
/// <summary>
52+
/// Gets a single role with permissions
53+
/// </summary>
54+
/// <param name="roleId">Role identifier</param>
55+
/// <returns>The selected role with permissions</returns>
56+
[HttpGet("[action]/{roleId:int}")]
57+
[ProducesResponseType(StatusCodes.Status200OK)]
58+
[ProducesResponseType(StatusCodes.Status204NoContent)]
59+
public IActionResult GetRole(int roleId)
60+
{
61+
var result = query.GetRoleQuery(roleId);
62+
return Ok(result);
63+
}
64+
65+
66+
}
67+
}

TaleEngine/TaleEngine/Controllers/Backoffice/UserController.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace TaleEngine.API.Controllers.Backoffice
66
{
77
[ApiController]
8-
[Route("api/v1/[controller]")]
8+
[Route("api/[controller]")]
99
public class UserController : Controller
1010
{
1111
private readonly IUserCommands _command;
@@ -64,5 +64,13 @@ public IActionResult ReviewUser(int userId)
6464

6565
return Ok();
6666
}
67+
68+
[HttpPost("[action]")]
69+
public IActionResult AssignRole(int userId, int roleId)
70+
{
71+
_command.AssignRoleCommand(userId, roleId);
72+
73+
return Ok();
74+
}
6775
}
6876
}

TaleEngine/TaleEngine/Controllers/V1/RolesController.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)