Add magic card identifier '@username' for transaction and balance api calls

This commit is contained in:
Laura Hausmann 2023-05-19 23:47:05 +02:00
parent 9ba88decfb
commit 27bb6a4bc6
Signed by: zotan
GPG key ID: D044E84C5BE01605

View file

@ -64,7 +64,7 @@ public class CardController : Controller {
/// <summary>
/// Creates a transaction that changes the balance of the the user the card is linked to by the specified amount.
/// </summary>
/// <param name="card">The ID of the card</param>
/// <param name="card">The ID of the card (alternatively, magic card identifier @username)</param>
/// <param name="ik">Random string (idempotency key) which is consistent across request retries</param>
/// <param name="amount">Positive or negative number of cents representing the relative change in balance</param>
/// <param name="reader">Type of reader that scanned the card</param>
@ -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 {
/// <summary>
/// Returns the balance of the the user the card is linked to.
/// </summary>
/// <param name="card">The ID of the card</param>
/// <param name="card">The ID of the card (alternatively, magic card identifier @username)</param>
/// <param name="reader">Type of reader that scanned the card</param>
/// <response code="200">Returns 200 if the request succeeded</response>
/// <response code="404">Returns 404 if the card isn't linked to any account</response>
@ -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,