Skip to content

Commit

Permalink
add getTopRatedTour
Browse files Browse the repository at this point in the history
  • Loading branch information
augustus281 committed May 19, 2024
1 parent 1ef2ca8 commit 8e28e66
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
14 changes: 13 additions & 1 deletion server/src/controllers/tour.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const DestinationTour = require("../models/destination_tour.model")
const Op = Sequelize.Op
const Attraction = require("../models/attraction.model")
const { StatusTour } = require("../common/status")
const { findTourById, duplicateTour } = require("../services/tour.service")
const { findTourById, duplicateTour, getTopRatedTour } = require("../services/tour.service")
const AttractionTour = require("../models/attraction_tour.model")
const Review = require("../models/review.model")
const UserTour = require("../models/user_tour.model")
Expand Down Expand Up @@ -277,6 +277,18 @@ class TourController {
}
};

getTopRatedTours = async (req, res, next) => {
try {
const tours = await getTopRatedTour()
return res.status(200).json({
message: "Get top rated tour successfully!",
data: tours
})
} catch (error) {
return res.status(500).json({ message: error.message })
}
}

getCommentOfTour = async (req, res, next) => {
try {
const tour_id = req.params.tour_id;
Expand Down
18 changes: 17 additions & 1 deletion server/src/services/tour.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Attraction = require("../models/attraction.model")
const AttractionTour = require("../models/attraction_tour.model")
const Destination = require("../models/destination.model")
const DestinationTour = require("../models/destination_tour.model")
const Review = require("../models/review.model")
const Schedule = require("../models/schedule.model")
const Tour = require("../models/tour.model")

Expand Down Expand Up @@ -74,8 +75,23 @@ const duplicateTour = async(tour_id) => {
}
}

const getTopRatedTour = async () => {
try {
const tours = await Tour.findAll({
order: [['average_rate', 'DESC']],
limit: 10
})

return tours
} catch (error) {
console.error("Error fetching top rated tours: ", error)
throw error
}
}

module.exports = {
findTourById,
findIdByNameTour,
duplicateTour
duplicateTour,
getTopRatedTour
}

0 comments on commit 8e28e66

Please sign in to comment.