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.
This commit is contained in:
dahyeon committed 2025-11-13 16:30:19 +00:00
1 parent 9d65fbd235
commit f77f5f9543
5 files changed
+218 -8

No files matched your search

+16 -4
View File
@@ -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<TaxonomyPath[] | null> {
return this.articleService.getTaxonomyPathsBySlug(slug);
}
@Get(':slug')
getArticle(@Param('slug') slug: string): Promise<GetArticleResponseDto> {
return this.articleService.getArticle(slug);
}
}
+121 -1
View File
@@ -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<GetArticleResponseDto> {
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<TaxonomyPath[] | null> {
// 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;
}
}
@@ -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;
}
@@ -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
}
+14 -3
View File
@@ -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<mongoose.Types.ObjectId>,
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<mongoose.Types.ObjectId>,
MongooseTimestamps {
key: string;
label: string;
sections: ContentSection[];
}
export interface Article extends Document {
export interface Article
extends Document<mongoose.Types.ObjectId>,
MongooseTimestamps {
title: string;
slug: string;
content: string;