114 lines
4.1 KiB
C#
114 lines
4.1 KiB
C#
using e_suite.API.Common.repository;
|
|
using e_suite.Database.Audit;
|
|
using e_suite.Database.Core;
|
|
using e_suite.Database.Core.Extensions;
|
|
using e_suite.Workflow.Core;
|
|
using e_suite.Workflow.Core.Extensions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace e_suite.Modules.WorkflowTemplatesManager.Repository;
|
|
|
|
public interface IWorkflowConverter
|
|
{
|
|
WorkflowVersion DeserialiseFromDatabase(e_suite.Database.Core.Tables.Workflow.WorkflowVersion dbVersion);
|
|
|
|
Task<e_suite.Database.Core.Tables.Workflow.WorkflowVersion> SerialiseToDatabase(
|
|
WorkflowVersion runtime,
|
|
e_suite.Database.Core.Tables.Workflow.WorkflowVersion? dbObject = null,
|
|
CancellationToken cancellationToken = default
|
|
);
|
|
}
|
|
public class WorkflowConverter : IWorkflowConverter
|
|
{
|
|
private readonly IDomainRepository _domainRepository;
|
|
|
|
public WorkflowConverter(IDomainRepository domainRepository)
|
|
{
|
|
_domainRepository = domainRepository;
|
|
}
|
|
|
|
public WorkflowVersion DeserialiseFromDatabase(e_suite.Database.Core.Tables.Workflow.WorkflowVersion dbVersion)
|
|
{
|
|
var runtime = new WorkflowVersion
|
|
{
|
|
Id = dbVersion.Id,
|
|
Guid = dbVersion.Guid,
|
|
Version = dbVersion.Version,
|
|
ActivityNameTemplate = dbVersion.ActivityNameTemplate,
|
|
Description = dbVersion.Description,
|
|
Domain = dbVersion.Domain.ToGeneralIdRef()!,
|
|
Template = new WorkflowTemplate
|
|
{
|
|
Name = "Need to fix",
|
|
Id = 1,
|
|
Guid = Guid.Empty
|
|
} // however you load templates
|
|
};
|
|
|
|
foreach (var def in dbVersion.Tasks)
|
|
{
|
|
var task = def.ToTask();
|
|
runtime.Tasks.Add(task);
|
|
}
|
|
|
|
return runtime;
|
|
}
|
|
|
|
public async Task<e_suite.Database.Core.Tables.Workflow.WorkflowVersion> SerialiseToDatabase(WorkflowVersion runtime, e_suite.Database.Core.Tables.Workflow.WorkflowVersion? dbObject = null, CancellationToken cancellationToken = default)
|
|
{
|
|
if (runtime is null)
|
|
throw new NullReferenceException();
|
|
|
|
var domain = await _domainRepository.GetDomainById(runtime.Domain, cancellationToken);
|
|
if (domain is null)
|
|
throw new Exception($"Domain with id {runtime.Domain} not found.");
|
|
|
|
var dbVersion = dbObject ?? new e_suite.Database.Core.Tables.Workflow.WorkflowVersion();
|
|
|
|
if (dbObject == null)
|
|
{
|
|
dbVersion.Id = runtime.Id;
|
|
dbVersion.Guid = runtime.Guid;
|
|
}
|
|
else
|
|
{
|
|
//note cannot move a version from one workflow to another, that requires a new version.
|
|
//todo make sure that the Workflow is populated here.
|
|
//dbVersion.Workflow = runtime.Template.ToGeneralIdRef()
|
|
}
|
|
|
|
dbVersion.Version = runtime.Version; //todo make sure that the version number get incremented somewhere logical.
|
|
dbVersion.ActivityNameTemplate = runtime.ActivityNameTemplate;
|
|
dbVersion.Description = runtime.Description;
|
|
dbVersion.DomainId = domain.Id;
|
|
dbVersion.Tasks = runtime.Tasks
|
|
.Select(t => t.ToDefinition())
|
|
.ToList();
|
|
|
|
return dbVersion;
|
|
}
|
|
}
|
|
|
|
|
|
public class WorkflowTemplateRepository : RepositoryBase, IWorkflowTemplateRepository
|
|
{
|
|
public WorkflowTemplateRepository(IEsuiteDatabaseDbContext databaseDbContext) : base(databaseDbContext)
|
|
{
|
|
}
|
|
|
|
public IQueryable<e_suite.Database.Core.Tables.Workflow.Workflow> GetWorkflows()
|
|
{
|
|
return DatabaseDbContext.Workflows
|
|
.Include( x => x.Versions);
|
|
}
|
|
|
|
public IQueryable<e_suite.Database.Core.Tables.Workflow.WorkflowVersion> GetWorkflowVersions()
|
|
{
|
|
return DatabaseDbContext.WorkflowVersions;
|
|
}
|
|
|
|
public async Task EditWorkflowVersionAsync(AuditUserDetails auditUserDetails, e_suite.Database.Core.Tables.Workflow.WorkflowVersion workflowVersion, CancellationToken cancellationToken)
|
|
{
|
|
await DatabaseDbContext.SaveChangesAsync(auditUserDetails, cancellationToken);
|
|
}
|
|
} |