refactor: update article service methods to use external ID and improve caching
All checks were successful
Build and Test / run-test (20.x) (push) Successful in 2m5s
All checks were successful
Build and Test / run-test (20.x) (push) Successful in 2m5s
This commit is contained in:
@@ -8,42 +8,34 @@ import {
|
||||
} from '@/lib/feature/article/article.model';
|
||||
import * as service from '@/lib/feature/article/article.service';
|
||||
import { getSessionData } from '@/lib/session/session-storage';
|
||||
import { TypedResult, wrap, wrapCached } from '@/utils/types/results';
|
||||
import { TypedResult, wrap } from '@/utils/types/results';
|
||||
import { UUIDv4 } from '@/utils/types/uuid';
|
||||
import { revalidateTag } from 'next/cache';
|
||||
|
||||
export const getArticleByExternalId: (
|
||||
externalId: UUIDv4
|
||||
) => Promise<TypedResult<ArticleModel | null>> = wrapCached(
|
||||
) => Promise<TypedResult<ArticleModel | null>> = wrap(
|
||||
async (externalId: UUIDv4): Promise<ArticleModel | null> => {
|
||||
const result = await service.getArticleByExternalId(externalId);
|
||||
if (!result.ok) throw result.error;
|
||||
return result.value;
|
||||
},
|
||||
{
|
||||
key: (id) => [`article:${id}`],
|
||||
tags: (id) => ['articles', `article:${id}`],
|
||||
}
|
||||
);
|
||||
|
||||
export const getArticleBySlug: (
|
||||
slug: string
|
||||
) => Promise<TypedResult<ArticleModel | null>> = wrapCached(
|
||||
) => Promise<TypedResult<ArticleModel | null>> = wrap(
|
||||
async (slug: string): Promise<ArticleModel | null> => {
|
||||
const result = await service.getArticleBySlug(slug);
|
||||
if (!result.ok) throw result.error;
|
||||
return result.value;
|
||||
},
|
||||
{
|
||||
key: (slug) => [`article:slug:${slug}`],
|
||||
tags: (slug) => ['articles', `article:slug:${slug}`],
|
||||
}
|
||||
);
|
||||
|
||||
export const getArticlesPaginated: (
|
||||
page?: number,
|
||||
pageSize?: number
|
||||
) => Promise<TypedResult<PaginatedArticlesResult>> = wrapCached(
|
||||
) => Promise<TypedResult<PaginatedArticlesResult>> = wrap(
|
||||
async (
|
||||
page: number = 1,
|
||||
pageSize: number = 10
|
||||
@@ -51,13 +43,6 @@ export const getArticlesPaginated: (
|
||||
const result = await service.getArticlesPaginated(page, pageSize);
|
||||
if (!result.ok) throw result.error;
|
||||
return result.value;
|
||||
},
|
||||
{
|
||||
key: (page, pageSize) => [`articles:page:${page}:${pageSize}`],
|
||||
tags: (page, pageSize) => [
|
||||
'articles',
|
||||
`articles:page:${page}-${pageSize}`,
|
||||
],
|
||||
}
|
||||
);
|
||||
|
||||
@@ -81,12 +66,12 @@ export const saveArticle: (
|
||||
}
|
||||
);
|
||||
|
||||
export const updateArticle: (
|
||||
articleId: string,
|
||||
export const updateArticleByExternalId: (
|
||||
externalId: UUIDv4,
|
||||
article: UpdateArticleModel
|
||||
) => Promise<TypedResult<ArticleModel>> = wrap(
|
||||
async (
|
||||
articleId: string,
|
||||
externalId: UUIDv4,
|
||||
article: UpdateArticleModel
|
||||
): Promise<ArticleModel> => {
|
||||
const session = await getSessionData();
|
||||
@@ -96,11 +81,14 @@ export const updateArticle: (
|
||||
);
|
||||
}
|
||||
|
||||
const result = await service.updateArticle(articleId, article);
|
||||
const result = await service.updateArticleByExternalId(
|
||||
externalId,
|
||||
article
|
||||
);
|
||||
if (!result.ok) throw result.error;
|
||||
|
||||
revalidateTag('articles', 'max');
|
||||
revalidateTag(`article:${articleId}`, 'max');
|
||||
revalidateTag(`article:${externalId}`, 'max');
|
||||
revalidateTag(`article:slug:${result.value.slug}`, 'max');
|
||||
|
||||
return result.value;
|
||||
|
||||
Reference in New Issue
Block a user