Skip to content

Commit 250e8b8

Browse files
feat: Refactor authentication and user management
- Updated AuthController to inherit from BaseController and changed response handling to use SuccessResponse. - Added BankAccountController for managing bank accounts with a create endpoint. - Introduced BankAccount entity with properties for account management. - Modified BaseModel to change CreatedBy and UpdatedBy types from int to long. - Created IBankAccountReadRepository and IBankAccountWriteRepository interfaces for bank account data access. - Implemented CreateBankAccountCommand and its handler for bank account creation logic. - Enhanced CreateUserCommand to include validation attributes for required fields. - Updated GetUserProfileQueryHandler to use long for user ID comparison. - Removed obsolete PayExpressDbContext and related user model files. - Updated AppDbContext to include DbSet for BankAccounts. - Registered new repositories and services in ServiceCollectionExtensions. - Added PinHasher service for secure PIN handling. - Created migrations for initial database setup including Users and BankAccounts tables.
1 parent 86d8f84 commit 250e8b8

31 files changed

+435
-159
lines changed

Controllers/AuthController.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,22 @@ namespace PaymentCoreServiceApi.Controllers;
99
[ApiController]
1010
[Route("api/[controller]")]
1111
[Authorize]
12-
public class AuthController : ControllerBase
12+
public class AuthController : BaseController
1313
{
1414
private readonly IMediator _mediator;
1515

1616
public AuthController(IMediator mediator)
1717
{
1818
_mediator = mediator;
1919
}
20-
2120
[HttpPost("login")]
2221
[AllowAnonymous]
23-
public async Task<ActionResult<LoginResponse>> Login([FromBody] LoginCommand command)
22+
public async Task <IActionResult> Login([FromBody] LoginCommand command)
2423
{
2524
try
2625
{
2726
var response = await _mediator.Send(command);
28-
return Ok(response);
27+
return SuccessResponse(response);
2928
}
3029
catch (UnauthorizedAccessException)
3130
{
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using MediatR;
2+
using Microsoft.AspNetCore.Authorization;
3+
using Microsoft.AspNetCore.Mvc;
4+
using PaymentCoreServiceApi.Features.BankAccounts.Commands;
5+
6+
namespace PaymentCoreServiceApi.Controllers;
7+
[ApiController]
8+
[Route("api/[controller]")]
9+
[Authorize]
10+
public class BankAccountController: BaseController
11+
{
12+
private readonly IMediator _mediator;
13+
14+
public BankAccountController(IMediator mediator)
15+
{
16+
_mediator = mediator;
17+
}
18+
[HttpPost("create-bank-account")]
19+
public async Task<IActionResult> Create([FromBody] CreateBankAccountCommand command)
20+
{
21+
var bankAccount = await _mediator.Send(command);
22+
return SuccessResponse(bankAccount);
23+
}
24+
}

Controllers/UsersController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace PaymentCoreServiceApi.Controllers;
88

99
[ApiController]
1010
[Route("api/[controller]")]
11-
[Authorize] // Yêu cầu authentication cho tất cả các endpoints trong controller
11+
[Authorize]
1212
public class UsersController : BaseController
1313
{
1414
private readonly IMediator _mediator;
@@ -19,7 +19,7 @@ public UsersController(IMediator mediator)
1919
}
2020

2121
[HttpPost]
22-
[AllowAnonymous] // Cho phép tạo user mà không cần authentication
22+
[AllowAnonymous]
2323
public async Task<IActionResult> Create([FromBody] CreateUserCommand command)
2424
{
2525
var result = await _mediator.Send(command);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using PaymentCoreServiceApi.Core.Entities.BaseModel;
2+
3+
namespace PaymentCoreServiceApi.Core.Entities.BankAccountGenerated;
4+
5+
public class BankAccount: EntityBase
6+
{
7+
public string? AccountNumber { get; set; }
8+
public string? Currency { get; set; }
9+
public decimal Balance { get; set; }
10+
public long UserId { get; set; }
11+
public bool IsActive { get; set; } = true;
12+
public string CodePinHash { get; set; }
13+
}

Core/Entities/BaseModel/BaseModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ public abstract class EntityBase
99
public bool Deleted { get; set; } = false;
1010
public int? DeletedBy { get; set; } = null;
1111
public DateTime? DeletedAt { get; set; } = null;
12-
public int? CreatedBy { get; set; } = null;
13-
public int? UpdatedBy { get; set; } = null;
12+
public long CreatedBy { get; set; } = 0;
13+
public long UpdatedBy { get; set; } = 0;
1414
}

Core/Entities/UserGenerated/User.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace PaymentCoreServiceApi.Core.Entities.UserGenerated;
44

5-
public class User : EntityBase
5+
public class User: EntityBase
66
{
77
public string? NickName { get; set; }
88
public string Avatar { get; set; }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using PaymentCoreServiceApi.Core.Entities.BankAccountGenerated;
2+
3+
namespace PaymentCoreServiceApi.Core.Interfaces.Repositories.Read;
4+
5+
public interface IBankAccountReadRepository: IBaseReadRepository<BankAccount>
6+
{
7+
Task<bool> ExistsBankAccountByUserIdAsync(long userId);
8+
}

Core/Interfaces/Repositories/Read/IBaseReadRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace PaymentCoreServiceApi.Core.Interfaces.Repositories.Read;
44

55
public interface IBaseReadRepository<TEntity> where TEntity : class
66
{
7-
Task<TEntity> GetByIdAsync(int id);
7+
Task<TEntity> GetByIdAsync(long id);
88
Task<IEnumerable<TEntity>> GetAllAsync();
99
Task<(IEnumerable<TEntity> Items, int TotalCount)> GetPagedAsync(int pageIndex, int pageSize, Expression<Func<TEntity, bool>> predicate);
1010
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
using PaymentCoreServiceApi.Core.Entities.UserGenerated;
2+
13
namespace PaymentCoreServiceApi.Core.Interfaces.Repositories.Read;
24

3-
public interface IUserReadRepository
5+
public interface IUserReadRepository: IBaseReadRepository<User>
46
{
57
Task<bool> ExistsAsync(string username);
6-
}
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using PaymentCoreServiceApi.Core.Entities.BankAccountGenerated;
2+
3+
namespace PaymentCoreServiceApi.Core.Interfaces.Repositories.Write;
4+
5+
public interface IBankAccountWriteRepository: IBaseWriteOnlyRepository<BankAccount>
6+
{
7+
}

0 commit comments

Comments
 (0)