From 27bb6a4bc6e7b9c367dd809bcafe9493e5bee214 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Fri, 19 May 2023 23:47:05 +0200 Subject: [PATCH] Add magic card identifier '@username' for transaction and balance api calls --- AfRApay.Web/Controllers/CardController.cs | 27 ++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/AfRApay.Web/Controllers/CardController.cs b/AfRApay.Web/Controllers/CardController.cs index f42da20..0e04292 100644 --- a/AfRApay.Web/Controllers/CardController.cs +++ b/AfRApay.Web/Controllers/CardController.cs @@ -64,7 +64,7 @@ public class CardController : Controller { /// /// Creates a transaction that changes the balance of the the user the card is linked to by the specified amount. /// - /// The ID of the card + /// The ID of the card (alternatively, magic card identifier @username) /// Random string (idempotency key) which is consistent across request retries /// Positive or negative number of cents representing the relative change in balance /// Type of reader that scanned the card @@ -104,6 +104,22 @@ public class CardController : Controller { await db.SaveChangesAsync(); return Ok(new UserResponse(user)); } + + if (db.Users.Any(p => card.Length > 1 && p.Nickname == card.Substring(1))) { + var user = db.Users.First(p => card.Length > 1 && p.Nickname == card.Substring(1)); + if (ik == "" || ik != user.LastIdempotencyKey) { + user.LastIdempotencyKey = ik; + switch (user.Balance + amount) { + case < -9999: return StatusCode(412, new ErrorResponse("Balance too low!")); + case > 99999: return StatusCode(412, new ErrorResponse("Balance too high!")); + } + + user.Balance += amount; + } + + await db.SaveChangesAsync(); + return Ok(new UserResponse(user)); + } return NotFound(new ErrorResponse("Unknown card.")); } @@ -111,7 +127,7 @@ public class CardController : Controller { /// /// Returns the balance of the the user the card is linked to. /// - /// The ID of the card + /// The ID of the card (alternatively, magic card identifier @username) /// Type of reader that scanned the card /// Returns 200 if the request succeeded /// Returns 404 if the card isn't linked to any account @@ -138,6 +154,11 @@ public class CardController : Controller { return Ok(new UserResponse(user)); } + if (db.Users.Any(p => card.Length > 1 && p.Nickname == card.Substring(1))) { + var user = db.Users.First(p => card.Length > 1 && p.Nickname == card.Substring(1)); + return Ok(new UserResponse(user)); + } + return NotFound(new ErrorResponse("Unknown card.")); } @@ -157,7 +178,7 @@ public class CardController : Controller { public ErrorResponse GetExamples() => new("No active link process"); } - private Card.CardType GetCardType(string? reader, string cardNumber) => reader switch { + private static Card.CardType GetCardType(string? reader, string cardNumber) => reader switch { // these seem not be reliable //"pn532-iso14443a" when cardNumber.Length == 8 => Card.CardType.NfcMifareClassic, //"pn532-iso14443a" when cardNumber.Length == 12 && cardNumber.EndsWith("0218") => Card.CardType.NfcOvChipkaart,