SeemGen/Services/CssTemplateService.cs

141 lines
4.8 KiB
C#

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<CssTemplate> GetByIdAsync(int id)
{
using (var scope = _serviceScopeFactory.CreateScope())
{
var _context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
// 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<CssTemplate> GetByDesignTemplateIdAsync(int designTemplateId)
{
using (var scope = _serviceScopeFactory.CreateScope())
{
var _context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
// Now use dbContext safely without violating DI rules
return await _context.CssTemplates
.Include(ct => ct.DesignTemplate)
.FirstOrDefaultAsync(ct => ct.DesignTemplateId == designTemplateId);
}
}
public async Task<IEnumerable<CssTemplate>> GetAllAsync()
{
using (var scope = _serviceScopeFactory.CreateScope())
{
var _context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
// Now use dbContext safely without violating DI rules
return await _context.CssTemplates
.Include(ct => ct.DesignTemplate)
.ToListAsync();
}
}
public async Task<CssTemplate> CreateAsync(CssTemplate cssTemplate)
{
using (var scope = _serviceScopeFactory.CreateScope())
{
var _context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
// Now use dbContext safely without violating DI rules
_context.CssTemplates.Add(cssTemplate);
await _context.SaveChangesAsync();
return cssTemplate;
}
}
public async Task<CssTemplate> UpdateAsync(CssTemplate cssTemplate)
{
using (var scope = _serviceScopeFactory.CreateScope())
{
var _context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
// 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<bool> DeleteAsync(int id)
{
using (var scope = _serviceScopeFactory.CreateScope())
{
var _context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
// 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<string> 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;
}
}
}