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.FormElementOption; namespace eprintServer.Services; public class FormElementOptionService { private readonly DataContext _context; public FormElementOptionService(DataContext context) { _context = context; } /* public async Task CreateOptionAsync(CreateFormElementOptionRequest request) { var formElement = await _context.FormElements.FindAsync(request.FormElementId); if (formElement == null) throw new Exception("FormElement not found"); var option = new FormElementOption { FormElementId = request.FormElementId, OptionValue = request.OptionValue, OptionOrder = request.OptionOrder }; foreach (var labelDto in request.OptionLabel) { option.OptionLabel.Add(new DynamicValue { Value = labelDto.Value, LanguageTag = labelDto.LanguageTag }); } _context.FormElementOptions.Add(option); await _context.SaveChangesAsync(); return await GetOptionByIdAsync(option.Id); } */ public async Task CreateOptionAsync(CreateFormElementOptionRequest request) { var formElement = await _context.FormElements.FindAsync(request.FormElementId) ?? throw new Exception("FormElement not found"); var option = new FormElementOption { FormElementId = request.FormElementId, OptionValue = request.OptionValue, OptionOrder = request.OptionOrder, PriceConfig = request.PriceConfig // New Attribute }; foreach (var labelDto in request.OptionLabel) { option.OptionLabel.Add(new DynamicValue { Value = labelDto.Value, LanguageTag = labelDto.LanguageTag }); } _context.FormElementOptions.Add(option); await _context.SaveChangesAsync(); return await GetOptionByIdAsync(option.Id); } // NEW: BULK CREATE OPTIONS /* public async Task> CreateOptionsAsync(CreateFormElementOptionsRequest request) { var formElement = await _context.FormElements.FindAsync(request.FormElementId); if (formElement == null) throw new Exception("FormElement not found"); var createdOptions = new List(); foreach (var optionData in request.Options) { var option = new FormElementOption { FormElementId = request.FormElementId, OptionValue = optionData.OptionValue, OptionOrder = optionData.OptionOrder }; foreach (var labelDto in optionData.OptionLabel) { option.OptionLabel.Add(new DynamicValue { Value = labelDto.Value, LanguageTag = labelDto.LanguageTag }); } _context.FormElementOptions.Add(option); createdOptions.Add(option); } await _context.SaveChangesAsync(); var optionIds = createdOptions.Select(o => o.Id).ToList(); return await _context.FormElementOptions .Include(o => o.OptionLabel) .Where(o => optionIds.Contains(o.Id)) .OrderBy(o => o.OptionOrder) .ToListAsync(); } */ public async Task> CreateOptionsAsync(CreateFormElementOptionsRequest request) { var formElement = await _context.FormElements.FindAsync(request.FormElementId); if (formElement == null) throw new Exception("FormElement not found"); var createdOptions = new List(); foreach (var optionData in request.Options) { var option = new FormElementOption { FormElementId = request.FormElementId, OptionValue = optionData.OptionValue, OptionOrder = optionData.OptionOrder, // NEW ATTRIBUTE MAPPING PriceConfig = optionData.PriceConfig }; foreach (var labelDto in optionData.OptionLabel) { option.OptionLabel.Add(new DynamicValue { Value = labelDto.Value, LanguageTag = labelDto.LanguageTag }); } _context.FormElementOptions.Add(option); createdOptions.Add(option); } await _context.SaveChangesAsync(); var optionIds = createdOptions.Select(o => o.Id).ToList(); return await _context.FormElementOptions .Include(o => o.OptionLabel) .Where(o => optionIds.Contains(o.Id)) .OrderBy(o => o.OptionOrder) .ToListAsync(); } /* public async Task UpdateOptionAsync(long id, UpdateFormElementOptionRequest request) { var option = await _context.FormElementOptions .Include(o => o.OptionLabel) .FirstOrDefaultAsync(o => o.Id == id); if (option == null) throw new Exception("Option not found"); if (request.OptionValue != null) option.OptionValue = request.OptionValue; if (request.OptionOrder.HasValue) option.OptionOrder = request.OptionOrder.Value; if (request.OptionLabel != null) { _context.DynamicValues.RemoveRange(option.OptionLabel); option.OptionLabel.Clear(); foreach (var labelDto in request.OptionLabel) { option.OptionLabel.Add(new DynamicValue { Value = labelDto.Value, LanguageTag = labelDto.LanguageTag }); } } option.Timestamp = DateTime.UtcNow; await _context.SaveChangesAsync(); return await GetOptionByIdAsync(option.Id); } */ public async Task UpdateOptionAsync(long id, UpdateFormElementOptionRequest request) { var option = await _context.FormElementOptions.Include(o => o.OptionLabel).FirstOrDefaultAsync(o => o.Id == id) ?? throw new Exception("Option not found"); if (request.OptionValue != null) option.OptionValue = request.OptionValue; if (request.OptionOrder.HasValue) option.OptionOrder = request.OptionOrder.Value; if (request.PriceConfig != null) option.PriceConfig = request.PriceConfig; // New Attribute if (request.OptionLabel != null) { _context.DynamicValues.RemoveRange(option.OptionLabel); option.OptionLabel.Clear(); foreach (var labelDto in request.OptionLabel) { option.OptionLabel.Add(new DynamicValue { Value = labelDto.Value, LanguageTag = labelDto.LanguageTag }); } } option.Timestamp = DateTime.UtcNow; await _context.SaveChangesAsync(); return await GetOptionByIdAsync(option.Id); } public async Task GetOptionByIdAsync(long id) { var option = await _context.FormElementOptions .Include(o => o.OptionLabel) .FirstOrDefaultAsync(o => o.Id == id); if (option == null) throw new Exception("Option not found"); return option; } public async Task> GetOptionsByFormElementIdAsync(long formElementId) { return await _context.FormElementOptions .Include(o => o.OptionLabel) .Where(o => o.FormElementId == formElementId) .OrderBy(o => o.OptionOrder) .ToListAsync(); } public async Task DeleteOptionAsync(long id) { var option = await _context.FormElementOptions.FindAsync(id); if (option == null) return false; _context.FormElementOptions.Remove(option); await _context.SaveChangesAsync(); return true; } }