修改接口

This commit is contained in:
2025-10-16 15:21:52 +08:00
parent 82220ce0b8
commit dd398c1c32
274 changed files with 22777 additions and 22905 deletions

View File

@@ -7,8 +7,8 @@ namespace FutureMailAPI.Controllers
{
[ApiController]
[Route("api/v1/[controller]")]
[Authorize]
public class TimeCapsulesController : ControllerBase
public class TimeCapsulesController : BaseController
{
private readonly ITimeCapsuleService _timeCapsuleService;
private readonly ILogger<TimeCapsulesController> _logger;
@@ -20,7 +20,7 @@ namespace FutureMailAPI.Controllers
}
[HttpPost]
public async Task<ActionResult<ApiResponse<TimeCapsuleResponseDto>>> CreateTimeCapsule([FromBody] TimeCapsuleCreateDto createDto)
public async Task<IActionResult> CreateTimeCapsule([FromBody] TimeCapsuleCreateDto createDto)
{
if (!ModelState.IsValid)
{
@@ -30,12 +30,12 @@ namespace FutureMailAPI.Controllers
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
if (currentUserId == null)
if (currentUserId <= 0)
{
return Unauthorized(ApiResponse<TimeCapsuleResponseDto>.ErrorResult("未授权访问"));
}
var result = await _timeCapsuleService.CreateTimeCapsuleAsync(currentUserId.Value, createDto);
var result = await _timeCapsuleService.CreateTimeCapsuleAsync(currentUserId, createDto);
if (!result.Success)
{
@@ -49,17 +49,17 @@ namespace FutureMailAPI.Controllers
}
[HttpGet("{capsuleId}")]
public async Task<ActionResult<ApiResponse<TimeCapsuleResponseDto>>> GetTimeCapsule(int capsuleId)
public async Task<IActionResult> GetTimeCapsule(int capsuleId)
{
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
if (currentUserId == null)
if (currentUserId <= 0)
{
return Unauthorized(ApiResponse<TimeCapsuleResponseDto>.ErrorResult("未授权访问"));
}
var result = await _timeCapsuleService.GetTimeCapsuleByIdAsync(currentUserId.Value, capsuleId);
var result = await _timeCapsuleService.GetTimeCapsuleByIdAsync(currentUserId, capsuleId);
if (!result.Success)
{
@@ -70,23 +70,23 @@ namespace FutureMailAPI.Controllers
}
[HttpGet]
public async Task<ActionResult<ApiResponse<PagedResponse<TimeCapsuleResponseDto>>>> GetTimeCapsules([FromQuery] TimeCapsuleListQueryDto queryDto)
public async Task<IActionResult> GetTimeCapsules([FromQuery] TimeCapsuleListQueryDto queryDto)
{
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
if (currentUserId == null)
if (currentUserId <= 0)
{
return Unauthorized(ApiResponse<PagedResponse<TimeCapsuleResponseDto>>.ErrorResult("未授权访问"));
}
var result = await _timeCapsuleService.GetTimeCapsulesAsync(currentUserId.Value, queryDto);
var result = await _timeCapsuleService.GetTimeCapsulesAsync(currentUserId, queryDto);
return Ok(result);
}
[HttpPut("{capsuleId}")]
public async Task<ActionResult<ApiResponse<TimeCapsuleResponseDto>>> UpdateTimeCapsule(int capsuleId, [FromBody] TimeCapsuleUpdateDto updateDto)
public async Task<IActionResult> UpdateTimeCapsule(int capsuleId, [FromBody] TimeCapsuleUpdateDto updateDto)
{
if (!ModelState.IsValid)
{
@@ -96,12 +96,12 @@ namespace FutureMailAPI.Controllers
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
if (currentUserId == null)
if (currentUserId <= 0)
{
return Unauthorized(ApiResponse<TimeCapsuleResponseDto>.ErrorResult("未授权访问"));
}
var result = await _timeCapsuleService.UpdateTimeCapsuleAsync(currentUserId.Value, capsuleId, updateDto);
var result = await _timeCapsuleService.UpdateTimeCapsuleAsync(currentUserId, capsuleId, updateDto);
if (!result.Success)
{
@@ -112,17 +112,17 @@ namespace FutureMailAPI.Controllers
}
[HttpDelete("{capsuleId}")]
public async Task<ActionResult<ApiResponse<bool>>> DeleteTimeCapsule(int capsuleId)
public async Task<IActionResult> DeleteTimeCapsule(int capsuleId)
{
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
if (currentUserId == null)
if (currentUserId <= 0)
{
return Unauthorized(ApiResponse<bool>.ErrorResult("未授权访问"));
}
var result = await _timeCapsuleService.DeleteTimeCapsuleAsync(currentUserId.Value, capsuleId);
var result = await _timeCapsuleService.DeleteTimeCapsuleAsync(currentUserId, capsuleId);
if (!result.Success)
{
@@ -134,7 +134,7 @@ namespace FutureMailAPI.Controllers
[HttpGet("public")]
[AllowAnonymous]
public async Task<ActionResult<ApiResponse<PagedResponse<TimeCapsuleResponseDto>>>> GetPublicTimeCapsules([FromQuery] TimeCapsuleListQueryDto queryDto)
public async Task<IActionResult> GetPublicTimeCapsules([FromQuery] TimeCapsuleListQueryDto queryDto)
{
var result = await _timeCapsuleService.GetPublicTimeCapsulesAsync(queryDto);
@@ -142,17 +142,17 @@ namespace FutureMailAPI.Controllers
}
[HttpPost("public/{capsuleId}/claim")]
public async Task<ActionResult<ApiResponse<TimeCapsuleResponseDto>>> ClaimPublicCapsule(int capsuleId)
public async Task<IActionResult> ClaimPublicCapsule(int capsuleId)
{
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
if (currentUserId == null)
if (currentUserId <= 0)
{
return Unauthorized(ApiResponse<TimeCapsuleResponseDto>.ErrorResult("未授权访问"));
}
var result = await _timeCapsuleService.ClaimPublicCapsuleAsync(currentUserId.Value, capsuleId);
var result = await _timeCapsuleService.ClaimPublicCapsuleAsync(currentUserId, capsuleId);
if (!result.Success)
{
@@ -163,17 +163,17 @@ namespace FutureMailAPI.Controllers
}
[HttpGet("view")]
public async Task<ActionResult<ApiResponse<TimeCapsuleViewResponseDto>>> GetTimeCapsuleView()
public async Task<IActionResult> GetTimeCapsuleView()
{
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
if (currentUserId == null)
if (currentUserId <= 0)
{
return Unauthorized(ApiResponse<TimeCapsuleViewResponseDto>.ErrorResult("未授权访问"));
}
var result = await _timeCapsuleService.GetTimeCapsuleViewAsync(currentUserId.Value);
var result = await _timeCapsuleService.GetTimeCapsuleViewAsync(currentUserId);
if (!result.Success)
{
@@ -184,7 +184,7 @@ namespace FutureMailAPI.Controllers
}
[HttpPut("{capsuleId}/style")]
public async Task<ActionResult<ApiResponse<TimeCapsuleResponseDto>>> UpdateTimeCapsuleStyle(int capsuleId, [FromBody] TimeCapsuleStyleUpdateDto updateDto)
public async Task<IActionResult> UpdateTimeCapsuleStyle(int capsuleId, [FromBody] TimeCapsuleStyleUpdateDto updateDto)
{
if (!ModelState.IsValid)
{
@@ -194,12 +194,12 @@ namespace FutureMailAPI.Controllers
// 从JWT令牌中获取当前用户ID
var currentUserId = GetCurrentUserId();
if (currentUserId == null)
if (currentUserId <= 0)
{
return Unauthorized(ApiResponse<TimeCapsuleResponseDto>.ErrorResult("未授权访问"));
}
var result = await _timeCapsuleService.UpdateTimeCapsuleStyleAsync(currentUserId.Value, capsuleId, updateDto);
var result = await _timeCapsuleService.UpdateTimeCapsuleStyleAsync(currentUserId, capsuleId, updateDto);
if (!result.Success)
{
@@ -209,16 +209,5 @@ namespace FutureMailAPI.Controllers
return Ok(result);
}
private int? GetCurrentUserId()
{
var userIdClaim = User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier);
if (userIdClaim == null || !int.TryParse(userIdClaim.Value, out var userId))
{
return null;
}
return userId;
}
}
}