엔티티 변경
User는 여러개의 게시판을 쓸수 있으므로 User와 Board는 one to many 관계를 설정한다. Comment 도 마찬가지로 User와 one to many 관계이다.
src/entity/User.ts
@Entity()
@Unique(['email'])
export class User {
...
@OneToMany(type => Board, board => board.user)
boards: Board[];
@OneToMany(type => Comment, comment => comment.user)
comments: Comment[];
}
src/entity/Board.ts
@Entity()
export class Board {
...
@ManyToOne(type => User, user => user.boards)
user: User;
}
src/entity/Comment.ts
@Entity()
export class Comment {
...
@ManyToOne(type => User, user => user.comments)
user: User;
}
게시판에 사용자 정보 적용
게시판 리스트에서 작성자를 표시하기 위해서는 사용자 정보를 포함해야 한다.
src/controller/BoardController.ts
export class BoardController {
static addBoard = async (req, res) => {
const {title, content, user_id} = req.body;
const user = await getConnection().getRepository(User).findOne({id: user_id});
const board = new Board();
board.title = title;
board.content = content;
board.user = user;
const result = await getConnection().getRepository(Board).save(board);
res.send(result);
}
static findAllBoard = async (req, res) => {
const {page_number, page_size} = req.query;
const options = {};
options['select'] = ["id", "title", "content", "created", "updated"];
options['order'] = {id: 'DESC'};
options['relations'] = ['user']
if (page_number && page_size) {
options['skip'] = (page_number - 1) * page_size;
options['take'] = page_size;
}
const boards = await getConnection().getRepository(Board).find(options);
res.send(boards);
}
static findOneBoard = async (req, res) => {
// 동적 파라메터가 정규표현식으로 변경이 되면 첫번째 파라메터를 가져와야 한다. ex: {'0': '1'}
// const id = req.params[0];
const {id} = req.params;
const board = await getConnection().getRepository(Board).findOne({relations: ['user'], where: {id}});
res.send(board);
}
static countBoard = async (req, res) => {
const total = await getConnection().getRepository(Board).count();
res.send({total});
}
static modifyBoard = async (req, res) => {
const {id, title, content} = req.body;
const updateOption = {};
if (title) {
updateOption['title'] = title;
}
if (content) {
updateOption['content'] = content;
}
const result = await getConnection().createQueryBuilder().update(Board)
.set(updateOption)
.where("id = :id", {id})
.execute();
res.send(result);
}
static removeBoard = async (req, res) => {
const {id} = req.query;
const result = await getConnection()
.createQueryBuilder()
.delete()
.from(Board)
.where("id = :id", {id})
.execute();
res.send(result);
}
}
댓글에 사용자 정보 적용
게시판 리스트에서 작성자를 표시하기 위해서는 사용자 정보를 포함해야 한다.
src/controller/CommentController.ts
export class CommentController {
static addComment = async (req, res) => {
const {board_id, content, user_id} = req.body;
const board = await getConnection().getRepository(Board).findOne({id: board_id});
const user = await getConnection().getRepository(User).findOne({id: user_id});
const comment = new Comment();
comment.content = content;
comment.board = board;
comment.user = user;
await getConnection().getRepository(Comment).save(comment);
res.send(comment);
}
static findAllComment = async (req, res) => {
const {board_id} = req.query;
const result = await getConnection().getRepository(Comment).createQueryBuilder('comment')
.innerJoinAndSelect('comment.board', 'board')
.innerJoinAndSelect('comment.user', 'user')
.where('board.id=:board_id', {board_id})
.getMany()
console.log(result);
res.send(result);
// const boards = await getConnection().getRepository(Comment).find({ where: { board_id: board_id } });
/* const board = await getConnection().getRepository(Board)
.findOne({relations: ["comments"], where: {id: board_id}, order: {id: 'DESC'}});
res.send(board.comments);*/
}
static findOneComment = async (req, res) => {
const {id} = req.query;
const comment = await getConnection().getRepository(Comment).findOne({id});
console.log(comment);
res.send(comment);
}
static modifyComment = async (req, res) => {
const {id, content} = req.body;
const result = await getConnection().createQueryBuilder().update(Comment)
.set({content})
.where("id = :id", {id})
.execute();
res.send(result);
}
static removeComment = async (req, res) => {
const {id} = req.query;
const result = await getConnection()
.createQueryBuilder()
.delete()
.from(Comment)
.where("id = :id", {id})
.execute();
res.send(result);
}
}