From f77f5f9543f92112f4f2ad568d95c37c7b3566bb Mon Sep 17 00:00:00 2001 From: Park Dahyeon Date: Thu, 13 Nov 2025 16:30:19 +0000 Subject: [PATCH] Adds article retrieval and taxonomy path APIs Implements endpoints to fetch article details and associated taxonomy paths by slug, enhancing content discovery and structure. Introduces response DTOs and updates data models to support richer metadata and timestamp fields for improved API consistency. --- src/modules/article/article.controller.ts | 20 ++- src/modules/article/article.service.ts | 122 +++++++++++++++++- .../article/dto/get-article-response-dto.ts | 41 ++++++ .../article/dto/get-taxonomy-path-dto.ts | 26 ++++ src/modules/global/database/schema.ts | 17 ++- 5 files changed, 218 insertions(+), 8 deletions(-) create mode 100644 src/modules/article/dto/get-article-response-dto.ts create mode 100644 src/modules/article/dto/get-taxonomy-path-dto.ts diff --git a/src/modules/article/article.controller.ts b/src/modules/article/article.controller.ts index a5d4c8e..fd563bb 100644 --- a/src/modules/article/article.controller.ts +++ b/src/modules/article/article.controller.ts @@ -1,9 +1,21 @@ -import { Controller, Get } from '@nestjs/common'; +import { Controller, Get, Param } from '@nestjs/common'; +import { GetArticleResponseDto } from './dto/get-article-response-dto'; +import { ArticleService, TaxonomyPath } from './article.service.js'; +import { TaxonomyPathDto } from './dto/get-taxonomy-path-dto'; @Controller('articles') export class ArticleController { - @Get('node') - getHello(): string { - return 'Hello World!'; + constructor(private readonly articleService: ArticleService) {} + + @Get(':slug/taxonomy-path') + getArticleTaxonomyPath( + @Param('slug') slug: string, + ): Promise { + return this.articleService.getTaxonomyPathsBySlug(slug); + } + + @Get(':slug') + getArticle(@Param('slug') slug: string): Promise { + return this.articleService.getArticle(slug); } } diff --git a/src/modules/article/article.service.ts b/src/modules/article/article.service.ts index 8f0ed47..23776a7 100644 --- a/src/modules/article/article.service.ts +++ b/src/modules/article/article.service.ts @@ -1,4 +1,124 @@ import { Injectable } from '@nestjs/common'; +import { ConnectionService } from '../global/database/connection.service'; +import { GetArticleResponseDto } from './dto/get-article-response-dto'; +import { NotFoundError } from 'rxjs'; +import { TaxonomyChildItem } from '../global/database/schema'; +import mongoose from 'mongoose'; + +export interface TaxonomyPathItem { + title: string; + slug?: string; + articleId?: mongoose.Types.ObjectId; + order: number; +} +export interface TaxonomyPath { + taxonomyId: mongoose.Types.ObjectId; + taxonomyLabel: string; + items: TaxonomyPathItem[]; // root β†’ leaf 순 +} @Injectable() -export class ArticleService {} +export class ArticleService { + constructor(private readonly connectionService: ConnectionService) {} + + public async getArticle(slug: string): Promise { + const articleModel = await this.connectionService.getMoeumModels('article'); + + const response = await articleModel + .findOne({ + slug: slug, + }) + .lean(); + + // response?.slug + + if (!response) { + throw new NotFoundError('Article not found'); + } + + const dto: GetArticleResponseDto = { + ...response, + _id: response._id.toString(), // πŸ‘ˆ μ—¬κΈ°μ„œ ObjectId -> string λ³€ν™˜ + }; + + return dto; + } + + findPathsInTree( + items: TaxonomyChildItem[], + predicate: (item: TaxonomyChildItem) => boolean, + ): TaxonomyPathItem[][] { + const results: TaxonomyPathItem[][] = []; + + function dfs(nodes: TaxonomyChildItem[], currentPath: TaxonomyPathItem[]) { + for (const node of nodes) { + const nextPath: TaxonomyPathItem[] = [ + ...currentPath, + { + title: node.title, + slug: node.slug, + articleId: node.articleId as any, + order: node.order, + }, + ]; + + if (predicate(node)) { + results.push(nextPath); + } + + if (node.children && node.children.length > 0) { + dfs(node.children as any, nextPath); + } + } + } + + dfs(items, []); + return results; + } + + async getTaxonomyPathsBySlug(slug: string): Promise { + // 1) Article μ°ΎκΈ° + const articleModel = await this.connectionService.getMoeumModels('article'); + const taxonomyModel = + await this.connectionService.getMoeumModels('taxonomy'); + + const article = await articleModel.findOne({ slug }).lean().exec(); + if (!article) { + return null; + } + + const articleId = article._id as unknown as mongoose.Types.ObjectId; + + // 2) κ΄€λ ¨ Taxonomy μ „λΆ€ λ‘œλ“œ (규λͺ¨κ°€ 크지 μ•Šλ‹€λŠ” κ°€μ •) + const taxonomies = await taxonomyModel.find().lean().exec(); + + const paths: TaxonomyPath[] = []; + + for (const taxonomy of taxonomies) { + const pathsInThisTaxonomy = this.findPathsInTree( + taxonomy.items as any, + (item) => { + const matchesById = + !!item.articleId && + (item.articleId as unknown as mongoose.Types.ObjectId).equals( + articleId, + ); + + const matchesBySlug = !!item.slug && item.slug === article.slug; + + return matchesById || matchesBySlug; + }, + ); + + for (const p of pathsInThisTaxonomy) { + paths.push({ + taxonomyId: taxonomy._id as any, + taxonomyLabel: taxonomy.label, + items: p, + }); + } + } + + return paths; + } +} diff --git a/src/modules/article/dto/get-article-response-dto.ts b/src/modules/article/dto/get-article-response-dto.ts new file mode 100644 index 0000000..5870b0b --- /dev/null +++ b/src/modules/article/dto/get-article-response-dto.ts @@ -0,0 +1,41 @@ +import { ApiProperty } from '@nestjs/swagger'; + +export class GetArticleMetadataDto { + @ApiProperty({ type: String }) + key: string; + + @ApiProperty({ type: 'string', nullable: true, isArray: true }) + value: string | any[] | null; + + @ApiProperty({ type: String }) + dataType: string; + + @ApiProperty({ type: Boolean }) + isReserved: boolean; +} + +export class GetArticleResponseDto { + @ApiProperty({ type: String }) + _id!: string; + + @ApiProperty({ type: String }) + title!: string; + + @ApiProperty({ type: String }) + slug!: string; + + @ApiProperty({ type: String }) + content!: string; + + @ApiProperty({ type: String, enum: ['draft', 'published', 'archived'] }) + status!: 'draft' | 'published' | 'archived'; + + @ApiProperty({ type: [GetArticleMetadataDto] }) + metadatas!: GetArticleMetadataDto[]; + + @ApiProperty({ type: String, format: 'date-time' }) + createdAt!: Date; + + @ApiProperty({ type: String, format: 'date-time' }) + updatedAt!: Date; +} diff --git a/src/modules/article/dto/get-taxonomy-path-dto.ts b/src/modules/article/dto/get-taxonomy-path-dto.ts new file mode 100644 index 0000000..2e19b0e --- /dev/null +++ b/src/modules/article/dto/get-taxonomy-path-dto.ts @@ -0,0 +1,26 @@ +import { ApiProperty } from '@nestjs/swagger'; + +export class TaxonomyPathItemDto { + @ApiProperty({ type: String }) + title: string; + + @ApiProperty({ type: String, required: false }) + slug?: string; + + @ApiProperty({ type: String, required: false }) + articleId?: string; + + @ApiProperty({ type: Number }) + order: number; +} + +export class TaxonomyPathDto { + @ApiProperty({ type: String }) + taxonomyId: string; + + @ApiProperty({ type: String }) + taxonomyLabel: string; + + @ApiProperty({ type: [TaxonomyPathItemDto] }) + items: TaxonomyPathItemDto[]; // root β†’ leaf +} diff --git a/src/modules/global/database/schema.ts b/src/modules/global/database/schema.ts index 1777505..6057ec8 100644 --- a/src/modules/global/database/schema.ts +++ b/src/modules/global/database/schema.ts @@ -1,6 +1,11 @@ import mongoose, { Schema, Document } from 'mongoose'; import { ModelDefinition } from './types'; +export interface MongooseTimestamps { + createdAt: Date; + updatedAt: Date; +} + export interface Metadata { key: string; value: string | any[] | null; @@ -16,7 +21,9 @@ export interface TaxonomyChildItem { children?: TaxonomyChildItem[]; } -export interface Taxonomy extends Document { +export interface Taxonomy + extends Document, + MongooseTimestamps { label: string; items: TaxonomyChildItem[]; metadatas: Metadata[]; @@ -27,13 +34,17 @@ export interface ContentSection { taxonomies: mongoose.Types.ObjectId[]; } -export interface Content extends Document { +export interface Content + extends Document, + MongooseTimestamps { key: string; label: string; sections: ContentSection[]; } -export interface Article extends Document { +export interface Article + extends Document, + MongooseTimestamps { title: string; slug: string; content: string;