You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
3.2 KiB
115 lines
3.2 KiB
using System.Collections;
|
|
using AppContext.Entities;
|
|
using Converters;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Model;
|
|
using Services;
|
|
|
|
namespace DbServices;
|
|
|
|
public class DbTacticService(AppContext.AppContext context) : ITacticService
|
|
{
|
|
public Task<IEnumerable<Tactic>> ListTacticsOf(int userId)
|
|
{
|
|
return Task.FromResult(
|
|
context.Tactics
|
|
.Where(t => t.OwnerId == userId)
|
|
.AsEnumerable()
|
|
.Select(e => e.ToModel())
|
|
);
|
|
}
|
|
|
|
public async Task<bool> HasAnyRights(int userId, int tacticId)
|
|
{
|
|
var tacticEntity = await context.Tactics.FirstOrDefaultAsync(u => u.Id == tacticId);
|
|
if (tacticEntity == null)
|
|
return false;
|
|
|
|
return tacticEntity.OwnerId == userId;
|
|
}
|
|
|
|
public async Task<bool> UpdateName(int tacticId, string name)
|
|
{
|
|
var entity = await context.Tactics.FirstOrDefaultAsync(t => t.Id == tacticId);
|
|
if (entity == null)
|
|
return false;
|
|
|
|
entity.Name = name;
|
|
return await context.SaveChangesAsync() > 0;
|
|
}
|
|
|
|
public async Task<bool> SetTacticStepContent(int tacticId, int stepId, string json)
|
|
{
|
|
var entity = await context.TacticSteps
|
|
.FirstOrDefaultAsync(t => t.TacticId == tacticId && t.StepId == stepId);
|
|
if (entity == null)
|
|
return false;
|
|
|
|
entity.JsonContent = json;
|
|
return await context.SaveChangesAsync() > 0;
|
|
}
|
|
|
|
public async Task<string?> GetTacticStepContent(int tacticId, int stepId)
|
|
{
|
|
return (await context
|
|
.TacticSteps
|
|
.FirstOrDefaultAsync(t => t.TacticId == tacticId && t.StepId == stepId)
|
|
)?.JsonContent;
|
|
}
|
|
|
|
public Task<IEnumerable<Tactic>> ListUserTactics(int userId)
|
|
{
|
|
return Task.FromResult(context
|
|
.Tactics
|
|
.Where(t => t.OwnerId == userId)
|
|
.AsEnumerable()
|
|
.Select(e => e.ToModel())
|
|
);
|
|
}
|
|
|
|
public async Task<int> AddTacticStep(int tacticId, int parentStepId, string initialJson)
|
|
{
|
|
var entity = new TacticStepEntity
|
|
{
|
|
JsonContent = "{components: []}",
|
|
ParentId = parentStepId,
|
|
TacticId = tacticId
|
|
};
|
|
|
|
await context.AddAsync(entity);
|
|
await context.SaveChangesAsync();
|
|
|
|
return entity.StepId;
|
|
}
|
|
|
|
public async Task<bool> RemoveTacticStep(int tacticId, int stepId)
|
|
{
|
|
var toRemove = new List<int> { stepId };
|
|
|
|
while (toRemove.Count != 0)
|
|
{
|
|
var id = toRemove[0];
|
|
toRemove.RemoveAt(0);
|
|
|
|
var step = await context.TacticSteps
|
|
.Include(s => s.Children)
|
|
.FirstOrDefaultAsync(t => t.TacticId == tacticId && t.StepId == id);
|
|
|
|
if (step == null)
|
|
{
|
|
if (id == stepId)
|
|
return false;
|
|
throw new Exception(
|
|
$"step contains a children that does not exists in the database ({tacticId} / {id})"
|
|
);
|
|
}
|
|
|
|
var stepChildren = step.Children.Select(s => s.StepId);
|
|
toRemove.AddRange(stepChildren);
|
|
|
|
context.Remove(step);
|
|
}
|
|
|
|
return await context.SaveChangesAsync() > 0;
|
|
}
|
|
} |