✨ Quota and better drive dashboard
This commit is contained in:
@@ -1,25 +1,49 @@
|
||||
using DysonNetwork.Shared.Cache;
|
||||
using DysonNetwork.Shared.Proto;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace DysonNetwork.Drive.Billing;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/billing/usage")]
|
||||
public class UsageController(UsageService usageService) : ControllerBase
|
||||
public class UsageController(UsageService usage, QuotaService quota, ICacheService cache) : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<TotalUsageDetails>> GetTotalUsage()
|
||||
{
|
||||
return await usageService.GetTotalUsage();
|
||||
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
|
||||
var accountId = Guid.Parse(currentUser.Id);
|
||||
|
||||
var cacheKey = $"file:usage:{accountId}";
|
||||
|
||||
// Try to get from cache first
|
||||
var (found, cachedResult) = await cache.GetAsyncWithStatus<TotalUsageDetails>(cacheKey);
|
||||
if (found && cachedResult != null)
|
||||
return Ok(cachedResult);
|
||||
|
||||
// If not in cache, get from services
|
||||
var result = await usage.GetTotalUsage(accountId);
|
||||
var totalQuota = await quota.GetQuota(accountId);
|
||||
result.TotalQuota = totalQuota;
|
||||
|
||||
// Cache the result for 5 minutes
|
||||
await cache.SetAsync(cacheKey, result, TimeSpan.FromMinutes(5));
|
||||
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("{poolId:guid}")]
|
||||
public async Task<ActionResult<UsageDetails>> GetPoolUsage(Guid poolId)
|
||||
{
|
||||
var usageDetails = await usageService.GetPoolUsage(poolId);
|
||||
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
|
||||
var accountId = Guid.Parse(currentUser.Id);
|
||||
|
||||
var usageDetails = await usage.GetPoolUsage(poolId, accountId);
|
||||
if (usageDetails == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
return usageDetails;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user