diff --git a/AppContext/AppContext.cs b/AppContext/AppContext.cs index 55a95b0..f875684 100644 --- a/AppContext/AppContext.cs +++ b/AppContext/AppContext.cs @@ -14,7 +14,7 @@ public class AppContext : DbContext public DbSet Members { get; init; } public DbSet TacticSteps { get; set; } - public AppContext() : base() + public AppContext() { } diff --git a/AppContext/Entities/TacticStepEntity.cs b/AppContext/Entities/TacticStepEntity.cs index ee56354..117fc1d 100644 --- a/AppContext/Entities/TacticStepEntity.cs +++ b/AppContext/Entities/TacticStepEntity.cs @@ -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; } } \ No newline at end of file diff --git a/Converters/ModelToEntities.cs b/Converters/ModelToEntities.cs index 93d4427..4ecd15c 100644 --- a/Converters/ModelToEntities.cs +++ b/Converters/ModelToEntities.cs @@ -19,9 +19,9 @@ public static class EntitiesToModels public static TacticStep ToModel(this TacticStepEntity entity, IQueryable 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 diff --git a/DbServices/DbTacticService.cs b/DbServices/DbTacticService.cs index 42d70e1..466246e 100644 --- a/DbServices/DbTacticService.cs +++ b/DbServices/DbTacticService.cs @@ -9,7 +9,6 @@ namespace DbServices; public class DbTacticService(AppContext.AppContext context) : ITacticService { - public Task> 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 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 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) + var entity = new TacticStepEntity { - nextStepId = context.TacticSteps - .Where(s => s.TacticId == tacticId) - .Max(s => s.StepId) + 1; - - entity = new TacticStepEntity - { - JsonContent = initialJson, - ParentId = parentStepId, - TacticId = tacticId, - StepId = nextStepId - }; - - context.Add(entity); - context.SaveChanges(); - } + JsonContent = initialJson, + ParentId = parentStepId, + TacticId = tacticId, + }; + + await context.AddAsync(entity); + await context.SaveChangesAsync(); - return entity.StepId; + return entity.Id; } public async Task RemoveTacticStep(int tacticId, int stepId) @@ -149,18 +136,17 @@ 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; - + toRemove.Push(beginStep); 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);