334 lines
11 KiB
C#
334 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using AutoMapper;
|
|
using gehGassi.Common.Data;
|
|
using gehGassi.Core.Interfaces;
|
|
using gehGassi.Domain.Common;
|
|
using gehGassi.Permissions;
|
|
using gehGassi.Web.Auth.Attributes;
|
|
using gehGassi.Web.Auth;
|
|
using gehGassi.Web.Helper;
|
|
using gehGassi.Web.Models;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Localization;
|
|
using gehGassi.Core.Services;
|
|
using System.IO;
|
|
|
|
namespace gehGassi.Web.Controllers
|
|
{
|
|
/// <summary>
|
|
/// Controller für die Verwaltung von FAQs
|
|
/// </summary>
|
|
[Authorize]
|
|
public class FaqController : BaseController
|
|
{
|
|
private readonly IMapper _mapper;
|
|
private readonly IStringLocalizer<FaqController> _localizer;
|
|
private readonly ILanguageService _languageService;
|
|
private readonly IFaqService _faqService;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="mapper">Instanz eines IMapper</param>
|
|
/// <param name="localizer">Instanz eines IStringLocalizer</param>
|
|
/// <param name="languageService">Instanz eines ILanguageService</param>
|
|
/// <param name="faqService">Instanz eines IFaqService</param>
|
|
public FaqController(IMapper mapper, IStringLocalizer<FaqController> localizer, ILanguageService languageService, IFaqService faqService)
|
|
{
|
|
_mapper = mapper;
|
|
_localizer = localizer;
|
|
_languageService = languageService;
|
|
_faqService = faqService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt einen View für die Verwaltung von Faqs zurück
|
|
/// </summary>
|
|
/// <returns>View</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsPages)]
|
|
public IActionResult Index()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von Entitäten basierend auf Abfragekriterien zurück
|
|
/// </summary>
|
|
/// <param name="dm">Abfragekriterien</param>
|
|
/// <returns>Liste von gefundenen Entitäten</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsPages)]
|
|
[HttpPost]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public IActionResult GetFaqs([FromBody] DataManager dm)
|
|
{
|
|
if (dm != null)
|
|
{
|
|
var propList = new List<ComplexProperty>();
|
|
dm.SetComplexProperties(propList);
|
|
|
|
if (dm.Where != null)
|
|
{
|
|
foreach (var whereFilter in dm.Where)
|
|
{
|
|
if (whereFilter.predicates == null)
|
|
continue;
|
|
foreach (var whereFilterPredicate in whereFilter.predicates)
|
|
{
|
|
if (whereFilterPredicate.Field == "onlineStatus")
|
|
whereFilterPredicate.value = (OnlineStatus)((int)((long)whereFilterPredicate.value));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
var resultList = _faqService.FilterWithNames(dm?.SearchValue ?? "", SelectedLanguage, FallbackLanguage, false);
|
|
|
|
//Sortierung
|
|
resultList = dm.ApplySorting(resultList);
|
|
//Filter
|
|
resultList = dm.ApplyFiltering(resultList, out var countFiltered);
|
|
//Paging
|
|
resultList = dm.ApplyPaging(resultList);
|
|
|
|
var resultListVm = resultList.ToList().Select(item => _mapper.Map<FaqListVm>(item)).ToList();
|
|
|
|
foreach (var itemVm in resultListVm)
|
|
{
|
|
itemVm.OnlineStatusText = itemVm.OnlineStatus.GetDisplayName(AnnotationsLocalizer);
|
|
itemVm.TypeText = itemVm.Type.GetDisplayName(AnnotationsLocalizer);
|
|
}
|
|
|
|
//FilterPreview?
|
|
if (!dm.RequiresCounts)
|
|
return Json(resultListVm);
|
|
|
|
return Json(new { result = resultListVm, count = countFiltered });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Anlegen einer FAQ
|
|
/// </summary>
|
|
/// <returns>PartialView</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsPages)]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public async Task<IActionResult> Create()
|
|
{
|
|
var count = await _faqService.CountAsync();
|
|
var model = new FaqVm()
|
|
{
|
|
Id = Guid.NewGuid().ToString("N"),
|
|
TextVms = new List<FaqTextVm>(),
|
|
OnlineStatus = OnlineStatusVm.Offline,
|
|
Type = FaqTypeVm.Both,
|
|
Order = count + 1
|
|
};
|
|
|
|
foreach (var language in _languageService.GetAllIso2())
|
|
{
|
|
model.TextVms.Add(new FaqTextVm() { Id = model.Id, Language = language, Question = string.Empty, Answer = string.Empty });
|
|
}
|
|
|
|
return PartialView("_Create", model);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Anlegen einer FAQ
|
|
/// </summary>
|
|
/// <param name="model">Model</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsPages)]
|
|
[ValidateAntiForgeryToken]
|
|
[HttpPost]
|
|
public async Task<IActionResult> Create(FaqVm model)
|
|
{
|
|
var result = new ResponseVm { Success = false };
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
var item = _faqService.Create();
|
|
_mapper.Map(model, item);
|
|
item.Created = DateTimeOffset.UtcNow;
|
|
item.UpdatedAt = DateTimeOffset.UtcNow;
|
|
|
|
// Texte setzen
|
|
foreach (var textVm in model.TextVms)
|
|
{
|
|
item.Set("Question", textVm.Language, textVm.Question);
|
|
item.Set("Answer", textVm.Language, textVm.Answer);
|
|
}
|
|
|
|
_faqService.Add(item);
|
|
await _faqService.CommitAsync(User.Identity.Name);
|
|
|
|
result.Data = item.ToCamelCaseJson();
|
|
result.Success = true;
|
|
result.Html = string.Empty;
|
|
return Json(result);
|
|
}
|
|
|
|
result.Html = await PartialView("_Create", model).ToStringAsync(ControllerContext);
|
|
return Json(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bearbeiten einer FAQ
|
|
/// </summary>
|
|
/// <param name="id">Id der Kategorie</param>
|
|
/// <returns>PartialView</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsPages)]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public async Task<IActionResult> Edit(string id)
|
|
{
|
|
var item = await _faqService.GetAsync(id);
|
|
if (item != null)
|
|
{
|
|
var model = _mapper.Map<FaqVm>(item);
|
|
|
|
model.TextVms = new List<FaqTextVm>();
|
|
foreach (var language in _languageService.GetAllIso2())
|
|
{
|
|
model.TextVms.Add(new FaqTextVm()
|
|
{
|
|
Id = item.Id,
|
|
Language = language,
|
|
Question = item.Get("Question", language, true),
|
|
Answer = item.Get("Answer", language, true)
|
|
});
|
|
}
|
|
|
|
return PartialView("_Edit", model);
|
|
}
|
|
return PartialView("_Error");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bearbeiten einer FAQ
|
|
/// </summary>
|
|
/// <param name="model">Model</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsPages)]
|
|
[ValidateAntiForgeryToken]
|
|
[HttpPost]
|
|
public async Task<IActionResult> Edit(FaqVm model)
|
|
{
|
|
var result = new ResponseVm { Success = false };
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
var item = await _faqService.GetAsync(model.Id);
|
|
if (item is { Deleted: false })
|
|
{
|
|
if (item.Version.SequenceEqual(model.Version))
|
|
{
|
|
_mapper.Map(model, item);
|
|
|
|
//Texte setzen
|
|
foreach (var textVm in model.TextVms)
|
|
{
|
|
item.Set("Question", textVm.Language, textVm.Question);
|
|
item.Set("Answer", textVm.Language, textVm.Answer);
|
|
}
|
|
|
|
item.UpdatedAt = DateTimeOffset.UtcNow;
|
|
|
|
await _faqService.CommitAsync(User.Identity.Name);
|
|
|
|
result.Data = item.ToCamelCaseJson();
|
|
result.Success = true;
|
|
result.Html = string.Empty;
|
|
return Json(new { result.Success, result.Html, result.Data });
|
|
}
|
|
else
|
|
{
|
|
ModelState.AddModelError("", _localizer["Err_Entity_Changed"].Value);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ModelState.AddModelError("", _localizer["Err_Entity_Deleted"].Value);
|
|
}
|
|
}
|
|
|
|
result.Html = await PartialView("_Edit", model).ToStringAsync(ControllerContext);
|
|
return Json(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Löschen einer Faq
|
|
/// </summary>
|
|
/// <param name="ids">Liste Id der Entität</param>
|
|
/// <param name="forceDelete">Sollen die Daten physisch gelöscht werden?</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsPages)]
|
|
[HttpPost]
|
|
public async Task<IActionResult> Delete(List<string> ids, bool forceDelete = false)
|
|
{
|
|
var batchErrorHeader = $"<p><strong>{_localizer["Common_BatchDelete_Failed"].Value}</strong></p>";
|
|
var batchResult = new BatchResponseVm(batchErrorHeader) { Success = true };
|
|
|
|
foreach (var id in ids)
|
|
{
|
|
var item = await _faqService.GetAsync(id);
|
|
if (item != null)
|
|
{
|
|
//TODO: News berücksichtigen
|
|
//await _customerService.ResetTypeAsync(item.Id); //Spezial zurücksetzen wenn nötig
|
|
//await _advertisementService.DeleteByCategoryAsync(item.Id, forceDelete);
|
|
var usedCount = 0;
|
|
|
|
//var productsCount = await _productService.CountByAdvertisementCategoryAsync(item.Id);
|
|
//usedCount += productsCount;
|
|
|
|
if (usedCount == 0)
|
|
{
|
|
//await _productService.ResetAdvertisementCategoryAsync(item.Id, true);
|
|
|
|
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = item.Id.ToString(), Name = item.Title, Success = true, NotFound = false, ErrorMessage = "" });
|
|
|
|
if (forceDelete)
|
|
{
|
|
_faqService.Remove(item);
|
|
}
|
|
else
|
|
{
|
|
item.Deleted = true;
|
|
item.UpdatedAt = DateTimeOffset.UtcNow;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (usedCount != 0)
|
|
{
|
|
batchResult.Success = false;
|
|
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = id.ToString(), Name = item.Title, Success = false, NotFound = false, ErrorMessage = string.Format(_localizer["Common_BatchDelete_InUse"], $"<strong>{item.Title}</strong>", $"<strong>{usedCount}</strong>") + "<br/>" });
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
batchResult.Success = false;
|
|
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = id.ToString(), Name = "", Success = false, NotFound = true, ErrorMessage = string.Format(_localizer["Common_BatchDelete_NotFound"], $"<strong>{id}</strong>") + "<br/>" });
|
|
}
|
|
}
|
|
|
|
if (batchResult.BatchResponseList.Any(c => c.Success))
|
|
{
|
|
await _faqService.CommitAsync(User.Identity.Name);
|
|
}
|
|
|
|
return Json(batchResult);
|
|
}
|
|
}
|
|
}
|