gehgassi_backend/gehGassi.Web/Controllers/ShopSettingsController.cs

103 lines
3.6 KiB
C#

using System.Threading.Tasks;
using AutoMapper;
using gehGassi.Core.Interfaces;
using gehGassi.Core.Services;
using gehGassi.Permissions;
using gehGassi.Web.Auth;
using gehGassi.Web.Auth.Attributes;
using gehGassi.Web.Helper;
using gehGassi.Web.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
namespace gehGassi.Web.Controllers
{
/// <summary>
/// Controller für die Verwaltung der Shop-Einstellungen
/// </summary>
public class ShopSettingsController : BaseController
{
private readonly IMapper _mapper;
private readonly IStringLocalizer<ShopSettingsController> _localizer;
private readonly IShopSettingsService _shopSettingsService;
/// <summary>
/// Erstellt eine Instanz
/// </summary>
/// <param name="mapper">Instanz eines IMapper</param>
/// <param name="localizer">Instanz eines IStringLocalizer</param>
/// <param name="shopSettingsService">Instanz eines IShipmentCostService</param>
public ShopSettingsController(IMapper mapper, IStringLocalizer<ShopSettingsController> localizer, IShopSettingsService shopSettingsService)
{
_mapper = mapper;
_localizer = localizer;
_shopSettingsService = shopSettingsService;
}
/// <summary>
/// Gibt einen View für die Verwaltung von Shopeinstellungen zurück
/// </summary>
/// <returns>View</returns>
[Authorize(Policy = Policies.PowerUserOnly)]
[HasPermission(Permission.SettingsManage, Permission.SettingsShop)]
public IActionResult Index()
{
return View();
}
/// <summary>
/// Bearbeiten der Shop-Einstellungen
/// </summary>
/// <returns>PartialView</returns>
[Authorize(Policy = Policies.PowerUserOnly)]
[HasPermission(Permission.SettingsManage, Permission.SettingsShop)]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public async Task<IActionResult> Edit()
{
var item = await _shopSettingsService.GetAsync();
if (item != null)
{
var model = _mapper.Map<ShopSettingsVm>(item);
return PartialView("_Edit", model);
}
return PartialView("_Error");
}
/// <summary>
/// Bearbeiten der Shop-Einstellungen
/// </summary>
/// <param name="model">Model</param>
/// <returns>Json</returns>
[Authorize(Policy = Policies.PowerUserOnly)]
[HasPermission(Permission.CustomersManage, Permission.SettingsShop)]
[ValidateAntiForgeryToken]
[HttpPost]
public async Task<IActionResult> Edit(ShopSettingsVm model)
{
var result = new ResponseVm { Success = false };
if (ModelState.IsValid)
{
var item = await _shopSettingsService.GetAsync();
if (item != null)
{
_mapper.Map(model, item);
await _shopSettingsService.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);
}
}
}