Skip to content

Commit

Permalink
refactor: refactor methods
Browse files Browse the repository at this point in the history
  • Loading branch information
fbuireu committed Sep 29, 2024
1 parent 76aaf4c commit 32f47d8
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 42 deletions.
28 changes: 4 additions & 24 deletions src/application/dto/tag/tagDTO.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
import { getCollection } from "astro:content";
import { TagType } from "@application/dto/tag/types";
import type { RawTag, TagDTO } from "@application/dto/tag/types";
import { getAuthors } from "@application/dto/tag/utils/getAuthors";
import { getTags } from "@application/dto/tag/utils/getTags";
import type { BaseDTO } from "@shared/application/dto/baseDTO";
import { getArticlesByTag } from "./utils/getArticlesByTag";
import { groupBy } from "./utils/groupBy";

export const tagDTO: BaseDTO<RawTag[], Promise<TagDTO>> = {
create: async (raw) => {
const articles = await getCollection("articles");

const tags = await Promise.all(
raw.map(async (rawTag) => {
const articlesByTag = getArticlesByTag({ rawTag, articles });

return {
name: rawTag.fields.name as unknown as string,
slug: rawTag.fields.slug,
type: TagType.TAG,
count: articlesByTag.length,
articles: articlesByTag,
};
}),
);

const authors = (await getCollection("authors")).map((author) => ({
name: author.data.name,
slug: author.data.slug,
type: TagType.AUTHOR,
count: author.data.articles.length,
articles: author.data.articles,
}));
const tags = await getTags({ raw, articles });
const authors = await getAuthors();

return groupBy({
array: [...tags, ...authors],
Expand Down
17 changes: 0 additions & 17 deletions src/application/dto/tag/utils/getArticlesByTag/getArticlesByTag.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/application/dto/tag/utils/getArticlesByTag/index.ts

This file was deleted.

12 changes: 12 additions & 0 deletions src/application/dto/tag/utils/getAuthors/getAuthors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getCollection } from "astro:content";
import { TagType } from "@application/dto/tag/types";

export async function getAuthors() {
return (await getCollection("authors")).map((author) => ({
name: author.data.name,
slug: author.data.slug,
type: TagType.AUTHOR,
count: author.data.articles.length,
articles: author.data.articles,
}));
}
1 change: 1 addition & 0 deletions src/application/dto/tag/utils/getAuthors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./getAuthors";
39 changes: 39 additions & 0 deletions src/application/dto/tag/utils/getTags/getTags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { CollectionEntry } from "astro:content";
import { type RawTag, TagType } from "@application/dto/tag/types";
import type { Reference } from "@shared/application/types";

interface Articles {
articles: CollectionEntry<"articles">[];
}

interface GetTags extends Articles {
raw: RawTag[];
}

interface GetArticlesByTagParams extends Articles {
rawTag: RawTag;
}

const getArticlesByTag = ({ rawTag, articles }: GetArticlesByTagParams): Reference<"articles">[] =>
articles
.filter(({ data: { tags } }) => tags?.some(({ slug }) => slug === String(rawTag.fields.slug)))
.map(({ data: { slug } }) => ({
id: slug,
collection: "articles",
}));

export async function getTags({ raw, articles }: GetTags) {
return await Promise.all(
raw.map(async (rawTag) => {
const articlesByTag = getArticlesByTag({ rawTag, articles });

return {
name: rawTag.fields.name as unknown as string,
slug: rawTag.fields.slug,
type: TagType.TAG,
count: articlesByTag.length,
articles: articlesByTag,
};
}),
);
}
1 change: 1 addition & 0 deletions src/application/dto/tag/utils/getTags/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./getTags";

0 comments on commit 32f47d8

Please sign in to comment.