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<TacticStepEntity> TacticSteps { get; set; }
public AppContext() : base()
public AppContext()
{
}

@ -15,6 +15,5 @@ public class TacticStepEntity
public required int? ParentId { get; set; }
public TacticStepEntity? Parent { get; set; }
public int StepId { 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)
{
return new TacticStep(
entity.StepId,
entity.Id,
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()
.Select(e => e.ToModel(steps)),
entity.JsonContent

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

Loading…
Cancel
Save