-
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
Showing
22 changed files
with
5,117 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
LOCAL_URL= | ||
JWT_SECRET= |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
.env | ||
|
||
# testing | ||
/coverage | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Backend for Travel Buddies |
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import Attraction from "../models/Attraction.js"; | ||
import Location from "../models/Location.js"; | ||
// import { createError } from '../utils/error.js'; | ||
|
||
export const createAttraction = async (req, res, next) => { | ||
const locationId = req.params.locationid; | ||
const newAttraction = new Attraction(req.body); | ||
|
||
try { | ||
const savedAttraction = await newAttraction.save(); | ||
try { | ||
await Location.findByIdAndUpdate(locationId, { | ||
$push: { | ||
Attractions: savedAttraction._id, | ||
}, | ||
}); | ||
} catch (err) { | ||
next(err); | ||
} | ||
res.status(200).json(savedAttraction); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; | ||
|
||
export const updateAttraction = async (req, res) => { | ||
try { | ||
const updatedAttraction = await Location.findByIdAndUpdate( | ||
req.params.id, | ||
{ $set: req.body }, | ||
{ new: true } | ||
); | ||
res.status(200).json(updatedAttraction); | ||
} catch (err) { | ||
res.status(500).json(err); | ||
} | ||
}; | ||
|
||
//Deleting a Attraction | ||
export const deleteAttraction = async (req, res, next) => { | ||
const LocationId = req.params.Locationid; | ||
try { | ||
await Attraction.findByIdAndDelete(req.params.id); | ||
try { | ||
await Location.findByIdAndUpdate(LocationId, { | ||
$pull: { | ||
Attractions: req.params._id, | ||
}, | ||
}); | ||
} catch (err) { | ||
next(err); | ||
} | ||
res.status(200).json("Attraction has been deleted"); | ||
} catch (err) { | ||
res.status(500).json(err); | ||
} | ||
}; | ||
|
||
//Getting a Attraction | ||
export const getAttraction = async (req, res) => { | ||
try { | ||
const Attraction = await Attraction.findById(req.params.id); | ||
res.status(200).json(Attraction); | ||
} catch (err) { | ||
res.status(500).json(err); | ||
} | ||
}; | ||
|
||
//Getting all Attractions | ||
export const getAttractions = async (req, res, next) => { | ||
try { | ||
const Attractions = await Location.find(); | ||
res.status(200).json(Attractions); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import User from "../models/User.js"; | ||
import dotenv from "dotenv"; | ||
import jwt from "jsonwebtoken"; | ||
import { createError } from "../utils/error.js"; | ||
|
||
dotenv.config(); | ||
export const register = async (req, res, next) => { | ||
try { | ||
const newUser = new User({ | ||
username: req.body.username, | ||
email: req.body.email, | ||
}); | ||
await newUser.save(); | ||
|
||
const accessToken = jwt.sign( | ||
{ id: newUser._id, isAdmin: newUser.isAdmin }, | ||
process.env.JWT_SECRET | ||
); | ||
const { isAdmin, ...others } = newUser._doc; | ||
res | ||
.cookie("access_token", accessToken, { | ||
httpOnly: true, | ||
}) | ||
.status(200) | ||
|
||
.json({ ...others, accessToken }); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; | ||
|
||
export const login = async (req, res, next) => { | ||
try { | ||
const user = await User.findOne({ | ||
username: req.body.username, | ||
}); | ||
|
||
!user && createError("User not found", 404); | ||
|
||
const accessToken = jwt.sign( | ||
{ id: user._id, isAdmin: user.isAdmin }, | ||
process.env.JWT_SECRET | ||
); | ||
const { isAdmin, ...others } = user._doc; | ||
res | ||
.cookie("access_token", accessToken, { | ||
httpOnly: true, | ||
}) | ||
.status(200) | ||
.json({ ...others, isAdmin }); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; | ||
|
||
// try { | ||
// const user = await User.findOne({ | ||
// username: req.body.username, | ||
// }); | ||
// if (!user) return next(createError(404, "User not found")); | ||
|
||
// const isEmailCorrect = await User.findOne({ | ||
// email: req.body.email, | ||
// }); | ||
// if (!isEmailCorrect) | ||
// return next(createError(400, "Wrong Username or Email")); | ||
|
||
// const token = jwt.sign( | ||
// { id: user._id, isAdmin: user.isAdmin }, | ||
// process.env.JWT_SECRET | ||
// ); | ||
|
||
// const { isAdmin, ...otherDetails } = user._doc; | ||
// res | ||
// .cookie("access_token", token, { | ||
// httpOnly: true, | ||
// }) | ||
// .status(200) | ||
// .json(otherDetails, isAdmin); | ||
// } catch (err) { | ||
// next(err); | ||
// } |
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import Location from "../models/Location.js"; | ||
import Attraction from "../models/Attraction.js"; | ||
|
||
//Creating a Location | ||
export const createLocation = async (req, res) => { | ||
const newLocation = new Location(req.body); | ||
|
||
try { | ||
const savedLocation = await newLocation.save(); | ||
res.status(200).json(savedLocation); | ||
} catch (err) { | ||
res.status(500).json(err); | ||
} | ||
}; | ||
|
||
//Updating a Location | ||
export const updateLocation = async (req, res) => { | ||
try { | ||
const updatedLocation = await Location.findByIdAndUpdate( | ||
req.params.id, | ||
{ $set: req.body }, | ||
{ new: true } | ||
); | ||
res.status(200).json(updatedLocation); | ||
} catch (err) { | ||
res.status(500).json(err); | ||
} | ||
}; | ||
|
||
//Deleting a Location | ||
export const deleteLocation = async (req, res) => { | ||
try { | ||
await Location.findByIdAndDelete(req.params.id); | ||
res.status(200).json("Location has been deleted"); | ||
} catch (err) { | ||
res.status(500).json(err); | ||
} | ||
}; | ||
|
||
//Getting a Location | ||
export const getLocation = async (req, res) => { | ||
try { | ||
const Location = await Location.findById(req.params.id); | ||
res.status(200).json(Location); | ||
} catch (err) { | ||
res.status(500).json(err); | ||
} | ||
}; | ||
|
||
//Getting all Locations | ||
export const getLocations = async (req, res, next) => { | ||
const { min, max, ...others } = req.query; | ||
try { | ||
const Locations = await Location.find({ | ||
...others, | ||
cheapestPrice: { $gt: min | 1, $lt: max || 999 }, | ||
}).limit(req.query.limit); | ||
res.status(200).json(Locations); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; | ||
|
||
export const getAttraction = async (req, res, next) => { | ||
try { | ||
const location = await Location.findById(req.params.id); | ||
const list = await Promise.all( | ||
location.attraction.map((attraction) => { | ||
return Attraction.findById(attraction); | ||
}) | ||
); | ||
res.status(200).json(list); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import Trip from "../models/Trip.js"; | ||
|
||
//Creating a Trip | ||
export const createTrip = async (req, res) => { | ||
const newTrip = new Trip(req.body); | ||
|
||
try { | ||
const savedTrip = await newTrip.save(); | ||
res.status(200).json(savedTrip); | ||
} catch (err) { | ||
res.status(500).json(err); | ||
} | ||
}; | ||
|
||
//Updating a Trip | ||
export const updateTrip = async (req, res) => { | ||
try { | ||
const updatedTrip = await Trip.findByIdAndUpdate( | ||
req.params.id, | ||
{ $set: req.body }, | ||
{ new: true } | ||
); | ||
res.status(200).json(updatedTrip); | ||
} catch (err) { | ||
res.status(500).json(err); | ||
} | ||
}; | ||
|
||
//Deleting a Trip | ||
export const deleteTrip = async (req, res) => { | ||
try { | ||
await Trip.findByIdAndDelete(req.params.id); | ||
res.status(200).json("Trip has been deleted"); | ||
} catch (err) { | ||
res.status(500).json(err); | ||
} | ||
}; | ||
|
||
//Getting a Trip | ||
export const getTrip = async (req, res) => { | ||
try { | ||
const Trip = await Trip.findById(req.params.id); | ||
res.status(200).json(Trip); | ||
} catch (err) { | ||
res.status(500).json(err); | ||
} | ||
}; | ||
|
||
//Getting all Trips | ||
export const getTrips = async (req, res, next) => { | ||
const { min, max, ...others } = req.query; | ||
try { | ||
const Trips = await Trip.find({ | ||
...others, | ||
cheapestPrice: { $gt: min | 1, $lt: max || 999 }, | ||
}).limit(req.query.limit); | ||
res.status(200).json(Trips); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import User from "../models/User.js"; | ||
|
||
//Updating a User | ||
export const updateUser = async (req, res) => { | ||
try { | ||
const updatedUser = await User.findByIdAndUpdate( | ||
req.params.id, | ||
{ $set: req.body }, | ||
{ new: true } | ||
); | ||
res.status(200).json(updatedUser); | ||
} catch (err) { | ||
res.status(500).json(err); | ||
} | ||
}; | ||
|
||
//Deleting a User | ||
export const deleteUser = async (req, res) => { | ||
try { | ||
await User.findByIdAndDelete(req.params.id); | ||
res.status(200).json("User has been deleted"); | ||
} catch (err) { | ||
res.status(500).json(err); | ||
} | ||
}; | ||
|
||
//Getting a User | ||
export const getUser = async (req, res) => { | ||
try { | ||
const user = await User.findById(req.params.id); | ||
res.status(200).json(user); | ||
} catch (err) { | ||
res.status(500).json(err); | ||
} | ||
}; | ||
|
||
//Getting all Users | ||
export const getUsers = async (req, res, next) => { | ||
try { | ||
const users = await User.find(); | ||
res.status(200).json(users); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; |
Oops, something went wrong.