using Microsoft.EntityFrameworkCore; using eprintServer.Data; using eprintServer.Entities; using eprintServer.Requests.FormElement; namespace eprintServer.Services; public class FormElementService { private readonly DataContext _context; public FormElementService(DataContext context) { _context = context; } /* public async Task CreateFormElementAsync(CreateFormElementRequest request) { var step = await _context.Steps.FindAsync(request.StepId); if (step == null) throw new Exception("Step not found"); var formElement = new FormElement { StepId = request.StepId, InputType = request.InputType, FieldName = request.FieldName, IsRequired = request.IsRequired, ElementOrder = request.ElementOrder, DefaultValue = request.DefaultValue, ValidationRules = request.ValidationRules, Placeholder = request.Placeholder }; foreach (var labelDto in request.Label) { formElement.Label.Add(new DynamicValue { Value = labelDto.Value, LanguageTag = labelDto.LanguageTag }); } _context.FormElements.Add(formElement); await _context.SaveChangesAsync(); return await GetFormElementByIdAsync(formElement.Id); } */ public async Task CreateFormElementAsync(CreateFormElementRequest request) { var step = await _context.Steps.FindAsync(request.StepId) ?? throw new Exception("Step not found"); var formElement = new FormElement { StepId = request.StepId, InputType = request.InputType, FieldName = request.FieldName, IsRequired = request.IsRequired, ElementOrder = request.ElementOrder, DefaultValue = request.DefaultValue, ValidationRules = request.ValidationRules, Placeholder = request.Placeholder, // New Attributes PriceConfigType = request.PriceConfigType, ColorPricingConfig = request.ColorPricingConfig, QuantityPricingConfig = request.QuantityPricingConfig }; foreach (var labelDto in request.Label) { formElement.Label.Add(new DynamicValue { Value = labelDto.Value, LanguageTag = labelDto.LanguageTag }); } _context.FormElements.Add(formElement); await _context.SaveChangesAsync(); return await GetFormElementByIdAsync(formElement.Id); } /* public async Task> CreateFormElementsAsync(CreateFormElementsRequest request) { var step = await _context.Steps.FindAsync(request.StepId); if (step == null) throw new Exception("Step not found"); var createdElements = new List(); foreach (var elementData in request.FormElements) { var formElement = new FormElement { StepId = request.StepId, InputType = elementData.InputType, FieldName = elementData.FieldName, IsRequired = elementData.IsRequired, ElementOrder = elementData.ElementOrder, DefaultValue = elementData.DefaultValue, ValidationRules = elementData.ValidationRules, Placeholder = elementData.Placeholder }; foreach (var labelDto in elementData.Label) { formElement.Label.Add(new DynamicValue { Value = labelDto.Value, LanguageTag = labelDto.LanguageTag }); } _context.FormElements.Add(formElement); createdElements.Add(formElement); } await _context.SaveChangesAsync(); var elementIds = createdElements.Select(e => e.Id).ToList(); return await _context.FormElements .Include(fe => fe.Label) .Include(fe => fe.Options.OrderBy(o => o.OptionOrder)) .ThenInclude(o => o.OptionLabel) .Where(fe => elementIds.Contains(fe.Id)) .OrderBy(fe => fe.ElementOrder) .ToListAsync(); } */ public async Task> CreateFormElementsAsync(CreateFormElementsRequest request) { var step = await _context.Steps.FindAsync(request.StepId); if (step == null) throw new Exception("Step not found"); var createdElements = new List(); foreach (var elementData in request.FormElements) { var formElement = new FormElement { StepId = request.StepId, InputType = elementData.InputType, FieldName = elementData.FieldName, IsRequired = elementData.IsRequired, ElementOrder = elementData.ElementOrder, DefaultValue = elementData.DefaultValue, ValidationRules = elementData.ValidationRules, Placeholder = elementData.Placeholder, // NEW ATTRIBUTES MAPPING PriceConfigType = elementData.PriceConfigType, ColorPricingConfig = elementData.ColorPricingConfig, QuantityPricingConfig = elementData.QuantityPricingConfig }; foreach (var labelDto in elementData.Label) { formElement.Label.Add(new DynamicValue { Value = labelDto.Value, LanguageTag = labelDto.LanguageTag }); } _context.FormElements.Add(formElement); createdElements.Add(formElement); } await _context.SaveChangesAsync(); var elementIds = createdElements.Select(e => e.Id).ToList(); return await _context.FormElements .Include(fe => fe.Label) .Include(fe => fe.Options.OrderBy(o => o.OptionOrder)) .ThenInclude(o => o.OptionLabel) .Where(fe => elementIds.Contains(fe.Id)) .OrderBy(fe => fe.ElementOrder) .ToListAsync(); } /* public async Task UpdateFormElementAsync(long id, UpdateFormElementRequest request) { var formElement = await _context.FormElements .Include(fe => fe.Label) .FirstOrDefaultAsync(fe => fe.Id == id); if (formElement == null) throw new Exception("FormElement not found"); if (request.InputType.HasValue) formElement.InputType = request.InputType.Value; if (request.FieldName != null) formElement.FieldName = request.FieldName; if (request.IsRequired.HasValue) formElement.IsRequired = request.IsRequired.Value; if (request.ElementOrder.HasValue) formElement.ElementOrder = request.ElementOrder.Value; if (request.DefaultValue != null) formElement.DefaultValue = request.DefaultValue; if (request.ValidationRules != null) formElement.ValidationRules = request.ValidationRules; if (request.Placeholder != null) formElement.Placeholder = request.Placeholder; if (request.Label != null) { _context.DynamicValues.RemoveRange(formElement.Label); formElement.Label.Clear(); foreach (var labelDto in request.Label) { formElement.Label.Add(new DynamicValue { Value = labelDto.Value, LanguageTag = labelDto.LanguageTag }); } } formElement.Timestamp = DateTime.UtcNow; await _context.SaveChangesAsync(); return await GetFormElementByIdAsync(formElement.Id); } */ public async Task UpdateFormElementAsync(long id, UpdateFormElementRequest request) { var formElement = await _context.FormElements.Include(fe => fe.Label).FirstOrDefaultAsync(fe => fe.Id == id) ?? throw new Exception("FormElement not found"); if (request.InputType.HasValue) formElement.InputType = request.InputType.Value; if (request.FieldName != null) formElement.FieldName = request.FieldName; if (request.IsRequired.HasValue) formElement.IsRequired = request.IsRequired.Value; if (request.ElementOrder.HasValue) formElement.ElementOrder = request.ElementOrder.Value; if (request.DefaultValue != null) formElement.DefaultValue = request.DefaultValue; if (request.ValidationRules != null) formElement.ValidationRules = request.ValidationRules; if (request.Placeholder != null) formElement.Placeholder = request.Placeholder; // New Attributes Null Checks if (request.PriceConfigType != null) formElement.PriceConfigType = request.PriceConfigType; if (request.ColorPricingConfig != null) formElement.ColorPricingConfig = request.ColorPricingConfig; if (request.QuantityPricingConfig != null) formElement.QuantityPricingConfig = request.QuantityPricingConfig; if (request.Label != null) { _context.DynamicValues.RemoveRange(formElement.Label); formElement.Label.Clear(); foreach (var labelDto in request.Label) { formElement.Label.Add(new DynamicValue { Value = labelDto.Value, LanguageTag = labelDto.LanguageTag }); } } formElement.Timestamp = DateTime.UtcNow; await _context.SaveChangesAsync(); return await GetFormElementByIdAsync(formElement.Id); } public async Task GetFormElementByIdAsync(long id) { var formElement = await _context.FormElements .Include(fe => fe.Label) .Include(fe => fe.Options.OrderBy(o => o.OptionOrder)) .ThenInclude(o => o.OptionLabel) .FirstOrDefaultAsync(fe => fe.Id == id); if (formElement == null) throw new Exception("FormElement not found"); return formElement; } public async Task> GetFormElementsByStepIdAsync(long stepId) { return await _context.FormElements .Include(fe => fe.Label) .Include(fe => fe.Options.OrderBy(o => o.OptionOrder)) .ThenInclude(o => o.OptionLabel) .Where(fe => fe.StepId == stepId) .OrderBy(fe => fe.ElementOrder) .ToListAsync(); } public async Task DeleteFormElementAsync(long id) { var formElement = await _context.FormElements.FindAsync(id); if (formElement == null) return false; _context.FormElements.Remove(formElement); await _context.SaveChangesAsync(); return true; } }