Adding user CRUD methods and controller + automapper
This commit is contained in:
@@ -23,9 +23,9 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
|
||||
[HttpPost("authenticate")]
|
||||
[ProducesResponseType<AuthenticateResponse>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<BaseResponse>(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType<BaseResponse>(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType<BaseResponse>(StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> AuthenticateAsync([FromBody] AuthenticateRequest request)
|
||||
{
|
||||
try
|
||||
@@ -34,7 +34,7 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
{
|
||||
return BadRequest("Request is not well formed");
|
||||
}
|
||||
|
||||
|
||||
if (
|
||||
request == null ||
|
||||
request.Data == null ||
|
||||
|
||||
@@ -20,9 +20,9 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
}
|
||||
|
||||
#nullable enable
|
||||
private static BaseResponse CreateResponse(HttpStatusCode status, string message, object? data = null)
|
||||
private static BaseResponse<T> CreateResponse<T>(HttpStatusCode status, string message, T? data)
|
||||
{
|
||||
return new BaseResponse((int)status, message, data);
|
||||
return new BaseResponse<T>((int)status, message, data);
|
||||
}
|
||||
|
||||
protected new IActionResult Created(string message, object? data = null)
|
||||
@@ -58,7 +58,7 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
protected IActionResult InternalServerError(string message)
|
||||
{
|
||||
message = String.IsNullOrEmpty(message) ? "Internal server error" : message;
|
||||
return StatusCode((int)HttpStatusCode.InternalServerError, CreateResponse(HttpStatusCode.InternalServerError, message));
|
||||
return StatusCode((int)HttpStatusCode.InternalServerError, CreateResponse(HttpStatusCode.InternalServerError, message, new object()));
|
||||
}
|
||||
|
||||
#nullable disable
|
||||
|
||||
62
MainProject/Controllers/UserController.cs
Normal file
62
MainProject/Controllers/UserController.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using BasicDotnetTemplate.MainProject.Core.Attributes;
|
||||
using BasicDotnetTemplate.MainProject.Services;
|
||||
//using BasicDotnetTemplate.MainProject.Models.Api.Request.User;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Response.User;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
{
|
||||
[Route("[controller]")]
|
||||
public class UserController : BaseController
|
||||
{
|
||||
private readonly IUserService _userService;
|
||||
public UserController(
|
||||
IConfiguration configuration,
|
||||
IUserService userService
|
||||
) : base(configuration)
|
||||
{
|
||||
this._userService = userService;
|
||||
}
|
||||
|
||||
[HttpGet("get/{guid}")]
|
||||
[ProducesResponseType<GetUserResponse>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> GetUserByGuidAsync(string guid)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest("Request is not well formed");
|
||||
}
|
||||
|
||||
if (String.IsNullOrEmpty(guid))
|
||||
{
|
||||
return BadRequest("Request is not well formed");
|
||||
}
|
||||
var data = await this._userService.GetUserByGuidAsync(guid);
|
||||
|
||||
if (data == null || String.IsNullOrEmpty(data.Guid))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return Success(String.Empty, data);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
var message = "Something went wrong";
|
||||
if (!String.IsNullOrEmpty(exception.Message))
|
||||
{
|
||||
message += $". {exception.Message}";
|
||||
}
|
||||
return InternalServerError(message);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
14
MainProject/Core/Middlewares/AutoMapperConfiguration.cs
Normal file
14
MainProject/Core/Middlewares/AutoMapperConfiguration.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Common.User;
|
||||
using SqlServerDatabase = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
using AutoMapper;
|
||||
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Core.Middlewares;
|
||||
public class AutoMapperConfiguration : Profile
|
||||
{
|
||||
public AutoMapperConfiguration()
|
||||
{
|
||||
CreateMap<SqlServerDatabase.User, UserDto>();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="14.0.0" />
|
||||
<PackageReference Include="coverlet.msbuild" Version="6.0.2">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
|
||||
19
MainProject/Models/Api/Common/User/UserDto.cs
Normal file
19
MainProject/Models/Api/Common/User/UserDto.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Common.Role;
|
||||
using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Models.Api.Common.User;
|
||||
|
||||
public class UserDto
|
||||
{
|
||||
#nullable enable
|
||||
public string? Guid { get; set; }
|
||||
public string? Username { get; set; }
|
||||
public string? FirstName { get; set; }
|
||||
public string? LastName { get; set; }
|
||||
public string? Email { get; set; }
|
||||
#nullable disable
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Data.Auth;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Common.User;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Models.Api.Response.Auth;
|
||||
|
||||
public class AuthenticateResponse: BaseResponse
|
||||
public class AuthenticateResponse : BaseResponse<AuthenticatedUser>
|
||||
{
|
||||
public AuthenticateResponse(int status, string? message, AuthenticatedUser? data) : base(status, message, data) {}
|
||||
public AuthenticateResponse(int status, string? message, AuthenticatedUser? data) : base(status, message, data) { }
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
namespace BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||
|
||||
public class BaseResponse
|
||||
public class BaseResponse<T>
|
||||
{
|
||||
#nullable enable
|
||||
public BaseResponse(int status, string? message, dynamic? data)
|
||||
public BaseResponse(int status, string? message, T? data)
|
||||
{
|
||||
this.Status = status;
|
||||
this.Message = message;
|
||||
|
||||
8
MainProject/Models/Api/Response/User/GetUserResponse.cs
Normal file
8
MainProject/Models/Api/Response/User/GetUserResponse.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Common.User;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Models.Api.Response.User;
|
||||
|
||||
public class GetUserResponse : BaseResponse<UserDto>
|
||||
{
|
||||
public GetUserResponse(int status, string? message, UserDto? data) : base(status, message, data) { }
|
||||
}
|
||||
@@ -1,22 +1,22 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Models.Database.SqlServer
|
||||
{
|
||||
public class User : Base
|
||||
{
|
||||
public required string Username { get; set; }
|
||||
public required string FirstName { get; set; }
|
||||
public required string LastName { get; set; }
|
||||
public required string Email { get; set; }
|
||||
public required string PasswordSalt { get; set; }
|
||||
public required string PasswordHash { get; set; }
|
||||
public required Role Role { get; set; }
|
||||
public required bool IsTestUser { get; set; }
|
||||
namespace BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
|
||||
[JsonIgnore]
|
||||
public required string Password { get; set; }
|
||||
}
|
||||
public class User : Base
|
||||
{
|
||||
public required string Username { get; set; }
|
||||
public required string FirstName { get; set; }
|
||||
public required string LastName { get; set; }
|
||||
public required string Email { get; set; }
|
||||
public required string PasswordSalt { get; set; }
|
||||
public required string PasswordHash { get; set; }
|
||||
public required Role Role { get; set; }
|
||||
public required bool IsTestUser { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public required string Password { get; set; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -44,7 +44,8 @@ public class JwtService : BaseService, IJwtService
|
||||
string? guid = _jwtTokenUtils.ValidateToken(headerAuthorization);
|
||||
if(!String.IsNullOrEmpty(guid))
|
||||
{
|
||||
user = this._userService.GetUserByGuid(guid);
|
||||
var userTask = Task.Run(() => this._userService.GetUserByGuidAsync(guid));
|
||||
user = userTask.Result;
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
@@ -8,8 +8,8 @@ namespace BasicDotnetTemplate.MainProject.Services;
|
||||
|
||||
public interface IUserService
|
||||
{
|
||||
User? GetUserById(int id);
|
||||
User? GetUserByGuid(string guid);
|
||||
Task<User?> GetUserByIdAsync(int id);
|
||||
Task<User?> GetUserByGuidAsync(string guid);
|
||||
Task<User?> GetUserByUsernameAndPassword(string username, string password);
|
||||
}
|
||||
|
||||
@@ -34,14 +34,14 @@ public class UserService : BaseService, IUserService
|
||||
);
|
||||
}
|
||||
|
||||
public User? GetUserById(int id)
|
||||
public async Task<User?> GetUserByIdAsync(int id)
|
||||
{
|
||||
return this.GetUsers().Where(x => x.Id == id).FirstOrDefault();
|
||||
return await this.GetUsers().Where(x => x.Id == id).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public User? GetUserByGuid(string guid)
|
||||
public async Task<User?> GetUserByGuidAsync(string guid)
|
||||
{
|
||||
return this.GetUsers().Where(x => x.Guid == guid).FirstOrDefault();
|
||||
return await this.GetUsers().Where(x => x.Guid == guid).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<User?> GetUserByUsernameAndPassword(string username, string password)
|
||||
@@ -64,5 +64,12 @@ public class UserService : BaseService, IUserService
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
// public async Task<User?> CreateUser(CreateUserRequestData data)
|
||||
// {
|
||||
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -71,10 +71,9 @@ public class CryptUtils
|
||||
string hashedPassword = password;
|
||||
for(var i = 0; i <= iteration; i++)
|
||||
{
|
||||
using var sha256 = SHA256.Create();
|
||||
var passwordSaltPepper = $"{hashedPassword}{salt}{this._pepper}";
|
||||
var byteValue = Encoding.UTF8.GetBytes(passwordSaltPepper);
|
||||
var byteHash = sha256.ComputeHash(byteValue);
|
||||
var byteHash = SHA256.HashData(byteValue);
|
||||
hashedPassword = Convert.ToBase64String(byteHash);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ using Microsoft.OpenApi.Models;
|
||||
using MongoDB.Driver;
|
||||
using NLog;
|
||||
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||
using BasicDotnetTemplate.MainProject.Core.Middlewares;
|
||||
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||
using BasicDotnetTemplate.MainProject.Services;
|
||||
|
||||
@@ -218,4 +219,11 @@ public static class ProgramUtils
|
||||
Logger.Info("[ProgramUtils][AddScopes] Done scopes");
|
||||
}
|
||||
|
||||
public static void AddAutoMapper(ref WebApplicationBuilder builder)
|
||||
{
|
||||
Logger.Info("[ProgramUtils][AddAutoMapper] Adding AutoMapperConfiguration");
|
||||
builder.Services.AddAutoMapper(typeof(AutoMapperConfiguration));
|
||||
Logger.Info("[ProgramUtils][AddScopes] Done AutoMapperConfiguration");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user