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

API for admin #25

Merged
merged 6 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1,347 changes: 1,335 additions & 12 deletions server/package-lock.json

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "mocha --recursive",
"start": "nodemon server.js"
},
"keywords": [],
Expand All @@ -15,6 +15,7 @@
"bcryptjs": "^2.4.3",
"bignumber.js": "^9.1.2",
"body-parser": "^1.20.2",
"chai": "^4.4.1",
"cloudinary": "^1.41.3",
"compression": "^1.7.4",
"config": "^3.3.11",
Expand Down Expand Up @@ -51,9 +52,14 @@
"randomstring": "^1.3.0",
"redis": "^4.6.13",
"sequelize": "^6.33.0",
"sinon": "^18.0.0",
"sinon-chai": "^3.7.0",
"socket.io": "^4.7.2",
"util": "^0.12.5",
"uuid": "^9.0.1",
"winston": "^3.13.0"
},
"devDependencies": {
"mocha": "^10.4.0"
}
}
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
50 changes: 50 additions & 0 deletions server/src/controllers/tour_guide.controller.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
'use strict'

const { RoleUser } = require("../common/status");
const GuideTour = require("../models/guide_tour.model");
const TourGuide = require("../models/tour_guide.model");
const { findTourById } = require("../services/tour.service")
const bcrypt = require('bcrypt');

const role_user = {
ADMIN: 'admin',
GUIDER: 'guider',
CUSTOMER: 'customer'
}

class TourGuideController {
assignTourToTourGuide = async (req, res, next) => {
Expand Down Expand Up @@ -45,6 +54,47 @@ class TourGuideController {
})
}
}

getAllTourGuide = async (req, res, next) => {
try {
const allTourGuides = await TourGuide.findAll()
return res.status(200).json({
message: "Get all tour guides successfully!",
data: allTourGuides
})
} catch (error) {
return res.status(500).json({ message: error.message })
}
}

createTourGuideDefault = async (req, res, next) => {
try {
const tourGuides = [
{ email: '[email protected]', password: 'tourguide@123', role_user: role_user.GUIDER },
{ email: '[email protected]', password: 'tourguide@123', role_user: role_user.GUIDER },
{ email: '[email protected]', password: 'tourguide@123', role_user: role_user.GUIDER },
]

for (const tourGuide of tourGuides) {
const existTourGuide = await TourGuide.findOne({ where: { email: tourGuide.email } });
if (!existTourGuide) {
const hashedPassword = await bcrypt.hashSync(tourGuide.password, 10);
console.log(2)
const newTourGuide = await TourGuide.create({
email: tourGuide.email,
password: hashedPassword,
role_user: tourGuide.role_user,
});
}
}

return res.status(201).json({
message: "Create tour guide accoutn successfully!"
})
} catch (error) {
throw new Error("Error: ", error.message)
}
}
}

module.exports = new TourGuideController()
29 changes: 0 additions & 29 deletions server/src/models/tour_guide.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const { DataTypes } = require("sequelize")
const sequelize = require("../database/connect.mysql")
const User = require("./user.model")
const { RoleUser } = require("../common/status")
const bcrypt = require('bcrypt');

class TourGuide extends User {}
TourGuide.init({
Expand Down Expand Up @@ -72,33 +71,5 @@ TourGuide.init({

}, { sequelize, modelName: 'tour_guide'})

const createTourGuideDefault = async () => {
try {
const tourGuides = [
{ email: '[email protected]', password: 'tourguide@123', role_user: RoleUser.TOUR_GUIDE },
{ email: '[email protected]', password: 'tourguide@123', role_user: RoleUser.TOUR_GUIDE },
{ email: '[email protected]', password: 'tourguide@123', role_user: RoleUser.TOUR_GUIDE },
]

for (const tourGuide of tourGuides) {
const existTourGuide = await TourGuide.findOne({ where: { email: tourGuide.email } });
if (!existTourGuide) {
const hashedPassword = await bcrypt.hash(guide.password, 10);
const tourGuideInstance = new TourGuide({
email: tourGuide.email,
password: hashedPassword,
role_user: tourGuide.role_user,
});

await tourGuideInstance.save();
}
}
} catch (error) {
throw new Error("Error: ", error)
}
}

createTourGuideDefault()

module.exports = TourGuide

Loading
Loading