From 8e28e66e2f2d7a08c26117b847bb15b036a75f5c Mon Sep 17 00:00:00 2001 From: DucHuy2801 Date: Sun, 19 May 2024 14:00:24 +0700 Subject: [PATCH] add getTopRatedTour --- server/src/controllers/tour.controller.js | 14 +++++++++++++- server/src/services/tour.service.js | 18 +++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/server/src/controllers/tour.controller.js b/server/src/controllers/tour.controller.js index c688389..07376b5 100644 --- a/server/src/controllers/tour.controller.js +++ b/server/src/controllers/tour.controller.js @@ -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") @@ -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; diff --git a/server/src/services/tour.service.js b/server/src/services/tour.service.js index ce0ec21..9e5acfb 100644 --- a/server/src/services/tour.service.js +++ b/server/src/services/tour.service.js @@ -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") @@ -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 } \ No newline at end of file