Skip to content

Commit

Permalink
FIN : 부분 프로토타입 배포
Browse files Browse the repository at this point in the history
FIN : 부분 프로토타입 배포
  • Loading branch information
unchaptered authored Aug 17, 2022
2 parents 0461324 + cde7873 commit 07afc6e
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 256 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const { corsMiddleware } = require('./middlewares/_.loader');
const {
globalRouter,
musicRouter,
commentRouter,
userRouter,
boardRouter,
commentRouter,
} = require('./layers/_.loader');

// TS 님의 Contritubte...., 아래 명령어 실행하면 migrate 자동실행
Expand All @@ -28,8 +28,8 @@ app.all('*', corsMiddleware);

app.use('/api', globalRouter);
app.use('/api/musics', musicRouter);
app.use('/api/comments', commentRouter);
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`));
6 changes: 3 additions & 3 deletions src/layers/_.loader.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const globalRouter = require('./routers/global.router');
const musicRouter = require('./routers/music.router');
const commentRouter = require('./routers/comment.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,
commentRouter,
userRouter,
boardRouter,
commentRouter,
};
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;
80 changes: 41 additions & 39 deletions src/layers/repositories/comment.repository.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,68 @@
const BaseRepository = require('./base.repository');
const { Comment } = require('../../sequelize/models');

const {
GetMusicCommentsDto,
PostCommentDto,
GetCommentsDto,
UpdateCommentsDto,
DeleteCommentsDto,
CustomException,
ConflictException,
UnkownException,
UnhandleMysqlSequelizeError,
NotFoundException,
DeleteCommentDto,
CommentDto,
} = require('../../models/_.loader');
const BaseRepository = require('./base.repository');

class CommentRepository extends BaseRepository {
constructor() {
super();
}

postComments = async (postCommentDto) => {
/**
*
* @param { number } commentId
* @throws { UnkownException | UnhandleMysqlSequelizeError }
* @returns { Promise<boolean> }
*/
isExistsCommentByCommentId = async (commentId) => {
try {
const findResult = await Comment.findOne({
where: { commentId },
attributes: ['commentId'],
});

if (findResult === null) return false;
else return true;
} catch (err) {
throw this.exeptionHandler(err);
}
};

/** @param { PostCommentDto } postCommentDto */
createComment = async (postCommentDto) => {
try {
// (추후 추가)s3 변환 정보(musicUrl) 받아오고 Post DB에 저장
console.log('테스트', postCommentDto);
const comment = await Comment.create({
userId: postCommentDto.userId,
commentId: postCommentDto.commentId,
musicId: postCommentDto.musicId,
content: postCommentDto.content,
});

const postDto = new PostCommentDto(comment?.dataValues);

return postDto;
return new CommentDto(comment.dataValues);
} catch (err) {
console.log(err);
throw err;
throw this.exeptionHandler(err);
}
};

getComments = async (getCommentsDto) => {
/** @param { DeleteCommentDto } deleteCommentDto */
deleteComment = async (deleteCommentDto) => {
try {
console.log(Comment);
console.log('테스트', getCommentsDto);

const comments = await Comment.findAll();

for (const comment of comments) {
// const getAllMusic = music.dataValues;
console.log(comment.dataValues);
}
// console.log(Object.keys(musics));
// const getDto = new GetMusicsDto(musics?.dataValues);
return;
const comment = await Comment.destroy({
where: {
userId: deleteCommentDto.commentId,
commentId: deleteCommentDto.userId,
},
});
if (comment === 0) throw new NotFoundException('이미 존재하지 않는 댓글입니다.');
else return true;
} catch (err) {
console.log(err);
throw err;
throw this.exeptionHandler(err);
}
return musics;
};

// getOneComment = () => {
// console.log('왜 안 되니?');
// return 'smile';
// };
}

module.exports = CommentRepository;
9 changes: 4 additions & 5 deletions src/layers/routers/comment.router.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const { Router } = require('express');
const { tokenGuard } = require('../../middlewares/_.loader');

const CommentController = require('../controllers/comment.controller');
const commentController = new CommentController();

const commentRouter = Router();
const commentController = new CommentController();

commentRouter.get('', commentController.getComments);
commentRouter.post('', commentController.postComments);
commentRouter.put('/:commentId', commentController.updateComments);
commentRouter.delete('/:comentId', commentController.deleteComments);
commentRouter.post('', tokenGuard, commentController.postComment);
commentRouter.delete('/:commentId', tokenGuard, commentController.deleteComment);

module.exports = commentRouter;
Loading

0 comments on commit 07afc6e

Please sign in to comment.