feat(core): add content use cases with tests (create-article, get-articles)
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import type { Article } from "@/entities/models/article.js";
|
||||
import { getInjection } from "@/di/container.js";
|
||||
|
||||
function generateSlug(title: string): string {
|
||||
return title
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, "-")
|
||||
.replace(/^-|-$/g, "");
|
||||
}
|
||||
|
||||
export async function createArticleUseCase(input: {
|
||||
title: string;
|
||||
content: string;
|
||||
authorId: string;
|
||||
slug?: string;
|
||||
}): Promise<Article> {
|
||||
const articlesRepository = getInjection("IArticlesRepository");
|
||||
|
||||
const now = new Date();
|
||||
const article: Article = {
|
||||
id: crypto.randomUUID(),
|
||||
title: input.title,
|
||||
slug: input.slug ?? generateSlug(input.title),
|
||||
content: input.content,
|
||||
status: "draft",
|
||||
authorId: input.authorId,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
};
|
||||
|
||||
return await articlesRepository.createArticle(article);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import type { Article } from "@/entities/models/article.js";
|
||||
import { getInjection } from "@/di/container.js";
|
||||
|
||||
export async function getArticlesUseCase(options?: {
|
||||
status?: string;
|
||||
authorId?: string;
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
}): Promise<Article[]> {
|
||||
const articlesRepository = getInjection("IArticlesRepository");
|
||||
return await articlesRepository.getArticles(options);
|
||||
}
|
||||
Reference in New Issue
Block a user