Initial commit

This commit is contained in:
2026-01-04 23:00:21 +08:00
commit d3178871eb
124 changed files with 19300 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
using System.Security.Cryptography;
using System.Text;
using License.Api.Data;
using License.Api.Models;
using Microsoft.EntityFrameworkCore;
namespace License.Api.Services;
public class IdempotencyService
{
private readonly AppDbContext _db;
public IdempotencyService(AppDbContext db)
{
_db = db;
}
public async Task<IdempotencyKeyRecord?> GetAsync(string key)
{
return await _db.IdempotencyKeys
.FirstOrDefaultAsync(x => x.IdempotencyKey == key && x.ExpiresAt > DateTime.UtcNow);
}
public async Task<IdempotencyKeyRecord> StoreAsync(string key, string path, string requestBodyHash, int responseCode, string responseBody)
{
var record = new IdempotencyKeyRecord
{
IdempotencyKey = key,
RequestPath = path,
RequestHash = requestBodyHash,
ResponseCode = responseCode,
ResponseBody = responseBody,
CreatedAt = DateTime.UtcNow,
ExpiresAt = DateTime.UtcNow.AddHours(24)
};
_db.IdempotencyKeys.Add(record);
await _db.SaveChangesAsync();
return record;
}
public static string ComputeRequestHash(string? body)
{
if (string.IsNullOrWhiteSpace(body))
return string.Empty;
var hash = SHA256.HashData(Encoding.UTF8.GetBytes(body));
return Convert.ToHexString(hash).ToLowerInvariant();
}
}