Adding Flags API
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
using MicCheck.Api.Authorization;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace MicCheck.Api.Environments;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/v1/environment-document")]
|
||||
[Authorize(Policy = AuthorizationPolicies.FlagsApiAccess)]
|
||||
public class EnvironmentDocumentController(EnvironmentDocumentService environmentDocumentService) : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<EnvironmentDocumentResponse>> Get(CancellationToken ct)
|
||||
{
|
||||
var environmentId = int.Parse(User.FindFirst("EnvironmentId")!.Value);
|
||||
var document = await environmentDocumentService.GetAsync(environmentId, ct);
|
||||
|
||||
return document is null ? NotFound() : Ok(document);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using MicCheck.Api.Features;
|
||||
using MicCheck.Api.Segments;
|
||||
|
||||
namespace MicCheck.Api.Environments;
|
||||
|
||||
public record EnvironmentDocumentResponse(
|
||||
int Id,
|
||||
string ApiKey,
|
||||
IReadOnlyList<FlagResponse> FeatureStates,
|
||||
EnvironmentProjectResponse Project);
|
||||
|
||||
public record EnvironmentProjectResponse(
|
||||
int Id,
|
||||
string Name,
|
||||
IReadOnlyList<SegmentDocumentResponse> Segments);
|
||||
|
||||
public record SegmentDocumentResponse(
|
||||
int Id,
|
||||
string Name,
|
||||
IReadOnlyList<SegmentRuleDocumentResponse> Rules)
|
||||
{
|
||||
public static SegmentDocumentResponse From(Segment segment) => new(
|
||||
segment.Id,
|
||||
segment.Name,
|
||||
segment.Rules
|
||||
.Where(r => r.ParentRuleId == null)
|
||||
.Select(SegmentRuleDocumentResponse.From)
|
||||
.ToList());
|
||||
}
|
||||
|
||||
public record SegmentRuleDocumentResponse(
|
||||
int Id,
|
||||
string Type,
|
||||
IReadOnlyList<SegmentConditionDocumentResponse> Conditions,
|
||||
IReadOnlyList<SegmentRuleDocumentResponse> Rules)
|
||||
{
|
||||
public static SegmentRuleDocumentResponse From(SegmentRule rule) => new(
|
||||
rule.Id,
|
||||
rule.Type.ToString().ToUpperInvariant(),
|
||||
rule.Conditions.Select(SegmentConditionDocumentResponse.From).ToList(),
|
||||
rule.ChildRules.Select(From).ToList());
|
||||
}
|
||||
|
||||
public record SegmentConditionDocumentResponse(string Property, string Operator, string Value)
|
||||
{
|
||||
public static SegmentConditionDocumentResponse From(SegmentCondition condition) => new(
|
||||
condition.Property,
|
||||
condition.Operator.ToString(),
|
||||
condition.Value);
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using MicCheck.Api.Data;
|
||||
using MicCheck.Api.Features;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using AppEnvironment = MicCheck.Api.Environments.Environment;
|
||||
|
||||
namespace MicCheck.Api.Environments;
|
||||
|
||||
public class EnvironmentDocumentService(MicCheckDbContext db)
|
||||
{
|
||||
public async Task<EnvironmentDocumentResponse?> GetAsync(int environmentId, CancellationToken ct = default)
|
||||
{
|
||||
var environment = await db.Environments
|
||||
.FindAsync([environmentId], ct);
|
||||
|
||||
if (environment is null)
|
||||
return null;
|
||||
|
||||
var featureStateResults = await (
|
||||
from fs in db.FeatureStates
|
||||
join f in db.Features on fs.FeatureId equals f.Id
|
||||
where fs.EnvironmentId == environmentId
|
||||
&& fs.IdentityId == null
|
||||
&& fs.FeatureSegmentId == null
|
||||
select new FeatureStateResult
|
||||
{
|
||||
Feature = f,
|
||||
Enabled = fs.Enabled,
|
||||
Value = fs.Value
|
||||
}
|
||||
).ToListAsync(ct);
|
||||
|
||||
var project = await db.Projects
|
||||
.Include(p => p.Segments)
|
||||
.ThenInclude(s => s.Rules)
|
||||
.ThenInclude(r => r.Conditions)
|
||||
.Include(p => p.Segments)
|
||||
.ThenInclude(s => s.Rules)
|
||||
.ThenInclude(r => r.ChildRules)
|
||||
.ThenInclude(cr => cr.Conditions)
|
||||
.FirstOrDefaultAsync(p => p.Id == environment.ProjectId, ct);
|
||||
|
||||
if (project is null)
|
||||
return null;
|
||||
|
||||
return new EnvironmentDocumentResponse(
|
||||
environment.Id,
|
||||
environment.ApiKey,
|
||||
featureStateResults.Select(FlagResponse.From).ToList(),
|
||||
new EnvironmentProjectResponse(
|
||||
project.Id,
|
||||
project.Name,
|
||||
project.Segments.Select(SegmentDocumentResponse.From).ToList()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user