-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2db5072
commit 940397c
Showing
4 changed files
with
47 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,24 @@ | ||
const reservation = require('../models/ordersModel'); | ||
const reservation = require("../models/ordersModel"); | ||
const Destination = require("../models/destinationModel"); | ||
const jwt = require("jsonwebtoken"); | ||
|
||
const saveReservation = async (req, res) => { | ||
const reservations = req.body; | ||
try { | ||
const reservedData = await reservation.saveReservation(reservations); | ||
res.json({ status: 'ok', reservedData }); | ||
} catch (error) { | ||
res.json({ status: 'error', error: error.message }); | ||
const cookie = req.cookies.jwt; | ||
const id = req.body; | ||
try { | ||
if (cookie) { | ||
const decoded = jwt.verify(cookie, process.env.JWT_SECRET); | ||
const dest = await Destination.getDestination(id); | ||
const reservedData = await reservation.saveReservation( | ||
dest._id, | ||
decoded.id | ||
); | ||
res.json({ status: "ok", reservedData }); | ||
} else { | ||
res.json({ status: "error", error: "User not authenticated" }); | ||
} | ||
} | ||
module.exports = {saveReservation} | ||
} catch (error) { | ||
res.json({ status: "error", error: error.message }); | ||
} | ||
}; | ||
module.exports = { saveReservation }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
const mongoose=require('mongoose'); | ||
const reservation = new mongoose.Schema({ | ||
id: String, | ||
title: String, | ||
host: String, | ||
price: Number, | ||
description: String, | ||
from: Date, | ||
to: Date, | ||
}); | ||
const mongoose = require("mongoose"); | ||
|
||
reservation.statics.saveReservation = async function (data) { | ||
const reservation = new this.insertMany(data); | ||
return reservation.save(); | ||
}; | ||
const Reservation = new mongoose.Schema( | ||
{ | ||
destinationID: { type: String }, | ||
userID: { type: String, required: true, unique: true }, | ||
}, | ||
{ collection: "reservation" } | ||
); | ||
|
||
module.exports = mongoose.model('Reservation', reservation); | ||
Reservation.statics.saveReservation = async function (destinationID, userID) { | ||
if (!destinationID || !userID) { | ||
throw("Missing IDs") | ||
} | ||
|
||
const reservation = await this.create(destinationID, userID); | ||
return reservation; | ||
}; | ||
|
||
module.exports = mongoose.model("Reservation", Reservation); |