添加项目文件。
This commit is contained in:
@@ -0,0 +1,246 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Common;
|
||||
using CommonDomain;
|
||||
using CommonInfrastructure;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using AdminService.Faq;
|
||||
|
||||
namespace AdminApi.Faq
|
||||
{
|
||||
[ApiController, Route("adminapi/[controller]")]
|
||||
public class FaqController : BaseController
|
||||
{
|
||||
private readonly IFaqService _faqService;
|
||||
|
||||
public FaqController(IFaqService faqService)
|
||||
{
|
||||
_faqService = faqService;
|
||||
}
|
||||
|
||||
|
||||
[HttpGet("Categories")]
|
||||
public async Task<BaseCommonResponse> GetCategories()
|
||||
{
|
||||
var list = await _faqService.GetAllCategories();
|
||||
if (list == null)
|
||||
return new ListResponse<FaqCategoryDto> { Code = CommonResponseCodeEnum.Success, Data = new List<FaqCategoryDto>() };
|
||||
var dtos = list.Select(MapToCategoryDto).ToList();
|
||||
return new ListResponse<FaqCategoryDto>
|
||||
{
|
||||
Code = CommonResponseCodeEnum.Success,
|
||||
Info = "获取成功",
|
||||
Data = dtos
|
||||
};
|
||||
}
|
||||
|
||||
[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
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet("CategoryDetail")]
|
||||
public async Task<BaseResponse<FaqCategoryDto>> GetCategoryDetail(string id)
|
||||
{
|
||||
var category = await _faqService.GetCategoryById(id);
|
||||
if (category == null)
|
||||
return this.ToFailResult<FaqCategoryDto>("板块不存在");
|
||||
|
||||
return this.ToResult(MapToCategoryDto(category));
|
||||
}
|
||||
|
||||
[HttpPost("AddCategory")]
|
||||
public async Task<BaseResponse<string>> AddCategory([FromBody] FaqCategory category)
|
||||
{
|
||||
var result = await _faqService.AddCategory(category);
|
||||
if (result.ok)
|
||||
return this.ToResult(result.msg);
|
||||
else
|
||||
return this.ToFailResult<string>(result.msg);
|
||||
}
|
||||
|
||||
[HttpPost("UpdateCategory")]
|
||||
public async Task<BaseResponse<string>> UpdateCategory([FromBody] FaqCategory category)
|
||||
{
|
||||
var result = await _faqService.UpdateCategory(category);
|
||||
if (result.ok)
|
||||
return this.ToResult(result.msg);
|
||||
else
|
||||
return this.ToFailResult<string>(result.msg);
|
||||
}
|
||||
|
||||
[HttpPost("DeleteCategory")]
|
||||
public async Task<BaseResponse<string>> DeleteCategory(string id)
|
||||
{
|
||||
var result = await _faqService.DeleteCategory(id);
|
||||
if (result.ok)
|
||||
return this.ToResult(result.msg);
|
||||
else
|
||||
return this.ToFailResult<string>(result.msg);
|
||||
}
|
||||
|
||||
|
||||
[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
|
||||
};
|
||||
}
|
||||
|
||||
[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
|
||||
};
|
||||
}
|
||||
|
||||
[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));
|
||||
}
|
||||
|
||||
[HttpPost("AddQuestion")]
|
||||
public async Task<BaseResponse<string>> AddQuestion([FromBody] FaqQuestion question)
|
||||
{
|
||||
var result = await _faqService.AddQuestion(question);
|
||||
if (result.ok)
|
||||
return this.ToResult(result.msg);
|
||||
else
|
||||
return this.ToFailResult<string>(result.msg);
|
||||
}
|
||||
|
||||
[HttpPost("UpdateQuestion")]
|
||||
public async Task<BaseResponse<string>> UpdateQuestion([FromBody] FaqQuestion question)
|
||||
{
|
||||
var result = await _faqService.UpdateQuestion(question);
|
||||
if (result.ok)
|
||||
return this.ToResult(result.msg);
|
||||
else
|
||||
return this.ToFailResult<string>(result.msg);
|
||||
}
|
||||
|
||||
[HttpPost("DeleteQuestion")]
|
||||
public async Task<BaseResponse<string>> DeleteQuestion(string id)
|
||||
{
|
||||
var result = await _faqService.DeleteQuestion(id);
|
||||
if (result.ok)
|
||||
return this.ToResult(result.msg);
|
||||
else
|
||||
return this.ToFailResult<string>(result.msg);
|
||||
}
|
||||
|
||||
|
||||
private static FaqCategoryDto MapToCategoryDto(FaqCategory entity)
|
||||
{
|
||||
if (entity == null) return new FaqCategoryDto();
|
||||
return new FaqCategoryDto
|
||||
{
|
||||
Id = entity.Id ?? "",
|
||||
ParentId = entity.ParentId ?? "",
|
||||
Name = entity.Name ?? "",
|
||||
Icon = entity.Icon ?? "",
|
||||
Sort = entity.Sort,
|
||||
IsEnable = entity.IsEnable
|
||||
};
|
||||
}
|
||||
|
||||
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 FaqCategoryDto
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string ParentId { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Icon { get; set; }
|
||||
public int Sort { get; set; }
|
||||
public bool IsEnable { get; set; }
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user