Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post관련 API 작업 #31

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11,377 changes: 6,371 additions & 5,006 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

27 changes: 22 additions & 5 deletions src/common/dto/pagination.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { ApiProperty } from '@nestjs/swagger';
import { Exclude, Expose, Type } from 'class-transformer';
import { IsNumber, IsOptional, Min } from 'class-validator';
import { IsEnum, IsNumber, IsOptional, Min } from 'class-validator';

export enum OrderType {
asc = 'asc',
desc = 'desc',
}

// Pagination 들어가는 API에 대해서 상속해주세용
export class PaginationQuery {
Expand All @@ -25,6 +30,14 @@ export class PaginationQuery {
@IsNumber()
@Min(1)
readonly limit = 10;

@ApiProperty({
required: false,
enum: OrderType,
})
@IsOptional()
@IsEnum(OrderType)
readonly order: OrderType;
}

// Pagination API Response의 Metadata로 추가해주세용
Expand Down Expand Up @@ -53,14 +66,18 @@ export class PaginationMetadata {
}

@Expose()
@ApiProperty()
get hasNext() {
@ApiProperty({
type: Boolean,
})
get hasNext(): boolean {
return Boolean(this._nextPage);
}

@Expose()
@ApiProperty()
get total() {
@ApiProperty({
type: Number,
})
get total(): number {
return this._total;
}
}
7 changes: 7 additions & 0 deletions src/modules/posts/docs/controller.docs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { applyDecorators } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';

export const PostControllerDocs = applyDecorators(
ApiTags('posts'),
ApiBearerAuth(),
);
3 changes: 1 addition & 2 deletions src/modules/posts/docs/createPost.docs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { applyDecorators } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { ApiOperation, ApiResponse } from '@nestjs/swagger';
import { Types } from 'mongoose';

export const CreatePostDocs = applyDecorators(
Expand All @@ -9,5 +9,4 @@ export const CreatePostDocs = applyDecorators(
ApiResponse({
type: Types.ObjectId,
}),
ApiBearerAuth(),
);
8 changes: 8 additions & 0 deletions src/modules/posts/docs/deletePost.docs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { applyDecorators } from '@nestjs/common';
import { ApiOperation } from '@nestjs/swagger';

export const DeletePostDocs = applyDecorators(
ApiOperation({
summary: 'URL 삭제',
}),
);
5 changes: 5 additions & 0 deletions src/modules/posts/docs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './createPost.docs';
export * from './updatePostFolder.docs';
export * from './controller.docs';
export * from './deletePost.docs';
export * from './listPost.docs';
12 changes: 12 additions & 0 deletions src/modules/posts/docs/listPost.docs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { applyDecorators } from '@nestjs/common';
import { ApiOperation, ApiResponse } from '@nestjs/swagger';
import { ListPostResponse } from '../response';

export const ListPostDocs = applyDecorators(
ApiOperation({
summary: '전체 피드 조회',
}),
ApiResponse({
type: ListPostResponse,
}),
);
8 changes: 8 additions & 0 deletions src/modules/posts/docs/updatePostFolder.docs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { applyDecorators } from '@nestjs/common';
import { ApiOperation } from '@nestjs/swagger';

export const UpdatePostFolderDocs = applyDecorators(
ApiOperation({
summary: 'URL 폴더 변경',
}),
);
7 changes: 3 additions & 4 deletions src/modules/posts/dto/create-post.dto.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { IsNotEmpty, IsString } from 'class-validator';
import { IsMongoId, IsNotEmpty, IsUrl } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import { Types } from 'mongoose';

export class CreatePostDto {
@IsString()
@IsMongoId()
@IsNotEmpty()
@ApiProperty({ description: '폴더 id', required: true })
folderId: string;

@IsString()
@IsUrl()
@IsNotEmpty()
@ApiProperty({ description: '저장할 url', required: true })
url: string;
Expand Down
4 changes: 4 additions & 0 deletions src/modules/posts/dto/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './create-post.dto';
export * from './updatePostFolder.dto';
export * from './list-post.dto';
export * from './updatePost.dto';
15 changes: 15 additions & 0 deletions src/modules/posts/dto/list-post.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ApiProperty } from '@nestjs/swagger';
import { PaginationQuery } from '@src/common';
import { Transform } from 'class-transformer';
import { IsBoolean, IsOptional } from 'class-validator';

export class ListPostQueryDto extends PaginationQuery {
@ApiProperty({
required: false,
description: '즐겨찾기 필터링 여부 확인',
})
@Transform(({ value }) => (value === 'true' ? true : false))
@IsBoolean()
@IsOptional()
favorite: boolean;
}
15 changes: 15 additions & 0 deletions src/modules/posts/dto/updatePost.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ApiProperty } from '@nestjs/swagger';
import { PostUpdateableFields } from '../type/type';
import { IsBoolean, IsOptional } from 'class-validator';

export class UpdatePostDto implements PostUpdateableFields {
// Temporary ignore in MVP level
title: string;
hye-on marked this conversation as resolved.
Show resolved Hide resolved
@ApiProperty({
required: false,
default: false,
})
@IsOptional()
@IsBoolean()
isFavorite: boolean;
}
9 changes: 9 additions & 0 deletions src/modules/posts/dto/updatePostFolder.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsMongoId, IsNotEmpty } from 'class-validator';

export class UpdatePostFolderDto {
@IsNotEmpty()
@IsMongoId()
@ApiProperty()
folderId: string;
hye-on marked this conversation as resolved.
Show resolved Hide resolved
}
62 changes: 56 additions & 6 deletions src/modules/posts/posts.controller.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
import { Controller, Post, Body, UseGuards } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { Types } from 'mongoose';
import {
Controller,
Post,
Body,
UseGuards,
Patch,
Param,
Get,
Query,
Delete,
} from '@nestjs/common';
import { PostsService } from '@src/modules/posts/posts.service';
import { CreatePostDto } from '@src/modules/posts/dto/create-post.dto';
import { GetUser } from '@src/common';
import { GetUser, PaginationMetadata } from '@src/common';
import { JwtGuard } from '@src/modules/users/guards';
import { CreatePostDocs } from '@src/modules/posts/docs/createPost.docs';
import { ListPostQueryDto, UpdatePostDto, UpdatePostFolderDto } from './dto';
import {
CreatePostDocs,
DeletePostDocs,
ListPostDocs,
PostControllerDocs,
UpdatePostFolderDocs,
} from './docs';
import { ListPostItem, ListPostResponse } from './response';

@ApiTags('posts')
@Controller('posts')
@PostControllerDocs
@UseGuards(JwtGuard)
export class PostsController {
constructor(private readonly postsService: PostsService) {}

@Get()
@ListPostDocs
async listPost(@GetUser() userId: string, @Query() query: ListPostQueryDto) {
const { count, posts } = await this.postsService.listPost(userId, query);
const postResponse = posts.map((post) => new ListPostItem(post));
const metadata = new PaginationMetadata(query.page, query.limit, count);
return new ListPostResponse(metadata, postResponse);
}

hye-on marked this conversation as resolved.
Show resolved Hide resolved
@Post()
@CreatePostDocs
async createPost(
Expand All @@ -21,4 +46,29 @@ export class PostsController {
): Promise<boolean> {
return await this.postsService.createPost(createPostDto, userId);
}

@Patch(':postId')
async updateFolder(
@GetUser() userId: string,
@Param('postId') postId: string,
@Body() dto: UpdatePostDto,
) {
return await this.postsService.updatePost(userId, postId, dto);
}

@Patch(':postId/move')
@UpdatePostFolderDocs
async updatePostFolder(
@GetUser() userId: string,
@Param('postId') postId: string,
@Body() dto: UpdatePostFolderDto,
) {
return await this.postsService.updatePostFolder(userId, postId, dto);
}

@Delete(':postId')
@DeletePostDocs
async deletePost(@GetUser() userId: string, @Param('postId') postId: string) {
return await this.postsService.deletePost(userId, postId);
}
}
10 changes: 9 additions & 1 deletion src/modules/posts/posts.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ import { PostsService } from './posts.service';
import { PostsController } from './posts.controller';
import { PostsRepository } from '@src/modules/posts/posts.repository';
import { MongooseModule } from '@nestjs/mongoose';
import { Post, PostSchema } from '@src/infrastructure';
import {
AIClassification,
Post,
PostAIClassificationSchema,
PostSchema,
} from '@src/infrastructure';
import { UsersModule } from '@src/modules/users/users.module';
import { AwsLambdaModule } from '@src/infrastructure/aws-lambda/aws-lambda.module';
import { AwsLambdaService } from '@src/infrastructure/aws-lambda/aws-lambda.service';

@Module({
imports: [
MongooseModule.forFeature([{ name: Post.name, schema: PostSchema }]),
MongooseModule.forFeature([
{ name: AIClassification.name, schema: PostAIClassificationSchema },
]),
UsersModule,
AwsLambdaModule,
],
Expand Down
Loading