155 lines
6.4 KiB
C#
155 lines
6.4 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// Controller für die Verwaltung von Konversationen
|
|
/// </summary>
|
|
[Authorize]
|
|
public class ConversationController : BaseController
|
|
{
|
|
private readonly IMapper _mapper;
|
|
private readonly IStringLocalizer<ConversationController> _localizer;
|
|
private readonly IMessageService _messageService;
|
|
private readonly IAppUserService _appUserService;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="mapper">Instanz eines IMapper</param>
|
|
/// <param name="localizer">Instanz eines IStringLocalizer</param>
|
|
/// <param name="messageService">Instanz eines IMessageService</param>
|
|
/// <param name="appUserService">Instanz eines IAppUserService</param>
|
|
public ConversationController(IMapper mapper, IStringLocalizer<ConversationController> localizer, IMessageService messageService, IAppUserService appUserService)
|
|
{
|
|
_mapper = mapper;
|
|
_localizer = localizer;
|
|
_messageService = messageService;
|
|
_appUserService = appUserService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt einen View für die Konversations-Verwaltung zurück
|
|
/// </summary>
|
|
/// <returns>View</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.AppUsersManage, Permission.AppUsersRead)]
|
|
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.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<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 == "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<ConversationListVm>(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 });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Details einer Konversation der letzten 7 Tage
|
|
/// </summary>
|
|
/// <param name="senderId">Id des Senders oder Empfängers</param>
|
|
/// <param name="receiverId">Id des Senders oder Empfängers</param>
|
|
/// <returns>PartialView</returns>
|
|
[Authorize(Policy = Policies.PowerUserOnly)]
|
|
[HasPermission(Permission.AppUsersManage, Permission.AppUsersRead)]
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public async Task<IActionResult> Details(string senderId, string receiverId)
|
|
{
|
|
var conversation = await _messageService.GetConversationAsync(senderId, receiverId);
|
|
if (conversation != null)
|
|
{
|
|
var namesDict = new Dictionary<string, string>();
|
|
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<List<MessageCopyVm>>(messages);
|
|
|
|
foreach (var conversationListVm in model)
|
|
{
|
|
conversationListVm.SenderName = namesDict[conversationListVm.SenderId];
|
|
conversationListVm.ReceiverName = namesDict[conversationListVm.ReceiverId];
|
|
}
|
|
|
|
return PartialView("_Details", model);
|
|
}
|
|
|
|
return PartialView("_Error");
|
|
}
|
|
}
|
|
}
|