feat(journal): add journal publish feature
1. add PublishAsync method to IJournalService and implement it in JournalService 2. add publish start/end time fields to Journal entity, DTOs and edit form 3. add journal publish API endpoint 4. add rabbitMQ message sending for book and page publish events 5. clean up some commented-out old controller methods
This commit is contained in:
@ -21,8 +21,17 @@ public class JournalService(BaseRepository<JournalPage> JournalPageRepository,
|
||||
BaseRepository<JournalPageTaskAnswer> JournalPageTaskAnswerRepository,
|
||||
BaseRepository<JournalCatalog> JournalCatalogRepository,
|
||||
BaseRepository<DotFile> dotFileRepository,
|
||||
BaseRepository<DotFileDetail> dotFileDetailRepository, OssService ossService, ILogger<JournalService> logger) : BaseRepository<Journal>, IJournalService
|
||||
BaseRepository<DotFileDetail> dotFileDetailRepository,
|
||||
OssService ossService,
|
||||
IRabbitMQService rabbitMqService,
|
||||
ILogger<JournalService> logger) : BaseRepository<Journal>, IJournalService
|
||||
{
|
||||
private const string JournalExchange = "ex.journal";
|
||||
private const string PublishBookQueue = "mq.journal.publish.book";
|
||||
private const string PublishBookRoutingKey = "rk.journal.publish.book";
|
||||
private const string PublishBookPageQueue = "mq.journal.publish.bookpage";
|
||||
private const string PublishBookPageRoutingKey = "rk.journal.publish.bookpage";
|
||||
|
||||
/// <summary>
|
||||
/// 查询List
|
||||
/// </summary>
|
||||
@ -82,6 +91,8 @@ public class JournalService(BaseRepository<JournalPage> JournalPageRepository,
|
||||
Cover = input.Cover?.RemoveDomain(),
|
||||
BackCover = input.BackCover?.RemoveDomain(),
|
||||
PdfPreviewUrl = input.PdfPreviewUrl?.RemoveDomain(),
|
||||
StartTime = input.StartTime,
|
||||
EndTime = input.EndTime,
|
||||
CreatedBy = "System",
|
||||
UpdatedBy = "System",
|
||||
CreatedAt = DateTime.Now,
|
||||
@ -167,6 +178,9 @@ public class JournalService(BaseRepository<JournalPage> JournalPageRepository,
|
||||
Journal.Height = input.Height;
|
||||
Journal.Name = input.Name;
|
||||
Journal.Title = input.Title;
|
||||
Journal.StartTime = input.StartTime;
|
||||
Journal.EndTime = input.EndTime;
|
||||
Journal.UpdatedAt = DateTime.Now;
|
||||
|
||||
var res = await UseTranAsync(async () =>
|
||||
{
|
||||
@ -320,4 +334,59 @@ public class JournalService(BaseRepository<JournalPage> JournalPageRepository,
|
||||
var res = await base.Updateable().SetColumns(s => s.Status, status).Where(w => w.Id == id).ExecuteCommandAsync() > 0;
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> PublishAsync(long id)
|
||||
{
|
||||
var book = await base.GetByIdAsync(id);
|
||||
BusinessException.ThrowIf(book == null || book.IsDeleted, "杂志不存在", ResultCode.NOT_FOUND);
|
||||
|
||||
var pages = await JournalPageRepository.Queryable()
|
||||
.Where(x => x.JournalId == id && !x.IsDeleted)
|
||||
.OrderBy(x => x.PageNum)
|
||||
.OrderBy(x => x.Sort)
|
||||
.ToListAsync();
|
||||
BusinessException.ThrowIf(pages.Count == 0, "书籍未添加任何书页,无法发布", ResultCode.UNPROCESSABLE_ENTITY);
|
||||
|
||||
BusinessException.ThrowIf(!book.StartTime.HasValue, "发布开始时间不能为空", ResultCode.UNPROCESSABLE_ENTITY);
|
||||
BusinessException.ThrowIf(!book.EndTime.HasValue, "发布结束时间不能为空", ResultCode.UNPROCESSABLE_ENTITY);
|
||||
BusinessException.ThrowIf(book.EndTime < book.StartTime, "发布结束时间不能早于开始时间", ResultCode.UNPROCESSABLE_ENTITY);
|
||||
|
||||
var bookMessageSent = await rabbitMqService.SendAsync(new RabbitMQSendParam
|
||||
{
|
||||
Exchange = JournalExchange,
|
||||
Queue = PublishBookQueue,
|
||||
RoutingKey = PublishBookRoutingKey,
|
||||
Data = new JournalPublishBookMessage
|
||||
{
|
||||
BookId = id,
|
||||
StartTime = book.StartTime.Value,
|
||||
EndTime = book.EndTime.Value
|
||||
}
|
||||
});
|
||||
BusinessException.ThrowIf(!bookMessageSent, "发布书籍消息发送失败", ResultCode.GLOBAL_ERROR);
|
||||
|
||||
foreach (var page in pages)
|
||||
{
|
||||
var pageMessageSent = await rabbitMqService.SendAsync(new RabbitMQSendParam
|
||||
{
|
||||
Exchange = JournalExchange,
|
||||
Queue = PublishBookPageQueue,
|
||||
RoutingKey = PublishBookPageRoutingKey,
|
||||
Data = new JournalPublishBookPageMessage
|
||||
{
|
||||
BookId = id,
|
||||
PageId = page.Id,
|
||||
PageNo = page.PageNo,
|
||||
Layout = page.Layout
|
||||
}
|
||||
});
|
||||
BusinessException.ThrowIf(!pageMessageSent, $"发布书页消息发送失败,PageId: {page.Id}", ResultCode.GLOBAL_ERROR);
|
||||
}
|
||||
|
||||
return await base.Updateable()
|
||||
.SetColumns(s => s.Status, JournalStatusEnum.Published)
|
||||
.SetColumns(s => s.UpdatedAt, DateTime.Now)
|
||||
.Where(w => w.Id == id)
|
||||
.ExecuteCommandAsync() > 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user