408 lines
16 KiB
C#
408 lines
16 KiB
C#
using AutoMapper;
|
|
using gehGassi.Core.Interfaces;
|
|
using gehGassi.Permissions;
|
|
using gehGassi.Web.Auth.Attributes;
|
|
using gehGassi.Web.Auth;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Localization;
|
|
using gehGassi.Common.Data;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using gehGassi.Web.Models;
|
|
using gehGassi.Core.Services;
|
|
using System.Threading.Tasks;
|
|
using System;
|
|
using gehGassi.Domain.Common;
|
|
using gehGassi.Web.Helper;
|
|
using System.IO;
|
|
using Microsoft.Extensions.Options;
|
|
using gehGassi.Domain.Users;
|
|
|
|
namespace gehGassi.Web.Controllers
|
|
{
|
|
/// <summary>
|
|
/// Controller für die Verwaltung von Text-Seiten
|
|
/// </summary>
|
|
[Authorize]
|
|
public class PageController : BaseController
|
|
{
|
|
private readonly IMapper _mapper;
|
|
private readonly IStringLocalizer<ProductController> _localizer;
|
|
private readonly ILanguageService _languageService;
|
|
private readonly IPageService _pageService;
|
|
private readonly IFaqService _faqService;
|
|
private readonly ICoinsService _coinsService;
|
|
private readonly IUserService _userService;
|
|
private readonly IAppUserService _appUserService;
|
|
private readonly IOptions<MinCoinsForPot> _minCoinsOptions;
|
|
private readonly IOptions<CoinsOptions> _coinsOptions;
|
|
|
|
/// <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="pageService">Instanz eines IPageService</param>
|
|
/// <param name="faqService">Instanz eines IFaqService</param>
|
|
/// <param name="coinsService">Instanz eines ICoinsService</param>
|
|
/// <param name="userService">Instanz eines IUserService</param>
|
|
/// <param name="appUserService">Instanz eines IAppUserService</param>
|
|
/// <param name="minCoinsOptions">MinCoinsForPot</param>
|
|
/// <param name="coinsOptions">CoinsOptions</param>
|
|
public PageController(IMapper mapper, IStringLocalizer<ProductController> localizer, ILanguageService languageService, IPageService pageService, IFaqService faqService, ICoinsService coinsService,
|
|
IUserService userService, IAppUserService appUserService, IOptions<MinCoinsForPot> minCoinsOptions, IOptions<CoinsOptions> coinsOptions)
|
|
{
|
|
_mapper = mapper;
|
|
_localizer = localizer;
|
|
_languageService = languageService;
|
|
_pageService = pageService;
|
|
_faqService = faqService;
|
|
_coinsService = coinsService;
|
|
_userService = userService;
|
|
_appUserService = appUserService;
|
|
_minCoinsOptions = minCoinsOptions;
|
|
_coinsOptions = coinsOptions;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt einen View für die Text-Seiten-Verwaltung 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 GetPages([FromBody] DataManager dm)
|
|
{
|
|
if (dm != null)
|
|
{
|
|
var propList = new List<ComplexProperty>();
|
|
dm.SetComplexProperties(propList);
|
|
}
|
|
|
|
var resultList = _pageService.FilterWithNames(dm?.SearchValue ?? "", SelectedLanguage, FallbackLanguage);
|
|
|
|
//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<PageListVm>(item)).ToList();
|
|
|
|
//FilterPreview?
|
|
if (!dm.RequiresCounts)
|
|
return Json(resultListVm);
|
|
|
|
return Json(new { result = resultListVm, count = countFiltered });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Anlegen eine Text-Seite
|
|
/// </summary>
|
|
/// <returns>PartialView</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsPages)]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public IActionResult Create()
|
|
{
|
|
var model = new PageVm()
|
|
{
|
|
TextVms = new List<PageTextVm>()
|
|
};
|
|
|
|
foreach (var language in _languageService.GetAllIso2())
|
|
{
|
|
model.TextVms.Add(new PageTextVm() { Id = model.Id, Language = language, Content = string.Empty});
|
|
}
|
|
|
|
return PartialView("_Create", model);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Anlegen einer Test-Seite
|
|
/// </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(PageVm model)
|
|
{
|
|
var result = new ResponseVm { Success = false };
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
var item = _pageService.Create();
|
|
_mapper.Map(model, item);
|
|
item.Code = item.Code.ToUpperInvariant();
|
|
|
|
// Texte setzen
|
|
foreach (var textVm in model.TextVms)
|
|
{
|
|
item.Set("Content", textVm.Language, textVm.Content);
|
|
}
|
|
|
|
_pageService.Add(item);
|
|
await _pageService.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 text-Seite
|
|
/// </summary>
|
|
/// <param name="id">Id des Produktes</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(int id)
|
|
{
|
|
var item = await _pageService.GetAsync(id);
|
|
if (item != null)
|
|
{
|
|
var model = _mapper.Map<PageVm>(item);
|
|
|
|
model.TextVms = new List<PageTextVm>();
|
|
foreach (var language in _languageService.GetAllIso2())
|
|
{
|
|
model.TextVms.Add(new PageTextVm()
|
|
{
|
|
Id = item.Id,
|
|
Language = language,
|
|
Content = item.Get("Content", language, true)
|
|
});
|
|
}
|
|
|
|
return PartialView("_Edit", model);
|
|
}
|
|
return PartialView("_Error");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bearbeiten einer Text-Seite
|
|
/// </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(PageVm model)
|
|
{
|
|
var result = new ResponseVm { Success = false };
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
var item = await _pageService.GetAsync(model.Id);
|
|
|
|
_mapper.Map(model, item);
|
|
item.Code = item.Code.ToUpperInvariant();
|
|
|
|
//Texte setzen
|
|
foreach (var textVm in model.TextVms)
|
|
{
|
|
item.Set("Content", textVm.Language, textVm.Content);
|
|
}
|
|
|
|
await _pageService.CommitAsync(User.Identity.Name);
|
|
|
|
result.Data = item.ToCamelCaseJson();
|
|
result.Success = true;
|
|
result.Html = string.Empty;
|
|
return Json(new { result.Success, result.Html, result.Data });
|
|
}
|
|
|
|
result.Html = await PartialView("_Edit", model).ToStringAsync(ControllerContext);
|
|
return Json(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Löschen einer Text-Seite
|
|
/// </summary>
|
|
/// <param name="ids">Liste Id der Entität</param>
|
|
/// <returns>Json</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.SettingsManage, Permission.SettingsProductCategories)]
|
|
[HttpPost]
|
|
public async Task<IActionResult> Delete(List<int> ids)
|
|
{
|
|
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 _pageService.GetAsync(id);
|
|
if (item != null)
|
|
{
|
|
var useCount = 0;
|
|
if (useCount == 0)
|
|
{
|
|
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = item.Id.ToString(), Name = item.Title, Success = true, NotFound = false, ErrorMessage = "" });
|
|
|
|
_pageService.Remove(item);
|
|
}
|
|
else
|
|
{
|
|
if (useCount != 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>{useCount}</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 _pageService.CommitAsync(User.Identity.Name);
|
|
return Json(batchResult);
|
|
}
|
|
|
|
#region App
|
|
|
|
/// <summary>
|
|
/// Anzeigen der Text-Seite für die App
|
|
/// </summary>
|
|
/// <param name="code">Code der Seite</param>
|
|
/// <param name="language">Gewünschte Sprache</param>
|
|
/// <param name="dark">Darkmode?</param>
|
|
/// <param name="faqType">Wenn FAQ welcher Typ?</param>
|
|
/// <returns></returns>
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> Show(string code, string language, bool dark = false, FaqType faqType = FaqType.Both)
|
|
{
|
|
var page = await _pageService.GetwithNamesAsync(code, language, FallbackLanguage);
|
|
if (page != null)
|
|
{
|
|
page.SetLanguage(language.ToUpperInvariant());
|
|
ViewBag.DarkMode = dark;
|
|
var model = _mapper.Map<PageViewVm>(page);
|
|
|
|
if (code.ToUpper() == "FAQ" && !string.IsNullOrWhiteSpace(model.Content) && model.Content.Contains("[FAQ]"))
|
|
{
|
|
var faqHtml = await _faqService.GenerateHtmlForPageAsync(language, FallbackLanguage, faqType);
|
|
model.Content = model.Content.Replace("[FAQ]", faqHtml);
|
|
}
|
|
|
|
return View(model);
|
|
}
|
|
|
|
return NotFound();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Anzeigen der Seite für die App für den Monatswettbewerb der Dogwalker
|
|
/// </summary>
|
|
/// <param name="year">Jahr</param>
|
|
/// <param name="month">Monat</param>
|
|
/// <param name="a">Id des AppUsers</param>
|
|
/// <param name="dark">Darkmode?</param>
|
|
/// <returns></returns>
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> ShowCompetitionMonthWalker(int? year, int? month, string a = "", bool dark = false)
|
|
{
|
|
year ??= DateTimeOffset.UtcNow.Year;
|
|
month ??= DateTimeOffset.UtcNow.Month;
|
|
|
|
ViewBag.DarkMode = dark;
|
|
ViewBag.Year = year;
|
|
ViewBag.Month = month;
|
|
ViewBag.MinCoins = _minCoinsOptions.Value.DogWalkerMonthly;
|
|
ViewBag.CoinsOptions = _coinsOptions.Value;
|
|
|
|
var topList = await _coinsService.GetMonthlyTopAsync(10, AppUserType.DogWalker, year.Value, month.Value, _minCoinsOptions.Value.DogWalkerMonthly);
|
|
|
|
if (!string.IsNullOrWhiteSpace(a))
|
|
{
|
|
var appUser = await _appUserService.GetAsync(a);
|
|
if (appUser is { Type: AppUserType.DogWalker or AppUserType.Both })
|
|
{
|
|
ViewBag.AppUser = appUser;
|
|
var myCoins = await _coinsService.GetOrCreateCoinsPerMonthAsync(appUser.Id, AppUserType.DogWalker, new DateTimeOffset(year.Value, month.Value, 1, 0, 0, 0, TimeSpan.Zero));
|
|
ViewBag.MyCoins = myCoins;
|
|
}
|
|
}
|
|
|
|
return View(topList);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Anzeigen der Seite für die App für den Jahrewettbewerb der Hundebesitzer
|
|
/// </summary>
|
|
/// <param name="year">Jahr</param>
|
|
/// <param name="a">Id des AppUsers</param>
|
|
/// <param name="dark">Darkmode?</param>
|
|
/// <returns></returns>
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> ShowCompetitionYearOwner(int? year, string a = "", bool dark = false)
|
|
{
|
|
year ??= DateTimeOffset.UtcNow.Year;
|
|
|
|
ViewBag.DarkMode = dark;
|
|
ViewBag.Year = year;
|
|
ViewBag.MinCoins = _minCoinsOptions.Value.OwnerYearly;
|
|
ViewBag.CoinsOptions = _coinsOptions.Value;
|
|
|
|
var topList = await _coinsService.GetYearlyTopAsync(10, AppUserType.DogOwner, year.Value, _minCoinsOptions.Value.OwnerYearly);
|
|
|
|
if (!string.IsNullOrWhiteSpace(a))
|
|
{
|
|
var appUser = await _appUserService.GetAsync(a);
|
|
if (appUser is { Type: AppUserType.DogOwner or AppUserType.Both })
|
|
{
|
|
ViewBag.AppUser = appUser;
|
|
var myCoins = await _coinsService.GetOrCreateCoinsPerYearAsync(appUser.Id, AppUserType.DogOwner, new DateTimeOffset(year.Value, 1, 1, 0, 0, 0, TimeSpan.Zero));
|
|
ViewBag.MyCoins = myCoins;
|
|
}
|
|
}
|
|
|
|
return View(topList);
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
#region Helper
|
|
|
|
/// <summary>
|
|
/// Überprüft ob ein Code für eine Text-Seite noch verfügbar ist
|
|
/// </summary>
|
|
/// <returns>true wenn möglich, false sonst</returns>
|
|
[HttpPost]
|
|
public async Task<IActionResult> IsCodeAvailable(string code, int id)
|
|
{
|
|
return Json(await _pageService.IsCodeAvailableAsync(code, id));
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|