This commit is contained in:
2025-04-30 09:51:36 +08:00
parent 561881c9aa
commit 09afa239a6
848 changed files with 385870 additions and 205 deletions

View File

@@ -0,0 +1,49 @@
namespace Yes.Application.Admins.Pages
{
public record CreatePageCommand(
string Title,
string? Slug,
string Content,
string? CoverUrl,
string? Summary,
int Sort
) : IRequest<CreatePageCommandResponse>;
public record CreatePageCommandResponse(int Id);
public class CreatePageCommandHandler(
IArticleService articleService,
BlogDbContext db,
IIdentityContext identity,
IMediator mediator) : IRequestHandler<CreatePageCommand, CreatePageCommandResponse>
{
private readonly IArticleService _articleService = articleService;
private readonly BlogDbContext _db = db;
private readonly IIdentityContext _identity = identity;
private readonly IMediator _mediator = mediator;
public async Task<CreatePageCommandResponse> Handle(CreatePageCommand request, CancellationToken cancellationToken)
{
var userId = _identity.Id;
if (!string.IsNullOrEmpty(request.Slug) && await _articleService.IsSlugInUse(request.Slug))
{
throw new SlugInUseException(request.Slug);
}
var author = await _db.Users.FindAsync(userId);
var authorObject = ValueObject<UserEntity>.Create(userId, author);
var page = ArticleEntity.CreatePage(request.Title, request.Content, request.Summary, request.Slug, request.CoverUrl, request.Sort, authorObject);
await _db.Articles.AddAsync(page);
await _db.SaveChangesAsync();
await _mediator.Publish(new PageAddedEvent(page.Id));
return new CreatePageCommandResponse(page.Id);
}
}
}

View File

@@ -0,0 +1,24 @@
namespace Yes.Application.Admins.Pages
{
public record DeletePageCommand(int Id) : IRequest<DeletePageCommandResponse>;
public record DeletePageCommandResponse(int Id);
public class DeletePageCommandHandler(BlogDbContext db) : IRequestHandler<DeletePageCommand, DeletePageCommandResponse>
{
private readonly BlogDbContext _db = db;
public async Task<DeletePageCommandResponse> Handle(DeletePageCommand request, CancellationToken cancellationToken)
{
var article = await _db.Articles.FindAsync(request.Id);
if (article != null && article.IsPage())
{
article.Delete();
_db.Articles.Update(article);
await _db.SaveChangesAsync();
}
return new DeletePageCommandResponse(request.Id);
}
}
}

View File

@@ -0,0 +1,46 @@
namespace Yes.Application.Admins.Pages
{
public record GetPageQuery(int Id) : IRequest<GetPageQueryResponse>;
public record GetPageQueryResponse(
int Id,
string Title,
int CategoryId,
string Content,
string Slug,
string CoverUrl,
string? Summary,
string[] Tags,
int Sort
);
public class GetPageQueryHandler(BlogDbContext db,IMapper mapper) : IRequestHandler<GetPageQuery, GetPageQueryResponse>
{
private readonly BlogDbContext _db = db;
private readonly IMapper _mapper = mapper;
public async Task<GetPageQueryResponse> Handle(GetPageQuery request, CancellationToken cancellationToken)
{
var page = await _db.Articles.FindAsync(request.Id);
if (page == null)
{
throw new PageNotExistsException(request.Id);
}
var response = _mapper.Map<GetPageQueryResponse>(page);
return response;
}
}
internal class CreatePageMappingProfile : IRegister
{
public void Register(TypeAdapterConfig config)
{
config.ForType<ArticleEntity, GetPageQueryResponse>()
.Map(dest => dest.Tags, src => src.Tag.ToTags()); ;
}
}
}

View File

@@ -0,0 +1,33 @@
namespace Yes.Application.Admins.Pages
{
public record GetPagesQuery() : IRequest<List<GetPagesQueryResponse>>;
public record GetPagesQueryResponse(
int Id,
string Title,
DateTime ModifyDate,
string Slug
);
public class GetPagesQueryHandler(BlogDbContext db) : IRequestHandler<GetPagesQuery, List<GetPagesQueryResponse>>
{
private readonly BlogDbContext _db = db;
public async Task<List<GetPagesQueryResponse>> Handle(GetPagesQuery request, CancellationToken cancellationToken)
{
//throw new Exception("111");
var Pages = await _db.Articles.Where(x=>x.Type== ArticleTypeEnum.Page && x.Status != ArticleStatusEnum.Deleted).Select(x =>
new GetPagesQueryResponse
(
x.Id,
x.Title,
x.ModifyDate,
x.Slug
)
).ToListAsync(cancellationToken);
return Pages;
}
}
}

View File

@@ -0,0 +1,20 @@
namespace Yes.Application.Admins.Pages
{
public record PageAddedEvent(int PageId) : INotification;
public class PageAddedEventHandler(BlogDbContext db, IMemoryCache cache) : INotificationHandler<PageAddedEvent>
{
private readonly BlogDbContext _db = db;
private readonly IMemoryCache _cache = cache;
public async Task Handle(PageAddedEvent notification, CancellationToken cancellationToken)
{
var pageId = notification.PageId;
_cache.Remove($"SinglePages_10");
}
}
}

View File

@@ -0,0 +1,19 @@
namespace Yes.Application.Admins.Pages
{
public record PageUpdatedEvent(int PageId) : INotification;
public class PageUpdatedEventHandler(BlogDbContext db, IMemoryCache cache) : INotificationHandler<PageUpdatedEvent>
{
private readonly BlogDbContext _db = db;
private readonly IMemoryCache _cache = cache;
public async Task Handle(PageUpdatedEvent notification, CancellationToken cancellationToken)
{
var pageId = notification.PageId;
_cache.Remove($"SinglePages_10");
}
}
}

View File

@@ -0,0 +1,67 @@
namespace Yes.Application.Admins.Pages
{
public record UpdatePageCommand(
int Id,
string Title,
string? Slug,
string Content,
string? CoverUrl,
string? Summary,
int Sort
) : IRequest<UpdatePageCommandResponse>;
public record UpdatePageCommandResponse(int Id);
public class UpdatePageCommandHandler(
IArticleService articleService,
BlogDbContext db,
IIdentityContext identity,
IMediator mediator) : IRequestHandler<UpdatePageCommand, UpdatePageCommandResponse>
{
private readonly IArticleService _articleService = articleService;
private readonly BlogDbContext _db = db;
private readonly IIdentityContext _identity = identity;
private readonly IMediator _mediator = mediator;
public async Task<UpdatePageCommandResponse> Handle(UpdatePageCommand request, CancellationToken cancellationToken)
{
var userId = _identity.Id;
var author = await _db.Users.FindAsync(userId);
var authorObject = ValueObject<UserEntity>.Create(userId, author);
var page = await _db.Articles.FindAsync(request.Id);
if (page == null)
{
throw new ArticleNotExistsException(request.Id);
}
if (!string.IsNullOrEmpty(request.Slug) &&
request.Slug != page.Id.ToString() &&
request.Slug != page.Slug.ToString() &&
await _articleService.IsSlugInUse(request.Slug))
{
throw new SlugInUseException(request.Slug);
}
page.UpdatePage(request.Title, request.Content, request.Summary, request.Slug, request.CoverUrl, request.Sort, authorObject);
if (page.IsSlugEmpty())
{
var slug = await _articleService.CreateUniqueSlug();
page.UpdateSlug(slug);
}
_db.Articles.Update(page);
await _db.SaveChangesAsync();
await _mediator.Publish(new PageUpdatedEvent(page.Id));
return new UpdatePageCommandResponse(request.Id);
}
}
}