diff --git a/packages/blog/src/application/use-cases/get-articles.use-case.test.ts b/packages/blog/src/application/use-cases/get-articles.use-case.test.ts new file mode 100644 index 0000000..1807af4 --- /dev/null +++ b/packages/blog/src/application/use-cases/get-articles.use-case.test.ts @@ -0,0 +1,64 @@ +import { beforeEach, describe, expect, it } from "vitest"; +import { blogContainer } from "@/di/container"; +import { BLOG_SYMBOLS } from "@/di/symbols"; +import type { IArticlesRepository } from "@/application/repositories/articles-repository.interface"; +import { MockArticlesRepository } from "@/infrastructure/repositories/mock-articles.repository"; +import { getArticlesUseCase } from "./get-articles.use-case"; + +describe("getArticlesUseCase", () => { + let repo: MockArticlesRepository; + + beforeEach(() => { + if (blogContainer.isBound(BLOG_SYMBOLS.IArticlesRepository)) { + blogContainer.unbind(BLOG_SYMBOLS.IArticlesRepository); + } + repo = new MockArticlesRepository(); + blogContainer + .bind(BLOG_SYMBOLS.IArticlesRepository) + .toConstantValue(repo); + }); + + it("returns all articles with no filters", async () => { + const now = new Date(); + await repo.createArticle({ + id: "1", + title: "A", + slug: "a", + content: null, + status: "draft", + authorId: "u1", + createdAt: now, + updatedAt: now, + }); + const result = await getArticlesUseCase(); + expect(result).toHaveLength(1); + expect(result[0]?.id).toBe("1"); + }); + + it("filters by status", async () => { + const now = new Date(); + await repo.createArticle({ + id: "1", + title: "A", + slug: "a", + content: null, + status: "draft", + authorId: "u1", + createdAt: now, + updatedAt: now, + }); + await repo.createArticle({ + id: "2", + title: "B", + slug: "b", + content: null, + status: "published", + authorId: "u1", + createdAt: now, + updatedAt: now, + }); + const result = await getArticlesUseCase({ status: "published" }); + expect(result).toHaveLength(1); + expect(result[0]?.id).toBe("2"); + }); +}); diff --git a/packages/blog/src/application/use-cases/get-articles.use-case.ts b/packages/blog/src/application/use-cases/get-articles.use-case.ts new file mode 100644 index 0000000..8a8b70f --- /dev/null +++ b/packages/blog/src/application/use-cases/get-articles.use-case.ts @@ -0,0 +1,16 @@ +import type { Article } from "@/entities/article"; +import { blogContainer } from "@/di/container"; +import { BLOG_SYMBOLS } from "@/di/symbols"; +import type { IArticlesRepository } from "@/application/repositories/articles-repository.interface"; + +export async function getArticlesUseCase(options?: { + status?: string; + authorId?: string; + limit?: number; + offset?: number; +}): Promise { + const repo = blogContainer.get( + BLOG_SYMBOLS.IArticlesRepository, + ); + return repo.getArticles(options); +}