DevBoi

[NestJs] 게시판 정보 불러오기 및 게시판 마무리 본문

Develop/[NestJs]

[NestJs] 게시판 정보 불러오기 및 게시판 마무리

HiSmith 2023. 5. 27. 14:14
반응형

 

board controller

import { Body, Controller, Get, Post } from '@nestjs/common';
import { BoardsService } from './boards.service';
import {Board} from './board.model'

@Controller('boards')
export class BoardsController {
   constructor(private boardService: BoardsService){}
   
   @Get('/')
   getAllBoard(): Board[]{
      return this.boardService.getAllBoards();
   }

   @Post('/create')
   createBoard(@Body('title') title: string,
               @Body('description') description: string){
         this.boardService.createBoard(title,description);
   }
   
}

 

board service

import { Injectable } from '@nestjs/common';
import { Board, BoardStatus } from './board.model';
import {v1 as uuid} from 'uuid'

@Injectable()
export class BoardsService {
    private boards: Board[] = [];
    
    getAllBoards() : Board[]{
        return this.boards;
    }
    createBoard(title: string, description: string){
        const board: Board = {
            id: uuid(),
            title,
            description,
            status: BoardStatus.PUBLIC
        }
        this.boards.push(board);
        return board;
    }
}

 

 

Test

 

 

정상적으로 됨을 확인할 수 있다.

반응형