using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Dynamic; using System.Globalization; using System.IO; using System.Linq; using System.Threading.Tasks; using AutoMapper; using gehGassi.Core.Interfaces; using gehGassi.Domain.Common; using gehGassi.Web.Auth; using gehGassi.Web.Auth.Attributes; using gehGassi.Web.Helper; using gehGassi.Web.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Localization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Routing; namespace gehGassi.Web.Controllers { /// /// Controller für die Verwaltung des Dashboards je nach Rolle /// [Authorize] public class HomeController : BaseController { public static ConcurrentBag bs = new ConcurrentBag(); private readonly IUserService _userService; private readonly IMapper _mapper; private readonly ICustomerService _customerService; private readonly IAppUserService _appUserService; private readonly IListingService _listingService; private readonly IAdvertisementService _advertisementService; private readonly IBannerService _bannerService; private readonly IPinService _pinService; private readonly IWebHostEnvironment _environment; private readonly IUrlHelperFactory _urlHelperFactory; private readonly IOrderService _orderService; private readonly IInvoiceService _invoiceService; private readonly IAppUserReportService _appUserReportService; private readonly IWalkComplaintService _walkComplaintService; private readonly IAppFeedbackService _appFeedbackService; /// /// Erstellt eine Instanz /// /// Instanz eines IUserService /// Instanz eines IMapper /// Instanz eines ICustomerService /// Instanz eines IDogOwnerService /// Instanz eines IListingService /// Instanz eines IAdvertisementService /// Instanz eines IBannerService /// Instanz eines IPinService /// Instanz eines IWebHostEnvironment /// Instanz einer IUrlHelperFactory /// Instanz eines IOrderService /// Instanz eines IInvoiceService /// Instanz eines IAppUserReportService /// Instanz eines IWalkComplaintService /// Instanz eines IAppFeedbackService public HomeController(IUserService userService, IMapper mapper, ICustomerService customerService, IAppUserService appUserService, IListingService listingService, IAdvertisementService advertisementService, IBannerService bannerService, IPinService pinService, IWebHostEnvironment environment, IUrlHelperFactory urlHelperFactory, IOrderService orderService, IInvoiceService invoiceService, IAppUserReportService appUserReportService, IWalkComplaintService walkComplaintService, IAppFeedbackService appFeedbackService) : base() { _userService = userService; _mapper = mapper; _customerService = customerService; _appUserService = appUserService; _listingService = listingService; _advertisementService = advertisementService; _bannerService = bannerService; _pinService = pinService; _environment = environment; _urlHelperFactory = urlHelperFactory; _orderService = orderService; _invoiceService = invoiceService; _appUserReportService = appUserReportService; _walkComplaintService = walkComplaintService; _appFeedbackService = appFeedbackService; } //[AllowAnonymous] //public async Task Test(long orderId) //{ // var order = await _orderService.GetAsync(orderId); // var invoice = await _invoiceService.GetByOrderAsync(orderId); // var fileName = $"{invoice.Number}.pdf"; // var file = ControllerContext.GetPdfWithHeaderAndFooter(_urlHelperFactory, _environment, "Print", "CreditNote", new { orderId = order.Id, language = SelectedLanguage }); // var outputStream = new MemoryStream(); // outputStream.Write(file, 0, file.Length); // outputStream.Position = 0; // return File(file, "application/pdf"); //} /// /// Dash-Board View /// /// View [Authorize(Policy = Policies.PowerUserOnly)] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public async Task Index() { var users = await _userService.CountAsync(); var customers = await _customerService.CountAsync(); var appUsers = await _appUserService.CountAsync(false); var listingsTotal = await _listingService.CountAsync(false, true); var listingsRunning = await _listingService.CountAsync(ListingStatus.Running, false); var advertisementTotal = await _advertisementService.CountAsync(false, true); var advertisementsRunning = await _advertisementService.CountAsync(AdvertisementStatus.Running, false); var bannersTotal = await _bannerService.CountAsync(false, true); var bannersRunning = await _bannerService.CountAsync(BannerStatus.Running, false); var pinsTotal = await _pinService.CountAsync(false, true); var pinsRunning = await _pinService.CountAsync(PinStatus.Running, false); var appUserReportsTotal = await _appUserReportService.CountAsync(); var appUserReportsOpen = await _appUserReportService.CountAsync(AppUserReportStatus.Open); appUserReportsOpen += await _appUserReportService.CountAsync(AppUserReportStatus.Processing); var complaintsTotal = await _walkComplaintService.CountAsync(); var complaintssOpen = await _walkComplaintService.CountAsync(WalkComplaintStatus.Open); complaintssOpen += await _walkComplaintService.CountAsync(WalkComplaintStatus.InProgress); var feedbackTotal = await _appFeedbackService.CountAsync(); var feedbackOpen = await _appFeedbackService.CountAsync(AppFeedbackStatus.Open); dynamic model = new ExpandoObject(); model.Users = users; model.Customers = customers; model.DogOwners = appUsers; model.AppUsers = appUsers; model.ListingsTotal = listingsTotal; model.ListingsRunning = listingsRunning; model.AdvertisementsTotal = advertisementTotal; model.AdvertisementsRunning = advertisementsRunning; model.BannersTotal = bannersTotal; model.BannersRunning = bannersRunning; model.PinsTotal = pinsTotal; model.PinsRunning = pinsRunning; model.AppUserReportsTotal = appUserReportsTotal; model.AppUserReportsOpen = appUserReportsOpen; model.ComplaintsTotal = complaintsTotal; model.ComplaintssOpen = complaintssOpen; model.FeedbackTotal = feedbackTotal; model.FeedbackOpen = feedbackOpen; return View(model); } /// /// Dash-Board View Kunden /// /// View [Authorize(Policy = Policies.CustomerOnly)] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult IndexCustomer() { return View(); } /// /// Dash-Board View App-User /// /// View [Authorize(Policy = Policies.AppUserOnly)] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult IndexAppUser() { return View(); } #region Common [AllowAnonymous] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorVm { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } [AllowAnonymous] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Status401() { return View(); } [AllowAnonymous] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Status403() { return View(); } [AllowAnonymous] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Status404() { return View(); } /// /// Setzen der neue gewählten Sprache /// /// gewünschte Sprache /// Return Url /// Redirect [HttpPost] [AllowAnonymous] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult SetLanguage(string culture, string returnUrl) { if(!IsValidCulture(culture)) return RedirectToAction("Login", "Account"); Response.Cookies.Append( CookieRequestCultureProvider.DefaultCookieName, CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)), new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) } ); var currentCultureIso = CultureInfo.CurrentCulture.TwoLetterISOLanguageName; CultureInfo.CurrentCulture = new CultureInfo(culture); if (string.IsNullOrWhiteSpace(returnUrl)) returnUrl = "/"; if (!Uri.IsWellFormedUriString(returnUrl, UriKind.RelativeOrAbsolute)) returnUrl = "/"; if(returnUrl.ToLower().Contains("setlanguage")) returnUrl = "/"; if (returnUrl.Contains($"/{currentCultureIso}/")) returnUrl = returnUrl.Replace($"/{currentCultureIso}/", $"/{culture}/"); else if (returnUrl.Length <= 2 || returnUrl == $"/{currentCultureIso}") returnUrl = $"~/{culture}/"; //else if (returnUrl.StartsWith("/") && !returnUrl.Contains($"/{currentCultureIso}/")) // returnUrl = $"/{culture}{returnUrl}"; return LocalRedirect(returnUrl); } /// /// Prüfen ob ein Eingabestring eine gültige Culture repräsentiert /// /// Zu prüfende Culture /// true wenn exisitert, false sonst private bool IsValidCulture(string culture) { return CultureInfo .GetCultures(CultureTypes.SpecificCultures) .Any(c => c.TwoLetterISOLanguageName == culture); } #endregion } }