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.Step; namespace eprintServer.Services; public class StepService { private readonly DataContext _context; public StepService(DataContext context) { _context = context; } public async Task CreateStepAsync(CreateStepRequest request) { var product = await _context.CustomizableProducts.FindAsync(request.ProductId); if (product == null) throw new Exception("Product not found"); var step = new Step { ProductId = request.ProductId, StepOrder = request.StepOrder, StepType = request.StepType }; foreach (var nameDto in request.StepName) { step.StepName.Add(new DynamicValue { Value = nameDto.Value, LanguageTag = nameDto.LanguageTag }); } _context.Steps.Add(step); await _context.SaveChangesAsync(); return await GetStepByIdAsync(step.Id); } // NEW: BULK CREATE STEPS public async Task> CreateStepsAsync(CreateStepsRequest request) { var product = await _context.CustomizableProducts.FindAsync(request.ProductId); if (product == null) throw new Exception("Product not found"); var createdSteps = new List(); foreach (var stepData in request.Steps) { var step = new Step { ProductId = request.ProductId, StepOrder = stepData.StepOrder, StepType = stepData.StepType }; foreach (var nameDto in stepData.StepName) { step.StepName.Add(new DynamicValue { Value = nameDto.Value, LanguageTag = nameDto.LanguageTag }); } _context.Steps.Add(step); createdSteps.Add(step); } await _context.SaveChangesAsync(); var stepIds = createdSteps.Select(s => s.Id).ToList(); return await _context.Steps .Include(s => s.StepName) .Include(s => s.FormElements.OrderBy(fe => fe.ElementOrder)) .ThenInclude(fe => fe.Label) .Include(s => s.FormElements) .ThenInclude(fe => fe.Options.OrderBy(o => o.OptionOrder)) .ThenInclude(o => o.OptionLabel) .Where(s => stepIds.Contains(s.Id)) .OrderBy(s => s.StepOrder) .ToListAsync(); } public async Task UpdateStepAsync(long id, UpdateStepRequest request) { var step = await _context.Steps .Include(s => s.StepName) .FirstOrDefaultAsync(s => s.Id == id); if (step == null) throw new Exception("Step not found"); if (request.StepOrder.HasValue) step.StepOrder = request.StepOrder.Value; if (request.StepType.HasValue) step.StepType = request.StepType.Value; if (request.Archive.HasValue) step.Archive = request.Archive.Value; if (request.StepName != null) { _context.DynamicValues.RemoveRange(step.StepName); step.StepName.Clear(); foreach (var nameDto in request.StepName) { step.StepName.Add(new DynamicValue { Value = nameDto.Value, LanguageTag = nameDto.LanguageTag }); } } step.Timestamp = DateTime.UtcNow; await _context.SaveChangesAsync(); return await GetStepByIdAsync(step.Id); } public async Task GetStepByIdAsync(long id) { var step = await _context.Steps .Include(s => s.StepName) .Include(s => s.FormElements.OrderBy(fe => fe.ElementOrder)) .ThenInclude(fe => fe.Label) .Include(s => s.FormElements) .ThenInclude(fe => fe.Options.OrderBy(o => o.OptionOrder)) .ThenInclude(o => o.OptionLabel) .FirstOrDefaultAsync(s => s.Id == id); if (step == null) throw new Exception("Step not found"); return step; } public async Task> GetStepsByProductIdAsync(long productId) { return await _context.Steps .Include(s => s.StepName) .Include(s => s.FormElements.OrderBy(fe => fe.ElementOrder)) .ThenInclude(fe => fe.Label) .Include(s => s.FormElements) .ThenInclude(fe => fe.Options.OrderBy(o => o.OptionOrder)) .ThenInclude(o => o.OptionLabel) .Where(s => s.ProductId == productId && !s.Archive) .OrderBy(s => s.StepOrder) .ToListAsync(); } public async Task DeleteStepAsync(long id) { var step = await _context.Steps.FindAsync(id); if (step == null) return false; _context.Steps.Remove(step); await _context.SaveChangesAsync(); return true; } }