初始化

This commit is contained in:
2025-10-16 09:56:36 +08:00
commit de704db577
272 changed files with 37331 additions and 0 deletions

View File

@@ -0,0 +1,225 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using FutureMailAPI.Services;
using FutureMailAPI.DTOs;
using System.Security.Claims;
namespace FutureMailAPI.Controllers
{
[ApiController]
[Route("api/v1/[controller]")]
[Authorize]
public class MailsController : ControllerBase
{
private readonly IMailService _mailService;
public MailsController(IMailService mailService)
{
_mailService = mailService;
}
[HttpPost]
public async Task<ActionResult<ApiResponse<SentMailResponseDto>>> CreateMail([FromBody] SentMailCreateDto createDto)
{
if (!ModelState.IsValid)
{
return BadRequest(ApiResponse<SentMailResponseDto>.ErrorResult("输入数据无效"));
}
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
if (currentUserId == null)
{
return Unauthorized(ApiResponse<SentMailResponseDto>.ErrorResult("未授权访问"));
}
var result = await _mailService.CreateMailAsync(currentUserId.Value, createDto);
if (!result.Success)
{
return BadRequest(result);
}
return CreatedAtAction(
nameof(GetMail),
new { mailId = result.Data!.Id },
result);
}
[HttpGet("{mailId}")]
public async Task<ActionResult<ApiResponse<SentMailResponseDto>>> GetMail(int mailId)
{
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
if (currentUserId == null)
{
return Unauthorized(ApiResponse<SentMailResponseDto>.ErrorResult("未授权访问"));
}
var result = await _mailService.GetSentMailByIdAsync(currentUserId.Value, mailId);
if (!result.Success)
{
return NotFound(result);
}
return Ok(result);
}
[HttpGet]
public async Task<ActionResult<ApiResponse<PagedResponse<SentMailResponseDto>>>> GetMails([FromQuery] MailListQueryDto queryDto)
{
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
if (currentUserId == null)
{
return Unauthorized(ApiResponse<PagedResponse<SentMailResponseDto>>.ErrorResult("未授权访问"));
}
var result = await _mailService.GetSentMailsAsync(currentUserId.Value, queryDto);
return Ok(result);
}
[HttpPut("{mailId}")]
public async Task<ActionResult<ApiResponse<SentMailResponseDto>>> UpdateMail(int mailId, [FromBody] SentMailUpdateDto updateDto)
{
if (!ModelState.IsValid)
{
return BadRequest(ApiResponse<SentMailResponseDto>.ErrorResult("输入数据无效"));
}
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
if (currentUserId == null)
{
return Unauthorized(ApiResponse<SentMailResponseDto>.ErrorResult("未授权访问"));
}
var result = await _mailService.UpdateMailAsync(currentUserId.Value, mailId, updateDto);
if (!result.Success)
{
return BadRequest(result);
}
return Ok(result);
}
[HttpDelete("{mailId}")]
public async Task<ActionResult<ApiResponse<bool>>> DeleteMail(int mailId)
{
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
if (currentUserId == null)
{
return Unauthorized(ApiResponse<bool>.ErrorResult("未授权访问"));
}
var result = await _mailService.DeleteMailAsync(currentUserId.Value, mailId);
if (!result.Success)
{
return BadRequest(result);
}
return Ok(result);
}
[HttpGet("received")]
public async Task<ActionResult<ApiResponse<PagedResponse<ReceivedMailResponseDto>>>> GetReceivedMails([FromQuery] MailListQueryDto queryDto)
{
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
if (currentUserId == null)
{
return Unauthorized(ApiResponse<PagedResponse<ReceivedMailResponseDto>>.ErrorResult("未授权访问"));
}
var result = await _mailService.GetReceivedMailsAsync(currentUserId.Value, queryDto);
return Ok(result);
}
[HttpGet("received/{id}")]
public async Task<ActionResult<ApiResponse<ReceivedMailResponseDto>>> GetReceivedMail(int id)
{
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
if (currentUserId == null)
{
return Unauthorized(ApiResponse<ReceivedMailResponseDto>.ErrorResult("未授权访问"));
}
var result = await _mailService.GetReceivedMailByIdAsync(currentUserId.Value, id);
if (!result.Success)
{
return NotFound(result);
}
return Ok(result);
}
[HttpPost("received/{id}/mark-read")]
public async Task<ActionResult<ApiResponse<bool>>> MarkReceivedMailAsRead(int id)
{
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
if (currentUserId == null)
{
return Unauthorized(ApiResponse<bool>.ErrorResult("未授权访问"));
}
var result = await _mailService.MarkReceivedMailAsReadAsync(currentUserId.Value, id);
if (!result.Success)
{
return BadRequest(result);
}
return Ok(result);
}
[HttpPost("{mailId}/revoke")]
public async Task<ActionResult<ApiResponse<bool>>> RevokeMail(int mailId)
{
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
if (currentUserId == null)
{
return Unauthorized(ApiResponse<bool>.ErrorResult("未授权访问"));
}
var result = await _mailService.RevokeMailAsync(currentUserId.Value, mailId);
if (!result.Success)
{
return BadRequest(result);
}
return Ok(result);
}
private int? GetCurrentUserId()
{
var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier);
if (userIdClaim == null || !int.TryParse(userIdClaim.Value, out var userId))
{
return null;
}
return userId;
}
}
}