SeemGen/Services/DesignTemplateService.cs

166 lines
5.9 KiB
C#

using BLAIzor.Data;
using BLAIzor.Models;
using DocumentFormat.OpenXml.Spreadsheet;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace BLAIzor.Services
{
public class DesignTemplateService
{
//private readonly ApplicationDbContext _context;
private readonly IServiceScopeFactory _serviceScopeFactory;
public DesignTemplateService(ApplicationDbContext context, IServiceScopeFactory serviceScopeFactory)
{
_serviceScopeFactory = serviceScopeFactory;
//_context = context;
}
public async Task<int> GetLastTemplateId()
{
using (var scope = _serviceScopeFactory.CreateScope())
{
var _context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
// Now use dbContext safely without violating DI rules
var result = await _context.DesignTemplates.OrderByDescending(x => x.Id)
.Take(1)
.Select(x => x.Id)
.ToListAsync();
return result.FirstOrDefault();
}
}
public async Task<DesignTemplate> GetByIdAsync(int id)
{
using (var scope = _serviceScopeFactory.CreateScope())
{
var _context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
// Now use dbContext safely without violating DI rules
var reslt = await _context.DesignTemplates
.Include(dt => dt.CssTemplate)
.Include(dt => dt.Sites)
.FirstOrDefaultAsync(dt => dt.Id == id);
if (reslt == null)
{
return null;
}
else
{
return reslt;
}
}
}
public async Task<IEnumerable<DesignTemplate>> GetAllAsync()
{
using (var scope = _serviceScopeFactory.CreateScope())
{
var _context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
// Now use dbContext safely without violating DI rules
return await _context.DesignTemplates
.Include(dt => dt.CssTemplate)
.Include(dt => dt.Sites)
.ToListAsync();
}
}
public async Task<IEnumerable<DesignTemplate>> GetPublishedTemplatesAsync()
{
using (var scope = _serviceScopeFactory.CreateScope())
{
var _context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
// Now use dbContext safely without violating DI rules
return await _context.DesignTemplates
.Where(dt => dt.IsPublished && !dt.IsDeprecated)
.ToListAsync();
}
}
public async Task<IEnumerable<DesignTemplate>> GetTemplatesByUserAsync(string userId)
{
using (var scope = _serviceScopeFactory.CreateScope())
{
var _context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
// Now use dbContext safely without violating DI rules
return await _context.DesignTemplates
.Where(dt => dt.UserId == userId)
.ToListAsync();
}
}
public async Task<DesignTemplate> CreateAsync(DesignTemplate designTemplate)
{
using (var scope = _serviceScopeFactory.CreateScope())
{
var _context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
// Now use dbContext safely without violating DI rules
designTemplate.CreatedAt = DateTime.UtcNow;
_context.DesignTemplates.Add(designTemplate);
await _context.SaveChangesAsync();
return designTemplate;
}
}
public async Task<DesignTemplate> UpdateAsync(DesignTemplate designTemplate)
{
using (var scope = _serviceScopeFactory.CreateScope())
{
var _context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
// Now use dbContext safely without violating DI rules
var existing = await _context.DesignTemplates.FindAsync(designTemplate.Id);
if (existing == null) return null;
existing.TemplateName = designTemplate.TemplateName;
existing.TemplatePhotoUrl = designTemplate.TemplatePhotoUrl;
existing.Description = designTemplate.Description;
existing.Tags = designTemplate.Tags;
existing.UpdatedAt = DateTime.UtcNow;
existing.IsPrivate = designTemplate.IsPrivate;
existing.IsPublished = designTemplate.IsPublished;
existing.Status = designTemplate.Status;
existing.IsDeprecated = designTemplate.IsDeprecated;
existing.Version = designTemplate.Version;
_context.DesignTemplates.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 designTemplate = await _context.DesignTemplates.FindAsync(id);
if (designTemplate == null) return false;
_context.DesignTemplates.Remove(designTemplate);
await _context.SaveChangesAsync();
return true;
}
}
}
}