Skip to content

Commit 584d4e6

Browse files
Add support for updating bank accounts via API
Introduced an UpdateBankAccountCommand with its handler and integrated the update functionality into the BankAccountController. Updated the EntityBase model to allow modification of the `Id` property and adjusted response serialization behavior in ApiResponse. Minor project structure enhancements included.
1 parent 5a17609 commit 584d4e6

File tree

8 files changed

+52
-8
lines changed

8 files changed

+52
-8
lines changed

Common/Controllers/ApiControllerBase.cs

Whitespace-only changes.

Common/Models/ApiResponse.cs

Whitespace-only changes.

Common/Responses.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ public class ApiResponse<T>
1010
public int Code { get; set; } = 0;
1111
public string Message { get; set; } = "Success";
1212

13-
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
13+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
1414
public T? Data { get; set; }
15-
1615
public ApiResponse() { }
1716

1817
public ApiResponse(T data, string message = "Success", int code = 0, int httpStatus = 200)

Controllers/BankAccountController.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,13 @@ public async Task<IActionResult> Create([FromBody] CreateBankAccountCommand comm
2222
var result = await _mediator.Send(command);
2323
return OK(result);
2424
}
25+
26+
[HttpPut("update-bank-account")]
27+
public async Task<IActionResult> Update([FromBody] UpdateBankAccountCommand command)
28+
{
29+
var result = await _mediator.Send(command);
30+
return OK(result);
31+
}
32+
2533

2634
}

Core/Entities/BaseModel/BaseModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace PaymentCoreServiceApi.Core.Entities.BaseModel;
22

33
public abstract class EntityBase
44
{
5-
public long Id { get; private set; }
5+
public long Id { get; set; }
66
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
77
public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
88
public bool Active { get; set; } = true;
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
2+
using PaymentCoreServiceApi.Common.Mediator;
3+
using PaymentCoreServiceApi.Core.Entities.BankAccountGenerated;
4+
15
namespace PaymentCoreServiceApi.Features.BankAccounts.Commands;
26

3-
public class UpdateBankAccountCommand
4-
{
5-
7+
public class UpdateBankAccountCommand: IRequestApiResponse<bool>
8+
{ public BankAccount UpdateBankAccount { get; set; }
69
}
Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,39 @@
1+
using AutoMapper;
2+
using Microsoft.AspNetCore.Http.Extensions;
3+
using PaymentCoreServiceApi.Common;
4+
using PaymentCoreServiceApi.Common.Mediator;
5+
using PaymentCoreServiceApi.Core.Interfaces.Repositories.Read;
6+
using PaymentCoreServiceApi.Core.Interfaces.Repositories.Write;
7+
using PaymentCoreServiceApi.Services;
8+
19
namespace PaymentCoreServiceApi.Features.BankAccounts.Commands;
210

3-
public class UpdateBankAccountCommandHandler
11+
public class UpdateBankAccountCommandHandler: IRequestApiResponseHandler<UpdateBankAccountCommand, bool>
412
{
5-
13+
private readonly IExecutionContext _executionContext;
14+
private readonly IBankAccountWriteRepository _bankAccountWriteRepository;
15+
private readonly IBankAccountReadRepository _bankAccountReadRepository;
16+
private readonly IMapper _mapper;
17+
18+
public UpdateBankAccountCommandHandler(IExecutionContext executionContext, IBankAccountWriteRepository bankAccountWriteRepository, IBankAccountReadRepository bankAccountReadRepository, IMapper mapper)
19+
{
20+
_executionContext = executionContext;
21+
_bankAccountWriteRepository = bankAccountWriteRepository;
22+
_bankAccountReadRepository = bankAccountReadRepository;
23+
_mapper = mapper;
24+
}
25+
26+
public async Task<ApiResponse<bool>> Handle(UpdateBankAccountCommand request, CancellationToken cancellationToken)
27+
{
28+
var bankAccount = await _bankAccountReadRepository.GetByIdAsync(request.UpdateBankAccount.Id);
29+
if (bankAccount == null)
30+
{
31+
return ApiResponse<bool>.NotFound($"Bank account with id {request.UpdateBankAccount.Id} not found");
32+
}
33+
await _bankAccountWriteRepository.UpdateAsync(request.UpdateBankAccount);
34+
35+
await _bankAccountWriteRepository.CommitAsync();
36+
37+
return ApiResponse<bool>.Success(true);
38+
}
639
}

Infrastructure/Extensions/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using PaymentCoreServiceApi.Core.Interfaces.Repositories.Read;
66
using PaymentCoreServiceApi.Core.Interfaces.Repositories.Write;
77
using PaymentCoreServiceApi.Features.Auth;
8+
using PaymentCoreServiceApi.Features.BankAccounts.Commands;
89
using PaymentCoreServiceApi.Infrastructure.Repositories.Read;
910
using PaymentCoreServiceApi.Infrastructure.Repositories.Write;
1011
using PaymentCoreServiceApi.Middlewares;

0 commit comments

Comments
 (0)