From 41d7883e7aea53e4b065ce51c378fca4f0569d8d Mon Sep 17 00:00:00 2001 From: Danijel Martinek Date: Mon, 4 May 2026 22:14:14 +0200 Subject: [PATCH] feat(blog): add MockArticlesRepository for tests --- .../repositories/mock-articles.repository.ts | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 packages/blog/src/infrastructure/repositories/mock-articles.repository.ts diff --git a/packages/blog/src/infrastructure/repositories/mock-articles.repository.ts b/packages/blog/src/infrastructure/repositories/mock-articles.repository.ts new file mode 100644 index 0000000..269e30b --- /dev/null +++ b/packages/blog/src/infrastructure/repositories/mock-articles.repository.ts @@ -0,0 +1,54 @@ +import "reflect-metadata"; +import { injectable } from "inversify"; + +import type { IArticlesRepository } from "@/application/repositories/articles-repository.interface"; +import type { Article } from "@/entities/article"; + +@injectable() +export class MockArticlesRepository implements IArticlesRepository { + private _articles: Article[] = []; + + async getArticle(id: string): Promise
{ + return this._articles.find((a) => a.id === id); + } + + async getArticleBySlug(slug: string): Promise
{ + return this._articles.find((a) => a.slug === slug); + } + + async getArticles(options?: { + status?: string; + authorId?: string; + limit?: number; + offset?: number; + }): Promise { + let result = [...this._articles]; + if (options?.status) { + result = result.filter((a) => a.status === options.status); + } + if (options?.authorId) { + result = result.filter((a) => a.authorId === options.authorId); + } + const offset = options?.offset ?? 0; + const limit = options?.limit ?? 50; + return result.slice(offset, offset + limit); + } + + async createArticle(input: Article): Promise
{ + this._articles.push(input); + return input; + } + + async updateArticle( + id: string, + input: Partial
, + ): Promise
{ + const index = this._articles.findIndex((a) => a.id === id); + if (index === -1) return undefined; + const current = this._articles[index]; + if (!current) return undefined; + const updated: Article = { ...current, ...input, id: current.id }; + this._articles[index] = updated; + return updated; + } +}