修改接口
This commit is contained in:
		@@ -1,25 +1,25 @@
 | 
			
		||||
using Microsoft.AspNetCore.Authorization;
 | 
			
		||||
using Microsoft.AspNetCore.Mvc;
 | 
			
		||||
using FutureMailAPI.Services;
 | 
			
		||||
using Microsoft.AspNetCore.Authorization;
 | 
			
		||||
using FutureMailAPI.DTOs;
 | 
			
		||||
using System.Security.Claims;
 | 
			
		||||
using FutureMailAPI.Services;
 | 
			
		||||
 | 
			
		||||
namespace FutureMailAPI.Controllers
 | 
			
		||||
{
 | 
			
		||||
    [ApiController]
 | 
			
		||||
    [Route("api/v1/[controller]")]
 | 
			
		||||
    [Authorize]
 | 
			
		||||
    public class MailsController : ControllerBase
 | 
			
		||||
    [Route("api/v1/mails")]
 | 
			
		||||
    public class MailsController : BaseController
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IMailService _mailService;
 | 
			
		||||
        
 | 
			
		||||
        public MailsController(IMailService mailService)
 | 
			
		||||
        private readonly ILogger<MailsController> _logger;
 | 
			
		||||
 | 
			
		||||
        public MailsController(IMailService mailService, ILogger<MailsController> logger)
 | 
			
		||||
        {
 | 
			
		||||
            _mailService = mailService;
 | 
			
		||||
            _logger = logger;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        [HttpPost]
 | 
			
		||||
        public async Task<ActionResult<ApiResponse<SentMailResponseDto>>> CreateMail([FromBody] SentMailCreateDto createDto)
 | 
			
		||||
        public async Task<IActionResult> CreateMail([FromBody] SentMailCreateDto createDto)
 | 
			
		||||
        {
 | 
			
		||||
            if (!ModelState.IsValid)
 | 
			
		||||
            {
 | 
			
		||||
@@ -29,12 +29,12 @@ namespace FutureMailAPI.Controllers
 | 
			
		||||
            // 从JWT令牌中获取当前用户ID
 | 
			
		||||
            var currentUserId = GetCurrentUserId();
 | 
			
		||||
            
 | 
			
		||||
            if (currentUserId == null)
 | 
			
		||||
            if (currentUserId <= 0)
 | 
			
		||||
            {
 | 
			
		||||
                return Unauthorized(ApiResponse<SentMailResponseDto>.ErrorResult("未授权访问"));
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            var result = await _mailService.CreateMailAsync(currentUserId.Value, createDto);
 | 
			
		||||
            var result = await _mailService.CreateMailAsync(currentUserId, createDto);
 | 
			
		||||
            
 | 
			
		||||
            if (!result.Success)
 | 
			
		||||
            {
 | 
			
		||||
@@ -48,17 +48,17 @@ namespace FutureMailAPI.Controllers
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        [HttpGet("{mailId}")]
 | 
			
		||||
        public async Task<ActionResult<ApiResponse<SentMailResponseDto>>> GetMail(int mailId)
 | 
			
		||||
        public async Task<IActionResult> GetMail(int mailId)
 | 
			
		||||
        {
 | 
			
		||||
            // 从JWT令牌中获取当前用户ID
 | 
			
		||||
            var currentUserId = GetCurrentUserId();
 | 
			
		||||
            
 | 
			
		||||
            if (currentUserId == null)
 | 
			
		||||
            if (currentUserId <= 0)
 | 
			
		||||
            {
 | 
			
		||||
                return Unauthorized(ApiResponse<SentMailResponseDto>.ErrorResult("未授权访问"));
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            var result = await _mailService.GetSentMailByIdAsync(currentUserId.Value, mailId);
 | 
			
		||||
            var result = await _mailService.GetSentMailByIdAsync(currentUserId, mailId);
 | 
			
		||||
            
 | 
			
		||||
            if (!result.Success)
 | 
			
		||||
            {
 | 
			
		||||
@@ -69,23 +69,36 @@ namespace FutureMailAPI.Controllers
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        [HttpGet]
 | 
			
		||||
        public async Task<ActionResult<ApiResponse<PagedResponse<SentMailResponseDto>>>> GetMails([FromQuery] MailListQueryDto queryDto)
 | 
			
		||||
        public async Task<IActionResult> GetMails([FromQuery] MailListQueryDto queryDto)
 | 
			
		||||
        {
 | 
			
		||||
            // 从JWT令牌中获取当前用户ID
 | 
			
		||||
            var currentUserId = GetCurrentUserId();
 | 
			
		||||
            
 | 
			
		||||
            if (currentUserId == null)
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                return Unauthorized(ApiResponse<PagedResponse<SentMailResponseDto>>.ErrorResult("未授权访问"));
 | 
			
		||||
                // 从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)
 | 
			
		||||
            {
 | 
			
		||||
                _logger.LogError(ex, "获取邮件列表时发生错误");
 | 
			
		||||
                return StatusCode(500, 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)
 | 
			
		||||
        public async Task<IActionResult> UpdateMail(int mailId, [FromBody] SentMailUpdateDto updateDto)
 | 
			
		||||
        {
 | 
			
		||||
            if (!ModelState.IsValid)
 | 
			
		||||
            {
 | 
			
		||||
@@ -95,12 +108,12 @@ namespace FutureMailAPI.Controllers
 | 
			
		||||
            // 从JWT令牌中获取当前用户ID
 | 
			
		||||
            var currentUserId = GetCurrentUserId();
 | 
			
		||||
            
 | 
			
		||||
            if (currentUserId == null)
 | 
			
		||||
            if (currentUserId <= 0)
 | 
			
		||||
            {
 | 
			
		||||
                return Unauthorized(ApiResponse<SentMailResponseDto>.ErrorResult("未授权访问"));
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            var result = await _mailService.UpdateMailAsync(currentUserId.Value, mailId, updateDto);
 | 
			
		||||
            var result = await _mailService.UpdateMailAsync(currentUserId, mailId, updateDto);
 | 
			
		||||
            
 | 
			
		||||
            if (!result.Success)
 | 
			
		||||
            {
 | 
			
		||||
@@ -111,17 +124,17 @@ namespace FutureMailAPI.Controllers
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        [HttpDelete("{mailId}")]
 | 
			
		||||
        public async Task<ActionResult<ApiResponse<bool>>> DeleteMail(int mailId)
 | 
			
		||||
        public async Task<IActionResult> DeleteMail(int mailId)
 | 
			
		||||
        {
 | 
			
		||||
            // 从JWT令牌中获取当前用户ID
 | 
			
		||||
            var currentUserId = GetCurrentUserId();
 | 
			
		||||
            
 | 
			
		||||
            if (currentUserId == null)
 | 
			
		||||
            if (currentUserId <= 0)
 | 
			
		||||
            {
 | 
			
		||||
                return Unauthorized(ApiResponse<bool>.ErrorResult("未授权访问"));
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            var result = await _mailService.DeleteMailAsync(currentUserId.Value, mailId);
 | 
			
		||||
            var result = await _mailService.DeleteMailAsync(currentUserId, mailId);
 | 
			
		||||
            
 | 
			
		||||
            if (!result.Success)
 | 
			
		||||
            {
 | 
			
		||||
@@ -132,33 +145,33 @@ namespace FutureMailAPI.Controllers
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        [HttpGet("received")]
 | 
			
		||||
        public async Task<ActionResult<ApiResponse<PagedResponse<ReceivedMailResponseDto>>>> GetReceivedMails([FromQuery] MailListQueryDto queryDto)
 | 
			
		||||
        public async Task<IActionResult> GetReceivedMails([FromQuery] MailListQueryDto queryDto)
 | 
			
		||||
        {
 | 
			
		||||
            // 从JWT令牌中获取当前用户ID
 | 
			
		||||
            var currentUserId = GetCurrentUserId();
 | 
			
		||||
            
 | 
			
		||||
            if (currentUserId == null)
 | 
			
		||||
            if (currentUserId <= 0)
 | 
			
		||||
            {
 | 
			
		||||
                return Unauthorized(ApiResponse<PagedResponse<ReceivedMailResponseDto>>.ErrorResult("未授权访问"));
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            var result = await _mailService.GetReceivedMailsAsync(currentUserId.Value, queryDto);
 | 
			
		||||
            var result = await _mailService.GetReceivedMailsAsync(currentUserId, queryDto);
 | 
			
		||||
            
 | 
			
		||||
            return Ok(result);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        [HttpGet("received/{id}")]
 | 
			
		||||
        public async Task<ActionResult<ApiResponse<ReceivedMailResponseDto>>> GetReceivedMail(int id)
 | 
			
		||||
        public async Task<IActionResult> GetReceivedMail(int id)
 | 
			
		||||
        {
 | 
			
		||||
            // 从JWT令牌中获取当前用户ID
 | 
			
		||||
            var currentUserId = GetCurrentUserId();
 | 
			
		||||
            
 | 
			
		||||
            if (currentUserId == null)
 | 
			
		||||
            if (currentUserId <= 0)
 | 
			
		||||
            {
 | 
			
		||||
                return Unauthorized(ApiResponse<ReceivedMailResponseDto>.ErrorResult("未授权访问"));
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            var result = await _mailService.GetReceivedMailByIdAsync(currentUserId.Value, id);
 | 
			
		||||
            var result = await _mailService.GetReceivedMailByIdAsync(currentUserId, id);
 | 
			
		||||
            
 | 
			
		||||
            if (!result.Success)
 | 
			
		||||
            {
 | 
			
		||||
@@ -169,17 +182,17 @@ namespace FutureMailAPI.Controllers
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        [HttpPost("received/{id}/mark-read")]
 | 
			
		||||
        public async Task<ActionResult<ApiResponse<bool>>> MarkReceivedMailAsRead(int id)
 | 
			
		||||
        public async Task<IActionResult> MarkReceivedMailAsRead(int id)
 | 
			
		||||
        {
 | 
			
		||||
            // 从JWT令牌中获取当前用户ID
 | 
			
		||||
            var currentUserId = GetCurrentUserId();
 | 
			
		||||
            
 | 
			
		||||
            if (currentUserId == null)
 | 
			
		||||
            if (currentUserId <= 0)
 | 
			
		||||
            {
 | 
			
		||||
                return Unauthorized(ApiResponse<bool>.ErrorResult("未授权访问"));
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            var result = await _mailService.MarkReceivedMailAsReadAsync(currentUserId.Value, id);
 | 
			
		||||
            var result = await _mailService.MarkReceivedMailAsReadAsync(currentUserId, id);
 | 
			
		||||
            
 | 
			
		||||
            if (!result.Success)
 | 
			
		||||
            {
 | 
			
		||||
@@ -190,17 +203,17 @@ namespace FutureMailAPI.Controllers
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        [HttpPost("{mailId}/revoke")]
 | 
			
		||||
        public async Task<ActionResult<ApiResponse<bool>>> RevokeMail(int mailId)
 | 
			
		||||
        public async Task<IActionResult> RevokeMail(int mailId)
 | 
			
		||||
        {
 | 
			
		||||
            // 从JWT令牌中获取当前用户ID
 | 
			
		||||
            var currentUserId = GetCurrentUserId();
 | 
			
		||||
            
 | 
			
		||||
            if (currentUserId == null)
 | 
			
		||||
            if (currentUserId <= 0)
 | 
			
		||||
            {
 | 
			
		||||
                return Unauthorized(ApiResponse<bool>.ErrorResult("未授权访问"));
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            var result = await _mailService.RevokeMailAsync(currentUserId.Value, mailId);
 | 
			
		||||
            var result = await _mailService.RevokeMailAsync(currentUserId, mailId);
 | 
			
		||||
            
 | 
			
		||||
            if (!result.Success)
 | 
			
		||||
            {
 | 
			
		||||
@@ -209,17 +222,5 @@ namespace FutureMailAPI.Controllers
 | 
			
		||||
            
 | 
			
		||||
            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;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user