Files
emall-api/FutureMailAPI/Controllers/MailsController.cs

259 lines
8.4 KiB
C#
Raw Normal View History

2025-10-16 09:56:36 +08:00
using Microsoft.AspNetCore.Mvc;
2025-10-16 15:21:52 +08:00
using Microsoft.AspNetCore.Authorization;
2025-10-16 09:56:36 +08:00
using FutureMailAPI.DTOs;
2025-10-16 15:21:52 +08:00
using FutureMailAPI.Services;
2025-10-16 09:56:36 +08:00
namespace FutureMailAPI.Controllers
{
[ApiController]
2025-10-16 15:21:52 +08:00
[Route("api/v1/mails")]
public class MailsController : BaseController
2025-10-16 09:56:36 +08:00
{
private readonly IMailService _mailService;
2025-10-16 15:21:52 +08:00
private readonly ILogger<MailsController> _logger;
public MailsController(IMailService mailService, ILogger<MailsController> logger)
2025-10-16 09:56:36 +08:00
{
_mailService = mailService;
2025-10-16 15:21:52 +08:00
_logger = logger;
2025-10-16 09:56:36 +08:00
}
[HttpPost]
2025-10-16 15:21:52 +08:00
public async Task<IActionResult> CreateMail([FromBody] SentMailCreateDto createDto)
2025-10-16 09:56:36 +08:00
{
if (!ModelState.IsValid)
{
return BadRequest(ApiResponse<SentMailResponseDto>.ErrorResult("输入数据无效"));
}
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
2025-10-16 15:21:52 +08:00
if (currentUserId <= 0)
2025-10-16 09:56:36 +08:00
{
return Unauthorized(ApiResponse<SentMailResponseDto>.ErrorResult("未授权访问"));
}
2025-10-16 15:21:52 +08:00
var result = await _mailService.CreateMailAsync(currentUserId, createDto);
2025-10-16 09:56:36 +08:00
if (!result.Success)
2025-10-16 16:21:22 +08:00
{
return BadRequest(result);
}
return CreatedAtAction(
nameof(GetMail),
new { mailId = result.Data!.Id },
result);
}
// 兼容前端请求格式的创建邮件接口
[HttpPost("create")]
public async Task<IActionResult> CreateMailCompat([FromBody] SentMailCreateCompatDto createDto)
{
if (!ModelState.IsValid)
{
return BadRequest(ApiResponse<SentMailResponseDto>.ErrorResult("输入数据无效"));
}
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
if (currentUserId <= 0)
{
return Unauthorized(ApiResponse<SentMailResponseDto>.ErrorResult("未授权访问"));
}
// 转换为内部DTO
var internalDto = createDto.ToInternalDto();
var result = await _mailService.CreateMailAsync(currentUserId, internalDto);
if (!result.Success)
2025-10-16 09:56:36 +08:00
{
return BadRequest(result);
}
return CreatedAtAction(
nameof(GetMail),
new { mailId = result.Data!.Id },
result);
}
[HttpGet("{mailId}")]
2025-10-16 15:21:52 +08:00
public async Task<IActionResult> GetMail(int mailId)
2025-10-16 09:56:36 +08:00
{
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
2025-10-16 15:21:52 +08:00
if (currentUserId <= 0)
2025-10-16 09:56:36 +08:00
{
return Unauthorized(ApiResponse<SentMailResponseDto>.ErrorResult("未授权访问"));
}
2025-10-16 15:21:52 +08:00
var result = await _mailService.GetSentMailByIdAsync(currentUserId, mailId);
2025-10-16 09:56:36 +08:00
if (!result.Success)
{
return NotFound(result);
}
return Ok(result);
}
[HttpGet]
2025-10-16 15:21:52 +08:00
public async Task<IActionResult> GetMails([FromQuery] MailListQueryDto queryDto)
2025-10-16 09:56:36 +08:00
{
2025-10-16 15:21:52 +08:00
try
{
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
if (currentUserId <= 0)
{
return Unauthorized(ApiResponse<PagedResponse<SentMailResponseDto>>.ErrorResult("未授权访问"));
}
var result = await _mailService.GetSentMailsAsync(currentUserId, queryDto);
if (result.Success)
{
return Ok(result);
}
return BadRequest(result);
}
catch (Exception ex)
2025-10-16 09:56:36 +08:00
{
2025-10-16 15:21:52 +08:00
_logger.LogError(ex, "获取邮件列表时发生错误");
return StatusCode(500, ApiResponse<PagedResponse<SentMailResponseDto>>.ErrorResult("服务器内部错误"));
2025-10-16 09:56:36 +08:00
}
}
[HttpPut("{mailId}")]
2025-10-16 15:21:52 +08:00
public async Task<IActionResult> UpdateMail(int mailId, [FromBody] SentMailUpdateDto updateDto)
2025-10-16 09:56:36 +08:00
{
if (!ModelState.IsValid)
{
return BadRequest(ApiResponse<SentMailResponseDto>.ErrorResult("输入数据无效"));
}
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
2025-10-16 15:21:52 +08:00
if (currentUserId <= 0)
2025-10-16 09:56:36 +08:00
{
return Unauthorized(ApiResponse<SentMailResponseDto>.ErrorResult("未授权访问"));
}
2025-10-16 15:21:52 +08:00
var result = await _mailService.UpdateMailAsync(currentUserId, mailId, updateDto);
2025-10-16 09:56:36 +08:00
if (!result.Success)
{
return BadRequest(result);
}
return Ok(result);
}
[HttpDelete("{mailId}")]
2025-10-16 15:21:52 +08:00
public async Task<IActionResult> DeleteMail(int mailId)
2025-10-16 09:56:36 +08:00
{
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
2025-10-16 15:21:52 +08:00
if (currentUserId <= 0)
2025-10-16 09:56:36 +08:00
{
return Unauthorized(ApiResponse<bool>.ErrorResult("未授权访问"));
}
2025-10-16 15:21:52 +08:00
var result = await _mailService.DeleteMailAsync(currentUserId, mailId);
2025-10-16 09:56:36 +08:00
if (!result.Success)
{
return BadRequest(result);
}
return Ok(result);
}
[HttpGet("received")]
2025-10-16 15:21:52 +08:00
public async Task<IActionResult> GetReceivedMails([FromQuery] MailListQueryDto queryDto)
2025-10-16 09:56:36 +08:00
{
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
2025-10-16 15:21:52 +08:00
if (currentUserId <= 0)
2025-10-16 09:56:36 +08:00
{
return Unauthorized(ApiResponse<PagedResponse<ReceivedMailResponseDto>>.ErrorResult("未授权访问"));
}
2025-10-16 15:21:52 +08:00
var result = await _mailService.GetReceivedMailsAsync(currentUserId, queryDto);
2025-10-16 09:56:36 +08:00
return Ok(result);
}
[HttpGet("received/{id}")]
2025-10-16 15:21:52 +08:00
public async Task<IActionResult> GetReceivedMail(int id)
2025-10-16 09:56:36 +08:00
{
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
2025-10-16 15:21:52 +08:00
if (currentUserId <= 0)
2025-10-16 09:56:36 +08:00
{
return Unauthorized(ApiResponse<ReceivedMailResponseDto>.ErrorResult("未授权访问"));
}
2025-10-16 15:21:52 +08:00
var result = await _mailService.GetReceivedMailByIdAsync(currentUserId, id);
2025-10-16 09:56:36 +08:00
if (!result.Success)
{
return NotFound(result);
}
return Ok(result);
}
[HttpPost("received/{id}/mark-read")]
2025-10-16 15:21:52 +08:00
public async Task<IActionResult> MarkReceivedMailAsRead(int id)
2025-10-16 09:56:36 +08:00
{
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
2025-10-16 15:21:52 +08:00
if (currentUserId <= 0)
2025-10-16 09:56:36 +08:00
{
return Unauthorized(ApiResponse<bool>.ErrorResult("未授权访问"));
}
2025-10-16 15:21:52 +08:00
var result = await _mailService.MarkReceivedMailAsReadAsync(currentUserId, id);
2025-10-16 09:56:36 +08:00
if (!result.Success)
{
return BadRequest(result);
}
return Ok(result);
}
[HttpPost("{mailId}/revoke")]
2025-10-16 15:21:52 +08:00
public async Task<IActionResult> RevokeMail(int mailId)
2025-10-16 09:56:36 +08:00
{
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
2025-10-16 15:21:52 +08:00
if (currentUserId <= 0)
2025-10-16 09:56:36 +08:00
{
return Unauthorized(ApiResponse<bool>.ErrorResult("未授权访问"));
}
2025-10-16 15:21:52 +08:00
var result = await _mailService.RevokeMailAsync(currentUserId, mailId);
2025-10-16 09:56:36 +08:00
if (!result.Success)
{
return BadRequest(result);
}
return Ok(result);
}
}
}