160 lines
5.6 KiB
C#
160 lines
5.6 KiB
C#
using FreeSql;
|
|
|
|
using LiveForum.Code.Base;
|
|
using LiveForum.Code.JwtInfrastructure;
|
|
using LiveForum.IService.Cdk;
|
|
using LiveForum.IService.Permission;
|
|
using LiveForum.IService.Posts;
|
|
using LiveForum.Model;
|
|
using LiveForum.Model.Dto.Posts;
|
|
using LiveForum.Model.Dto.PostComments;
|
|
using LiveForum.Model.Enum;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace LiveForum.WebApi.Controllers
|
|
{
|
|
/// <summary>
|
|
/// 帖子相关接口
|
|
/// </summary>
|
|
[Route("api/[controller]/[action]")]
|
|
[ApiController]
|
|
public class PostsController : ControllerBase
|
|
{
|
|
private readonly IPostsService _posts;
|
|
private readonly ICdkService _cdkService;
|
|
private readonly IPermissionService _permissionService;
|
|
private readonly JwtUserInfoModel _userInfoModel;
|
|
private readonly IBaseRepository<T_Posts> _postsRepository;
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
public PostsController(IPostsService posts, ICdkService cdkService, IPermissionService permissionService, JwtUserInfoModel userInfoModel, IBaseRepository<T_Posts> postsRepository)
|
|
{
|
|
_posts = posts;
|
|
_cdkService = cdkService;
|
|
_permissionService = permissionService;
|
|
_userInfoModel = userInfoModel;
|
|
_postsRepository = postsRepository;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取帖子列表(瀑布流)
|
|
/// </summary>
|
|
/// <param name="request">请求参数</param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public Task<BaseResponse<GetPostsRespDto>> GetPosts([FromQuery] GetPostsReq request) => _posts.GetPosts(request);
|
|
|
|
/// <summary>
|
|
/// 获取帖子详情
|
|
/// </summary>
|
|
/// <param name="request">请求参数</param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public Task<BaseResponse<PostDetailDto>> GetPostDetail([FromQuery] GetPostDetailReq request) => _posts.GetPostDetail(request);
|
|
|
|
/// <summary>
|
|
/// 发布帖子
|
|
/// </summary>
|
|
/// <param name="request">请求参数</param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[Authorize]
|
|
public async Task<BaseResponse<PublishPostsRespDto>> PublishPosts([FromBody] PublishPostsReq request)
|
|
{
|
|
var userId = _userInfoModel.UserId;
|
|
|
|
// 权限校验:发贴权限
|
|
await _permissionService.CheckPermissionAsync(userId, PermissionType.Post);
|
|
|
|
// CDK激活检查
|
|
if (await _cdkService.RequiresCdkActivationAsync(userId))
|
|
{
|
|
return new BaseResponse<PublishPostsRespDto>(ResponseCode.CdkNotActivated, "需要激活 CDK");
|
|
}
|
|
return await _posts.PublishPosts(request);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 编辑帖子
|
|
/// </summary>
|
|
/// <param name="request">请求参数</param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[Authorize]
|
|
public Task<BaseResponse<UpdatePostsRespDto>> UpdatePosts([FromBody] UpdatePostsReq request) => _posts.UpdatePosts(request);
|
|
|
|
/// <summary>
|
|
/// 删除帖子
|
|
/// </summary>
|
|
/// <param name="request">请求参数</param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[Authorize]
|
|
public async Task<BaseResponseBool> DeletePosts([FromBody] DeletePostsReq request)
|
|
{
|
|
var userId = _userInfoModel.UserId;
|
|
|
|
// 查询帖子作者,仅当删除者非帖子作者时校验删除其他用户帖子权限
|
|
var post = await _postsRepository.Select
|
|
.Where(x => x.Id == request.PostId && !x.IsDeleted)
|
|
.FirstAsync();
|
|
|
|
if (post != null && post.UserId != userId)
|
|
{
|
|
await _permissionService.CheckPermissionAsync(userId, PermissionType.DeleteOtherPost);
|
|
}
|
|
|
|
return await _posts.DeletePosts(request);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取我的帖子列表
|
|
/// </summary>
|
|
/// <param name="request">请求参数</param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[Authorize]
|
|
public Task<BaseResponse<GetMyPostsRespDto>> GetMyPosts([FromQuery] GetMyPostsReq request) => _posts.GetMyPosts(request);
|
|
|
|
/// <summary>
|
|
/// 点赞/取消点赞帖子
|
|
/// </summary>
|
|
/// <param name="request">请求参数</param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[Authorize]
|
|
public async Task<BaseResponse<LikePostRespDto>> LikePost([FromBody] LikePostReq request)
|
|
{
|
|
var userId = _userInfoModel.UserId;
|
|
|
|
// 权限校验:点赞权限
|
|
await _permissionService.CheckPermissionAsync(userId, PermissionType.Like);
|
|
|
|
return await _posts.LikePost(request);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取我点赞的帖子列表
|
|
/// </summary>
|
|
/// <param name="request">请求参数</param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[Authorize]
|
|
public Task<BaseResponse<GetLikedPostsRespDto>> GetLikedPosts([FromQuery] GetLikedPostsReq request) => _posts.GetLikedPosts(request);
|
|
|
|
/// <summary>
|
|
/// 修改帖子回复权限
|
|
/// </summary>
|
|
/// <param name="request">请求参数</param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[Authorize]
|
|
public Task<BaseResponse<UpdateReplyPermissionRespDto>> UpdateReplyPermission([FromBody] UpdateReplyPermissionReq request) => _posts.UpdateReplyPermission(request);
|
|
}
|
|
}
|