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

Post review api #48

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
28 changes: 28 additions & 0 deletions src/controllers/photo-controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const PhotoService = require('../services/photo-service');
const ReviewService = require('../services/review-service');

const resSuccess = require('./helpers/response');
const getUser = require('./helpers/get-user');
const { paginationLimitMap } = require('../constants/pagination-constant');
Expand Down Expand Up @@ -43,6 +45,32 @@ const photoController = {
next(err);
}
},
postReview: async (req, res, next) => {
try {
const { useful } = req.body;
const userId = getUser(req).id;
const photoId = req.params.id;

const reviewService = new ReviewService({ logger: req.logger });
const review = await reviewService.postReview(userId, photoId, useful);
res.status(200).json(resSuccess(review));
} catch (err) {
next(err);
}
},
postUnreview: async (req, res, next) => {
try {
const { useful } = req.body;
const userId = getUser(req).id;
const photoId = req.params.id;

const reviewService = new ReviewService({ logger: req.logger });
const review = await reviewService.postUnreview(userId, photoId, useful);
res.status(200).json(resSuccess(review));
} catch (err) {
next(err);
}
},
};

module.exports = photoController;
20 changes: 20 additions & 0 deletions src/errors/review-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const reviewErrorMap = {
alreadyReview: {
message: '你已經評價過此照片',
code: 're001',
},
notReviewYet: {
message: '你未曾評價過此照片',
code: 're002',
},
alreadyUnReview: {
message: '你早已經取消此照片的評價',
code: 're003',
},
badUnReviewRequest: {
message: '你想取消的評價與之前評價的不同',
code: 're004',
},
};

module.exports = reviewErrorMap;
34 changes: 34 additions & 0 deletions src/models/review/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,40 @@ class ReviewModel {

return photoWithReviewCount;
}
async updateReview(userId, photoId, useful) {
const review = await prisma.reviews.update({
where: {
userId_photoId: {
userId: Number(userId),
photoId: Number(photoId),
},
},
data: { useful },
select: {
id: true,
userId: true,
photoId: true,
useful: true,
},
});
return review;
}
async getReview(userId, photoId) {
const review = await prisma.reviews.findUnique({
where: {
userId_photoId: {
userId: Number(userId),
photoId: Number(photoId),
},
},
select: {
userId: true,
photoId: true,
useful: true,
},
});
return review;
}
async _truncate() {
await prisma.reviews.deleteMany({});
}
Expand Down
2 changes: 2 additions & 0 deletions src/routes/modules/photo.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const { authenticated } = require('../../middleware/auth');

const router = express.Router();

router.post('/:id/reviews', authenticated, photoController.postReview);
router.post('/:id/unreviews', authenticated, photoController.postUnreview);
router.get('/', photoController.getPhotos);
router.post('/', authenticated, uploadImages, photoController.postPhotos);

Expand Down
74 changes: 74 additions & 0 deletions src/services/review-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const { isEmpty, isNil } = require('ramda');
const BaseService = require('./base');
const ReviewModel = require('../models/review/index');
const PhotoModel = require('../models/photo');

const GeneralError = require('../errors/error/general-error');
const getPhotoErrorMap = require('../errors/get-photo-error');
const reviewErrorMap = require('../errors//review-error');

class ReviewService extends BaseService {
async postReview(userId, photoId, useful) {
// check space exist
const photoModel = new PhotoModel();
const photoCheck = await photoModel.getPhoto(photoId);
if (!photoCheck) throw new GeneralError(getPhotoErrorMap['photoNotFound']);

// check existed review
const reviewModel = new ReviewModel();
const review = await reviewModel.getReview(userId, photoId);

// if already review (not cancel)
if (review && !isEmpty(review.useful))
throw new GeneralError(reviewErrorMap['alreadyReview']);

// if already review but cancel
if (review && isEmpty(review.useful)) {
const updateReview = await reviewModel.updateReview(
userId,
photoId,
useful
);

this.logger.debug('update a review', { updateReview });

return updateReview;
}

// if review not exist
const newReview = await reviewModel.createReview(userId, photoId, useful);

this.logger.debug('create a review', { newReview });

return newReview;
}
async postUnreview(userId, photoId, useful) {
// check space exist
const photoModel = new PhotoModel();
const photoCheck = await photoModel.getPhoto(photoId);
if (!photoCheck) throw new GeneralError(getPhotoErrorMap['photoNotFound']);

// check existed review
const reviewModel = new ReviewModel();
const review = await reviewModel.getReview(userId, photoId);

// if review not existed
if (isNil(review)) throw new GeneralError(reviewErrorMap['notReviewYet']);

// if review already be canceled
if (review && isEmpty(review.useful))
throw new GeneralError(reviewErrorMap['alreadyUnReview']);

// if unreview's useful not equal reviewed before
if (review && useful !== review.useful)
throw new GeneralError(reviewErrorMap['badUnReviewRequest']);

const unReview = await reviewModel.updateReview(userId, photoId, '');

this.logger.debug('update a review', { unReview });

return unReview;
}
}

module.exports = ReviewService;