Minor fixes for CreateUser

This commit is contained in:
2025-03-16 23:26:15 +01:00
parent 18e713153b
commit f6cd629fe2
4 changed files with 53 additions and 11 deletions

View File

@@ -2,11 +2,14 @@ using System.Net;
using Microsoft.AspNetCore.Mvc;
using BasicDotnetTemplate.MainProject.Models.Api.Response;
using BasicDotnetTemplate.MainProject.Models.Settings;
using AutoMapper;
using BasicDotnetTemplate.MainProject.Core.Middlewares;
namespace BasicDotnetTemplate.MainProject.Controllers
{
public abstract class BaseController : ControllerBase
{
protected readonly IMapper? _mapper;
protected readonly IConfiguration _configuration;
protected readonly AppSettings _appSettings;
protected readonly string _requestNotWellFormed = "Request is not well formed";
@@ -18,6 +21,13 @@ namespace BasicDotnetTemplate.MainProject.Controllers
_configuration = configuration;
_appSettings = new AppSettings();
_configuration.GetSection("AppSettings").Bind(_appSettings);
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile<AutoMapperConfiguration>();
});
_mapper = config.CreateMapper();
}
#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.User;
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
using BasicDotnetTemplate.MainProject.Models.Api.Common.User;
namespace BasicDotnetTemplate.MainProject.Controllers
{
[Route("[controller]")]
public class UserController : BaseController
{
private readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
private readonly IUserService _userService;
private readonly IRoleService _roleService;
public UserController(
@@ -44,14 +44,16 @@ namespace BasicDotnetTemplate.MainProject.Controllers
{
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 Success(String.Empty, data);
var userDto = _mapper?.Map<UserDto>(user);
return Success(String.Empty, userDto);
}
catch (Exception exception)
{
@@ -95,15 +97,22 @@ namespace BasicDotnetTemplate.MainProject.Controllers
}
else
{
Role? role = null; // TODO
var data = await this._userService.CreateUser(request.Data, role);
var role = await this._roleService.GetRoleForUser(request.Data.RoleGuid);
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 Success(String.Empty, data);
var userDto = _mapper?.Map<UserDto>(user);
return Success(String.Empty, userDto);
}
}