Files
taxbaik/TaxBaik.Application/Services/BlogService.cs
T

138 lines
5.2 KiB
C#

namespace TaxBaik.Application.Services;
using System.Text.RegularExpressions;
using TaxBaik.Application.DTOs;
using TaxBaik.Domain.Entities;
using TaxBaik.Domain.Interfaces;
public class BlogService(IBlogPostRepository repository)
{
public async Task<BlogPost?> GetBySlugAsync(string slug, CancellationToken ct = default) =>
await repository.GetBySlugAsync(slug, ct);
public async Task<(IEnumerable<BlogPost>, int)> GetPublishedPagedAsync(
int page, int pageSize, int? categoryId = null, CancellationToken ct = default) =>
await repository.GetPublishedPagedAsync(NormalizePage(page), NormalizePageSize(pageSize), categoryId, ct);
public async Task<IEnumerable<BlogPost>> GetAllAsync(CancellationToken ct = default) =>
await repository.GetAllForAdminAsync(ct);
public async Task<IEnumerable<BlogPost>> GetAllForAdminAsync(CancellationToken ct = default) =>
await repository.GetAllForAdminAsync(ct);
public async Task<int> CreateAsync(BlogPost post, CancellationToken ct = default)
{
ValidatePost(post);
post.Title = post.Title.Trim();
post.Content = post.Content.Trim();
post.Slug = await GenerateUniqueSlugAsync(post.Title, ct: ct);
post.PublishedAt = post.IsPublished ? DateTime.UtcNow : null;
return await repository.CreateAsync(post, ct);
}
public async Task<BlogPost> CreateAsync(CreateBlogPostDto dto, CancellationToken ct = default)
{
var post = new BlogPost
{
Title = dto.Title,
Content = dto.Content,
CategoryId = dto.CategoryId,
Tags = dto.Tags,
SeoTitle = dto.SeoTitle,
SeoDescription = dto.SeoDescription,
ThumbnailUrl = dto.ThumbnailUrl,
IsPublished = dto.IsPublished,
AuthorId = dto.AuthorId,
CreatedAt = DateTime.UtcNow
};
var id = await CreateAsync(post, ct);
post.Id = id;
return post;
}
public async Task UpdateAsync(BlogPost post, CancellationToken ct = default) =>
await repository.UpdateAsync(post, ct);
public async Task<BlogPost?> UpdateAsync(int id, CreateBlogPostDto dto, CancellationToken ct = default)
{
var post = await repository.GetByIdAsync(id, ct);
if (post == null)
return null;
post.Title = dto.Title;
post.Content = dto.Content;
post.CategoryId = dto.CategoryId;
post.Tags = dto.Tags;
post.SeoTitle = dto.SeoTitle;
post.SeoDescription = dto.SeoDescription;
post.ThumbnailUrl = dto.ThumbnailUrl;
post.IsPublished = dto.IsPublished;
post.PublishedAt = dto.IsPublished
? post.PublishedAt ?? DateTime.UtcNow
: null;
ValidatePost(post);
await UpdateAsync(post, ct);
return post;
}
public async Task DeleteAsync(int id, CancellationToken ct = default) =>
await repository.DeleteAsync(id, ct);
public async Task IncrementViewCountAsync(int id, CancellationToken ct = default) =>
await repository.IncrementViewCountAsync(id, ct);
private static string GenerateSlug(string title)
{
var slug = Regex.Replace(title.ToLowerInvariant(), @"[^\w\s-]", "");
slug = Regex.Replace(slug, @"\s+", "-");
slug = Regex.Replace(slug, @"-+", "-").Trim('-');
if (string.IsNullOrWhiteSpace(slug))
slug = $"post-{DateTime.UtcNow:yyyyMMddHHmmss}";
return slug.Length > 100 ? slug[..100] : slug;
}
private async Task<string> GenerateUniqueSlugAsync(string title, int? existingPostId = null, CancellationToken ct = default)
{
var baseSlug = GenerateSlug(title);
var slug = baseSlug;
var suffix = 2;
var allPosts = (await repository.GetAllForAdminAsync(ct)).ToList();
while (allPosts.Any(x => x.Id != existingPostId && string.Equals(x.Slug, slug, StringComparison.OrdinalIgnoreCase)))
{
var suffixText = $"-{suffix++}";
var maxBaseLength = Math.Max(1, 100 - suffixText.Length);
slug = $"{baseSlug[..Math.Min(baseSlug.Length, maxBaseLength)]}{suffixText}";
}
return slug;
}
private static void ValidatePost(BlogPost post)
{
if (string.IsNullOrWhiteSpace(post.Title))
throw new ValidationException("제목을 입력하세요.");
if (string.IsNullOrWhiteSpace(post.Content))
throw new ValidationException("본문을 입력하세요.");
if (post.IsPublished && string.IsNullOrWhiteSpace(post.SeoTitle))
throw new ValidationException("발행하려면 SEO 제목을 입력하세요.");
if (post.IsPublished && string.IsNullOrWhiteSpace(post.SeoDescription))
throw new ValidationException("발행하려면 SEO 설명을 입력하세요.");
}
private static int NormalizePage(int page) => Math.Max(1, page);
private static int NormalizePageSize(int pageSize) => Math.Clamp(pageSize, 1, 100);
public async Task<(int TotalPosts, int PublishedPosts)> GetStatsAsync(CancellationToken ct = default)
{
var posts = (await repository.GetAllForAdminAsync(ct)).ToList();
return (posts.Count, posts.Count(x => x.IsPublished));
}
}