littleshop/LittleShop/Areas/Admin/Controllers/MessagesController.cs

72 lines
2.1 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using LittleShop.DTOs;
using LittleShop.Services;
using LittleShop.Models;
namespace LittleShop.Areas.Admin.Controllers;
[Area("Admin")]
[Authorize(Policy = "AdminOnly")]
public class MessagesController : Controller
{
private readonly ICustomerMessageService _messageService;
private readonly ILogger<MessagesController> _logger;
public MessagesController(ICustomerMessageService messageService, ILogger<MessagesController> logger)
{
_messageService = messageService;
_logger = logger;
}
public async Task<IActionResult> Index()
{
var threads = await _messageService.GetActiveThreadsAsync();
return View(threads);
}
public async Task<IActionResult> Customer(Guid id)
{
var conversation = await _messageService.GetMessageThreadAsync(id);
if (conversation == null)
{
return NotFound();
}
return View(conversation);
}
[HttpPost]
public async Task<IActionResult> Reply(Guid customerId, string content, bool isUrgent = false)
{
try
{
var createMessageDto = new CreateCustomerMessageDto
{
CustomerId = customerId,
Type = MessageType.CustomerService,
Subject = "Support Reply", // Simple subject for admin replies
Content = content,
IsUrgent = isUrgent,
Priority = isUrgent ? 1 : 5
};
var message = await _messageService.CreateMessageAsync(createMessageDto);
if (message != null)
{
TempData["Success"] = "Reply sent successfully!";
}
else
{
TempData["Error"] = "Failed to send reply.";
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error sending reply");
TempData["Error"] = "An error occurred sending the reply.";
}
return RedirectToAction("Customer", new { id = customerId });
}
}