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 gehGassi.Domain.Common; using gehGassi.Web.Helper; using System.Collections.Generic; using System.Linq; using gehGassi.Web.Models; using gehGassi.Core.Services; using gehGassi.Domain.Dogs; using System.Text.Json; using System.Threading.Tasks; namespace gehGassi.Web.Controllers { /// /// Controller für die Verwaltung von Konversationen /// [Authorize] public class ConversationController : BaseController { private readonly IMapper _mapper; private readonly IStringLocalizer _localizer; private readonly IMessageService _messageService; private readonly IAppUserService _appUserService; /// /// Erstellt eine Instanz /// /// Instanz eines IMapper /// Instanz eines IStringLocalizer /// Instanz eines IMessageService /// Instanz eines IAppUserService public ConversationController(IMapper mapper, IStringLocalizer localizer, IMessageService messageService, IAppUserService appUserService) { _mapper = mapper; _localizer = localizer; _messageService = messageService; _appUserService = appUserService; } /// /// Gibt einen View für die Konversations-Verwaltung zurück /// /// View [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.AppUsersManage, Permission.AppUsersRead)] public IActionResult Index() { return View(); } /// /// Gibt eine Liste von Entitäten basierend auf Abfragekriterien zurück /// /// Abfragekriterien /// Liste von gefundenen Entitäten [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.AppUsersManage, Permission.AppUsersRead)] [HttpPost] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult GetConversations([FromBody] DataManager dm) { if (dm != null) { var propList = new List(); 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 == "senderType") whereFilterPredicate.value = (AppUserType)((int)((long)whereFilterPredicate.value)); if (whereFilterPredicate.Field == "receiverType") whereFilterPredicate.value = (AppUserType)((int)((long)whereFilterPredicate.value)); } } } } var resultList = _messageService.FilterConversationsWithNames(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(item)).ToList(); foreach (var itemVm in resultListVm) { itemVm.SenderTypeText = itemVm.SenderType.GetDisplayName(AnnotationsLocalizer); itemVm.ReceiverTypeText = itemVm.ReceiverType.GetDisplayName(AnnotationsLocalizer); itemVm.SenderPhoto = Tools.GetPhotoThumb(itemVm.SenderPhoto, 100); itemVm.ReceiverPhoto = Tools.GetPhotoThumb(itemVm.ReceiverPhoto, 100); } //FilterPreview? if (!dm.RequiresCounts) return Json(resultListVm); return Json(new { result = resultListVm, count = countFiltered }); } /// /// Details einer Konversation der letzten 7 Tage /// /// Id des Senders oder Empfängers /// Id des Senders oder Empfängers /// PartialView [Authorize(Policy = Policies.PowerUserOnly)] [HasPermission(Permission.AppUsersManage, Permission.AppUsersRead)] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public async Task Details(string senderId, string receiverId) { var conversation = await _messageService.GetConversationAsync(senderId, receiverId); if (conversation != null) { var namesDict = new Dictionary(); var appUserSender = await _appUserService.GetAsync(conversation.SenderId); var appUserReceiver = await _appUserService.GetAsync(conversation.ReceiverId); namesDict.Add(appUserSender.Id, $"{appUserSender.FirstName} {appUserSender.LastName}"); namesDict.Add(appUserReceiver.Id, $"{appUserReceiver.FirstName} {appUserReceiver.LastName}"); var messages = await _messageService.GetMessageCopiesAsync(conversation.Id); var model = _mapper.Map>(messages); foreach (var conversationListVm in model) { conversationListVm.SenderName = namesDict[conversationListVm.SenderId]; conversationListVm.ReceiverName = namesDict[conversationListVm.ReceiverId]; } return PartialView("_Details", model); } return PartialView("_Error"); } } }