初始化
This commit is contained in:
174
FutureMailAPI/Controllers/PersonalSpaceController.cs
Normal file
174
FutureMailAPI/Controllers/PersonalSpaceController.cs
Normal file
@@ -0,0 +1,174 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using FutureMailAPI.Services;
|
||||
using FutureMailAPI.DTOs;
|
||||
using FutureMailAPI.Helpers;
|
||||
|
||||
namespace FutureMailAPI.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/v1/[controller]")]
|
||||
[Authorize]
|
||||
public class PersonalSpaceController : ControllerBase
|
||||
{
|
||||
private readonly IPersonalSpaceService _personalSpaceService;
|
||||
private readonly ILogger<PersonalSpaceController> _logger;
|
||||
|
||||
public PersonalSpaceController(IPersonalSpaceService personalSpaceService, ILogger<PersonalSpaceController> logger)
|
||||
{
|
||||
_personalSpaceService = personalSpaceService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户时间线
|
||||
/// </summary>
|
||||
/// <param name="type">时间线类型</param>
|
||||
/// <param name="startDate">开始日期</param>
|
||||
/// <param name="endDate">结束日期</param>
|
||||
/// <returns>用户时间线</returns>
|
||||
[HttpGet("timeline")]
|
||||
public async Task<IActionResult> GetTimeline(
|
||||
[FromQuery] TimelineType type = TimelineType.ALL,
|
||||
[FromQuery] DateTime? startDate = null,
|
||||
[FromQuery] DateTime? endDate = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId <= 0)
|
||||
{
|
||||
return Unauthorized(ApiResponse<object>.ErrorResult("无效的用户令牌"));
|
||||
}
|
||||
|
||||
var query = new TimelineQueryDto
|
||||
{
|
||||
Type = type,
|
||||
StartDate = startDate,
|
||||
EndDate = endDate
|
||||
};
|
||||
|
||||
var result = await _personalSpaceService.GetTimelineAsync(userId, query);
|
||||
|
||||
if (result.Success)
|
||||
{
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
return BadRequest(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "获取时间线时发生错误");
|
||||
return StatusCode(500, ApiResponse<TimelineResponseDto>.ErrorResult("服务器内部错误"));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户统计数据
|
||||
/// </summary>
|
||||
/// <returns>用户统计数据</returns>
|
||||
[HttpGet("statistics")]
|
||||
public async Task<IActionResult> GetStatistics()
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId <= 0)
|
||||
{
|
||||
return Unauthorized(ApiResponse<object>.ErrorResult("无效的用户令牌"));
|
||||
}
|
||||
|
||||
var result = await _personalSpaceService.GetStatisticsAsync(userId);
|
||||
|
||||
if (result.Success)
|
||||
{
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
return BadRequest(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "获取统计数据时发生错误");
|
||||
return StatusCode(500, ApiResponse<StatisticsResponseDto>.ErrorResult("服务器内部错误"));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户订阅信息
|
||||
/// </summary>
|
||||
/// <returns>用户订阅信息</returns>
|
||||
[HttpGet("subscription")]
|
||||
public async Task<IActionResult> GetSubscription()
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId <= 0)
|
||||
{
|
||||
return Unauthorized(ApiResponse<object>.ErrorResult("无效的用户令牌"));
|
||||
}
|
||||
|
||||
var result = await _personalSpaceService.GetSubscriptionAsync(userId);
|
||||
|
||||
if (result.Success)
|
||||
{
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
return BadRequest(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "获取订阅信息时发生错误");
|
||||
return StatusCode(500, ApiResponse<SubscriptionResponseDto>.ErrorResult("服务器内部错误"));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户资料
|
||||
/// </summary>
|
||||
/// <returns>用户资料</returns>
|
||||
[HttpGet("profile")]
|
||||
public async Task<IActionResult> GetUserProfile()
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId <= 0)
|
||||
{
|
||||
return Unauthorized(ApiResponse<object>.ErrorResult("无效的用户令牌"));
|
||||
}
|
||||
|
||||
var result = await _personalSpaceService.GetUserProfileAsync(userId);
|
||||
|
||||
if (result.Success)
|
||||
{
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
return BadRequest(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "获取用户资料时发生错误");
|
||||
return StatusCode(500, ApiResponse<UserProfileResponseDto>.ErrorResult("服务器内部错误"));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从当前请求中获取用户ID
|
||||
/// </summary>
|
||||
/// <returns>用户ID</returns>
|
||||
private int GetCurrentUserId()
|
||||
{
|
||||
var userIdClaim = User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier);
|
||||
if (userIdClaim != null && int.TryParse(userIdClaim.Value, out var userId))
|
||||
{
|
||||
return userId;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user