gehgassi_backend/gehGassi.Web/Controllers/TransactionFeeController.cs

482 lines
19 KiB
C#

using System;
using AutoMapper;
using gehGassi.Common.Data;
using gehGassi.Permissions;
using gehGassi.Web.Auth;
using gehGassi.Web.Auth.Attributes;
using gehGassi.Web.Helper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using gehGassi.Core.Interfaces;
using gehGassi.Domain.Common;
using gehGassi.Web.Models;
using gehGassi.Core.Services;
namespace gehGassi.Web.Controllers
{
/// <summary>
/// Controller für die Verwaltung von Transaktionsgebühren
/// </summary>
[Authorize]
public class TransactionFeeController : BaseController
{
private readonly IMapper _mapper;
private readonly IStringLocalizer<TransactionFeeController> _localizer;
private readonly ITransactionFeeService _transactionFeeService;
/// <summary>
/// Erstellt eine Instanz
/// </summary>
/// <param name="mapper">Instanz eines IMapper</param>
/// <param name="localizer">Instanz eines IStringLocalizer</param>
/// <param name="transactionFeeService">Instanz eines ITransactionFeeService</param>
public TransactionFeeController(IMapper mapper, IStringLocalizer<TransactionFeeController> localizer, ITransactionFeeService transactionFeeService)
{
_mapper = mapper;
_localizer = localizer;
_transactionFeeService = transactionFeeService;
}
#region MangoPay Transaktionsgebühren
/// <summary>
/// Gibt einen View für die Verwaltung von Transaktionsgebühren zurück
/// </summary>
/// <returns>View</returns>
[Authorize(Policy = Policies.PowerUserOnly)]
[HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)]
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.SettingsTransactionFees)]
[HttpPost]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult GetList([FromBody] DataManager dm)
{
if (dm != null)
{
var propList = new List<ComplexProperty>();
dm.SetComplexProperties(propList);
}
var resultList = _transactionFeeService.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<TransactionFeeListVm>(item)).ToList();
foreach (var itemVm in resultListVm)
{
itemVm.PayInTypeText = itemVm.PayInType.GetDisplayName(AnnotationsLocalizer);
}
//FilterPreview?
if (!dm.RequiresCounts)
return Json(resultListVm);
return Json(new { result = resultListVm, count = countFiltered });
}
/// <summary>
/// Anlegen einer Transaktionsgebühr
/// </summary>
/// <returns>PartialView</returns>
[Authorize(Policy = Policies.PowerUserOnly)]
[HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Create()
{
var model = new TransactionFeeVm()
{
Id = Guid.NewGuid().ToString("N"),
PayInType = PayInTypeVm.CB_VISA_MASTERCARD,
Percent = 0,
Fixed = 0
};
return PartialView("_Create", model);
}
/// <summary>
/// Anlegen einer Transaktionsgebühr
/// </summary>
/// <param name="model">Model</param>
/// <returns>Json</returns>
[Authorize(Policy = Policies.PowerUserOnly)]
[HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)]
[ValidateAntiForgeryToken]
[HttpPost]
public async Task<IActionResult> Create(TransactionFeeVm model)
{
var result = new ResponseVm { Success = false };
if (ModelState.IsValid)
{
var exists = await _transactionFeeService.ExistsAsync((PayInType)model.PayInType);
if (!exists)
{
var item = _transactionFeeService.Create();
_mapper.Map(model, item);
item.UpdatedAt = DateTimeOffset.UtcNow;
_transactionFeeService.Add(item);
await _transactionFeeService.CommitAsync(User.Identity.Name);
result.Data = item.ToCamelCaseJson();
result.Success = true;
result.Html = string.Empty;
return Json(result);
}
else
{
ModelState.AddModelError("", _localizer["Err_TransactionFee_Exists"].Value);
}
}
result.Html = await PartialView("_Create", model).ToStringAsync(ControllerContext);
return Json(result);
}
/// <summary>
/// Bearbeiten einer Transaktionsgebühr
/// </summary>
/// <param name="id">Id der Transaktionsgebühr</param>
/// <returns>PartialView</returns>
[Authorize(Policy = Policies.PowerUserOnly)]
[HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public async Task<IActionResult> Edit(string id)
{
var item = await _transactionFeeService.GetAsync(id);
if (item != null)
{
var model = _mapper.Map<TransactionFeeVm>(item);
return PartialView("_Edit", model);
}
return PartialView("_Error");
}
/// <summary>
/// Bearbeiten einer Transaktionsgebühr
/// </summary>
/// <param name="model">Model</param>
/// <returns>Json</returns>
[Authorize(Policy = Policies.PowerUserOnly)]
[HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)]
[ValidateAntiForgeryToken]
[HttpPost]
public async Task<IActionResult> Edit(TransactionFeeVm model)
{
var result = new ResponseVm { Success = false };
if (ModelState.IsValid)
{
var item = await _transactionFeeService.GetAsync(model.Id);
if (item != null)
{
_mapper.Map(model, item);
item.UpdatedAt = DateTimeOffset.UtcNow;
await _transactionFeeService.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 Transaktionsgebühr
/// </summary>
/// <param name="ids">Liste Id der Entität</param>
/// <returns>Json</returns>
[Authorize(Policy = Policies.PowerUserOnly)]
[HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)]
[HttpPost]
public async Task<IActionResult> Delete(List<string> 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 _transactionFeeService.GetAsync(id);
if (item != null)
{
//await _customerService.ResetTypeAsync(item.Id); //Spezial zurücksetzen wenn nötig
var usedCount = 0;
if (usedCount == 0)
{
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = item.Id.ToString(), Name = item.PayInType.ToString(), Success = true, NotFound = false, ErrorMessage = "" });
item.Deleted = true;
item.UpdatedAt = DateTimeOffset.UtcNow;
}
else
{
if (usedCount != 0)
{
batchResult.Success = false;
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = id.ToString(), Name = item.PayInType.ToString(), Success = false, NotFound = false, ErrorMessage = string.Format(_localizer["Common_BatchDelete_InUse"], $"<strong>{item.PayInType.ToString()}</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 _transactionFeeService.CommitAsync(User.Identity.Name);
return Json(batchResult);
}
#endregion
#region GehGassi-Gebühren
/// <summary>
/// Gibt einen View für die Verwaltung von GehGassi-Gebühren zurück
/// </summary>
/// <returns>View</returns>
[Authorize(Policy = Policies.PowerUserOnly)]
[HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)]
public IActionResult IndexGehGassi()
{
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.SettingsTransactionFees)]
[HttpPost]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult GetListGehGassi([FromBody] DataManager dm)
{
if (dm != null)
{
var propList = new List<ComplexProperty>();
dm.SetComplexProperties(propList);
}
var resultList = _transactionFeeService.FilterGehGassiFees(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<GehGassiFeeVm>(item)).ToList();
//FilterPreview?
if (!dm.RequiresCounts)
return Json(resultListVm);
return Json(new { result = resultListVm, count = countFiltered });
}
/// <summary>
/// Anlegen einer GehGassi-Gebühr
/// </summary>
/// <returns>PartialView</returns>
[Authorize(Policy = Policies.PowerUserOnly)]
[HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult CreateGehGassi()
{
var model = new GehGassiFeeVm()
{
Id = Guid.NewGuid().ToString("N"),
Percent = 0,
Fixed = 0,
StartAmmount = 0
};
return PartialView("_CreateGehGassi", model);
}
/// <summary>
/// Anlegen einer GehGassi-Gebühr
/// </summary>
/// <param name="model">Model</param>
/// <returns>Json</returns>
[Authorize(Policy = Policies.PowerUserOnly)]
[HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)]
[ValidateAntiForgeryToken]
[HttpPost]
public async Task<IActionResult> CreateGehGassi(GehGassiFeeVm model)
{
var result = new ResponseVm { Success = false };
if (ModelState.IsValid)
{
var exists = await _transactionFeeService.ExistsGehGassiFeeAsync(model.StartAmmount, model.Id);
if (!exists)
{
var item = _transactionFeeService.CreateGehGassiFee();
_mapper.Map(model, item);
item.UpdatedAt = DateTimeOffset.UtcNow;
_transactionFeeService.AddGehGassiFee(item);
await _transactionFeeService.CommitAsync(User.Identity.Name);
result.Data = item.ToCamelCaseJson();
result.Success = true;
result.Html = string.Empty;
return Json(result);
}
else
{
ModelState.AddModelError("", _localizer["Err_GehGassiFee_Exists"].Value);
}
}
result.Html = await PartialView("_CreateGehGassi", model).ToStringAsync(ControllerContext);
return Json(result);
}
/// <summary>
/// Bearbeiten einer GehGassi-Gebühr
/// </summary>
/// <param name="id">Id der GehGassi-Gebühr</param>
/// <returns>PartialView</returns>
[Authorize(Policy = Policies.PowerUserOnly)]
[HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public async Task<IActionResult> EditGehGassi(string id)
{
var item = await _transactionFeeService.GetGehGassiFeeAsync(id);
if (item != null)
{
var model = _mapper.Map<GehGassiFeeVm>(item);
return PartialView("_EditGehGassi", model);
}
return PartialView("_Error");
}
/// <summary>
/// Bearbeiten einer GehGassi-Gebühr
/// </summary>
/// <param name="model">Model</param>
/// <returns>Json</returns>
[Authorize(Policy = Policies.PowerUserOnly)]
[HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)]
[ValidateAntiForgeryToken]
[HttpPost]
public async Task<IActionResult> EditGehGassi(GehGassiFeeVm model)
{
var result = new ResponseVm { Success = false };
if (ModelState.IsValid)
{
var exists = await _transactionFeeService.ExistsGehGassiFeeAsync(model.StartAmmount, model.Id);
if (!exists)
{
var item = await _transactionFeeService.GetGehGassiFeeAsync(model.Id);
if (item != null)
{
_mapper.Map(model, item);
item.UpdatedAt = DateTimeOffset.UtcNow;
await _transactionFeeService.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_GehGassiFee_Exists"].Value);
}
}
result.Html = await PartialView("_EditGehGassi", model).ToStringAsync(ControllerContext);
return Json(result);
}
/// <summary>
/// Löschen einer GehGassi-Gebühr
/// </summary>
/// <param name="ids">Liste Id der Entität</param>
/// <returns>Json</returns>
[Authorize(Policy = Policies.PowerUserOnly)]
[HasPermission(Permission.SettingsManage, Permission.SettingsTransactionFees)]
[HttpPost]
public async Task<IActionResult> DeleteGehGassi(List<string> 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 _transactionFeeService.GetGehGassiFeeAsync(id);
if (item != null)
{
//await _customerService.ResetTypeAsync(item.Id); //Spezial zurücksetzen wenn nötig
var usedCount = 0;
if (usedCount == 0)
{
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = item.Id.ToString(), Name = item.StartAmmount.ToString(), Success = true, NotFound = false, ErrorMessage = "" });
item.Deleted = true;
item.UpdatedAt = DateTimeOffset.UtcNow;
}
else
{
if (usedCount != 0)
{
batchResult.Success = false;
batchResult.BatchResponseList.Add(new BatchResponseItemVm() { Id = id.ToString(), Name = item.StartAmmount.ToString(), Success = false, NotFound = false, ErrorMessage = string.Format(_localizer["Common_BatchDelete_InUse"], $"<strong>{item.StartAmmount.ToString()}</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 _transactionFeeService.CommitAsync(User.Identity.Name);
return Json(batchResult);
}
#endregion
}
}