first commit
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.ServiceClaims;
|
||||
|
||||
public interface IServiceClaimsManager
|
||||
{
|
||||
public Task<Claim> GetServiceClaims(ServiceClaim claim);
|
||||
public Task<List<Claim>> GetListServiceClaims();
|
||||
public Task<bool> UpdateClaim(ServiceClaim claim, string value);
|
||||
public Task<bool> ClaimIsSet(ServiceClaim claim);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.ServiceClaims;
|
||||
|
||||
public enum ServiceClaim
|
||||
{
|
||||
ServiceAcoountUsername, ServiceAcoountToken, SericeIsnitialized,
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.ServiceClaims;
|
||||
|
||||
public static class ServiceClaimsClass
|
||||
{
|
||||
private static readonly Dictionary<ServiceClaim, string> ServiceClaimsMap = new()
|
||||
{
|
||||
{ ServiceClaim.ServiceAcoountToken, "Service_account_token" },
|
||||
{ ServiceClaim.ServiceAcoountUsername, "Service_account_username" }
|
||||
};
|
||||
|
||||
public static string GetName(this ServiceClaim serviceClaim)
|
||||
{
|
||||
var result = ServiceClaimsMap.TryGetValue(serviceClaim, out var name);
|
||||
return (result ? name : $"Uknow-{serviceClaim}") ?? $"Role {serviceClaim} Uknow";
|
||||
}
|
||||
|
||||
public static Claim GerClaims(this ServiceClaim serviceClaim, string value)
|
||||
{
|
||||
var result = ServiceClaimsMap.TryGetValue(serviceClaim, out var name);
|
||||
if (!result || value == null) throw new Exception("Claims type not implement");
|
||||
|
||||
return new Claim(name ?? throw new NullReferenceException("Claim name excemption"), value);
|
||||
}
|
||||
|
||||
public static Data.ServiceClaims CreateServiceClaims(this ServiceClaim serviceClaim, string value)
|
||||
{
|
||||
return new Data.ServiceClaims() { ClaimType = serviceClaim.GetName(), ClaimValue = value };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
namespace OpenWarehouse.warehouse.api.Common.ServiceClaims;
|
||||
|
||||
public class ServiceClaimsManager(ApplicationDbContext context) : IServiceClaimsManager
|
||||
{
|
||||
public async Task<Claim> GetServiceClaims(ServiceClaim claim)
|
||||
{
|
||||
var result = await context.ServiceClaims
|
||||
.Where(claims => claims.ClaimType == claim.GetName())
|
||||
.FirstOrDefaultAsync();
|
||||
if (result == null)
|
||||
{
|
||||
return new Claim(claim.GetName(), "");
|
||||
}
|
||||
|
||||
return result.Claim;
|
||||
}
|
||||
|
||||
public async Task<List<Claim>> GetListServiceClaims()
|
||||
{
|
||||
return await context.ServiceClaims.Select(q => new Claim(q.ClaimType ?? "Uknow", q.ClaimValue ?? "")).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateClaim(ServiceClaim claim, string value)
|
||||
{
|
||||
var result = await context.ServiceClaims
|
||||
.Where(claims => claims.ClaimType == claim.GetName())
|
||||
.FirstOrDefaultAsync();
|
||||
if (result == null)
|
||||
{
|
||||
Data.ServiceClaims claims = claim.CreateServiceClaims(value);
|
||||
context.ServiceClaims.Add(claims);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.ClaimValue = value;
|
||||
}
|
||||
|
||||
var res = await context.SaveChangesAsync();
|
||||
|
||||
return res > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> ClaimIsSet(ServiceClaim claim)
|
||||
{
|
||||
var result = await context.ServiceClaims
|
||||
.Where(q => q.ClaimType == claim.GetName()).AnyAsync();
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user