Skip to content

Commit

Permalink
Ratings and hotels added
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnasSalman committed Dec 28, 2020
1 parent 8564035 commit 0991141
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 16 deletions.
6 changes: 3 additions & 3 deletions classes/Plan.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ class Plan{
this._hotelDistanceSmallWeight = 60;
this._hotelDistanceMediumWeight = 30;
this._hotelDistanceLargeWeight = 10;
this._hotelDistanceSmall = 1500;
this._hotelDistanceMedium = 2500;
this._hotelDistanceLarge = 4000;
this._hotelDistanceSmall = 2000;
this._hotelDistanceMedium = 6500;
this._hotelDistanceLarge = 15000;
}

async calculatePlaceStaysProbability (lat, lng) {
Expand Down
38 changes: 38 additions & 0 deletions middlewares/multerHotels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const multer = require('multer')
const path = require('path')

const storage = multer.diskStorage({
destination: './public/uploads/',
filename: function(req, file, cb){
cb(null,req.params.hotelid+'-'+file.fieldname + '.jpg');
console.log(file)
}
});

// Check File Type
function checkFileType(file, cb){
// Allowed ext
const filetypes = /jpeg|jpg|png|gif/;
// Check ext
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
// Check mime
const mimetype = filetypes.test(file.mimetype);

if(mimetype && extname){
return cb(null,true);
} else {
cb('Error: Images Only!');
}
}

// Init Upload
const uploadHotel = multer({
storage: storage,
fileFilter: function(req, file, cb){
checkFileType(file, cb);
}
}).fields([
{ name: 'hotel1', maxCount: 1 },
]);

module.exports = uploadHotel
16 changes: 15 additions & 1 deletion models/tour.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,21 @@ var Tour = new Schema({
ratings: [],
geometry: {
coordinates: { type: [Number], index: '2dsphere'}
}
},
fuelAverage: {
type: 'Number'
},
fuelPrice:{
type: 'Decimal128'
},
guests: {
type: 'Number'
},
totalBudget: {
type: 'Number'
},
hobbies: []


});

Expand Down
37 changes: 28 additions & 9 deletions routes/bookings.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,41 @@ const path = require('path')
router.use(bodyParser.json());

router.get('/findrooms', async (req, res)=>{
Booking.find({
try{
const hotelids = await Hotel.find({},'_id').getNearbyHotels(req.query.lat, req.query.lng, 15000)
const ids = hotelids.map((hotel)=>{
return hotel._id
})
console.log(ids)
const rooms = await Booking.find({
guestlimit: {$gt: parseInt(req.query.guests)-1},
price: {$gte: req.query.lower, $lte: req.query.upper},
bookings: {
$not: {
$elemMatch: {from: {$lt: req.query.to.substring(0,10)}, to: {$gt: req.query.from.substring(0,10)}}
}
}
}, function(err, rooms){
if(err){
console.log('entered')
res.send(err);
} else {
res.json(rooms);
}
});
}).where('hotelid').in(ids);
res.send(rooms)
}
catch(e){
res.status(400).send()
}
// Booking.find({
// guestlimit: {$gt: parseInt(req.query.guests)-1},
// price: {$gte: req.query.lower, $lte: req.query.upper},
// bookings: {
// $not: {
// $elemMatch: {from: {$lt: req.query.to.substring(0,10)}, to: {$gt: req.query.from.substring(0,10)}}
// }
// }
// }, function(err, rooms){
// if(err){
// res.send(err);
// } else {
// res.json(rooms);
// }
// });
})

router.get('/findroomsbyid/:hotelid/:roomid', async (req, res)=> {
Expand Down
56 changes: 56 additions & 0 deletions routes/hotels.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const bodyParser = require('body-parser');
const Hotel = require('../models/hotel')
const authenticate = require('../middlewares/authenticate');
const upload = require('../middlewares/multer')
const hotelUpload = require('../middlewares/multerHotels')
const glob = require('glob')
const path = require('path')
const fs = require('fs')
Expand All @@ -25,11 +26,40 @@ router.get('/:hotelid/getimages/:roomid', (req, res)=>{
})
})

router.get('/:hotelid/getimages', (req, res)=>{
glob(req.params.hotelid+'-hotel1.jpg',options, (err, files) => {
if(err){
res.send(err)
}
else{
res.send(files)
}
})
})

router.get('/gethotelsnearme', async(req, res)=>{
try{
const lat = req.query.lat
const lng = req.query.lng
const hotels = await Hotel.find({}).getNearbyHotels(lat, lng, 12000)
res.send(hotels)
}
catch(e){
res.status(400).send(e)
}
})

router.get('/room/images/:image', (req, res)=>{
res.sendFile(path.join(__dirname, '../public/uploads', req.params.image))
})

router.get('/hotel/images/:image', (req, res)=>{
console.log('eintachat')
res.sendFile(path.join(__dirname, '../public/uploads', req.params.image))
})

router.post('/:hotelid/uploadimages/:roomid', (req, res) => {
console.log(req.files)
upload(req, res, (err) => {
if(err){
res.status(400).send(err)
Expand All @@ -43,6 +73,32 @@ router.post('/:hotelid/uploadimages/:roomid', (req, res) => {
});
});

router.post('/:hotelid/uploadimages', (req, res) => {
hotelUpload(req, res, (err) => {
if(err){
res.status(400).send(err)
} else {
if(req.files == undefined){
res.status(400).send({error: 'undefined'});
} else {
res.status(200).send({status: 'done'});
}
}
});
});

router.post('/:hotelid/updatehotellocation', async(req,res)=>{
try{
const status = await Hotel.findByIdAndUpdate(req.params.hotelid,{
geometry: req.body.geometry
})
res.send(status)
}
catch(e){
res.status(400).send(e)
}
})

router.delete('/room/deleteimage/:image',(req, res)=>{
fs.unlink(path.join(__dirname, '../public/uploads', req.params.image), (err) => {
if (err) {
Expand Down
36 changes: 33 additions & 3 deletions routes/tours.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const _averageRatings = (ratings) => {
return (sum/ratings.length).toFixed(1)
}



router.get('/generatetour', async(req, res)=>{
const {coordinates, dates, budget, hobbies, fuelAverage, fuelType, guests} = req.query
let coordinateString = ''
Expand Down Expand Up @@ -143,10 +145,10 @@ router.get('/generatetour', async(req, res)=>{
hotel: hotelData,
rooms: result,
total: total,
stayDuration: stayLocation.stayDuration
stayDuration: stayLocation.stayDuration,
})
}
tourWithBookings = {...tourWithBookings, dateSchedule: {...tourWithBookings.dateSchedule, [checkInDate]: [{...tourWithBookings.dateSchedule[checkInDate][0], bookings: resultsWithTotals}]}}
tourWithBookings = {...tourWithBookings, dateSchedule: {...tourWithBookings.dateSchedule, [checkInDate]: [{...tourWithBookings.dateSchedule[checkInDate][0], bookings: resultsWithTotals}]},fuelPrice, fuelAverage, hobbies, guests}
}
}

Expand Down Expand Up @@ -217,7 +219,17 @@ router.get('/gettoursnearme', async(req, res)=> {
}
})

router.post('/rateTour/:tourId', async (req, res) => {
router.get('/gettourbyid/:id', async(req, res)=>{
try{
const tours = await Tour.findById(req.params.id)
res.send(tours)
}
catch(e){
res.status(400).send()
}
})

router.post('/ratetour/:tourId', async (req, res) => {
const {tourId} = req.params
const {rating} = req.body
try{
Expand All @@ -235,6 +247,24 @@ router.post('/rateTour/:tourId', async (req, res) => {
}
})

router.post('/updatetourrating/:tourId', async (req, res) => {
const {tourId} = req.params
const {rating} = req.body
try{
await Tour.update({_id: tourId, 'ratings.userid': rating.userid},
{
$set: {
'ratings.$.stars':rating.stars
}
}
)
res.status(200).send()
}
catch(e){
res.status(400).send()
}
})

router.post('/savetour', async (req, res) => {
try{
const lon = req.body.tour.route[0].geometry.coordinates.lng
Expand Down

0 comments on commit 0991141

Please sign in to comment.