Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
unchaptered committed Aug 17, 2022
2 parents be03755 + 07afc6e commit 2f9d2f1
Show file tree
Hide file tree
Showing 48 changed files with 1,250 additions and 339 deletions.
11 changes: 10 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
const morgan = require('morgan');
const express = require('express');

const { corsMiddleware } = require('./middlewares/_.loader');
const { globalRouter, musicRouter, commentRouter } = require('./layers/_.loader');
const {
globalRouter,
musicRouter,
userRouter,
boardRouter,
commentRouter,
} = require('./layers/_.loader');

// TS 님의 Contritubte...., 아래 명령어 실행하면 migrate 자동실행
// const { sequelize } = require('./sequelize/models');
Expand All @@ -21,6 +28,8 @@ app.all('*', corsMiddleware);

app.use('/api', globalRouter);
app.use('/api/musics', musicRouter);
app.use('/api/users', userRouter);
app.use('/api/comments', commentRouter);
app.use('/api/posts', boardRouter);

app.listen(3000, () => console.log(`Server is running on 3000`));
4 changes: 4 additions & 0 deletions src/layers/_.loader.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const globalRouter = require('./routers/global.router');
const userRouter = require('./routers/user.router');
const musicRouter = require('./routers/music.router');
const boardRouter = require('./routers/board.router');
const commentRouter = require('./routers/comment.router');

module.exports = {
globalRouter,
musicRouter,
userRouter,
boardRouter,
commentRouter,
};
95 changes: 95 additions & 0 deletions src/layers/controllers/board.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const e = require('express');
const joi = require('joi');

const BoardService = require('../services/board.service');
const { BoardPostDto, BoardGetDto } = require('../../models/_.loader');
const {
BadRequestException,
UnkownException,
NotFoundException,
} = require('../../models/_.loader');
const { FormDtoProvider, JoiValidator, exceptionHandler } = require('../../modules/_.loader');

class BoardController {
boardService;
formProvider;
joiValidator;

constructor() {
this.boardService = new BoardService();
this.formProvider = new FormDtoProvider();
this.joiValidator = new JoiValidator();
}

// 게시글 생성
/** @param { e.Request } req @param { e.Response } res @param { e.NextFunction } next */
postBoard = async (req, res, next) => {
console.log(req.body);

try {
const boardPostDto = new BoardPostDto(req.body);
await this.joiValidator.validate(boardPostDto);

const post = await this.boardService.postBoard(boardPostDto);
console.log(post.dataValues);

return res.status(200).json(
this.formProvider.getSuccessFormDto('게시물 작성이 완료됐습니다.', {
post: post,
}),
);
} catch (err) {
const exception = exceptionHandler(err);

return res.status(exception.statusCode).json(
this.formProvider.getFailureFormDto(exception.message, {
post: {
title: boardPostDto.title,
content: boardPostDto.password,
},
}),
);
}
};

// 게시글 전체 조회
getBoard = async (req, res, next) => {
try {
const page = req?.query?.page ?? 1;

const boardGetDto = new BoardGetDto({ page });
await this.joiValidator.validate(boardGetDto);

const post = await this.boardService.getBoard(boardGetDto);

return res.status(200).json(
this.formProvider.getSuccessFormDto('게시글 조회가 완료되었습니다.', {
postList: post,
}),
);
} catch (err) {
const exception = exceptionHandler(err);

return res
.status(exception.statusCode)
.json(this.formProvider.getFailureFormDto(exception.message));
}
};

// 게시글 상세 조회
getOneBoard = async (req, res, next) => {
return 'hello';
};

// 게시글 수정
putBoard = async (req, res, next) => {
return 'hello';
};

// 게시글 삭제
deleteBoard = async (req, res, next) => {
return 'hello';
};
}

module.exports = BoardController;
111 changes: 21 additions & 90 deletions src/layers/controllers/comment.controller.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,32 @@
const e = require('express');
const joi = require('joi');
const CommentService = require('../services/comment.service');
const {
PostCommentDto,
GetCommentsDto,
UpdateCommentsDto,
DeleteCommentsDto,
} = require('../../models/_.loader');
const { FormDtoProvider, JoiValidator, exceptionHandler } = require('../../modules/_.loader');
const { PostCommentDto, DeleteCommentDto } = require('../../models/_.loader');

class CommentController {
formProvider;
joiValidator;
joiValidatorMusic;
commentService;

constructor() {
this.formProvider = new FormDtoProvider();
this.joiValidator = new JoiValidator();
this.formProvider = new FormDtoProvider();
this.commentService = new CommentService();
}

/** @param { e.Request } req @param { e.Response } res @param { e.NextFunction } next */
postComments = async (req, res, next) => {
const userId = 1;
const { commentId } = req.params;
const { content } = req.body;

postComment = async (req, res, next) => {
try {
const postCommentDto = new PostCommentDto({
commentId,
userId,
content,
});
this.joiValidator.validate(postCommentDto);
const { userId, musicId, content } = req?.body;

const comment = await this.commentService.postComments(postCommentDto);
const postCommentDto = new PostCommentDto({ userId, musicId, content });
await this.joiValidator.validate(postCommentDto);

const comment = await this.commentService.postComment(postCommentDto);

return res
.status(200)
.json(
this.formProvider.getSuccessFormDto('댓글 생성에 성공하셨습니다.', { comment }),
this.formProvider.getSuccessFormDto('댓글 작성에 성공했습니다.', { comment }),
);
} catch (err) {
const exception = exceptionHandler(err);
Expand All @@ -51,15 +38,19 @@ class CommentController {
};

/** @param { e.Request } req @param { e.Response } res @param { e.NextFunction } next */
getComments = async (req, res, next) => {
deleteComment = async (req, res, next) => {
try {
const comment = await this.commentService.getComments();
const userId = req?.body?.userId;
const commentId = req?.params?.commentId;

const deleteCommentDto = new DeleteCommentDto({ userId, commentId });
await this.joiValidator.validate(deleteCommentDto);

const result = await this.commentService.deleteComment(deleteCommentDto);

return res.status(200).json(
this.formProvider.getSuccessFormDto('댓글 전체 조회에 성공했습니다.', {
comment,
}),
);
return res
.status(200)
.json(this.formProvider.getSuccessFormDto('댓글 삭제에 성공했습니다.', {}));
} catch (err) {
const exception = exceptionHandler(err);

Expand All @@ -68,66 +59,6 @@ class CommentController {
.json(this.formProvider.getFailureFormDto(exception.message));
}
};

// 댓글 상세 조회
// getOneComment = async (req, res, next) => {
// const commentId = 1;
// try {
// const commentOne = await this.commentService.getOneComment();

// return res.status(200).json(
// this.formProvider.getSuccessFormDto('댓글 상세 조회에 성공했습니다.', {
// commentOne,
// }),
// );
// } catch (err) {
// const exception = exceptionHandler(err);

// return res
// .status(exception.statusCode)
// .json(this.formProvider.getFailureFormDto(exception.message));
// }
// }
updateComments = async (req, res, next) => {
const { commentId } = req.params;
const { content, userId } = req.body;

try {
await joi
.object({
userId: joi.number().required(),
commentId: joi.number().required(),
content: joi.string().required(),
})
.validateAsync({ userId, commentId, content });

const result = await this.commentService.updateComment(userId, commentId, content);

return res.json(result);
} catch (err) {
return res.json(err.message);
}
};

deleteComments = async (req, res, next) => {
const { commentId } = req.params;
const { userId } = req.body;

try {
await joi
.object({
userId: joi.number().required(),
commentId: joi.number().required(),
})
.validateAsync({ userId, commentId });

const result = await this.commentService.deleteCommentById(userId, commentId);

return res.json(result);
} catch (err) {
return res.json(err.message);
}
};
}

module.exports = CommentController;
Loading

0 comments on commit 2f9d2f1

Please sign in to comment.