mirror of
https://github.com/QuotifyTeam/QuotifyBE.git
synced 2025-12-16 14:20:06 +01:00
feat: basic stats endpoint
This commit is contained in:
@@ -216,9 +216,10 @@ public class QuotesController : ControllerBase
|
||||
|
||||
// GET /api/v1/quotes/random
|
||||
/// <summary>
|
||||
/// Get a random quote summary
|
||||
/// Draw a random quote
|
||||
/// </summary>
|
||||
/// <returns>A quote: id, quote content and author, imageUrl and categories if successful, otherwise: error message</returns>
|
||||
/// <param name="category_id">(Optional) category id to draw from</param>
|
||||
/// <response code="200">Returned on valid request</response>
|
||||
/// <response code="204">Returned when no quotes exist matching provided criteria</response>
|
||||
/// <response code="404">Returned when no quotes exist (in the DB)</response>
|
||||
@@ -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());
|
||||
|
||||
}
|
||||
|
||||
59
Controllers/StatisticController.cs
Normal file
59
Controllers/StatisticController.cs
Normal file
@@ -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
|
||||
/// <summary>
|
||||
/// Return server statistics
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Provides, info on last commit # and date, branch name,
|
||||
/// sitewide stats (number of draws) and available endpoints
|
||||
/// (machine-friendly json).
|
||||
/// <br/>
|
||||
/// Has CORS set.
|
||||
/// </remarks>
|
||||
/// <returns>Dummy json</returns>
|
||||
// /// <response code="200">Returned on request with valid credentials</response>
|
||||
// /// <response code="401">Returned on request with invalid JWT</response>
|
||||
[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
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user