62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
'use server';
|
|
|
|
import {
|
|
ArticleModel,
|
|
CreateArticleModel,
|
|
PaginatedArticlesResult,
|
|
UpdateArticleModel,
|
|
} from '@/lib/feature/article/article.model';
|
|
import * as service from '@/lib/feature/article/article.service';
|
|
import { getSessionData } from '@/lib/session/session-storage';
|
|
import { UUIDv4 } from '@/utils/types/uuid';
|
|
|
|
export const getArticleByExternalId = async (
|
|
externalId: UUIDv4
|
|
): Promise<ArticleModel | null> => {
|
|
return await service.getArticleByExternalId(externalId);
|
|
};
|
|
|
|
export const getArticleBySlug = async (
|
|
slug: string
|
|
): Promise<ArticleModel | null> => {
|
|
return await service.getArticleBySlug(slug);
|
|
};
|
|
|
|
export const getArticlesPaginated = async (
|
|
page: number = 1,
|
|
pageSize: number = 10
|
|
): Promise<PaginatedArticlesResult> => {
|
|
return await service.getArticlesPaginated(page, pageSize);
|
|
};
|
|
|
|
export const saveArticle = async (
|
|
article: CreateArticleModel
|
|
): Promise<ArticleModel> => {
|
|
const session = await getSessionData();
|
|
if (!session || !session?.user || session?.user.role !== 'admin') {
|
|
throw new Error('Unauthorized: Only admin users can save articles.');
|
|
}
|
|
article.authorId = session.user.id;
|
|
|
|
return await service.saveArticle(article);
|
|
};
|
|
|
|
export const updateArticle = async (
|
|
articleId: string,
|
|
article: UpdateArticleModel
|
|
): Promise<ArticleModel> => {
|
|
const session = await getSessionData();
|
|
if (!session || !session?.user || session?.user.role !== 'admin') {
|
|
throw new Error('Unauthorized: Only admin users can save articles.');
|
|
}
|
|
return await service.updateArticle(articleId, article);
|
|
};
|
|
|
|
export const deleteArticle = async (articleId: string): Promise<void> => {
|
|
const session = await getSessionData();
|
|
if (!session || !session?.user || session?.user.role !== 'admin') {
|
|
throw new Error('Unauthorized: Only admin users can delete articles.');
|
|
}
|
|
await service.deleteArticle(articleId);
|
|
};
|