From 341755d77e7f46562ca65b04847f198d55d1b9b9 Mon Sep 17 00:00:00 2001
From: eee4 <41441600+eee4@users.noreply.github.com>
Date: Tue, 22 Jul 2025 11:39:40 +0200
Subject: [PATCH] feat: basic stats endpoint
---
Controllers/QuoteController.cs | 9 ++++-
Controllers/StatisticController.cs | 59 ++++++++++++++++++++++++++++++
2 files changed, 67 insertions(+), 1 deletion(-)
create mode 100644 Controllers/StatisticController.cs
diff --git a/Controllers/QuoteController.cs b/Controllers/QuoteController.cs
index 2d83a00..0bb5d86 100644
--- a/Controllers/QuoteController.cs
+++ b/Controllers/QuoteController.cs
@@ -216,9 +216,10 @@ public class QuotesController : ControllerBase
// GET /api/v1/quotes/random
///
- /// Get a random quote summary
+ /// Draw a random quote
///
/// A quote: id, quote content and author, imageUrl and categories if successful, otherwise: error message
+ /// (Optional) category id to draw from
/// Returned on valid request
/// Returned when no quotes exist matching provided criteria
/// Returned when no quotes exist (in the DB)
@@ -263,6 +264,12 @@ public class QuotesController : ControllerBase
if (quote == null)
return NotFound(new ErrorDTO { Status = "error", Error_msg = "Unknown error - couldn't get quote" });
+ // After getting and checking the quote, update the number of draws
+ Statistic s = await _db.Statistics
+ .FirstAsync(s => s.Label == "number_of_draws");
+ s.IValue += 1;
+ await _db.SaveChangesAsync();
+
return Ok(quote.ToQuoteShortDTO());
}
diff --git a/Controllers/StatisticController.cs b/Controllers/StatisticController.cs
new file mode 100644
index 0000000..e6da643
--- /dev/null
+++ b/Controllers/StatisticController.cs
@@ -0,0 +1,59 @@
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Mvc;
+using QuotifyBE.Data;
+using Microsoft.AspNetCore.Cors;
+
+namespace QuotifyBE.Controllers;
+
+
+[ApiController]
+[EnableCors]
+[Route("api/v1/stats")]
+[Produces("application/json")]
+public class StatisticController : ControllerBase
+{
+
+ private readonly ApplicationDbContext _db;
+
+ public StatisticController( ApplicationDbContext db)
+ {
+ _db = db;
+ }
+
+ // GET /api/v1/stats
+ ///
+ /// Return server statistics
+ ///
+ ///
+ /// Provides, info on last commit # and date, branch name,
+ /// sitewide stats (number of draws) and available endpoints
+ /// (machine-friendly json).
+ ///
+ /// Has CORS set.
+ ///
+ /// Dummy json
+ // /// Returned on request with valid credentials
+ // /// Returned on request with invalid JWT
+ [HttpGet]
+ [EnableCors]
+ [ProducesResponseType(200)]
+ // [ProducesResponseType(401)]
+ public IActionResult GetStats()
+ {
+ return Ok(new
+ {
+ version = new
+ {
+ lastCommit = ThisAssembly.Git.Commit,
+ lastUpdatedAt = ThisAssembly.Git.CommitDate,
+ currentBranch = ThisAssembly.Git.Branch
+ },
+ endpointDiscovery = "/swagger/v1/swagger.json",
+ sitewideStats = new
+ {
+ numberOfDraws = _db.Statistics.First(s => s.Label == "number_of_draws").IValue
+ }
+ });
+ }
+
+}