Files
Yi.Admin/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/Community/CommentController.cs

67 lines
2.3 KiB
C#
Raw Normal View History

2022-12-01 16:06:53 +08:00
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
2022-12-01 13:38:06 +08:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Yi.Framework.Common.Models;
2022-12-01 16:06:53 +08:00
using Yi.Framework.DTOModel.Vo;
2022-12-01 13:38:06 +08:00
using Yi.Framework.Interface;
using Yi.Framework.Model.Models;
using Yi.Framework.Repository;
using Yi.Framework.WebCore;
using Yi.Framework.WebCore.AttributeExtend;
using Yi.Framework.WebCore.AuthorizationPolicy;
namespace Yi.Framework.ApiMicroservice.Controllers
{
[ApiController]
[Route("api/[controller]/[action]")]
public class CommentController : BaseSimpleCrudController<CommentEntity>
{
private ICommentService _iCommentService;
2022-12-01 16:06:53 +08:00
private IMapper _mapper;
public CommentController(ILogger<CommentEntity> logger, ICommentService iCommentService, IMapper mapper) : base(logger, iCommentService)
2022-12-01 13:38:06 +08:00
{
_iCommentService = iCommentService;
2022-12-01 16:06:53 +08:00
_mapper = mapper;
2022-12-01 13:38:06 +08:00
}
/// <summary>
2022-12-01 16:06:53 +08:00
/// 获取文章的全部一级评论
2022-12-01 13:38:06 +08:00
/// </summary>
/// <returns></returns>
2022-12-01 16:06:53 +08:00
[HttpGet]
[Route("{articleId}")]
public async Task<Result> GetListByArticleId(long articleId)
2022-12-01 13:38:06 +08:00
{
2022-12-01 16:06:53 +08:00
//一级评论被回复的用户id为空
var data = await _repository._DbQueryable.Where(u => u.ParentId == 0 && u.ArticleId == articleId).Includes(u => u.CreateUserInfo).OrderByDescending(u=>u.CreateTime).ToListAsync();
return Result.Success().SetData(_mapper.Map<List<CommentVo>>(data));
2022-12-01 13:38:06 +08:00
}
/// <summary>
/// 获取一级评论详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
2022-12-01 16:06:53 +08:00
public override async Task<Result> GetById([FromRoute] long id)
2022-12-01 13:38:06 +08:00
{
2022-12-01 16:06:53 +08:00
var data = await _repository._DbQueryable.Includes(u => u.CreateUserInfo).Includes(u => u.UserInfo).FirstAsync(u => u.Id == id);
return Result.Success().SetData(_mapper.Map<CommentVo>(data));
2022-12-01 13:38:06 +08:00
}
/// <summary>
/// 回复文章或回复评论
/// </summary>
/// <returns></returns>
[HttpPost]
2022-12-01 16:06:53 +08:00
public override async Task<Result> Add(CommentEntity comment)
{
return Result.Success().SetStatus(await _iCommentService.AddAsync(comment));
2022-12-01 13:38:06 +08:00
}
}
}