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

최현정 #3

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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 changes: 0 additions & 11 deletions .env.example

This file was deleted.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule } from '@nestjs/config';
import { addTransactionalDataSource } from 'typeorm-transactional';
import { DataSource } from 'typeorm';
import { BoardController } from './module/board/board.controller';
import { UserController } from './module/user/user.controller';
import { UserModule } from './module/user/user.module';
import { BoardModule } from './module/board/board.module';
import { CommentService } from './module/comment/comment.service';
import { CommentModule } from './module/comment/comment.module';
import { User } from './module/user/entity/user.entity';
import { Board } from './module/board/entity/board.entity';
import { Comment } from './module/comment/entity/comment.entity';

@Module({
imports: [
Expand All @@ -17,6 +26,7 @@ import { DataSource } from 'typeorm';
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
synchronize: process.env.DB_SYNC === 'true',
entities: [User, Board, Comment],
timezone: 'Z',
};
},
Expand All @@ -28,8 +38,9 @@ import { DataSource } from 'typeorm';
return addTransactionalDataSource(new DataSource(options));
},
}),
],
controllers: [],
providers: [],
UserModule,
BoardModule,
CommentModule,
]
})
export class AppModule {}
13 changes: 13 additions & 0 deletions src/global/common/base.entitiy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { CreateDateColumn, DeleteDateColumn, BaseEntity as TypeOrmBaseEntity, UpdateDateColumn } from "typeorm";

export abstract class BaseEntity extends TypeOrmBaseEntity {

@CreateDateColumn()
createAt: Date;

@UpdateDateColumn()
updateAt: Date;

@DeleteDateColumn({type: 'timestamp', nullable: true})
deleteAt: Date | null;
}
16 changes: 16 additions & 0 deletions src/global/exception/customException.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { HttpException, HttpStatus } from "@nestjs/common";

export class CustomException extends HttpException {
constructor(
private readonly _errorcode: string,
message: string,
status: HttpStatus,
){
super({
errorcode: _errorcode,
message: message,
},
status,
)
}
}
11 changes: 11 additions & 0 deletions src/global/exception/errorCode/Errorcode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export enum ErrorCode {

//User
USER_NOT_FOUND = 'U001',
//Board
BOARD_NOT_FOUND = 'B001',
//Comment
COMMENT_PARENT_NOT_FOUND = 'C001',
COMMENT_NOT_FOUND = 'C002',
COMMENT_LIKES_ALREDY_PUSH = 'C003'
}
12 changes: 12 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { initializeTransactionalContext } from 'typeorm-transactional';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { ValidationPipe } from '@nestjs/common';

async function bootstrap() {
initializeTransactionalContext();

const app = await NestFactory.create(AppModule);
// TODO: 프로그램 구현

const swaggerConfig = new DocumentBuilder()
.setTitle('댓글 API')
.setDescription('댓글과제')
.setVersion('1.0')
.build()

const swagger = SwaggerModule.createDocument(app, swaggerConfig);
SwaggerModule.setup('swagger', app, swagger);

await app.listen(process.env.PORT || 8000);

console.log(`Application is running on: ${await app.getUrl()}`);
Expand Down
26 changes: 26 additions & 0 deletions src/module/board/board.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Body, Controller, HttpStatus, Post, Res } from '@nestjs/common';
import { BoardCreateDto } from './dto/board-create.dto';
import { Response } from 'express';
import { ApiOperation } from '@nestjs/swagger';
import { BoardService } from './board.service';
import { BoardMapper } from './mapper/board.mapper';

@Controller('board')
export class BoardController {
constructor(
private readonly boardService: BoardService,
private readonly boardMapper: BoardMapper
){}

@ApiOperation({
summary: '게시물 생성API'})
@Post()
async createBoard(
@Body() boardCreateDto: BoardCreateDto,
@Res() res: Response
): Promise<void>{
const newBoard = await this.boardService.createBoard(boardCreateDto);
const response = this.boardMapper.EntityToDto(newBoard);
res.status(HttpStatus.CREATED).json(response);
}
}
14 changes: 14 additions & 0 deletions src/module/board/board.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Board } from './entity/board.entity';
import { BoardController } from './board.controller';
import { BoardService } from './board.service';
import { User } from '../user/entity/user.entity';
import { BoardMapper } from './mapper/board.mapper';

@Module({
imports:[TypeOrmModule.forFeature([Board, User])],
providers:[BoardService,BoardMapper],
controllers:[BoardController]
})
export class BoardModule {}
27 changes: 27 additions & 0 deletions src/module/board/board.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Injectable } from '@nestjs/common';
import { BoardCreateDto } from './dto/board-create.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Board } from './entity/board.entity';
import { Repository } from 'typeorm';
import { User } from '../user/entity/user.entity';
import { NotFoundUserException } from '../user/userException/NotFoundUserException';
import { BoardMapper } from './mapper/board.mapper';

@Injectable()
export class BoardService{
constructor(
@InjectRepository(Board)
private readonly boardRepository: Repository<Board>,
@InjectRepository(User)
private readonly userRepository: Repository<User>,
private readonly boardMapper: BoardMapper
){}
async createBoard(boardCreateDto: BoardCreateDto): Promise<Board>{
const creator = await this.userRepository.findOne({where: {id: boardCreateDto.creatorId}});
if(!creator){
throw new NotFoundUserException();
}
const newBoard = this.boardMapper.DtoToEntity(boardCreateDto, creator);
return await this.boardRepository.save(newBoard);
}
}
13 changes: 13 additions & 0 deletions src/module/board/boardException/NotFoundBoardException.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { HttpStatus } from "@nestjs/common";
import { CustomException } from "../../../global/exception/customException";
import { ErrorCode } from "../../../global/exception/errorCode/Errorcode";

export class NotFoundBoardException extends CustomException{
constructor() {
super(
ErrorCode.BOARD_NOT_FOUND,
'해당 게시글을 찾을 수 없습니다!',
HttpStatus.CONFLICT,
);
}
}
4 changes: 4 additions & 0 deletions src/module/board/dto/board-create-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class BoardCreateReponseDto{
creatorId: number;
content: string;
}
14 changes: 14 additions & 0 deletions src/module/board/dto/board-create.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ApiProperty } from "@nestjs/swagger";

export class BoardCreateDto{
@ApiProperty({
example: '1',
description: '사용자Pk',
})
creatorId: number;
@ApiProperty({
example: '오늘 밥 뭡니까?',
description: '게시글내용',
})
content: string;
}
20 changes: 20 additions & 0 deletions src/module/board/entity/board.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { User } from "../../user/entity/user.entity";
import { BaseEntity } from "../../../global/common/base.entitiy";
import { Column, Entity, JoinColumn, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { Comment } from "../../comment/entity/comment.entity";

@Entity()
export class Board extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;

@Column()
content: string;

@ManyToOne((type) => User)
@JoinColumn({ name: 'user_id' })
creator: User;

@OneToMany((type) => Comment, (comment) => comment.board)
comments: Comment[];
}
21 changes: 21 additions & 0 deletions src/module/board/mapper/board.mapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { User } from "../../user/entity/user.entity";
import { BoardCreateReponseDto } from "../dto/board-create-response.dto";
import { BoardCreateDto } from "../dto/board-create.dto";
import { Board } from "../entity/board.entity";

export class BoardMapper{
DtoToEntity({content}:BoardCreateDto, creator: User): Board{
const board = new Board();
board.content = content;
board.creator = creator;

return board;
}

EntityToDto(board: Board): BoardCreateReponseDto{
return {
creatorId: board.creator.id,
content: board.content
}
}
}
49 changes: 49 additions & 0 deletions src/module/comment/comment.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Body, Controller, HttpStatus, Post, Res } from '@nestjs/common';
import { ApiOperation } from '@nestjs/swagger';
import { CommentCreateDto } from './dto/comment-create.dto';
import { Response } from 'express';
import { CommentService } from './comment.service';
import { CommentMapper } from './mapper/comment.mapper';
import { CommentReplyCreateDto } from './dto/comment-reply-create.dto';
import { CommentUpdateLikesDto } from './dto/comment-update-likes.dto';

@Controller('comment')
export class CommentController {
constructor(
private readonly commentService: CommentService,
private readonly commentMapper: CommentMapper
){}

@ApiOperation({ summary: '댓글 생성 API'})
@Post()
async createComment(
@Body() commentCreateDto: CommentCreateDto,
@Res() res: Response
): Promise<void>{
const newComment = await this.commentService.createComment(commentCreateDto);
const response = await this.commentMapper.EntityToDto(newComment);
res.status(HttpStatus.CREATED).json(response);
}

@ApiOperation({ summary: '대댓글 생성 API'})
@Post('/reply')
async createReplyComment(
@Body() commentReplyCreateDto: CommentReplyCreateDto,
@Res() res: Response
): Promise<void>{
const newReplyComment = await this.commentService.createReplyComment(commentReplyCreateDto);
const response = await this.commentMapper.ReplyEntityToDto(newReplyComment);
res.status(HttpStatus.CREATED).json(response);
}

@ApiOperation({ summary: '좋아요 API'})
@Post('likes')
async updateCommentLikes(
@Body() commentUpdateLikesDto: CommentUpdateLikesDto,
@Res() res: Response
): Promise<void>{
const updateLikesCount = await this.commentService.updateCommentLikes(commentUpdateLikesDto);
const response = this.commentMapper.EntityToDto(updateLikesCount);
res.status(HttpStatus.CREATED).json(response);
}
}
16 changes: 16 additions & 0 deletions src/module/comment/comment.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Module } from '@nestjs/common';
import { CommentController } from './comment.controller';
import { Comment } from './entity/comment.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
import { CommentService } from './comment.service';
import { User } from '../user/entity/user.entity';
import { Board } from '../board/entity/board.entity';
import { CommentMapper } from './mapper/comment.mapper';


@Module({
imports:[TypeOrmModule.forFeature([Comment,User,Board])],
providers:[CommentService,CommentMapper],
controllers:[CommentController]
})
export class CommentModule {}
Loading