168 lines
7.2 KiB
C#
168 lines
7.2 KiB
C#
using gehGassi.Core.Interfaces;
|
|
using gehGassi.Domain.News;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using gehGassi.Domain.Common;
|
|
using gehGassi.Domain.Pages;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace gehGassi.Core.Services
|
|
{
|
|
/// <summary>
|
|
/// Service der die Verwaltung von FAQs realisiert
|
|
/// </summary>
|
|
public class FaqService : ServiceBase<Faq>, IFaqService
|
|
{
|
|
private readonly IRepository<FaqWithNames> _namesRepository;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="unitOfWork">Instanz eines IUnitOfWork</param>
|
|
public FaqService(IUnitOfWork unitOfWork) : base(unitOfWork)
|
|
{
|
|
_namesRepository = unitOfWork.GetRepository<FaqWithNames>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine gefilterte Liste von FAQs zurück
|
|
/// </summary>
|
|
/// <param name="filter">Filterbegriff der in bestimmten Feldern gesucht wird</param>
|
|
/// <returns>List betroffener FAQs</returns>
|
|
public IQueryable<Faq> Filter(string filter)
|
|
{
|
|
return Repository.Query(c => c.Title.Contains(filter));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellen einer FAQ
|
|
/// </summary>
|
|
/// <returns>Faq</returns>
|
|
public Faq Create()
|
|
{
|
|
var item = new Faq()
|
|
{
|
|
Id = Guid.NewGuid().ToString("N"),
|
|
Created = DateTimeOffset.UtcNow
|
|
};
|
|
return item;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine gefilterte Liste von FAQs zurück. Sucht nur im Titel
|
|
/// </summary>
|
|
/// <param name="filter">Filterbegriff der in bestimmten Feldern gesucht wird</param>
|
|
/// <returns>List betroffener FAQs</returns>
|
|
public async Task<List<Faq>> SearchAsync(string filter)
|
|
{
|
|
return (await Repository.FindAsync(c => c.Title.Contains(filter))).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine gefilterte Liste von FAQs mit Namen zurück in einer gewählten Sprache
|
|
/// </summary>
|
|
/// <param name="filter">Filterbegriff der in bestimmten Feldern gesucht wird</param>
|
|
/// <param name="language">Gewünschte Sprache</param>
|
|
/// <param name="fallback">Fallback Sprache</param>
|
|
/// <param name="includeDeleted">Sollen gelöschte inkludiert werden?</param>
|
|
/// <returns>Liste betroffener FAQs</returns>
|
|
public IQueryable<FaqWithNames> FilterWithNames(string filter, string language, string fallback, bool includeDeleted)
|
|
{
|
|
language = language.ToUpperInvariant();
|
|
fallback = fallback.ToUpperInvariant();
|
|
|
|
var baseQuery = _namesRepository.QueryStoredProcedure("SELECT * FROM tv_FaqsWithNames({0},{1})", c => c.Title.Contains(filter) || c.Question.Contains(filter), language, fallback);
|
|
|
|
if (!includeDeleted)
|
|
baseQuery = baseQuery.Where(c => c.Deleted == false);
|
|
|
|
return baseQuery;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von FAQs zurück, welche nach dem glieferten Datum geändert wurden.
|
|
/// Wird kein Datum angegeben, werden alle zurückgegeben.
|
|
/// Es werden alle Kategorien zurückgegeben - egal ob on- oder offline.
|
|
/// </summary>
|
|
/// <param name="lastUpdate">Letztes Update oder null wenn noch keines</param>
|
|
/// <param name="language">Gewünschte Sprache</param>
|
|
/// <param name="fallback">Fallback Sprache</param>
|
|
/// <returns>Liste von FAQs</returns>
|
|
public async Task<List<FaqWithNames>> GetForSyncAsync(DateTimeOffset? lastUpdate, string language, string fallback)
|
|
{
|
|
language = language.ToUpperInvariant();
|
|
fallback = fallback.ToUpperInvariant();
|
|
|
|
IQueryable<FaqWithNames> baseQuery;
|
|
if (lastUpdate == null)
|
|
baseQuery = _namesRepository.QueryStoredProcedure("SELECT * FROM tv_FaqsWithNames({0},{1})", null, language, fallback);
|
|
else
|
|
baseQuery = _namesRepository.QueryStoredProcedure("SELECT * FROM tv_FaqsWithNames({0},{1})", c => c.UpdatedAt > lastUpdate, language, fallback);
|
|
return await baseQuery.ToListAsync().ConfigureAwait(false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellt das FAQ-Html für die Integration in die FAQ-Seite.
|
|
/// Es werden alle FAQs genommen, welche Online sind und nicht gelöscht wurden
|
|
/// </summary>
|
|
/// <param name="language">Gewünschte Sprache</param>
|
|
/// <param name="fallback">Fallback Sprache</param>
|
|
/// <param name="type">Typ</param>
|
|
/// <returns>string - HTML</returns>
|
|
public async Task<string> GenerateHtmlForPageAsync(string language, string fallback, FaqType type)
|
|
{
|
|
language = language.ToUpperInvariant();
|
|
fallback = fallback.ToUpperInvariant();
|
|
var faqsQuery = _namesRepository.QueryStoredProcedure("SELECT * FROM tv_FaqsWithNames({0},{1})", c => c.Deleted == false && c.OnlineStatus == OnlineStatus.Online, language, fallback);
|
|
if(type != FaqType.Both)
|
|
faqsQuery = faqsQuery.Where(c => c.Type == type || c.Type == FaqType.Both);
|
|
|
|
var faqs = await faqsQuery.OrderBy(c => c.Order).ThenBy(c => c.Question).ToListAsync().ConfigureAwait(false);
|
|
|
|
if (faqs.Any())
|
|
{
|
|
var result = $"<div class=\"accordion\" id=\"accordionFaq\">";
|
|
|
|
foreach (var faq in faqs)
|
|
{
|
|
result += $"<div class=\"accordion-item\">" +
|
|
$"<h2 class=\"accordion-header\" id=\"heading_{faq.Id}\">" +
|
|
$"<button class=\"accordion-button collapsed\" type=\"button\" data-bs-toggle=\"collapse\" data-bs-target=\"#collapse_{faq.Id}\" aria-expanded=\"false\" aria-controls=\"collapse_{faq.Id}\">" +
|
|
$"{faq.Question}" +
|
|
$"</button>" +
|
|
$"</h2>" +
|
|
$"<div id=\"collapse_{faq.Id}\" class=\"accordion-collapse collapse\" aria-labelledby=\"heading_{faq.Id}\" data-bs-parent=\"#accordionFaq\">" +
|
|
$"<div class=\"accordion-body\">" +
|
|
$"{faq.Answer}" +
|
|
$"</div>" +
|
|
$"</div>" +
|
|
$"</div>";
|
|
}
|
|
|
|
result += "</div>";
|
|
|
|
return result;
|
|
}
|
|
|
|
return string.Empty;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste aller FAQ mit einem speziellen Typ zurück
|
|
/// </summary>
|
|
/// <param name="type">Typ</param>
|
|
/// <returns>Liste FAQ</returns>
|
|
public async Task<List<Faq>> GetAllAsync(FaqType type)
|
|
{
|
|
var query = Repository.Query(c => c.OnlineStatus == OnlineStatus.Online && c.Deleted == false);
|
|
if(type != FaqType.Both)
|
|
query = query.Where(c => c.Type == type || c.Type == FaqType.Both);
|
|
var list = await query.OrderBy(c => c.Order).ToListAsync().ConfigureAwait(false);
|
|
return list;
|
|
}
|
|
}
|
|
}
|