Files
yes.blog/Yes.Blog/Endpoints/Admins/Pages/UpdatePageEndpoint.cs
2025-05-04 22:11:28 +08:00

54 lines
1.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

namespace Yes.Blog.Endpoints.Admins.Pages
{
public class UpdatePageEndpoint : PageEndpointScheme, IEndpoint
{
public void Map(IEndpointRouteBuilder app) => app.MapPut("/pages", Handle).WithRequestValidation<Request>();
public record Request(
int Id,
string Title,
string? Slug,
string Content,
string? CoverUrl,
string? Summary,
int Sort
);
public class RequestValidator : AbstractValidator<Request>
{
public RequestValidator()
{
RuleFor(x => x.Content).NotEmpty().WithMessage("文章内容不能为空!").NotNull().WithMessage("文章内容不能为空!");
RuleFor(x => x.Title).NotEmpty().WithMessage("标题不能为空!").NotNull().WithMessage("标题不能为空!");
RuleFor(x => x.CoverUrl).MaximumLength(256).WithMessage("封面图地址长度不能超过256!");
RuleFor(x => x.Slug).MaximumLength(128).WithMessage("缩略名长度不能超过128!");
RuleFor(x => x.Slug).NotEmpty().WithMessage("缩略名不能为空!")
.Must(x => x.IsSlug())
.WithMessage(x => $"缩略名{x.Slug}无效,只能为字母数字横线和下划线组合,且不能使用纯数字!");
}
}
private async Task<IResult> Handle([FromBody] Request request, IMediator mediator, IMapper mapper, CancellationToken cancellationToken)
{
var command = mapper.Map<UpdatePageCommand>(request);
var response = await mediator.Send(command, cancellationToken);
return Result.Ok(response);
}
}
internal class UpdatePageMappingProfile : IRegister
{
public void Register(TypeAdapterConfig config)
{
config.ForType<UpdatePageEndpoint.Request, UpdatePageCommand>();
}
}
}