-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
232 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System.Net.Mime; | ||
using App.Modules.Common; | ||
using App.Modules.Wallets.API.V1; | ||
using App.StartUp.Registry; | ||
using App.StartUp.Services.Auth; | ||
using App.Utility; | ||
using Asp.Versioning; | ||
using CSharp_Result; | ||
using Domain.Admin; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace App.Modules.Admin.API.V1; | ||
|
||
[ApiVersion(1.0)] | ||
[ApiController] | ||
[Consumes(MediaTypeNames.Application.Json)] | ||
[Route("api/v{version:apiVersion}/[controller]")] | ||
public class AdminController( | ||
IAdminService service, | ||
TransferReqValidator transferReqValidator, | ||
IAuthHelper h | ||
) : AtomiControllerBase(h) | ||
{ | ||
[Authorize(Policy = AuthPolicies.OnlyAdmin), HttpPost("inflow/{userId}")] | ||
public async Task<ActionResult<WalletPrincipalRes>> TransferIn(string userId, [FromBody] TransferReq req) | ||
{ | ||
var x = await transferReqValidator | ||
.ValidateAsyncResult(req, "Invalid TransferReq") | ||
.ThenAwait(q => service.TransferIn(userId, q.Amount, q.Desc)) | ||
.Then(x => x.ToRes(), Errors.MapNone); | ||
return this.ReturnResult(x); | ||
} | ||
|
||
[Authorize(Policy = AuthPolicies.OnlyAdmin), HttpPost("outflow/{userId}")] | ||
public async Task<ActionResult<WalletPrincipalRes>> TransferOut(string userId, [FromBody] TransferReq req) | ||
{ | ||
var x = await transferReqValidator | ||
.ValidateAsyncResult(req, "Invalid TransferReq") | ||
.ThenAwait(q => service.TransferOut(userId, q.Amount, q.Desc)) | ||
.Then(x => x.ToRes(), Errors.MapNone); | ||
return this.ReturnResult(x); | ||
} | ||
|
||
[Authorize(Policy = AuthPolicies.OnlyAdmin), HttpPost("promo/{userId}")] | ||
public async Task<ActionResult<WalletPrincipalRes>> PromotionalCredit(string userId, [FromBody] TransferReq req) | ||
{ | ||
var x = await transferReqValidator | ||
.ValidateAsyncResult(req, "Invalid TransferReq") | ||
.ThenAwait(q => service.Promo(userId, q.Amount, q.Desc)) | ||
.Then(x => x.ToRes(), Errors.MapNone); | ||
return this.ReturnResult(x); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace App.Modules.Admin.API.V1; | ||
|
||
public record TransferReq( | ||
decimal Amount, string Desc | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using App.Utility; | ||
using FluentValidation; | ||
|
||
namespace App.Modules.Admin.API.V1; | ||
|
||
public class TransferReqValidator : AbstractValidator<TransferReq> | ||
{ | ||
public TransferReqValidator() | ||
{ | ||
this.RuleFor(x => x.Amount) | ||
.NotNull() | ||
.Must(x => x > 0); | ||
this.RuleFor(x => x.Desc) | ||
.NotNull() | ||
.TransactionDescriptionValid(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using CSharp_Result; | ||
using Domain.Wallet; | ||
|
||
namespace Domain.Admin; | ||
|
||
public interface IAdminService | ||
{ | ||
Task<Result<WalletPrincipal>> TransferIn(string userId, decimal amount, string desc); | ||
|
||
Task<Result<WalletPrincipal>> TransferOut(string userId, decimal amount, string desc); | ||
|
||
Task<Result<WalletPrincipal>> Promo(string userId, decimal amount, string desc); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using CSharp_Result; | ||
using Domain.Transaction; | ||
using Domain.Wallet; | ||
|
||
namespace Domain.Admin; | ||
|
||
public class AdminService( | ||
IWalletRepository walletRepo, | ||
ITransactionRepository transactionRepo, | ||
ITransactionGenerator transactionGenerator, | ||
ITransactionManager transaction | ||
) : IAdminService | ||
{ | ||
|
||
public Task<Result<WalletPrincipal>> TransferIn(string userId, decimal amount, string desc) | ||
{ | ||
return transaction.Start(() => | ||
walletRepo.GetByUserId(userId) | ||
.NullToError(userId) | ||
.ThenAwait(w => walletRepo.Deposit(w.Principal.Id, amount)) | ||
.NullToError(userId) | ||
.DoAwait(DoType.MapErrors, w => transactionRepo.Create(w.Id, | ||
transactionGenerator.AdminInflow(amount, desc))) | ||
); | ||
} | ||
|
||
public Task<Result<WalletPrincipal>> TransferOut(string userId, decimal amount, string desc) | ||
{ | ||
return transaction.Start(() => | ||
walletRepo.GetByUserId(userId) | ||
.NullToError(userId) | ||
.ThenAwait(w => walletRepo.Collect(w.Principal.Id, amount)) | ||
.NullToError(userId) | ||
.DoAwait(DoType.MapErrors, w => transactionRepo.Create(w.Id, | ||
transactionGenerator.AdminOutflow(amount, desc))) | ||
); | ||
} | ||
|
||
public Task<Result<WalletPrincipal>> Promo(string userId, decimal amount, string desc) | ||
{ | ||
return transaction.Start(() => | ||
walletRepo.GetByUserId(userId) | ||
.NullToError(userId) | ||
.ThenAwait(w => walletRepo.Deposit(w.Principal.Id, amount)) | ||
.NullToError(userId) | ||
.DoAwait(DoType.MapErrors, w => transactionRepo.Create(w.Id, | ||
transactionGenerator.Promotional(amount, desc))) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters