217 lines
8.8 KiB
C#

using AutoMapper;
using gehGassi.Core.Interfaces;
using gehGassi.Core.Services;
using gehGassi.Permissions;
using gehGassi.Web.Auth.Attributes;
using gehGassi.Web.Auth;
using gehGassi.Web.Hubs;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using gehGassi.Common.Data;
using gehGassi.Domain.Common;
using System.Collections.Generic;
using System.Linq;
using gehGassi.Web.Helper;
using gehGassi.Web.Models;
using System.Threading.Tasks;
using System;
using gehGassi.Web.Services;
using System.IO;
namespace gehGassi.Web.Controllers
{
/// <summary>
/// Controller für die Verwaltung von Feedback
/// </summary>
[Authorize]
public class FeedbackController : BaseController
{
private readonly IMapper _mapper;
private readonly IStringLocalizer<FeedbackController> _localizer;
private readonly ILanguageService _languageService;
private readonly IAppFeedbackService _appFeedbackService;
/// <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="appFeedbackService">Instanz eines IAppFeedbackService</param>
public FeedbackController(IMapper mapper, IStringLocalizer<FeedbackController> localizer, ILanguageService languageService, IAppFeedbackService appFeedbackService)
{
_mapper = mapper;
_localizer = localizer;
_languageService = languageService;
_appFeedbackService = appFeedbackService;
}
/// <summary>
/// Gibt einen View für die Verwaltung von App-Feedback zurück
/// </summary>
/// <returns>View</returns>
[Authorize(Policy = Policies.PowerUserOnly)]
[HasPermission(Permission.SettingsManage, Permission.SettingsFeedback)]
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.SettingsFeedback)]
[HttpPost]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult GetFeedbacks([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 == "feedbackType")
whereFilterPredicate.value = (AppFeedbackType)((int)((long)whereFilterPredicate.value));
if (whereFilterPredicate.Field == "status")
whereFilterPredicate.value = (AppFeedbackStatus)((int)((long)whereFilterPredicate.value));
}
}
}
}
var resultList = _appFeedbackService.Filter(dm?.SearchValue ?? "");
//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<AppFeedbackListVm>(item)).ToList();
foreach (var itemVm in resultListVm)
{
itemVm.FeedbackTypeText = itemVm.FeedbackType.GetDisplayName(AnnotationsLocalizer);
itemVm.StatusText = itemVm.Status.GetDisplayName(AnnotationsLocalizer);
itemVm.AppModeText = itemVm.AppMode.GetDisplayName(AnnotationsLocalizer);
}
//FilterPreview?
if (!dm.RequiresCounts)
return Json(resultListVm);
return Json(new { result = resultListVm, count = countFiltered });
}
/// <summary>
/// Details eines Feedbacks
/// </summary>
/// <param name="id">Id des Feedbacks</param>
/// <returns>PartialView</returns>
[Authorize(Policy = Policies.PowerUserOnly)]
[HasPermission(Permission.SettingsManage, Permission.SettingsFeedback)]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public async Task<IActionResult> Details(long id)
{
var item = await _appFeedbackService.GetAsync(id);
if (item != null)
{
var model = _mapper.Map<AppFeedbackVm>(item);
return PartialView("_Details", model);
}
return PartialView("_Error");
}
/// <summary>
/// Details eines Feedbacks
/// </summary>
/// <param name="model">Model</param>
/// <returns>Json</returns>
[Authorize(Policy = Policies.PowerUserOnly)]
[HasPermission(Permission.SettingsManage, Permission.SettingsFeedback)]
[ValidateAntiForgeryToken]
[HttpPost]
public async Task<IActionResult> Details(AppFeedbackVm model)
{
var result = new ResponseVm { Success = false };
if (ModelState.IsValid)
{
var item = await _appFeedbackService.GetAsync(model.Id);
item.Status = (AppFeedbackStatus)model.Status;
await _appFeedbackService.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("_Details", model).ToStringAsync(ControllerContext);
return Json(result);
}
/// <summary>
/// Löschen von Feedback
/// </summary>
/// <param name="ids">Liste Id der Entität</param>
/// <returns>Json</returns>
[Authorize(Policy = Policies.PowerUserOnly)]
[HasPermission(Permission.SettingsManage, Permission.SettingsFeedback)]
[HttpPost]
public async Task<IActionResult> Delete(List<long> 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 _appFeedbackService.GetAsync(id);
if (item != null)
{
var usedCount = 0;
if (usedCount == 0)
{
//TODO: Gibt es Resets die notwendig sind?
//await _productService.ResetAdvertisementCategoryAsync(item.Id, true);
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = item.Id.ToString(), Name = item.Name, Success = true, NotFound = false, ErrorMessage = "" });
_appFeedbackService.Remove(item);
}
else
{
if (usedCount != 0)
{
batchResult.Success = false;
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = id.ToString(), Name = item.Name, Success = false, NotFound = false, ErrorMessage = string.Format(_localizer["Common_BatchDelete_InUse"], $"<strong>{item.Name}</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 _appFeedbackService.CommitAsync(User.Identity.Name);
return Json(batchResult);
}
}
}