feat(core): add content controller with tests (articles CRUD)
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { InputParseError } from "@/entities/errors/common.js";
|
||||
import type { Article } from "@/entities/models/article.js";
|
||||
import { createArticleUseCase } from "@/application/use-cases/content/create-article.use-case.js";
|
||||
import { getArticlesUseCase } from "@/application/use-cases/content/get-articles.use-case.js";
|
||||
|
||||
const createInputSchema = z.object({
|
||||
title: z.string().min(1).max(255),
|
||||
content: z.string(),
|
||||
authorId: z.string(),
|
||||
slug: z.string().optional(),
|
||||
});
|
||||
|
||||
const getInputSchema = z.object({
|
||||
status: z.string().optional(),
|
||||
authorId: z.string().optional(),
|
||||
limit: z.number().optional(),
|
||||
offset: z.number().optional(),
|
||||
});
|
||||
|
||||
export async function createArticleController(
|
||||
input: Partial<z.infer<typeof createInputSchema>>
|
||||
): Promise<Article> {
|
||||
const { data, error: inputParseError } = createInputSchema.safeParse(input);
|
||||
|
||||
if (inputParseError) {
|
||||
throw new InputParseError("Invalid data", { cause: inputParseError });
|
||||
}
|
||||
|
||||
return await createArticleUseCase(data);
|
||||
}
|
||||
|
||||
export async function getArticlesController(
|
||||
input: Partial<z.infer<typeof getInputSchema>>
|
||||
): Promise<Article[]> {
|
||||
const { data, error: inputParseError } = getInputSchema.safeParse(input);
|
||||
|
||||
if (inputParseError) {
|
||||
throw new InputParseError("Invalid data", { cause: inputParseError });
|
||||
}
|
||||
|
||||
return await getArticlesUseCase(data);
|
||||
}
|
||||
Reference in New Issue
Block a user