Files
BuYuHouDuan/UserSrc/UserApi/Faq/FaqController.cs
T

147 lines
4.8 KiB
C#

using Common;
using CommonInfrastructure;
using Microsoft.AspNetCore.Mvc;
using UserService.Notices;
using System.Linq;
using CommonDomain;
namespace UserApi.Faq
{
[ApiController, Route("adminapi/[controller]")]
public class FaqController : BaseController
{
private readonly IFaqService _faqService;
public FaqController(IFaqService faqService)
{
_faqService = faqService;
}
/// <summary>
/// 获取板块树形结构
/// </summary>
[HttpGet("CategoryTree")]
public async Task<BaseCommonResponse> GetCategoryTree()
{
var tree = await _faqService.GetCategoryTree();
var dtos = tree.Select(MapToTreeNodeDto).ToList();
return new ListResponse<FaqCategoryTreeNodeDto>
{
Code = CommonResponseCodeEnum.Success,
Info = "获取成功",
Data = dtos
};
}
/// <summary>
/// 获取板块下的问题列表
/// </summary>
[HttpGet("Questions")]
public async Task<BaseCommonResponse> GetQuestions(string categoryId)
{
var list = await _faqService.GetQuestionsByCategory(categoryId);
if (list == null)
return new ListResponse<FaqQuestionDto> { Code = CommonResponseCodeEnum.Success, Data = new List<FaqQuestionDto>() };
var dtos = list.Select(MapToQuestionDto).ToList();
return new ListResponse<FaqQuestionDto>
{
Code = CommonResponseCodeEnum.Success,
Info = "获取成功",
Data = dtos
};
}
/// <summary>
/// 分页查询问题
/// </summary>
[HttpPost("QuestionList")]
public async Task<BaseCommonResponse> GetQuestionList([FromBody] FaqQueryParam param)
{
var result = await _faqService.QueryQuestions(param.CategoryId, param.PageIndex, param.PageSize);
var dtos = result.list.Select(MapToQuestionDto).ToList();
return new PageDataResponse<FaqQuestionDto>
{
Code = CommonResponseCodeEnum.Success,
Info = "获取成功",
Data = dtos,
Count = (int)result.count
};
}
/// <summary>
/// 获取问题详情
/// </summary>
[HttpGet("QuestionDetail")]
public async Task<BaseResponse<FaqQuestionDto>> GetQuestionDetail(string id)
{
var question = await _faqService.GetQuestionById(id);
if (question == null)
return this.ToFailResult<FaqQuestionDto>("问题不存在");
return this.ToResult(MapToQuestionDto(question));
}
private static FaqCategoryTreeNodeDto MapToTreeNodeDto(FaqCategoryTreeNode entity)
{
if (entity == null) return new FaqCategoryTreeNodeDto();
return new FaqCategoryTreeNodeDto
{
Id = entity.Id ?? "",
Name = entity.Name ?? "",
Icon = entity.Icon ?? "",
Sort = entity.Sort,
IsEnable = entity.IsEnable,
Children = entity.Children?.Select(MapToTreeNodeDto).ToList() ?? new List<FaqCategoryTreeNodeDto>(),
QuestionCount = entity.QuestionCount
};
}
private static FaqQuestionDto MapToQuestionDto(FaqQuestion entity)
{
if (entity == null) return new FaqQuestionDto();
return new FaqQuestionDto
{
Id = entity.Id ?? "",
CategoryId = entity.CategoryId ?? "",
Title = entity.Title ?? "",
Content = entity.Content ?? "",
Sort = entity.Sort,
IsEnable = entity.IsEnable,
CreateTime = entity.CreateTime
};
}
}
public class FaqCategoryTreeNodeDto
{
public string Id { get; set; }
public string Name { get; set; }
public string Icon { get; set; }
public int Sort { get; set; }
public bool IsEnable { get; set; }
public List<FaqCategoryTreeNodeDto> Children { get; set; } = new();
public int QuestionCount { get; set; }
}
public class FaqQuestionDto
{
public string Id { get; set; }
public string CategoryId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int Sort { get; set; }
public bool IsEnable { get; set; }
public DateTime CreateTime { get; set; }
}
public class FaqQueryParam
{
public string? CategoryId { get; set; }
public int PageIndex { get; set; } = 1;
public int PageSize { get; set; } = 10;
}
}