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

Hyejung #7

Merged
merged 8 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions src/controllers/ReviewController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Request, Response } from 'express';
import { validationResult } from 'express-validator';
import { ReviewCreateDto } from '../interfaces/review/ReviewCreateDto';
import message from '../modules/responseMessage';
import statusCode from '../modules/statusCode';
import util from '../modules/util';
Expand All @@ -17,6 +19,26 @@ const getReviewList = async (req: Request, res: Response): Promise<Response | vo
}
};

const createReview = async (req: Request, res: Response): Promise<Response | void> => {
const error = validationResult(req);
if (!error.isEmpty()) {
return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, message.BAD_REQUEST));
}

const reviewCreateDto: ReviewCreateDto = req.body;
try {
const data = await ReviewService.createReview(reviewCreateDto);
if (!data) {
res.status(statusCode.NOT_FOUND).send(util.fail(statusCode.NOT_FOUND, message.NOT_FOUND));
}
res.status(statusCode.CREATED).send(util.success(statusCode.CREATED, message.CREATE_REVIEW_SUCCESS, data));
} catch (error) {
console.log(error);
res.status(statusCode.INTERNAL_SERVER_ERROR).send(util.fail(statusCode.INTERNAL_SERVER_ERROR, message.INTERNAL_SERVER_ERROR));
}
};

export default {
getReviewList,
createReview,
};
3 changes: 3 additions & 0 deletions src/interfaces/review/ReviewCreateDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ReviewInfo } from './ReviewInfo';

export interface ReviewCreateDto extends ReviewInfo {}
2 changes: 2 additions & 0 deletions src/modules/responseMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const message = {
INTERNAL_SERVER_ERROR: '์„œ๋ฒ„ ๋‚ด๋ถ€ ์˜ค๋ฅ˜',

READ_MAINPAGE_REVIEW_SUCCESS: '๋ฉ”์ธ ํŽ˜์ด์ง€ ๋ฆฌ๋ทฐ ์กฐํšŒ ์„ฑ๊ณต',
CREATE_REVIEW_SUCCESS: '๋ฆฌ๋ทฐ ๋“ฑ๋ก ์„ฑ๊ณต',

READ_SUBSCRIBE_OPTION_SUCCESS: '๋ผ๋ฉด ๊ตฌ๋… ์˜ต์…˜ ์กฐํšŒ ์„ฑ๊ณต',
};

Expand Down
2 changes: 2 additions & 0 deletions src/routes/ReviewRouter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Router } from 'express';
import { body } from 'express-validator';
import { ReviewController } from '../controllers';

const router: Router = Router();

router.get('/', ReviewController.getReviewList);
router.post('/', [body('userName').notEmpty(), body('description').notEmpty(), body('rates').notEmpty(), body('packageName').notEmpty(), body('product').notEmpty()], ReviewController.createReview);

export default router;
19 changes: 19 additions & 0 deletions src/services/ReviewService.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { PostBaseResponseDto } from '../interfaces/common/PostBaseResponseDto';
import { ReviewCreateDto } from '../interfaces/review/ReviewCreateDto';
import { ReviewResponseDto } from '../interfaces/review/ReviewResponseDto';
import Review from '../models/Review';

Expand Down Expand Up @@ -28,6 +30,23 @@ const getReviewList = async (): Promise<ReviewResponseDto[] | null> => {
}
};

const createReview = async (reviewCreateDto: ReviewCreateDto): Promise<PostBaseResponseDto> => {
try {
const review = new Review(reviewCreateDto);
await review.save();

const data = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id๋ฟ๋งŒ ์•„๋‹ˆ๋ผ db์— ๋“ฑ๋ก๋œ ๋‹ค๋ฅธ ๋‚ด์šฉ๋„ response๋กœ ๋ณด๋‚ด์ฃผ๋ฉด ์•Œ๋งž๊ฒŒ post๋๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์ข‹๋‹ต๋‹ˆ๋‹ค.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜ค์˜ค ์ข‹๋„ค์œ  ! ๊ทธ๋Ÿฌ๋ฉด findOne()์œผ๋กœ ํ•œ ๋ฒˆ ๊ฒ€์ƒ‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋‚ด์ฃผ๋Š”๊ฒŒ ์ข‹์„๊นŒ์š” ?!

_id: review._id,
};

return data;
} catch (error) {
console.log(error);
throw error;
}
};

export default {
getReviewList,
createReview,
};