213 lines
7.1 KiB
C#
213 lines
7.1 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using FutureMailAPI.Services;
|
|
using FutureMailAPI.DTOs;
|
|
|
|
namespace FutureMailAPI.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/v1/[controller]")]
|
|
|
|
public class TimeCapsulesController : BaseController
|
|
{
|
|
private readonly ITimeCapsuleService _timeCapsuleService;
|
|
private readonly ILogger<TimeCapsulesController> _logger;
|
|
|
|
public TimeCapsulesController(ITimeCapsuleService timeCapsuleService, ILogger<TimeCapsulesController> logger)
|
|
{
|
|
_timeCapsuleService = timeCapsuleService;
|
|
_logger = logger;
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> CreateTimeCapsule([FromBody] TimeCapsuleCreateDto createDto)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ApiResponse<TimeCapsuleResponseDto>.ErrorResult("输入数据无效"));
|
|
}
|
|
|
|
// 从JWT令牌中获取当前用户ID
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<TimeCapsuleResponseDto>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _timeCapsuleService.CreateTimeCapsuleAsync(currentUserId, createDto);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return BadRequest(result);
|
|
}
|
|
|
|
return CreatedAtAction(
|
|
nameof(GetTimeCapsule),
|
|
new { capsuleId = result.Data!.Id },
|
|
result);
|
|
}
|
|
|
|
[HttpGet("{capsuleId}")]
|
|
public async Task<IActionResult> GetTimeCapsule(int capsuleId)
|
|
{
|
|
// 从JWT令牌中获取当前用户ID
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<TimeCapsuleResponseDto>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _timeCapsuleService.GetTimeCapsuleByIdAsync(currentUserId, capsuleId);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return NotFound(result);
|
|
}
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetTimeCapsules([FromQuery] TimeCapsuleListQueryDto queryDto)
|
|
{
|
|
// 从JWT令牌中获取当前用户ID
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<PagedResponse<TimeCapsuleResponseDto>>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _timeCapsuleService.GetTimeCapsulesAsync(currentUserId, queryDto);
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpPut("{capsuleId}")]
|
|
public async Task<IActionResult> UpdateTimeCapsule(int capsuleId, [FromBody] TimeCapsuleUpdateDto updateDto)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ApiResponse<TimeCapsuleResponseDto>.ErrorResult("输入数据无效"));
|
|
}
|
|
|
|
// 从JWT令牌中获取当前用户ID
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<TimeCapsuleResponseDto>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _timeCapsuleService.UpdateTimeCapsuleAsync(currentUserId, capsuleId, updateDto);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return BadRequest(result);
|
|
}
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpDelete("{capsuleId}")]
|
|
public async Task<IActionResult> DeleteTimeCapsule(int capsuleId)
|
|
{
|
|
// 从JWT令牌中获取当前用户ID
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<bool>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _timeCapsuleService.DeleteTimeCapsuleAsync(currentUserId, capsuleId);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return BadRequest(result);
|
|
}
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpGet("public")]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> GetPublicTimeCapsules([FromQuery] TimeCapsuleListQueryDto queryDto)
|
|
{
|
|
var result = await _timeCapsuleService.GetPublicTimeCapsulesAsync(queryDto);
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpPost("public/{capsuleId}/claim")]
|
|
public async Task<IActionResult> ClaimPublicCapsule(int capsuleId)
|
|
{
|
|
// 从JWT令牌中获取当前用户ID
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<TimeCapsuleResponseDto>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _timeCapsuleService.ClaimPublicCapsuleAsync(currentUserId, capsuleId);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return BadRequest(result);
|
|
}
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpGet("view")]
|
|
public async Task<IActionResult> GetTimeCapsuleView()
|
|
{
|
|
// 从JWT令牌中获取当前用户ID
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<TimeCapsuleViewResponseDto>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _timeCapsuleService.GetTimeCapsuleViewAsync(currentUserId);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return BadRequest(result);
|
|
}
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpPut("{capsuleId}/style")]
|
|
public async Task<IActionResult> UpdateTimeCapsuleStyle(int capsuleId, [FromBody] TimeCapsuleStyleUpdateDto updateDto)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ApiResponse<TimeCapsuleResponseDto>.ErrorResult("输入数据无效"));
|
|
}
|
|
|
|
// 从JWT令牌中获取当前用户ID
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<TimeCapsuleResponseDto>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _timeCapsuleService.UpdateTimeCapsuleStyleAsync(currentUserId, capsuleId, updateDto);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return BadRequest(result);
|
|
}
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
}
|
|
} |