Roles #21

Merged
csimonapastore merged 36 commits from roles into main 2025-03-26 23:52:19 +01:00
4 changed files with 53 additions and 11 deletions
Showing only changes of commit f6cd629fe2 - Show all commits

View File

@@ -2,11 +2,14 @@ using System.Net;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using BasicDotnetTemplate.MainProject.Models.Api.Response; using BasicDotnetTemplate.MainProject.Models.Api.Response;
using BasicDotnetTemplate.MainProject.Models.Settings; using BasicDotnetTemplate.MainProject.Models.Settings;
using AutoMapper;
using BasicDotnetTemplate.MainProject.Core.Middlewares;
namespace BasicDotnetTemplate.MainProject.Controllers namespace BasicDotnetTemplate.MainProject.Controllers
{ {
public abstract class BaseController : ControllerBase public abstract class BaseController : ControllerBase
{ {
protected readonly IMapper? _mapper;
protected readonly IConfiguration _configuration; protected readonly IConfiguration _configuration;
protected readonly AppSettings _appSettings; protected readonly AppSettings _appSettings;
protected readonly string _requestNotWellFormed = "Request is not well formed"; protected readonly string _requestNotWellFormed = "Request is not well formed";
@@ -18,6 +21,13 @@ namespace BasicDotnetTemplate.MainProject.Controllers
_configuration = configuration; _configuration = configuration;
_appSettings = new AppSettings(); _appSettings = new AppSettings();
_configuration.GetSection("AppSettings").Bind(_appSettings); _configuration.GetSection("AppSettings").Bind(_appSettings);
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile<AutoMapperConfiguration>();
});
_mapper = config.CreateMapper();
} }
#nullable enable #nullable enable

View File

@@ -6,13 +6,13 @@ using BasicDotnetTemplate.MainProject.Models.Api.Request.User;
using BasicDotnetTemplate.MainProject.Models.Api.Response; using BasicDotnetTemplate.MainProject.Models.Api.Response;
using BasicDotnetTemplate.MainProject.Models.Api.Response.User; using BasicDotnetTemplate.MainProject.Models.Api.Response.User;
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer; using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
using BasicDotnetTemplate.MainProject.Models.Api.Common.User;
namespace BasicDotnetTemplate.MainProject.Controllers namespace BasicDotnetTemplate.MainProject.Controllers
{ {
[Route("[controller]")] [Route("[controller]")]
public class UserController : BaseController public class UserController : BaseController
{ {
private readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
private readonly IUserService _userService; private readonly IUserService _userService;
private readonly IRoleService _roleService; private readonly IRoleService _roleService;
public UserController( public UserController(
@@ -44,14 +44,16 @@ namespace BasicDotnetTemplate.MainProject.Controllers
{ {
return BadRequest(_requestNotWellFormed); return BadRequest(_requestNotWellFormed);
} }
var data = await this._userService.GetUserByGuidAsync(guid); var user = await this._userService.GetUserByGuidAsync(guid);
if (data == null || String.IsNullOrEmpty(data.Guid)) if (user == null || String.IsNullOrEmpty(user.Guid))
{ {
return NotFound(); return NotFound();
} }
return Success(String.Empty, data); var userDto = _mapper?.Map<UserDto>(user);
return Success(String.Empty, userDto);
} }
catch (Exception exception) catch (Exception exception)
{ {
@@ -95,15 +97,22 @@ namespace BasicDotnetTemplate.MainProject.Controllers
} }
else else
{ {
Role? role = null; // TODO var role = await this._roleService.GetRoleForUser(request.Data.RoleGuid);
var data = await this._userService.CreateUser(request.Data, role); if (role == null)
{
return BadRequest("Role not found");
}
if (data == null || String.IsNullOrEmpty(data.Guid)) var user = await this._userService.CreateUserAsync(request.Data, role);
if (user == null || String.IsNullOrEmpty(user.Guid))
{ {
return BadRequest(); return BadRequest();
} }
return Success(String.Empty, data); var userDto = _mapper?.Map<UserDto>(user);
return Success(String.Empty, userDto);
} }
} }

View File

@@ -13,6 +13,7 @@ public interface IRoleService
Task<Role?> GetRoleByGuidAsync(string guid); Task<Role?> GetRoleByGuidAsync(string guid);
Task<bool> CheckIfNameIsValid(string name, string? guid = ""); Task<bool> CheckIfNameIsValid(string name, string? guid = "");
Task<Role?> CreateRole(CreateRoleRequestData data); Task<Role?> CreateRole(CreateRoleRequestData data);
Task<Role?> GetRoleForUser(string? guid);
} }
public class RoleService : BaseService, IRoleService public class RoleService : BaseService, IRoleService
@@ -36,6 +37,8 @@ public class RoleService : BaseService, IRoleService
); );
} }
private Role CreateRoleData(CreateRoleRequestData data) private Role CreateRoleData(CreateRoleRequestData data)
{ {
Role role = new() Role role = new()
@@ -52,6 +55,9 @@ public class RoleService : BaseService, IRoleService
} }
public async Task<Role?> GetRoleByIdAsync(int id) public async Task<Role?> GetRoleByIdAsync(int id)
{ {
return await this.GetRolesQueryable().Where(x => x.Id == id).FirstOrDefaultAsync(); return await this.GetRolesQueryable().Where(x => x.Id == id).FirstOrDefaultAsync();
@@ -98,6 +104,20 @@ public class RoleService : BaseService, IRoleService
return role; return role;
} }
public async Task<Role?> GetRoleForUser(string? guid)
{
Role? role = null;
if (String.IsNullOrEmpty(guid))
{
role = await this.GetRoleByNameQueryable("Default").FirstOrDefaultAsync();
}
else
{
role = await this.GetRoleByGuidAsync(guid);
}
return role;
}
} }

View File

@@ -13,7 +13,7 @@ public interface IUserService
Task<User?> GetUserByGuidAsync(string guid); Task<User?> GetUserByGuidAsync(string guid);
Task<User?> GetUserByUsernameAndPassword(string email, string password); Task<User?> GetUserByUsernameAndPassword(string email, string password);
Task<bool> CheckIfEmailIsValid(string email, string? guid = ""); Task<bool> CheckIfEmailIsValid(string email, string? guid = "");
Task<User?> CreateUser(CreateUserRequestData data, Role role); Task<User?> CreateUserAsync(CreateUserRequestData data, Role role);
} }
public class UserService : BaseService, IUserService public class UserService : BaseService, IUserService
@@ -102,16 +102,19 @@ public class UserService : BaseService, IUserService
return valid; return valid;
} }
public async Task<User?> CreateUser(CreateUserRequestData data, Role role) public async Task<User?> CreateUserAsync(CreateUserRequestData data, Role role)
{ {
User? user = null; User? user = null;
using (var transaction = _sqlServerContext.Database.BeginTransactionAsync()) using (var transaction = _sqlServerContext.Database.BeginTransactionAsync())
{ {
var tempUser = this.CreateUserData(data, role); var tempUser = this.CreateUserData(data, role);
await _sqlServerContext.Users.AddAsync(tempUser);
await _sqlServerContext.SaveChangesAsync();
await (await transaction).CommitAsync();
user = tempUser;
} }
return user; return user;
} }