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.
167 lines
4.6 KiB
167 lines
4.6 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<int> AddTactic(int userId, string name, CourtType courtType)
|
|
{
|
|
var tacticEntity = new TacticEntity
|
|
{
|
|
Name = name,
|
|
CreationDate = DateTime.Now,
|
|
OwnerId = userId,
|
|
Type = courtType
|
|
};
|
|
|
|
await context.Tactics.AddAsync(tacticEntity);
|
|
await context.SaveChangesAsync();
|
|
|
|
var stepEntity = new TacticStepEntity
|
|
{
|
|
StepId = 1,
|
|
ParentId = null,
|
|
TacticId = tacticEntity.Id,
|
|
JsonContent = "{\"components\": []}"
|
|
};
|
|
|
|
await context.TacticSteps.AddAsync(stepEntity);
|
|
await context.SaveChangesAsync();
|
|
|
|
return tacticEntity.Id;
|
|
}
|
|
|
|
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 async Task<TacticStep> GetRootStep(int tacticId)
|
|
{
|
|
return (await context.TacticSteps
|
|
.FirstAsync(e => e.TacticId == tacticId && e.ParentId == null))
|
|
.ToModel(context.TacticSteps);
|
|
}
|
|
|
|
public async Task<Tactic?> GetTactic(int tacticId)
|
|
{
|
|
return (await context.Tactics
|
|
.FirstOrDefaultAsync(s => s.Id == tacticId))
|
|
?.ToModel();
|
|
}
|
|
|
|
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)
|
|
{
|
|
if (!context.Tactics.Any(t => t.Id == tacticId))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var nextStepId = context.TacticSteps
|
|
.Where(s => s.TacticId == tacticId)
|
|
.Max(s => s.StepId) + 1;
|
|
|
|
var entity = new TacticStepEntity
|
|
{
|
|
JsonContent = initialJson,
|
|
ParentId = parentStepId,
|
|
TacticId = tacticId,
|
|
StepId = nextStepId
|
|
};
|
|
|
|
await context.AddAsync(entity);
|
|
await context.SaveChangesAsync();
|
|
|
|
return entity.StepId;
|
|
}
|
|
|
|
public async Task<bool> RemoveTacticStep(int tacticId, int stepId)
|
|
{
|
|
var toRemove = new Stack<int>();
|
|
toRemove.Push(stepId);
|
|
|
|
while (toRemove.TryPop(out var id))
|
|
{
|
|
var step = await context.TacticSteps
|
|
.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 = context.TacticSteps.Where(s => s.ParentId == id);
|
|
foreach (var stepChild in stepChildren)
|
|
{
|
|
toRemove.Push(stepChild.StepId);
|
|
}
|
|
|
|
context.Remove(step);
|
|
}
|
|
|
|
return await context.SaveChangesAsync() > 0;
|
|
}
|
|
} |