UX tweaks, traits bug fix, adding member info
This commit is contained in:
@@ -23,6 +23,25 @@ public class AdminIdentityService(MicCheckDbContext db)
|
||||
return (total, items);
|
||||
}
|
||||
|
||||
public async Task<Identity?> CreateAsync(int environmentId, string identifier, CancellationToken ct = default)
|
||||
{
|
||||
var existing = await db.Identities
|
||||
.FirstOrDefaultAsync(i => i.EnvironmentId == environmentId && i.Identifier == identifier, ct);
|
||||
|
||||
if (existing is not null)
|
||||
return null;
|
||||
|
||||
var identity = new Identity
|
||||
{
|
||||
Identifier = identifier,
|
||||
EnvironmentId = environmentId,
|
||||
CreatedAt = DateTimeOffset.UtcNow,
|
||||
};
|
||||
db.Identities.Add(identity);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return identity;
|
||||
}
|
||||
|
||||
public async Task<Identity?> FindByIdAsync(int id, int environmentId, CancellationToken ct = default)
|
||||
{
|
||||
return await db.Identities
|
||||
@@ -30,6 +49,53 @@ public class AdminIdentityService(MicCheckDbContext db)
|
||||
.FirstOrDefaultAsync(i => i.Id == id && i.EnvironmentId == environmentId, ct);
|
||||
}
|
||||
|
||||
public async Task<TraitResponse?> UpsertTraitAsync(
|
||||
int identityId, int environmentId, string key, string value, CancellationToken ct = default)
|
||||
{
|
||||
var identity = await db.Identities
|
||||
.Include(i => i.Traits)
|
||||
.FirstOrDefaultAsync(i => i.Id == identityId && i.EnvironmentId == environmentId, ct);
|
||||
|
||||
if (identity is null) return null;
|
||||
|
||||
var existing = identity.Traits.FirstOrDefault(t => t.Key == key);
|
||||
if (existing is not null)
|
||||
{
|
||||
existing.Value = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
var trait = new IdentityTrait
|
||||
{
|
||||
IdentityId = identityId,
|
||||
Key = key,
|
||||
Value = value,
|
||||
ValueType = TraitValueType.String,
|
||||
};
|
||||
db.IdentityTraits.Add(trait);
|
||||
}
|
||||
|
||||
await db.SaveChangesAsync(ct);
|
||||
return new TraitResponse(key, value);
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteTraitAsync(
|
||||
int identityId, int environmentId, string key, CancellationToken ct = default)
|
||||
{
|
||||
var identity = await db.Identities
|
||||
.Include(i => i.Traits)
|
||||
.FirstOrDefaultAsync(i => i.Id == identityId && i.EnvironmentId == environmentId, ct);
|
||||
|
||||
if (identity is null) return false;
|
||||
|
||||
var trait = identity.Traits.FirstOrDefault(t => t.Key == key);
|
||||
if (trait is null) return false;
|
||||
|
||||
db.IdentityTraits.Remove(trait);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(int id, CancellationToken ct = default)
|
||||
{
|
||||
var identity = await db.Identities.FirstOrDefaultAsync(i => i.Id == id, ct);
|
||||
|
||||
3
src/api/MicCheck.Api/Identities/CreateIdentityRequest.cs
Normal file
3
src/api/MicCheck.Api/Identities/CreateIdentityRequest.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
namespace MicCheck.Api.Identities;
|
||||
|
||||
public record CreateIdentityRequest(string Identifier);
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace MicCheck.Api.Identities;
|
||||
|
||||
public record TraitResponse(string TraitKey, string TraitValue)
|
||||
public record TraitResponse(string Key, string Value)
|
||||
{
|
||||
public static TraitResponse From(IdentityTrait trait) =>
|
||||
new(trait.Key, trait.Value);
|
||||
|
||||
3
src/api/MicCheck.Api/Identities/UpsertTraitRequest.cs
Normal file
3
src/api/MicCheck.Api/Identities/UpsertTraitRequest.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
namespace MicCheck.Api.Identities;
|
||||
|
||||
public record UpsertTraitRequest(string Key, string Value);
|
||||
Reference in New Issue
Block a user