2023-02-06 02:23:00 +01:00
|
|
|
using AfRApay.Web.Backend;
|
|
|
|
using LinqToDB;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace AfRApay.Web.Controllers;
|
|
|
|
|
|
|
|
[ApiController, Route("/api/card/transaction")]
|
|
|
|
public class CardTransaction : Controller {
|
|
|
|
[HttpGet]
|
2023-02-08 22:21:25 +01:00
|
|
|
public string Get([FromQuery] string card, [FromQuery] decimal amount, [FromQuery] string ik) {
|
2023-02-06 02:23:00 +01:00
|
|
|
var db = new Database.DbConn();
|
|
|
|
if (db.Cards.Any(p => p.CardId == card)) {
|
|
|
|
var userId = db.Cards.First(p => p.CardId == card).UserId;
|
|
|
|
var user = db.Users.First(p => p.Id == userId);
|
2023-02-08 22:21:25 +01:00
|
|
|
|
|
|
|
// If an idempotency key is given, disregard duplicate requests.
|
|
|
|
if (ik == "" || ik != user.LastIdempotencyKey) {
|
|
|
|
if (user.Balance - amount < -50) {
|
|
|
|
return "E:Balance too low!";
|
|
|
|
}
|
|
|
|
|
|
|
|
user.Balance -= amount;
|
|
|
|
user.LastIdempotencyKey = ik;
|
|
|
|
db.Update(user);
|
2023-02-06 02:23:00 +01:00
|
|
|
}
|
2023-02-08 22:21:25 +01:00
|
|
|
|
2023-02-06 23:44:29 +01:00
|
|
|
return $"S:{user.Nickname}: {user.Balance:N2}";
|
2023-02-06 02:23:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return "E:Unknown card.";
|
|
|
|
}
|
|
|
|
}
|