remove stepId, steps now have a global id

tests
maxime 1 year ago
parent 461b3b3c20
commit 632ac19238

@ -14,7 +14,7 @@ public class AppContext : DbContext
public DbSet<MemberEntity> Members { get; init; } public DbSet<MemberEntity> Members { get; init; }
public DbSet<TacticStepEntity> TacticSteps { get; set; } public DbSet<TacticStepEntity> TacticSteps { get; set; }
public AppContext() : base() public AppContext()
{ {
} }

@ -15,6 +15,5 @@ public class TacticStepEntity
public required int? ParentId { get; set; } public required int? ParentId { get; set; }
public TacticStepEntity? Parent { get; set; } public TacticStepEntity? Parent { get; set; }
public int StepId { get; set; }
public required string JsonContent { get; set; } public required string JsonContent { get; set; }
} }

@ -19,9 +19,9 @@ public static class EntitiesToModels
public static TacticStep ToModel(this TacticStepEntity entity, IQueryable<TacticStepEntity> steps) public static TacticStep ToModel(this TacticStepEntity entity, IQueryable<TacticStepEntity> steps)
{ {
return new TacticStep( return new TacticStep(
entity.StepId, entity.Id,
entity.ParentId, entity.ParentId,
steps.Where(s =>s.TacticId == entity.TacticId && s.ParentId == entity.StepId) steps.Where(s =>s.TacticId == entity.TacticId && s.ParentId == entity.Id)
.AsEnumerable() .AsEnumerable()
.Select(e => e.ToModel(steps)), .Select(e => e.ToModel(steps)),
entity.JsonContent entity.JsonContent

@ -9,7 +9,6 @@ namespace DbServices;
public class DbTacticService(AppContext.AppContext context) : ITacticService public class DbTacticService(AppContext.AppContext context) : ITacticService
{ {
public Task<IEnumerable<Tactic>> ListTacticsOf(int userId) public Task<IEnumerable<Tactic>> ListTacticsOf(int userId)
{ {
return Task.FromResult( return Task.FromResult(
@ -44,7 +43,6 @@ public class DbTacticService(AppContext.AppContext context) : ITacticService
var stepEntity = new TacticStepEntity var stepEntity = new TacticStepEntity
{ {
StepId = 1,
ParentId = null, ParentId = null,
TacticId = tacticEntity.Id, TacticId = tacticEntity.Id,
JsonContent = "{\"components\": []}" JsonContent = "{\"components\": []}"
@ -69,7 +67,7 @@ public class DbTacticService(AppContext.AppContext context) : ITacticService
public async Task<bool> SetTacticStepContent(int tacticId, int stepId, string json) public async Task<bool> SetTacticStepContent(int tacticId, int stepId, string json)
{ {
var entity = await context.TacticSteps var entity = await context.TacticSteps
.FirstOrDefaultAsync(t => t.TacticId == tacticId && t.StepId == stepId); .FirstOrDefaultAsync(t => t.TacticId == tacticId && t.Id == stepId);
if (entity == null) if (entity == null)
return false; return false;
@ -81,7 +79,7 @@ public class DbTacticService(AppContext.AppContext context) : ITacticService
{ {
return (await context return (await context
.TacticSteps .TacticSteps
.FirstOrDefaultAsync(t => t.TacticId == tacticId && t.StepId == stepId) .FirstOrDefaultAsync(t => t.TacticId == tacticId && t.Id == stepId)
)?.JsonContent; )?.JsonContent;
} }
@ -112,35 +110,24 @@ public class DbTacticService(AppContext.AppContext context) : ITacticService
public async Task<int?> AddTacticStep(int tacticId, int parentStepId, string initialJson) public async Task<int?> AddTacticStep(int tacticId, int parentStepId, string initialJson)
{ {
var tactic = await context.Tactics.FirstOrDefaultAsync(t => t.Id == tacticId); var tactic = await context.Tactics.FirstOrDefaultAsync(t => t.Id == tacticId);
if (tactic == null) if (tactic == null)
{ {
return null; return null;
} }
TacticStepEntity entity; var entity = new TacticStepEntity
int nextStepId;
lock (tactic)
{
nextStepId = context.TacticSteps
.Where(s => s.TacticId == tacticId)
.Max(s => s.StepId) + 1;
entity = new TacticStepEntity
{ {
JsonContent = initialJson, JsonContent = initialJson,
ParentId = parentStepId, ParentId = parentStepId,
TacticId = tacticId, TacticId = tacticId,
StepId = nextStepId
}; };
context.Add(entity); await context.AddAsync(entity);
context.SaveChanges(); await context.SaveChangesAsync();
}
return entity.StepId; return entity.Id;
} }
public async Task<bool> RemoveTacticStep(int tacticId, int stepId) public async Task<bool> RemoveTacticStep(int tacticId, int stepId)
@ -149,7 +136,7 @@ public class DbTacticService(AppContext.AppContext context) : ITacticService
var beginStep = await context var beginStep = await context
.TacticSteps .TacticSteps
.FirstOrDefaultAsync(t => t.TacticId == tacticId && t.StepId == stepId); .FirstOrDefaultAsync(t => t.TacticId == tacticId && t.Id == stepId);
if (beginStep == null) if (beginStep == null)
return false; return false;
@ -158,9 +145,8 @@ public class DbTacticService(AppContext.AppContext context) : ITacticService
while (toRemove.TryPop(out var step)) while (toRemove.TryPop(out var step))
{ {
await context.TacticSteps await context.TacticSteps
.Where(s => s.TacticId == tacticId && s.ParentId == step.StepId) .Where(s => s.TacticId == tacticId && s.ParentId == step.Id)
.ForEachAsync(toRemove.Push); .ForEachAsync(toRemove.Push);
context.Remove(step); context.Remove(step);

Loading…
Cancel
Save