Meme Generation API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Kynareth/src/Attributes/EndpointMetrics.cs

39 lines
936 B

using Exsersewo.Common.Extensions;
using Kynareth.Helpers;
using Kynareth.Models;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Primitives;
namespace Kynareth.Attributes;
public class EndpointMetricsAttribute : ActionFilterAttribute
{
public string EndpointMetricName;
public EndpointMetricsAttribute(string metricName)
{
EndpointMetricName = metricName;
}
public override void OnActionExecuted(ActionExecutedContext context)
{
base.OnActionExecuted(context);
string key = context.HttpContext.Request.GetToken();
if (string.IsNullOrWhiteSpace(key)) return;
using var database = context.HttpContext.RequestServices.GetRequiredService<KynarethDbContext>();
var token = database.Tokens.FirstOrDefault(x => x.Token.Equals(key));
if (token is null) return;
token.ApiCalls.Add(new() {
TimeStamp = DateTime.UtcNow,
Route = EndpointMetricName
});
database.SaveChanges();
}
}