Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
augustus281 committed Mar 31, 2024
1 parent 04dab37 commit 44e9f45
Show file tree
Hide file tree
Showing 18 changed files with 388 additions and 84 deletions.
14 changes: 7 additions & 7 deletions server/src/config/config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const config = {
redis: {
port: process.env.REDIS_PORT,
host: process.env.REDIS_HOST
}
}
// const config = {
// redis: {
// port: process.env.REDIS_PORT,
// host: process.env.REDIS_HOST
// }
// }

module.exports = config
// module.exports = config
157 changes: 106 additions & 51 deletions server/src/controllers/comment.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,82 @@

const { Sequelize } = require("sequelize")
const Comment = require("../models/comment.model")
const Tour = require("../models/tour.model")

const Op = Sequelize.Op

class CommentController {
createComment = async (req, res, next) => {
try {
const { tour_id, content, user_id, parent_comment_id } = req.body;
const comment = await Comment.create({
tour_id, content, user_id, parent_comment_id
});
try {
const { tour_id, content, user_id, parent_comment_id } = req.body;
const comment = await Comment.create({
tour_id, content, user_id, parent_comment_id
});

let rightValue;
if (parent_comment_id) {
// reply comment
const parent_comment = await Comment.findOne({ where: { comment_id: parent_comment_id }});
if (!parent_comment) return res.status(404).json({ message: "Not found parent comment!" });
let rightValue;
if (parent_comment_id) {
// reply comment
const parent_comment = await Comment.findOne({ where: { comment_id: parent_comment_id }});
if (!parent_comment) return res.status(404).json({ message: "Not found parent comment!" });

console.log(`parent_comment:::`, parent_comment)
console.log(`parent_comment:::`, parent_comment)

rightValue = parent_comment.comment_right;
rightValue = parent_comment.comment_right;

// update many comments
await Comment.update({
comment_right: Sequelize.literal('comment_right + 2')
},{
where: {
tour_id: tour_id,
comment_right: { [Sequelize.Op.gte]: rightValue }
}
});

await Comment.update({
comment_left: Sequelize.literal('comment_left + 2')
// update many comments
await Comment.update({
comment_right: Sequelize.literal('comment_right + 2')
},{
where: {
tour_id: tour_id,
comment_left: { [Sequelize.Op.gt]: rightValue }
comment_right: { [Sequelize.Op.gte]: rightValue }
}
});
} else {
const maxRightValue = await Comment.findOne({
where: {
tour_id: tour_id
},
attributes: [[Sequelize.fn('MAX', Sequelize.col('comment_right')), 'maxRight']]
});
});

rightValue = maxRightValue && maxRightValue.dataValues.maxRight !== null ? maxRightValue.dataValues.maxRight + 1 : 1;
}
await Comment.update({
comment_left: Sequelize.literal('comment_left + 2')
},{
where: {
tour_id: tour_id,
comment_left: { [Sequelize.Op.gt]: rightValue }
}
});
} else {
const maxRightValue = await Comment.findOne({
where: {
tour_id: tour_id
},
attributes: [[Sequelize.fn('MAX', Sequelize.col('comment_right')), 'maxRight']]
});

// insert to comment
comment.comment_left = rightValue;
comment.comment_right = rightValue + 1;

await comment.save();
return res.status(200).json({
message: "You comment tour successfully!",
comment: comment
});
} catch (error) {
return res.status(500).json({ message: error.message });
}
};
rightValue = maxRightValue && maxRightValue.dataValues.maxRight !== null ? maxRightValue.dataValues.maxRight + 1 : 1;
}

// insert to comment
comment.comment_left = rightValue;
comment.comment_right = rightValue + 1;

await comment.save();
return res.status(200).json({
message: "You comment tour successfully!",
comment: comment
});
} catch (error) {
return res.status(500).json({ message: error.message });
}
};

getCommentsByParentId = async (req, res, next) => {
try {
const { tour_id, parent_comment_id, limit = 50, offset = 0 } = req.body;
let comments;

if (parent_comment_id) {
const parent = await Comment.findOne({ where: { parent_comment_id: parent_comment_id }});
const parent = await Comment.findOne({ where: { comment_id: parent_comment_id }});
if (!parent) return res.status(404).json({ message: "Not found comment for tour!"});

console.log(`parent:::`, parent)

comments = await Comment.findAll({
where: {
tour_id: tour_id,
Expand All @@ -90,8 +92,7 @@ class CommentController {
} else {
comments = await Comment.findAll({
where: {
tour_id: tour_id,
parent_comment_id: parent_comment_id
tour_id: tour_id
}, attributes: ['comment_left', 'comment_right', 'content', 'parent_comment_id'],
order: [['comment_left', 'ASC']],
limit,
Expand All @@ -107,6 +108,60 @@ class CommentController {
return res.status(500).json({ message: error.message })
}
}

deleteComment = async (req, res, next) => {
try {
const { tour_id, comment_id } = req.body

const tour = await Tour.findOne({ where: { tour_id: tour_id }})
if (!tour) return res.status(404).json({ message: "Not found tour for deleting comment!" })

const comment = await Comment.findOne({ where: { comment_id: comment_id }})
if (!comment) return res.status(404).json({ message: "Not found comment!" })

// 1. Define left & right value of comment
const leftValue = comment.comment_left
const rightValue = comment.comment_right

// 2. Calc width
const width = rightValue - leftValue + 1

// 3. Delete all child comments
await Comment.destroy({
where: {
tour_id: tour_id,
comment_left: { [Op.gte]: leftValue },
comment_left: { [Op.lte]: rightValue }
}
})

// 4. Update right & left
await Comment.update({
comment_right: Sequelize.literal(`comment_right - ${width}`)}, {
where: {
tour_id: tour_id,
comment_right: { [Op.gt]: rightValue }
}
}
)

await Comment.update({
comment_left: Sequelize.literal(`comment_left - ${width}`)}, {
where: {
tour_id: tour_id,
comment_left: { [Op.gt]: rightValue }
}
}
)

return res.status(200).json({
message: "Delete comment successfully!",
comment: await Comment.findOne({ where: { comment_id: comment_id }})
})
} catch (error) {
return res.status(500).json({ message: error.message })
}
}
}

module.exports = new CommentController()
7 changes: 5 additions & 2 deletions server/src/controllers/payment.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ const { findVoucherById } = require('../services/voucher.service');
const tmnCode = process.env.vnp_TmnCode;
const secretKey = process.env.vnp_HashSecret;
let url = process.env.vnp_Url;
const returnUrl = process.env.vnp_ReturnUrl; // cai nay ong de link giao dien thanh cong nha vi du:
// https:localhost:3000/success_payment
const returnUrl = process.env.vnp_ReturnUrl; // cai nay ong de link giao dien thanh cong nha vi du: https:localhost:3000/success_payment

class PaymentController {
/**
Expand Down Expand Up @@ -123,6 +122,10 @@ class PaymentController {
}
}

payDirectly = async (req, res, next) => {

}

getResultPayment = async (req, res, next) => {
try {
const vnp_Params = req.query;
Expand Down
89 changes: 89 additions & 0 deletions server/src/controllers/review.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
'use strict'

const cloudinary = require("../utils/cloudinary")
const { NotFoundError } = require("../core/error.response")
const Review = require("../models/review.model")
const { findTourById } = require("../services/tour.service")
const { findUserById } = require("../services/user.service")
const Comment = require("../models/comment.model")

class ReviewController {
creatReview = async (req, res, next) => {
try {
const {
user_id,
tour_id,
is_comment,
content,
parent_comment_id,
number_rate
} = req.fields;
console.log(`:::`, req.fields)

const user = await findUserById(user_id);
if (!user) throw new NotFoundError("Not found user!");

// check tour is ordered by user ?

const tour = await findTourById(tour_id);
if (!tour) throw new NotFoundError("Not found tour for reviewing!");

const list_image = [];
let i = 0;
while(req.files[`image[${i}]`]) {
const path_image = req.files[`image[${i}]`].path
const image = await cloudinary.uploader.upload(path_image)
list_image.push(image.secure_url)
i++;
}

const new_comment = is_comment ? ( await Comment.create({
content,
parent_comment_id,
image: list_image.length > 0 ? JSON.stringify(list_image) : null
}) ) : null;

const tour_review = await Review.findOne({ where: { tour_id: tour_id }});
if (!tour_review) {
const new_review = await Review.create({
user_id,
tour_id,
comment_id: new_comment ? new_comment.comment_id : null,
rating_1: number_rate == 1 ? 1 : 0,
rating_2: number_rate == 2 ? 1 : 0,
rating_3: number_rate == 3 ? 1 : 0,
rating_4: number_rate == 4 ? 1 : 0,
rating_5: number_rate == 5 ? 1 : 0,
count: 1,
average_rate: number_rate
})

return res.status(200).json({
message: "Review tour successfully!",
new_review: new_review
})
} else {
tour_review.rating_1 = number_rate == 1 ? tour_review.rating_1++ : tour_review.rating_1;
tour_review.rating_2 = number_rate == 2 ? tour_review.rating_2++ : tour_review.rating_2;
tour_review.rating_3 = number_rate == 3 ? tour_review.rating_3++ : tour_review.rating_3;
tour_review.rating_4 = number_rate == 4 ? tour_review.rating_4++ : tour_review.rating_4;
tour_review.rating_5 = number_rate == 5 ? tour_review.rating_5++ : tour_review.rating_5;
const av = (tour_review.average_rate * tour_review.count + number_rate) / (tour_review.count + 1);
console.log(`av:::`, av)
return res.status(200).json({ message: "OK"})
tour_review.average_rate = (tour_review.average_rate * tour_review.count + number_rate) / (tour_review.count + 1);
tour_review.count++;
await tour_review.save();

return res.status(200).json({
message: "Review tour successfully!",
new_review: tour_review
})
}
} catch (error) {
return res.status(500).json({ message: error.message })
}
}
}

module.exports = new ReviewController()
19 changes: 18 additions & 1 deletion server/src/controllers/voucher.controller.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict'

const { NotFoundError } = require("../core/error.response")
const Order = require("../models/order.model")
const Voucher = require("../models/voucher.model")
const cloudinary = require("../utils/cloudinary")
const Sequelize = require("sequelize")
const Op = Sequelize.Op
const cron = require("node-cron")

class VoucherController {

Expand Down Expand Up @@ -59,6 +60,22 @@ class VoucherController {
}
}

getVoucherByOrderId = async (req, res, next) => {
try {
const order_id = req.params;
const order = await Order.findByPk(order_id, { include: Voucher })
if (!order) throw new NotFoundError("Not found order!")

return res.status(200).json({
message: "Get vouchers of order successfully!",
vouchers: order.Voucher
})

} catch (error) {
return res.status(500).json({ message: error.message });
}
}

getAllVouchers = async (req, res, next) => {
try {
const all_vouchers = await Voucher.findAll({
Expand Down
4 changes: 4 additions & 0 deletions server/src/models/comment.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Comment.init({
type: DataTypes.TEXT,
allowNull: false
},
image: {
type: DataTypes.TEXT,
allowNull: true
},
comment_left: {
type: DataTypes.INTEGER,
allowNull: false,
Expand Down
Loading

0 comments on commit 44e9f45

Please sign in to comment.