using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using eprintServer.Data; using eprintServer.Entities; using eprintServer.Requests.StepTemplate; namespace eprintServer.Services; public class StepTemplateService { private readonly DataContext _context; public StepTemplateService(DataContext context) { _context = context; } public async Task CreateTemplateAsync(CreateStepTemplateRequest request) { var template = new StepTemplate { StepType = request.StepType }; foreach (var nameDto in request.TemplateName) { template.TemplateName.Add(new DynamicValue { Value = nameDto.Value, LanguageTag = nameDto.LanguageTag }); } _context.StepTemplates.Add(template); await _context.SaveChangesAsync(); return await GetTemplateByIdAsync(template.Id); } public async Task CloneTemplateToProductAsync(CloneStepTemplateRequest request) { var template = await _context.StepTemplates .Include(t => t.TemplateName) .Include(t => t.FormElements) .ThenInclude(fe => fe.Label) .Include(t => t.FormElements) .ThenInclude(fe => fe.Options) .ThenInclude(o => o.OptionLabel) .FirstOrDefaultAsync(t => t.Id == request.TemplateId); if (template == null) throw new Exception("Template not found"); var product = await _context.CustomizableProducts.FindAsync(request.ProductId); if (product == null) throw new Exception("Product not found"); var newStep = new Step { ProductId = request.ProductId, StepOrder = request.StepOrder, StepType = template.StepType, IsTemplate = false }; foreach (var name in template.TemplateName) { newStep.StepName.Add(new DynamicValue { Value = name.Value, LanguageTag = name.LanguageTag }); } _context.Steps.Add(newStep); await _context.SaveChangesAsync(); foreach (var templateElement in template.FormElements) { var newElement = new FormElement { StepId = newStep.Id, InputType = templateElement.InputType, FieldName = templateElement.FieldName, IsRequired = templateElement.IsRequired, ElementOrder = templateElement.ElementOrder, DefaultValue = templateElement.DefaultValue, ValidationRules = templateElement.ValidationRules, Placeholder = templateElement.Placeholder }; foreach (var label in templateElement.Label) { newElement.Label.Add(new DynamicValue { Value = label.Value, LanguageTag = label.LanguageTag }); } _context.FormElements.Add(newElement); await _context.SaveChangesAsync(); foreach (var templateOption in templateElement.Options) { var newOption = new FormElementOption { FormElementId = newElement.Id, OptionValue = templateOption.OptionValue, OptionOrder = templateOption.OptionOrder }; foreach (var optionLabel in templateOption.OptionLabel) { newOption.OptionLabel.Add(new DynamicValue { Value = optionLabel.Value, LanguageTag = optionLabel.LanguageTag }); } _context.FormElementOptions.Add(newOption); } await _context.SaveChangesAsync(); } return await _context.Steps .Include(s => s.StepName) .Include(s => s.FormElements) .ThenInclude(fe => fe.Label) .Include(s => s.FormElements) .ThenInclude(fe => fe.Options) .ThenInclude(o => o.OptionLabel) .FirstOrDefaultAsync(s => s.Id == newStep.Id); } public async Task GetTemplateByIdAsync(long id) { var template = await _context.StepTemplates .Include(t => t.TemplateName) .Include(t => t.FormElements) .ThenInclude(fe => fe.Label) .Include(t => t.FormElements) .ThenInclude(fe => fe.Options) .ThenInclude(o => o.OptionLabel) .FirstOrDefaultAsync(t => t.Id == id); if (template == null) throw new Exception("Template not found"); return template; } public async Task> GetAllTemplatesAsync() { return await _context.StepTemplates .Include(t => t.TemplateName) .Where(t => !t.Archive) .OrderByDescending(t => t.Timestamp) .ToListAsync(); } public async Task DeleteTemplateAsync(long id) { var template = await _context.StepTemplates.FindAsync(id); if (template == null) return false; _context.StepTemplates.Remove(template); await _context.SaveChangesAsync(); return true; } }