forked from EllieBotDevs/elliebot
finance api implementation
This commit is contained in:
parent
d2f70644ef
commit
c5b27421a3
5 changed files with 191 additions and 10 deletions
src/EllieBot/Services/GrpcApi
89
src/EllieBot/Services/GrpcApi/FinSvc.cs
Normal file
89
src/EllieBot/Services/GrpcApi/FinSvc.cs
Normal file
|
@ -0,0 +1,89 @@
|
|||
using Google.Protobuf.WellKnownTypes;
|
||||
using Grpc.Core;
|
||||
using EllieBot.Db.Models;
|
||||
using EllieBot.Modules.Gambling.Bank;
|
||||
using EllieBot.Modules.EllieExpressions;
|
||||
using EllieBot.Modules.Utility;
|
||||
|
||||
namespace EllieBot.GrpcApi;
|
||||
|
||||
public class FinSvc : GrpcFin.GrpcFinBase, IGrpcSvc, IEService
|
||||
{
|
||||
private readonly ICurrencyService _cs;
|
||||
private readonly IBankService _bank;
|
||||
|
||||
public FinSvc(ICurrencyService cs, IBankService bank)
|
||||
{
|
||||
_cs = cs;
|
||||
_bank = bank;
|
||||
}
|
||||
|
||||
public ServerServiceDefinition Bind()
|
||||
=> GrpcFin.BindService(this);
|
||||
|
||||
[GrpcNoAuthRequired]
|
||||
public override async Task<DepositReply> Deposit(DepositRequest request, ServerCallContext context)
|
||||
{
|
||||
if (request.Amount <= 0)
|
||||
throw new RpcException(new Status(StatusCode.InvalidArgument, "Amount must be greater than 0"));
|
||||
|
||||
var succ = await _bank.DepositAsync(request.UserId, request.Amount);
|
||||
|
||||
return new DepositReply
|
||||
{
|
||||
Success = succ
|
||||
};
|
||||
}
|
||||
|
||||
[GrpcNoAuthRequired]
|
||||
public override async Task<WithdrawReply> Withdraw(WithdrawRequest request, ServerCallContext context)
|
||||
{
|
||||
if (request.Amount <= 0)
|
||||
throw new RpcException(new Status(StatusCode.InvalidArgument, "Amount must be greater than 0"));
|
||||
|
||||
var succ = await _bank.WithdrawAsync(request.UserId, request.Amount);
|
||||
|
||||
return new WithdrawReply
|
||||
{
|
||||
Success = succ
|
||||
};
|
||||
}
|
||||
|
||||
[GrpcNoAuthRequired]
|
||||
public override async Task<GetHoldingsReply> GetHoldings(GetHoldingsRequest request, ServerCallContext context)
|
||||
{
|
||||
return new GetHoldingsReply
|
||||
{
|
||||
Bank = await _bank.GetBalanceAsync(request.UserId),
|
||||
Cash = await _cs.GetBalanceAsync(request.UserId)
|
||||
};
|
||||
}
|
||||
|
||||
[GrpcNoAuthRequired]
|
||||
public override async Task<GetTransactionsReply> GetTransactions(
|
||||
GetTransactionsRequest request,
|
||||
ServerCallContext context)
|
||||
{
|
||||
if (request.Page < 1)
|
||||
throw new RpcException(new Status(StatusCode.InvalidArgument, "Page must be greater than 0"));
|
||||
|
||||
var trs = await _cs.GetTransactionsAsync(request.UserId, request.Page - 1);
|
||||
|
||||
var reply = new GetTransactionsReply
|
||||
{
|
||||
Total = await _cs.GetTransactionsCountAsync(request.UserId)
|
||||
};
|
||||
|
||||
reply.Transactions.AddRange(trs.Select(x => new TransactionReply()
|
||||
{
|
||||
Id = new kwum(x.Id).ToString(),
|
||||
Timestamp = Timestamp.FromDateTime(DateTime.UtcNow),
|
||||
Amount = x.Amount,
|
||||
Extra = x.Extra ?? string.Empty,
|
||||
Note = x.Note ?? string.Empty,
|
||||
Type = x.Type ?? string.Empty,
|
||||
}));
|
||||
|
||||
return reply;
|
||||
}
|
||||
}
|
Reference in a new issue