refactor: 统一业务异常处理,标准化结果码和错误响应
1. 新增并完善ResultCode枚举,补充标准HTTP状态码对应的业务状态码 2. 重构BusinessException,新增基于ResultCode的构造函数和ThrowIf扩展方法 3. 替换所有硬编码的HTTP状态码为统一的ResultCode枚举 4. 优化全局异常中间件,根据业务状态码映射对应HTTP状态码并规范化JSON响应 5. 修复OssImageHelper和AutoDotCodeConsumer中的OSS文件处理逻辑 6. 新增用户答题快照实体类 7. 清理废弃的宠物模块迁移脚本
This commit is contained in:
@ -9,6 +9,7 @@ using QYZH.InteractiveMagazine.Infrastructure.OSS;
|
||||
using QYZH.InteractiveMagazine.Infrastructure.RabbitMQ;
|
||||
using QYZH.InteractiveMagazine.IService;
|
||||
using QYZH.InteractiveMagazine.Models.Common;
|
||||
using QYZH.InteractiveMagazine.Models.Dto;
|
||||
using QYZH.InteractiveMagazine.Models.Dto.Journal;
|
||||
using QYZH.InteractiveMagazine.Models.Entity;
|
||||
using QYZH.InteractiveMagazine.Models.Enum;
|
||||
@ -34,9 +35,9 @@ public class JournalPageService(BaseRepository<Journal> JournalRepository,
|
||||
{
|
||||
|
||||
var Journal = await JournalRepository.Queryable().Where(w => w.Id == input.JournalId).FirstAsync();
|
||||
BusinessException.ThrowIf(Journal.IsNull(), "未找到书本");
|
||||
BusinessException.ThrowIf(Journal.IsNull(), "未找到书本", ResultCode.NOT_FOUND);
|
||||
|
||||
BusinessException.ThrowIf(Journal?.Status == (int)JournalStatusEnum.Archive, "书籍已归档不能修改");
|
||||
BusinessException.ThrowIf(Journal?.Status == (int)JournalStatusEnum.Archive, "书籍已归档不能修改", ResultCode.CONFLICT);
|
||||
|
||||
var pages = new List<JournalPage>();
|
||||
//var dotMatrixpages = new List<DotMatrixPage>();
|
||||
@ -65,7 +66,7 @@ public class JournalPageService(BaseRepository<Journal> JournalRepository,
|
||||
{
|
||||
//using var uow = Context.Ado.BeginTran();
|
||||
var pageData = await base.InsertRangeAsync(pages);
|
||||
BusinessException.ThrowIf(pageData.IsNull(), "创建页失败");
|
||||
BusinessException.ThrowIf(pageData.IsNull(), "创建页失败", ResultCode.GLOBAL_ERROR);
|
||||
|
||||
//var result = await dotMatrixPageRepository.InsertRangeAsync(dotMatrixpages);
|
||||
//BusinessException.ThrowIf(result, "创建点阵页失败");
|
||||
@ -77,12 +78,12 @@ public class JournalPageService(BaseRepository<Journal> JournalRepository,
|
||||
public async Task<bool> UpdateAsync(PageLayoutInput input)
|
||||
{
|
||||
var page = await Queryable().Where(w => w.Id == input.Id).FirstAsync();
|
||||
BusinessException.ThrowIf(page == null, "不存在此页");
|
||||
BusinessException.ThrowIf(page == null, "不存在此页", ResultCode.NOT_FOUND);
|
||||
|
||||
var Journal = await JournalRepository.GetByIdAsync(page.JournalId);
|
||||
BusinessException.ThrowIf(Journal == null, "不存在此书");
|
||||
BusinessException.ThrowIf(Journal == null, "不存在此书", ResultCode.NOT_FOUND);
|
||||
|
||||
BusinessException.ThrowIf(Journal?.Status == (int)JournalStatusEnum.Archive, "书籍已归档不能修改");
|
||||
BusinessException.ThrowIf(Journal?.Status == (int)JournalStatusEnum.Archive, "书籍已归档不能修改", ResultCode.CONFLICT);
|
||||
var transResult = await UseTranAsync(async () =>
|
||||
{
|
||||
//await dotMatrixPageRepository.Updateable().SetColumns(s => s.Area == input.Layout)
|
||||
@ -115,21 +116,24 @@ public class JournalPageService(BaseRepository<Journal> JournalRepository,
|
||||
public async Task<bool> UpdatePageNoAsync(long JournalId)
|
||||
{
|
||||
var journalEntity = await JournalRepository.Queryable().FirstAsync(w => w.Id == JournalId);
|
||||
BusinessException.ThrowIf(journalEntity == null, "不存在此期刊");
|
||||
BusinessException.ThrowIf(journalEntity == null, "不存在此期刊", ResultCode.NOT_FOUND);
|
||||
|
||||
//如果书籍状态不等于“已归档”,“已废弃”,“已铺码”的情况下,就更新状态为“已铺码”
|
||||
BusinessException.ThrowIf((JournalStatusEnum)journalEntity.Status is JournalStatusEnum.Abandoned or JournalStatusEnum.Archive or JournalStatusEnum.Codeing, "当前【状态】不允许铺码");
|
||||
BusinessException.ThrowIf((JournalStatusEnum)journalEntity.Status is JournalStatusEnum.Abandoned or JournalStatusEnum.Archive or JournalStatusEnum.Codeing, "当前【状态】不允许铺码", ResultCode.UNPROCESSABLE_ENTITY);
|
||||
|
||||
var journalPageList = await JournalPageRepository.Queryable().Where(x => x.JournalId == JournalId).OrderBy(x => x.PageNum).ToListAsync();
|
||||
BusinessException.ThrowIf(journalPageList.Count == 0, "此期刊不存在任何书页");
|
||||
BusinessException.ThrowIf(journalPageList.Count == 0, "此期刊不存在任何书页", ResultCode.NOT_FOUND);
|
||||
|
||||
var uploadPdfUrl = DomainHelper.OssFullUrl(journalEntity?.PdfUrl!);
|
||||
BusinessException.ThrowIf(string.IsNullOrWhiteSpace(uploadPdfUrl), "此期刊上传的PDF路径错误,请检查期刊PDF文件是否上传成功");
|
||||
var uploadPdfKey = journalEntity?.PdfUrl?.RemoveDomain();
|
||||
BusinessException.ThrowIf(string.IsNullOrWhiteSpace(uploadPdfKey), "此期刊上传的PDF路径错误,请检查期刊PDF文件是否上传成功", ResultCode.BAD_REQUEST);
|
||||
|
||||
var uploadPdfUrl = DomainHelper.OssFullUrl(uploadPdfKey);
|
||||
BusinessException.ThrowIf(string.IsNullOrWhiteSpace(uploadPdfUrl), "此期刊上传的PDF路径错误,请检查期刊PDF文件是否上传成功", ResultCode.BAD_REQUEST);
|
||||
|
||||
logger.LogInformation("uploadPdfUrl:" + uploadPdfUrl);
|
||||
//验证是否上传了PDF文件
|
||||
var response = await httpClientFactory.CreateClient().SendAsync(new HttpRequestMessage(HttpMethod.Head, uploadPdfUrl));
|
||||
BusinessException.ThrowIf(response.StatusCode != HttpStatusCode.OK, "获取期刊上传的PDF文件失败,请检查PDF文件是否上传成功");
|
||||
var isPdfExists = ossService.DoesObjectExist(uploadPdfKey);
|
||||
BusinessException.ThrowIf(!isPdfExists, "获取期刊上传的PDF文件失败,请检查PDF文件是否上传成功", ResultCode.GLOBAL_ERROR);
|
||||
|
||||
journalEntity.Status = (int)JournalStatusEnum.Codeing;
|
||||
|
||||
@ -154,14 +158,14 @@ public class JournalPageService(BaseRepository<Journal> JournalRepository,
|
||||
return await UseTranAsync(async () =>
|
||||
{
|
||||
var journalEntity = await JournalRepository.Queryable().FirstAsync(w => w.Id == request.JournalId);
|
||||
BusinessException.ThrowIf(journalEntity == null, "不存在此书");
|
||||
BusinessException.ThrowIf(journalEntity == null, "不存在此书", ResultCode.NOT_FOUND);
|
||||
|
||||
//如果书籍状态不等于“铺码中”则不允许回调接口更新点阵码
|
||||
BusinessException.ThrowIf(journalEntity.Status != (int)JournalStatusEnum.Codeing, "当前【状态】不允许修改铺码");
|
||||
BusinessException.ThrowIf(journalEntity.Status != (int)JournalStatusEnum.Codeing, "当前【状态】不允许修改铺码", ResultCode.UNPROCESSABLE_ENTITY);
|
||||
|
||||
var bookPageList = await JournalPageRepository.Queryable().Where(x => x.JournalId == request.JournalId).OrderBy(x => x.PageNum).ToListAsync();
|
||||
|
||||
BusinessException.ThrowIf(bookPageList.Count == 0, "此书不存在任何书页");
|
||||
BusinessException.ThrowIf(bookPageList.Count == 0, "此书不存在任何书页", ResultCode.NOT_FOUND);
|
||||
|
||||
journalEntity.Status = (int)request.Status!.Value;
|
||||
journalEntity.UpdatedAt = DateTime.Now;
|
||||
@ -177,7 +181,7 @@ public class JournalPageService(BaseRepository<Journal> JournalRepository,
|
||||
{
|
||||
journalEntity.DownloadJournalPagePdfName = request.DownloadJournalPagePdfName;
|
||||
|
||||
BusinessException.ThrowIf(request.PageNo.Length != bookPageList.Count, $"点阵码条数与页码数量不匹配,点阵码条数:{request.PageNo.Length},页码数量:{bookPageList.Count}");
|
||||
BusinessException.ThrowIf(request.PageNo.Length != bookPageList.Count, $"点阵码条数与页码数量不匹配,点阵码条数:{request.PageNo.Length},页码数量:{bookPageList.Count}", ResultCode.BAD_REQUEST);
|
||||
|
||||
|
||||
for (var i = 0; i < bookPageList.Count; i++)
|
||||
|
||||
Reference in New Issue
Block a user