204 lines
6.6 KiB
C#
204 lines
6.6 KiB
C#
using BLAIzor.Data;
|
|
using BLAIzor.Models;
|
|
using Google.Cloud.Speech.V1;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace BLAIzor.Services
|
|
{
|
|
public class ScopedContentService
|
|
{
|
|
//private readonly ApplicationDbContext _context;
|
|
private readonly IServiceScopeFactory _serviceScopeFactory;
|
|
public ScopedContentService(ApplicationDbContext context, IServiceScopeFactory serviceScopeFactory)
|
|
{
|
|
//_context = context;
|
|
_serviceScopeFactory = serviceScopeFactory;
|
|
}
|
|
|
|
public string CurrentDOM { get; set; }
|
|
public string currentLocale { get; set; }
|
|
public string SelectedDocument { get; set; } = "Poppixel.docx";
|
|
|
|
private string _selectedBrandName;
|
|
public string SelectedBrandName
|
|
{
|
|
get => _selectedBrandName;
|
|
set
|
|
{
|
|
if (_selectedBrandName != value)
|
|
{
|
|
_selectedBrandName = value;
|
|
OnBrandNameChanged?.Invoke();
|
|
}
|
|
}
|
|
}
|
|
|
|
public event Action OnBrandNameChanged;
|
|
public int SelectedSiteId { get; set; } = 1;
|
|
//public string SelectedDocument { get; set; } = "Poppixel.docx";
|
|
//public string SelectedBrandName
|
|
//{
|
|
// get { return SelectedDocument.Split('.')[0]; }
|
|
|
|
// set { }
|
|
//}
|
|
public string SelectedLanguage { get; set; } = "English";
|
|
|
|
public string SessionId { get; set; }
|
|
|
|
public async Task<SiteInfo> GetSiteInfoByIdAsync(int SiteInfoId)
|
|
{
|
|
using (var scope = _serviceScopeFactory.CreateScope())
|
|
{
|
|
var _context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
|
|
// Now use dbContext safely without violating DI rules
|
|
var result = await _context.SiteInfos.Where(x => x.Id == SiteInfoId).FirstOrDefaultAsync();
|
|
if (result == null)
|
|
{
|
|
return await _context.SiteInfos.FirstOrDefaultAsync();
|
|
}
|
|
else
|
|
{
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
public async Task<SiteInfo?> GetSiteInfoWithFormsByIdAsync(int siteId)
|
|
{
|
|
|
|
using (var scope = _serviceScopeFactory.CreateScope())
|
|
{
|
|
var _context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
|
|
// Now use dbContext safely without violating DI rules
|
|
var result = await _context.SiteInfos.Include(s => s.FormDefinitions).FirstOrDefaultAsync(s => s.Id == siteId);
|
|
if (result == null)
|
|
{
|
|
return new SiteInfo();
|
|
}
|
|
else
|
|
{
|
|
return result;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public async Task<SiteInfo> GetSiteInfoByNameAsync(string siteName)
|
|
{
|
|
using (var scope = _serviceScopeFactory.CreateScope())
|
|
{
|
|
var _context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
|
|
// Now use dbContext safely without violating DI rules
|
|
var result = await _context.SiteInfos.Where(x => x.SiteName == siteName).FirstOrDefaultAsync();
|
|
if (result == null)
|
|
{
|
|
return await _context.SiteInfos.FirstOrDefaultAsync();
|
|
}
|
|
else
|
|
{
|
|
return result;
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public async Task<SiteInfo> GetSiteInfoByUrlAsync(string url)
|
|
{
|
|
using (var scope = _serviceScopeFactory.CreateScope())
|
|
{
|
|
var _context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
|
|
// Now use dbContext safely without violating DI rules
|
|
var result = await _context.SiteInfos.Where(x => x.DefaultUrl == url).FirstOrDefaultAsync();
|
|
if (result == null)
|
|
{
|
|
return await _context.SiteInfos.FirstOrDefaultAsync();
|
|
}
|
|
else
|
|
{
|
|
return result;
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public async Task UpdateSiteInfoAsync(SiteInfo siteInfo)
|
|
{
|
|
using (var scope = _serviceScopeFactory.CreateScope())
|
|
{
|
|
var _context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
|
|
// Now use dbContext safely without violating DI rules
|
|
_context.SiteInfos.Update(siteInfo);
|
|
await _context.SaveChangesAsync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public async Task<List<SiteInfo>> GetUserSitesAsync(string userId)
|
|
{
|
|
using (var scope = _serviceScopeFactory.CreateScope())
|
|
{
|
|
var _context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
|
|
// Now use dbContext safely without violating DI rules
|
|
return await _context.SiteInfos
|
|
.Where(s => s.UserId == userId)
|
|
.ToListAsync();
|
|
|
|
}
|
|
}
|
|
|
|
public async Task<List<SiteInfo>> GetSitesAsync()
|
|
{
|
|
using (var scope = _serviceScopeFactory.CreateScope())
|
|
{
|
|
var _context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
|
|
// Now use dbContext safely without violating DI rules
|
|
return await _context.SiteInfos.ToListAsync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public async Task<SiteInfo> AddSiteInfoAsync(SiteInfo siteInfo)
|
|
{
|
|
using (var scope = _serviceScopeFactory.CreateScope())
|
|
{
|
|
var _context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
|
|
// Now use dbContext safely without violating DI rules
|
|
if (siteInfo == null)
|
|
throw new ArgumentNullException(nameof(siteInfo), "SiteInfo cannot be null.");
|
|
|
|
try
|
|
{
|
|
await _context.SiteInfos.AddAsync(siteInfo);
|
|
await _context.SaveChangesAsync();
|
|
return siteInfo;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Log the exception (using a logging framework like Serilog, NLog, etc.)
|
|
throw new InvalidOperationException("An error occurred while adding the site info.", ex);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|