using BLAIzor.Data; using BLAIzor.Models; using Microsoft.EntityFrameworkCore; namespace BLAIzor.Services { using BLAIzor.Data; using BLAIzor.Models; using Microsoft.EntityFrameworkCore; public class CssTemplateService { //private readonly ApplicationDbContext _context; private readonly IServiceScopeFactory _serviceScopeFactory; public CssTemplateService(ApplicationDbContext context, IServiceScopeFactory serviceScopeFactory) { _serviceScopeFactory = serviceScopeFactory; //_context = context; } public async Task GetByIdAsync(int id) { using (var scope = _serviceScopeFactory.CreateScope()) { var _context = scope.ServiceProvider.GetRequiredService(); // Now use dbContext safely without violating DI rules return await _context.CssTemplates .Include(ct => ct.DesignTemplate) // Include related template if needed .FirstOrDefaultAsync(ct => ct.Id == id); } } public async Task GetByDesignTemplateIdAsync(int designTemplateId) { using (var scope = _serviceScopeFactory.CreateScope()) { var _context = scope.ServiceProvider.GetRequiredService(); // Now use dbContext safely without violating DI rules return await _context.CssTemplates .Include(ct => ct.DesignTemplate) .FirstOrDefaultAsync(ct => ct.DesignTemplateId == designTemplateId); } } public async Task> GetAllAsync() { using (var scope = _serviceScopeFactory.CreateScope()) { var _context = scope.ServiceProvider.GetRequiredService(); // Now use dbContext safely without violating DI rules return await _context.CssTemplates .Include(ct => ct.DesignTemplate) .ToListAsync(); } } public async Task CreateAsync(CssTemplate cssTemplate) { using (var scope = _serviceScopeFactory.CreateScope()) { var _context = scope.ServiceProvider.GetRequiredService(); // Now use dbContext safely without violating DI rules _context.CssTemplates.Add(cssTemplate); await _context.SaveChangesAsync(); return cssTemplate; } } public async Task UpdateAsync(CssTemplate cssTemplate) { using (var scope = _serviceScopeFactory.CreateScope()) { var _context = scope.ServiceProvider.GetRequiredService(); // Now use dbContext safely without violating DI rules var existing = await _context.CssTemplates.FindAsync(cssTemplate.Id); if (existing == null) return null; existing.CssContent = cssTemplate.CssContent; existing.LastUpdated = DateTime.UtcNow; _context.CssTemplates.Update(existing); await _context.SaveChangesAsync(); return existing; } } public async Task DeleteAsync(int id) { using (var scope = _serviceScopeFactory.CreateScope()) { var _context = scope.ServiceProvider.GetRequiredService(); // Now use dbContext safely without violating DI rules var cssTemplate = await _context.CssTemplates.FindAsync(id); if (cssTemplate == null) return false; _context.CssTemplates.Remove(cssTemplate); await _context.SaveChangesAsync(); return true; } } public async Task SaveTempCssFileAsync(string cssContent, string sessionId) { var fileName = $"style_{Guid.Parse(sessionId).ToString("N").Substring(0, 8)}.css"; var filePath = Path.Combine("wwwroot", "temp", fileName); if (!File.Exists(filePath)) { await File.WriteAllTextAsync(filePath, cssContent); } return $"/temp/{fileName}"; } public Task DeleteSessionCssFile(string sessionId) { var fileName = $"style_{Guid.Parse(sessionId).ToString("N").Substring(0, 8)}.css"; var filePath = Path.Combine("wwwroot", "temp", fileName); if (File.Exists(filePath)) { File.Delete(filePath); } return Task.CompletedTask; } } }