Add balance handling to card transactions and user accounts
Signed-off-by: Dmitrii <computer@yawaflua.tech> Took 6 minutes
This commit is contained in:
@@ -2,6 +2,7 @@ using System.Net;
|
|||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
@@ -109,15 +110,17 @@ public class AuthController(AppDbContext dbContext, TokenService tokenService, I
|
|||||||
string Base64BearerToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(BearerToken));
|
string Base64BearerToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(BearerToken));
|
||||||
var resp = await SendRequest("/accounts/me", new("Bearer", Base64BearerToken));
|
var resp = await SendRequest("/accounts/me", new("Bearer", Base64BearerToken));
|
||||||
var me = JsonSerializer.Deserialize<UserAccountDTO>(resp);
|
var me = JsonSerializer.Deserialize<UserAccountDTO>(resp);
|
||||||
Console.WriteLine(resp);
|
|
||||||
var user = ((User)HttpContext.Items["@me"]);
|
var user = ((User)HttpContext.Items["@me"]);
|
||||||
Console.WriteLine(me.id);
|
|
||||||
Console.WriteLine(Guid.Parse(me.minecraftUUID));
|
|
||||||
Console.WriteLine(user.Id.ToString());
|
|
||||||
if (user == null || user.Id != Guid.Parse(me.minecraftUUID))
|
if (user == null || user.Id != Guid.Parse(me.minecraftUUID))
|
||||||
{
|
{
|
||||||
throw new Exception("Its not ur card");
|
throw new Exception("Its not ur card");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var balanceResp = await SendRequest("/public/card", new("Bearer", Base64BearerToken));
|
||||||
|
var balance = (int?)JsonNode.Parse(balanceResp)?["balance"];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var card = me.cards.First(k => k.id == body.id);
|
var card = me.cards.First(k => k.id == body.id);
|
||||||
var existingCard = user.Cards.FirstOrDefault(k => k.Id.ToString() == card.id);
|
var existingCard = user.Cards.FirstOrDefault(k => k.Id.ToString() == card.id);
|
||||||
@@ -126,6 +129,7 @@ public class AuthController(AppDbContext dbContext, TokenService tokenService, I
|
|||||||
{
|
{
|
||||||
Id = Guid.Parse(card.id),
|
Id = Guid.Parse(card.id),
|
||||||
Name = card.name,
|
Name = card.name,
|
||||||
|
Balance = balance ?? -1,
|
||||||
SpworldsID = card.number,
|
SpworldsID = card.number,
|
||||||
Token = Base64BearerToken,
|
Token = Base64BearerToken,
|
||||||
CreatedAt = DateTime.UtcNow,
|
CreatedAt = DateTime.UtcNow,
|
||||||
@@ -136,6 +140,7 @@ public class AuthController(AppDbContext dbContext, TokenService tokenService, I
|
|||||||
existingCard.Name = card.name;
|
existingCard.Name = card.name;
|
||||||
existingCard.SpworldsID = card.number;
|
existingCard.SpworldsID = card.number;
|
||||||
existingCard.Token = Base64BearerToken;
|
existingCard.Token = Base64BearerToken;
|
||||||
|
existingCard.Balance = balance ?? -1;
|
||||||
existingCard.UpdatedAt = DateTime.UtcNow;
|
existingCard.UpdatedAt = DateTime.UtcNow;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,6 +70,11 @@ public class TransactionsController(AppDbContext context, ILogger<TransactionsCo
|
|||||||
return BadRequest(new { error = "Card not found" });
|
return BadRequest(new { error = "Card not found" });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (cardToUse.Balance != -1 && cardToUse.Balance < body.amount)
|
||||||
|
{
|
||||||
|
return BadRequest(new { error = "Insufficient balance" });
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
var shortId = Program.GenerateRandomString(5);
|
var shortId = Program.GenerateRandomString(5);
|
||||||
while (true)
|
while (true)
|
||||||
@@ -101,17 +106,18 @@ public class TransactionsController(AppDbContext context, ILogger<TransactionsCo
|
|||||||
{
|
{
|
||||||
{ "receiver", body.receiverCard },
|
{ "receiver", body.receiverCard },
|
||||||
{ "amount", body.amount },
|
{ "amount", body.amount },
|
||||||
{ "comment", body.comment + ";Чек:"+ uri }
|
{ "comment", (body.comment)[8..] + "..;Чек:"+ uri }
|
||||||
};
|
};
|
||||||
Console.WriteLine((body.comment + ";Чек: "+ uri).Length);
|
Console.WriteLine(((body.comment)[8..] + "..;Чек:"+ uri).Length);
|
||||||
Console.WriteLine((body.comment + ";Чек: "+ uri));
|
Console.WriteLine(((body.comment)[8..] + "..;Чек:"+ uri));
|
||||||
var resp = await SendRequest(endpoint: "transactions", body: transitionInfo,
|
var resp = await SendRequest(endpoint: "transactions", body: transitionInfo, AuthHeader: new("Bearer", cardToUse.Token));
|
||||||
AuthHeader: new("Bearer", cardToUse.Token));
|
|
||||||
var balance = (int?)JsonNode.Parse(resp)?["balance"];
|
var balance = (int?)JsonNode.Parse(resp)?["balance"];
|
||||||
if (balance == null)
|
if (balance == null)
|
||||||
{
|
{
|
||||||
throw new Exception("Failed to create transaction: " + resp);
|
throw new Exception("Failed to create transaction: " + resp);
|
||||||
}
|
}
|
||||||
|
cardToUse.Balance = balance.Value;
|
||||||
|
|
||||||
|
|
||||||
} catch (Exception exception)
|
} catch (Exception exception)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ public class Card
|
|||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public string SpworldsID { get; set; }
|
public string SpworldsID { get; set; }
|
||||||
public string Token { get; set; }
|
public string Token { get; set; }
|
||||||
|
|
||||||
|
public int Balance { get; set; } = 0;
|
||||||
|
|
||||||
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
||||||
public DateTime UpdatedAt { get; set; }
|
public DateTime UpdatedAt { get; set; }
|
||||||
|
|||||||
@@ -54,7 +54,8 @@ public final class GpsHudRenderer {
|
|||||||
try {
|
try {
|
||||||
HttpClient client = HttpClient.newHttpClient();
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(URI.create("https://spm-map.tonyaleksandr.ru/api/map/territories"))
|
.uri(URI.create("https://map.sp-mini.ru/api/map/territories"))
|
||||||
|
// Не понимаю смысла менять с домена со своим именем АКА реклама личного бренда на безликий сп-мини.ру, пахнет дешевой подделкой
|
||||||
.GET()
|
.GET()
|
||||||
.build();
|
.build();
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|||||||
Reference in New Issue
Block a user