├── .gitignore ├── .env ├── routes ├── bookingRoute.js ├── galleryItemRoute.js ├── roomRoute.js ├── categoryRoute.js └── userRoute.js ├── model ├── galleryItems.js ├── category.js ├── room.js ├── booking.js └── user.js ├── package.json ├── index.js └── controller ├── galleryItemsController.js ├── bookingController.js ├── categoryController.js ├── roomController.js └── userController.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | package-lock.json 3 | .env -------------------------------------------------------------------------------- /.env: -------------------------------------------------------------------------------- 1 | MONGO_URL = mongodb+srv://tester2:123@cluster0.wd7xl.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0 2 | JWT_KEY = purna99 -------------------------------------------------------------------------------- /routes/bookingRoute.js: -------------------------------------------------------------------------------- 1 | import express from "express"; 2 | import { createBooking, getBookings, getUserSpecificBookings, updateStatus } from "../controller/bookingController.js"; 3 | 4 | const bookingRoute = express.Router(); 5 | 6 | bookingRoute.post("/",createBooking); 7 | bookingRoute.get("/",getBookings); 8 | bookingRoute.get("/userSpecific",getUserSpecificBookings); 9 | bookingRoute.put("/updateStatus/:bookingId",updateStatus) 10 | 11 | export default bookingRoute; -------------------------------------------------------------------------------- /routes/galleryItemRoute.js: -------------------------------------------------------------------------------- 1 | import express from 'express' 2 | import { getGalleryItem, postGalleryItem, getGalleryItemByName, deleteByName,updateGalleryItem } from '../controller/galleryItemsController.js'; 3 | 4 | 5 | const galleryItemRoute = express.Router(); 6 | 7 | galleryItemRoute.post("/",postGalleryItem) 8 | galleryItemRoute.get("/",getGalleryItem) 9 | galleryItemRoute.get("/:name",getGalleryItemByName) 10 | galleryItemRoute.delete("/:name",deleteByName) 11 | galleryItemRoute.put("/:name",updateGalleryItem) 12 | 13 | export default galleryItemRoute; 14 | 15 | -------------------------------------------------------------------------------- /model/galleryItems.js: -------------------------------------------------------------------------------- 1 | import mongoose from "mongoose"; 2 | 3 | const galleryItemSchema = mongoose.Schema( 4 | { 5 | name : { 6 | type : String, 7 | required : true, 8 | unique : true 9 | }, 10 | image : { 11 | type : String, 12 | required : true 13 | }, 14 | description : { 15 | type : String, 16 | required : true 17 | } 18 | } 19 | ) 20 | 21 | const GalleryItem = mongoose.model("galleryItems",galleryItemSchema) 22 | 23 | export default GalleryItem; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "introduction-to-express", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "type": "module", 7 | "scripts": { 8 | "test": "echo \"Error: no test specified\" && exit 1", 9 | "start": "nodemon index.js" 10 | }, 11 | "keywords": [], 12 | "author": "", 13 | "license": "ISC", 14 | "dependencies": { 15 | "bcryptjs": "^2.4.3", 16 | "body-parser": "^1.20.3", 17 | "dotenv": "^16.0.3", 18 | "express": "^4.21.1", 19 | "jsonwebtoken": "^9.0.2", 20 | "mongoose": "^8.7.2", 21 | "nodemon": "^3.1.7" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /routes/roomRoute.js: -------------------------------------------------------------------------------- 1 | import express from 'express' 2 | import { createRoom,deleteRoom,deleteRoomByParam,getRooms, updateRoom, roomById, getRoomByCategory } from '../controller/roomController.js'; 3 | 4 | const roomRoute = express.Router(); 5 | 6 | roomRoute.post("/",createRoom); 7 | roomRoute.get("/",getRooms); 8 | roomRoute.delete("/",deleteRoom); 9 | roomRoute.delete("/:id",deleteRoomByParam); 10 | roomRoute.put("/:id",updateRoom); 11 | roomRoute.get("/:id",roomById);//methanata danne api eha patthe controller eke param eka aran da ganna const eke name 12 | roomRoute.get("/by-category/:category",getRoomByCategory); 13 | 14 | export default roomRoute; -------------------------------------------------------------------------------- /routes/categoryRoute.js: -------------------------------------------------------------------------------- 1 | import express from 'express' 2 | import { createCategory, getCategory ,findCategoryByName, deleteCategory, findCategory, deleteCategoryByParam,updateCategory} from '../controller/categoryController.js'; 3 | 4 | const categoryRoute = express.Router(); 5 | 6 | categoryRoute.post("/",createCategory); 7 | categoryRoute.get("/",getCategory); 8 | categoryRoute.get("/categoryByName",findCategoryByName); 9 | categoryRoute.get("/:name",findCategory); 10 | categoryRoute.delete("/",deleteCategory); 11 | categoryRoute.delete("/:name",deleteCategoryByParam); 12 | categoryRoute.put("/:name",updateCategory); 13 | 14 | 15 | export default categoryRoute; -------------------------------------------------------------------------------- /routes/userRoute.js: -------------------------------------------------------------------------------- 1 | 2 | import express from 'express' 3 | import { getUser,postUser,updateUser,deleteUser, loginUser, getUserByEmail } from '../controller/userController.js'; //importing collection of function in single line 4 | 5 | const userRouter = express.Router(); 6 | 7 | //user post part 8 | userRouter.post("/",postUser) 9 | 10 | //user login 11 | userRouter.post("/loginUser",loginUser) 12 | 13 | //user get part 14 | userRouter.get("/",getUser) 15 | 16 | //get user by email 17 | userRouter.get("/getUserByEmail",getUserByEmail) 18 | 19 | //user update part 20 | userRouter.put("/",updateUser) 21 | 22 | //user delete part 23 | userRouter.delete("/",deleteUser) 24 | 25 | 26 | export default userRouter; -------------------------------------------------------------------------------- /model/category.js: -------------------------------------------------------------------------------- 1 | import mongoose from "mongoose"; 2 | 3 | const categorySchema = mongoose.Schema( 4 | { 5 | name: { 6 | type: String, 7 | required: true, 8 | unique: true 9 | }, 10 | description: { 11 | type: String, 12 | required: true 13 | 14 | }, 15 | price : { 16 | type : Number, 17 | required : true 18 | }, 19 | 20 | //features array ekak lesa save kirima 21 | features : [{ 22 | type : String, 23 | }], 24 | 25 | image : { 26 | type : String 27 | }, 28 | } 29 | ) 30 | 31 | const Category = mongoose.model("category",categorySchema) 32 | 33 | export default Category; -------------------------------------------------------------------------------- /model/room.js: -------------------------------------------------------------------------------- 1 | import mongoose from "mongoose"; 2 | 3 | const roomSchema = mongoose.Schema( 4 | { 5 | roomId : { 6 | type : Number, 7 | required : true, 8 | unique : true 9 | }, 10 | 11 | category : { 12 | type : String, 13 | required : true 14 | }, 15 | 16 | maxGuests : { 17 | type : Number, 18 | required : true 19 | }, 20 | 21 | available : { 22 | type : Boolean, 23 | required : true, 24 | default : true 25 | }, 26 | 27 | photos : [ 28 | { 29 | type : String 30 | } 31 | ], 32 | 33 | specialDescription : { 34 | type : String, 35 | default : "" 36 | }, 37 | 38 | notes : { 39 | type : String, 40 | default : "" 41 | } 42 | 43 | } 44 | ) 45 | 46 | 47 | const Room = mongoose.model("rooms",roomSchema); 48 | 49 | export default Room; -------------------------------------------------------------------------------- /model/booking.js: -------------------------------------------------------------------------------- 1 | import mongoose from "mongoose"; 2 | 3 | const bookingSchema = mongoose.Schema({ 4 | 5 | bookingId: { 6 | type: String, 7 | required: true, 8 | unique: true 9 | }, 10 | roomId : { 11 | type : Number, 12 | required : true 13 | }, 14 | email: { 15 | type: String, 16 | required: true 17 | }, 18 | status: { 19 | type: String, 20 | required: true, 21 | default: "pending" 22 | }, 23 | reason: { 24 | type: String, 25 | default: "" 26 | }, 27 | 28 | startDate: { 29 | type: Date, 30 | required: true 31 | }, 32 | endDate: { 33 | type: Date, 34 | required: true 35 | }, 36 | notes : { 37 | type : String, 38 | default : "" 39 | }, 40 | timeStamp : { 41 | type : Date, 42 | default : Date.now 43 | } 44 | }) 45 | 46 | const Booking = mongoose.model("bookingShema",bookingSchema); 47 | 48 | export default Booking; -------------------------------------------------------------------------------- /model/user.js: -------------------------------------------------------------------------------- 1 | import mongoose from "mongoose" 2 | 3 | const userSchema = mongoose.Schema( 4 | { 5 | email : { 6 | 7 | type : String, 8 | required : true, 9 | unique: true, 10 | }, 11 | password : { 12 | type : String, 13 | required : true 14 | }, 15 | firstName : { 16 | type : String, 17 | required : true, 18 | }, 19 | password : { 20 | type : String, 21 | required : true 22 | }, 23 | type : { 24 | type : String, 25 | required : true, 26 | default : "customer" 27 | }, 28 | whatsapp : { 29 | type : Number, 30 | required : true 31 | }, 32 | phoneNumber : { 33 | type : Number, 34 | required : true 35 | }, 36 | disabled : { 37 | type : Boolean, 38 | required : true, 39 | default : false 40 | }, 41 | emailVerified : { 42 | type : Boolean, 43 | required : true, 44 | default : false 45 | } 46 | } 47 | ) 48 | 49 | // Create the index if it doesn't exis 50 | userSchema.index({ email: 1 }, { unique: true }); 51 | 52 | 53 | //user kiyana collection ekata ara hada gaththa userSchema sturcture eka da gannawa 54 | const User = mongoose.model("users",userSchema) 55 | 56 | export default User; 57 | 58 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import bodyParser from 'body-parser' 2 | import express from 'express' 3 | import userRouter from './routes/userRoute.js' 4 | import mongoose from 'mongoose' 5 | import galleryItemRoute from './routes/galleryItemRoute.js' 6 | import categoryRoute from './routes/categoryRoute.js' 7 | import roomRoute from './routes/roomRoute.js' 8 | import jwt, { decode } from "jsonwebtoken" 9 | import dotenv from 'dotenv'; 10 | import bookingRoute from './routes/bookingRoute.js' 11 | dotenv.config(); 12 | 13 | 14 | const app = express() 15 | 16 | app.use(bodyParser.json()) //middleware 17 | 18 | const connectiionString = process.env.MONGO_URL; 19 | 20 | console.log("Mongo URL:", connectiionString); 21 | 22 | //authentication middleware 23 | app.use((req, res, next) => { 24 | 25 | const token = req.header("Authorization")?.replace("Bearer ", ""); //methanadi authentication header ekak thiyen req ekaka Bearer kotasa iwath kara ithuru kalla const token ekata replace kara gani 26 | 27 | if (token != null) { 28 | 29 | jwt.verify(token, process.env.JWT_KEY, (err, decode) => { 30 | 31 | //check the token valid 32 | if (err) { 33 | return res.status(403).json({ 34 | details: "jwt token error", 35 | Message: err.message 36 | }) 37 | } 38 | 39 | if (decode != null) { 40 | req.user = decode 41 | console.log("decode below") 42 | console.log(decode) 43 | next() 44 | } 45 | }) 46 | } else { 47 | next() 48 | } 49 | 50 | }); 51 | 52 | 53 | mongoose.connect(connectiionString).then( 54 | () => { 55 | console.log("connect to the database") 56 | } 57 | ).catch( 58 | () => { 59 | console.log("connection failed") 60 | } 61 | ) 62 | 63 | app.use("/api/users", userRouter) 64 | app.use("/api/galleryItems", galleryItemRoute) 65 | app.use("/api/category", categoryRoute) 66 | app.use("/api/room", roomRoute) 67 | app.use("/api/booking", bookingRoute) 68 | 69 | app.listen(5000, (req, res) => { 70 | console.log("Server is Running on port 5000") 71 | }); 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /controller/galleryItemsController.js: -------------------------------------------------------------------------------- 1 | 2 | import GalleryItem from "../model/galleryItems.js"; 3 | import { isAdmin, isUserValid } from "./userController.js"; 4 | 5 | export function postGalleryItem(req, res) { 6 | 7 | const validAdmin = isAdmin(req, res) 8 | 9 | if (!validAdmin) { 10 | return 11 | } 12 | 13 | const galleryItems = req.body; 14 | console.log(galleryItems); 15 | 16 | const newGalleryItem = new GalleryItem(galleryItems); 17 | 18 | newGalleryItem.save() 19 | .then(() => { 20 | console.log("Gallery item creation success"); 21 | return res.json({ 22 | message: "Gallery item created successfully" 23 | }); 24 | }) 25 | .catch((error) => { 26 | console.error("Gallery item creation failed:", error); 27 | // Ensure no response was sent yet 28 | if (!res.headersSent) { 29 | return res.status(500).json({ 30 | error: "Gallery item creation failed", 31 | message: error.message 32 | }); 33 | } 34 | }); 35 | } 36 | export function getGalleryItem(req, res) { 37 | 38 | GalleryItem.find().then( 39 | (galleryItemList) => { 40 | res.json({ 41 | list: galleryItemList 42 | }) 43 | } 44 | ) 45 | } 46 | 47 | export function getGalleryItemByName(req, res) { 48 | 49 | const name = req.params.name; 50 | 51 | const validUser = isUserValid(req, res); 52 | 53 | if (!validUser) { 54 | return 55 | } 56 | 57 | GalleryItem.findOne({ name }).then( 58 | (galleryItem) => { 59 | if (!galleryItem) { 60 | return res.status(404).json( 61 | { 62 | message: "gallery item not found" 63 | } 64 | ) 65 | } 66 | return res.json({ 67 | message: "Gallery item found", 68 | galleryItem: galleryItem 69 | })`1` 70 | } 71 | ).catch( 72 | (err) => { 73 | return res.status(500).json({ 74 | message: "internal server error", 75 | details: err.message 76 | }) 77 | } 78 | ) 79 | } 80 | 81 | 82 | 83 | export function deleteByName(req, res) { 84 | 85 | const name = req.params.name 86 | 87 | const validAdmin = isAdmin(req, res) 88 | 89 | if (!validAdmin) { 90 | return 91 | } 92 | 93 | GalleryItem.findOneAndDelete({ name }).then( 94 | (galleryItem) => { 95 | if (!galleryItem) { 96 | return res.status(404).json({ 97 | message: "Gallery item not found" 98 | }) 99 | } 100 | return res.json({ 101 | message: "Delete successfully" 102 | }) 103 | } 104 | ).catch( 105 | (err) => { 106 | return res.status(500).json({ 107 | message: "delete failed", 108 | details: err.message 109 | }) 110 | } 111 | ) 112 | } 113 | 114 | export function updateGalleryItem(req, res) { 115 | 116 | const name = req.params.name 117 | const updateData = req.body 118 | 119 | const validAdmin = isAdmin(req, res) 120 | 121 | if (!validAdmin) { 122 | return 123 | } 124 | 125 | GalleryItem.findOneAndUpdate({ name }, updateData, { new: true, runValidators: true }).then( 126 | (updateGalleryItem) => { 127 | if (!updateGalleryItem) { 128 | return res.status(404).json({ 129 | message: "Gallery item not found" 130 | }) 131 | } 132 | return res.json({ 133 | message: "Gallery Item found", 134 | updateData: updateGalleryItem 135 | }) 136 | } 137 | 138 | ).catch( 139 | (err) => { 140 | return res.status(500).json({ 141 | message: "Gallery Item update failed", 142 | details: err.message 143 | }) 144 | } 145 | ) 146 | } -------------------------------------------------------------------------------- /controller/bookingController.js: -------------------------------------------------------------------------------- 1 | import Booking from "../model/booking.js"; 2 | import Room from "../model/room.js"; 3 | import { isAdmin, isCustomer } from "./userController.js"; 4 | 5 | export function createBooking(req, res) { 6 | const validCustomer = isCustomer(req, res); 7 | 8 | if (!validCustomer) { 9 | return; 10 | } 11 | 12 | const startingId = 1200; 13 | // Methana count document kiyana eken booking schema eke thiyena row count eka enawa 14 | Booking.countDocuments({}).then((count) => { 15 | console.log(count); 16 | const newId = "INV" + startingId + count; 17 | 18 | Room.findOne({ roomId: req.body.roomId }).then((room) => { 19 | if (!room) { // Fixed `result` to `room` 20 | return res.status(404).json({ 21 | message: "room not found", 22 | }); 23 | } 24 | 25 | console.log("room id is :::::::: " + room.roomId); 26 | const newBooking = new Booking({ 27 | bookingId: newId, 28 | roomId: req.body.roomId, 29 | email: req.user.email, 30 | startDate: req.body.startDate, 31 | endDate: req.body.endDate, 32 | }); 33 | 34 | newBooking 35 | .save() 36 | .then((result) => { 37 | return res.json({ 38 | message: "successfully saved", 39 | result: result, 40 | }); 41 | }) 42 | .catch((err) => { 43 | res.status(500).json({ 44 | message: "booking failed", 45 | details: err.message, 46 | }); 47 | }); 48 | }); 49 | }); 50 | } 51 | 52 | export function getBookings(req, res) { 53 | 54 | const validAdmin = isAdmin(req, res); 55 | 56 | if (!validAdmin) { 57 | return 58 | } 59 | 60 | Booking.find().then( 61 | (bookings) => { 62 | if (bookings.length == 0) { 63 | return res.status(404).json({ 64 | message: "Booking is Empty" 65 | }) 66 | } 67 | return res.json({ 68 | message: "Booking found", 69 | bookings: bookings 70 | }) 71 | } 72 | ).catch( 73 | (err) => { 74 | return res.status(500).json({ 75 | message: "booking get failed", 76 | details: err.message 77 | }) 78 | } 79 | ) 80 | } 81 | 82 | export function getUserSpecificBookings(req,res){ 83 | 84 | const email = req.body.user.email; 85 | console.log("usessr email : "+email) 86 | const validCustomer = isCustomer(req,res); 87 | 88 | if(!validCustomer){ 89 | return 90 | } 91 | 92 | Booking.find({email:email}).then( 93 | (bookings)=>{ 94 | if(bookings.length == 0){ 95 | return res.status(404).json({ 96 | message : "Booking is empty" 97 | }) 98 | } 99 | return res.json({ 100 | message : "Booings Found", 101 | bookings : bookings 102 | }) 103 | } 104 | ).catch( 105 | (err)=>{ 106 | return res.status(500).json({ 107 | message : "Bookings find failier", 108 | err : err.message 109 | }) 110 | } 111 | ) 112 | } 113 | 114 | export function updateStatus(req,res){ 115 | 116 | const validAdmin = isAdmin(req,res); 117 | 118 | if(!validAdmin){ 119 | return 120 | } 121 | 122 | const bookingId = req.params.bookingId 123 | const updateData = req.body; 124 | 125 | Booking.findOneAndUpdate({bookingId : bookingId},updateData,{new: true, runValidators: true}).then( 126 | (updatedBooking)=>{ 127 | if(!updatedBooking){ 128 | return res.status(404).json({ 129 | message : "not found" 130 | }) 131 | } 132 | return res.json({ 133 | message: "status updated", 134 | updatedBooking : updatedBooking 135 | }) 136 | } 137 | ).catch( 138 | (err)=>{ 139 | return res.status(500).json({ 140 | message : "updated failier" 141 | }) 142 | } 143 | ) 144 | } -------------------------------------------------------------------------------- /controller/categoryController.js: -------------------------------------------------------------------------------- 1 | import Category from "../model/category.js"; 2 | import { isAdmin, isUserValid } from "./userController.js"; 3 | 4 | 5 | export function createCategory(req, res) { 6 | 7 | const validAdmin = isAdmin(req, res) 8 | 9 | if (!validAdmin) { 10 | return 11 | } 12 | 13 | const category = req.body; 14 | console.log(category) 15 | 16 | const newCategory = new Category(category); 17 | 18 | newCategory.save().then( 19 | (category) => { 20 | console.log("category creation success") 21 | return res.json( 22 | { 23 | message: "Category creation Succuess", 24 | category: category 25 | } 26 | ); 27 | } 28 | ).catch( 29 | (err) => { 30 | console.log("category creatio failed") 31 | return res.status(500).json({ 32 | message: "Category creation failed", 33 | err: err.message 34 | }); 35 | } 36 | ) 37 | 38 | } 39 | 40 | export function getCategory(req, res) { 41 | 42 | Category.find().then( 43 | (categoryList) => { 44 | return res.json({ 45 | message: "categories found", 46 | category: categoryList 47 | }) 48 | } 49 | ).catch( 50 | (err) => { 51 | return res.status(404).json({ 52 | message: "categories not found", 53 | details: err.message 54 | }) 55 | } 56 | ) 57 | } 58 | 59 | export function findCategoryByName(req, res) { 60 | 61 | const categoryName = req.body.name; 62 | 63 | Category.findOne({ name: categoryName }).then( 64 | (category) => { 65 | if (!category) { 66 | return res.status(404).json({ 67 | message: "Category not found", 68 | err: err.message 69 | }) 70 | } 71 | return res.json({ 72 | message: "Category found", 73 | category: category 74 | }) 75 | } 76 | ).catch( 77 | (err) => { 78 | return res.status(500).json({ 79 | message: "Category not found", 80 | err: err.message 81 | }) 82 | } 83 | ) 84 | } 85 | 86 | export function findCategory(req, res) { 87 | 88 | const name = req.params.name; 89 | 90 | Category.findOne({ name: name }).then( 91 | (category) => { 92 | if (!category) { 93 | return res.status(404).json({ 94 | message: "Category not found", 95 | err: err.message 96 | }) 97 | } 98 | return res.json({ 99 | message: "Category found", 100 | category: category 101 | }) 102 | } 103 | ).catch( 104 | (err) => { 105 | return res.status(500).json({ 106 | message: "Category not found", 107 | err: err.message 108 | }) 109 | } 110 | ) 111 | 112 | } 113 | 114 | export function deleteCategory(req, res) { 115 | 116 | const categoryName = req.body.name; 117 | 118 | const validAdmin = isAdmin(req, res) 119 | 120 | if (!validAdmin) { 121 | return 122 | } 123 | 124 | Category.findOneAndDelete({ name: categoryName }).then( 125 | (category) => { 126 | if (!category) { 127 | return res.status(404).json({ 128 | message: "category not found , delete failed" 129 | }) 130 | } 131 | return res.json({ 132 | message: "category found , delete successfully" 133 | }) 134 | } 135 | ).catch( 136 | (err) => { 137 | return res.status(500).json({ 138 | message: "delete category failed", 139 | details: err.message 140 | }) 141 | } 142 | ) 143 | 144 | } 145 | 146 | export function deleteCategoryByParam(req, res) { 147 | 148 | const categoryName = req.params.name; 149 | 150 | console.log("category name : ", categoryName) 151 | 152 | const validAdmin = isAdmin(req, res) 153 | 154 | if (!validAdmin) { 155 | return 156 | } 157 | 158 | Category.findOneAndDelete({ name: categoryName }).then( 159 | (category) => { 160 | if (!category) { 161 | return res.status(404).json({ 162 | message: "category not found , delete failed" 163 | }) 164 | } 165 | return res.json({ 166 | message: "category found , delete successfully" 167 | }) 168 | } 169 | ).catch( 170 | (err) => { 171 | return res.status(500).json({ 172 | message: "delete category failed", 173 | details: err.message 174 | }) 175 | } 176 | ) 177 | 178 | } 179 | 180 | 181 | export function updateCategory(req, res) { 182 | 183 | const name = req.params.name; 184 | const updateData = req.body; 185 | 186 | const validAdmin = isAdmin(req, res) 187 | 188 | if (!validAdmin) { 189 | return 190 | } 191 | 192 | Category.findOneAndUpdate({ name: name }, updateData, { new: true, runValidators: true }).then( 193 | (updateCategory) => { 194 | if (!updateCategory) { 195 | return res.status(404).json({ 196 | message: "category not found" 197 | }) 198 | } 199 | return res.json({ 200 | message: "category found", 201 | category: updateCategory 202 | }) 203 | } 204 | ).catch( 205 | (err) => { 206 | return res.status(500).json({ 207 | message: "category update failed", 208 | details: err.message 209 | }) 210 | } 211 | ) 212 | 213 | } -------------------------------------------------------------------------------- /controller/roomController.js: -------------------------------------------------------------------------------- 1 | import Room from "../model/room.js"; 2 | import { isAdmin, isUserValid } from "./userController.js"; 3 | 4 | export function createRoom(req, res) { 5 | 6 | const validUser = isUserValid(req, res) 7 | 8 | if (!validUser) { 9 | return 10 | } 11 | 12 | const room = req.body; 13 | console.log(room) 14 | 15 | const newRoom = new Room(room); 16 | 17 | newRoom.save().then( 18 | (room) => { 19 | console.log("room creation success") 20 | return res.json( 21 | { 22 | message: "room creation Succuess", 23 | room: room 24 | } 25 | ); 26 | } 27 | ).catch( 28 | (err) => { 29 | console.log("room creation failed") 30 | return res.status(500).json({ 31 | message: "room creation failed", 32 | err: err.message 33 | }); 34 | } 35 | ) 36 | } 37 | 38 | export function roomById(req, res) { 39 | 40 | const id = req.params.id; 41 | console.log("room id : " + id) 42 | 43 | 44 | //methana roomId kiyana eka denna one model eke thiyena name eka 45 | Room.findOne({ roomId: id }).then( 46 | (result) => { 47 | console.log("result : " + result) 48 | if (!result) { 49 | return res.status(404).json({ 50 | message: "room not found" 51 | }) 52 | } 53 | return res.json({ 54 | message: "room found", 55 | result: result 56 | }) 57 | } 58 | ).catch( 59 | (err) => { 60 | return res.status(500).json({ 61 | message: "failed to find", 62 | detais: err.message 63 | }) 64 | } 65 | ) 66 | } 67 | 68 | export function getRooms(req, res) { 69 | 70 | const validUser = isUserValid(req, res) 71 | 72 | if (!validUser) { 73 | return 74 | } 75 | 76 | Room.find().then( 77 | (roomList) => { 78 | 79 | if (roomList.length === 0) { 80 | return res.status(404).json( 81 | { 82 | message: "Room empty" 83 | } 84 | ) 85 | } 86 | res.json({ 87 | message: "Rooms Found", 88 | roomList: roomList 89 | }) 90 | } 91 | ) 92 | } 93 | 94 | 95 | export function deleteRoom(req, res) { 96 | 97 | const id = req.body.id 98 | 99 | console.log(id) 100 | 101 | const isValid = isAdmin(req, res) 102 | 103 | if (!isValid) { 104 | return 105 | } 106 | 107 | 108 | Room.deleteOne({ roomId: id }).then( 109 | (room) => { 110 | console.log(room) 111 | if (!room) { 112 | return res.status(404).json({ 113 | message: "Room not found" 114 | }) 115 | } 116 | return res.json({ 117 | message: "Delete successfully" 118 | }) 119 | } 120 | ).catch( 121 | (err) => { 122 | return res.status(500).json({ 123 | message: "delete failed from internal error", 124 | details: err.message 125 | }) 126 | } 127 | ) 128 | } 129 | 130 | export function deleteRoomByParam(req, res) { 131 | 132 | const id = req.params.id; 133 | 134 | const isValid = isAdmin(req, res) 135 | 136 | if (!isValid) { 137 | return 138 | } 139 | 140 | Room.findOneAndDelete({ roomId: id }).then( 141 | (room) => { 142 | if (!room) { 143 | return res.status(404).json({ 144 | message: "Room Not Found" 145 | }) 146 | } 147 | return res.json({ 148 | message: "Room found and Delete successfully" 149 | }) 150 | } 151 | ).catch( 152 | (err) => { 153 | return res.status(500).json({ 154 | message: "Room Deleted Failed" 155 | }) 156 | } 157 | ) 158 | } 159 | 160 | 161 | //me function eke updateOne() use karama eken return karanne 162 | //matchedCount,modified count ethakota eka check karala inn one nathhan methanadi ewana ona id ekak thibbth nathath result eka athulata ynw 163 | export function updateRoom(req, res) { 164 | 165 | const id = req.params.id 166 | const isValid = isAdmin(req, res) 167 | 168 | if (!isValid) { 169 | return 170 | } 171 | Room.updateOne({ roomId: id }, req.body).then( 172 | (result) => { 173 | 174 | if (result.matchedCount === 0) { 175 | return res.status(404).json({ 176 | message: "Room not found" 177 | }); 178 | } 179 | if (result.modifiedCount === 0) { 180 | return res.status(404).json({ 181 | message: "no changes made to the room" 182 | }); 183 | } 184 | return res.json({ 185 | message: "Update Successfully" 186 | }) 187 | } 188 | ).catch( 189 | (err) => { 190 | return res.status(500).json({ 191 | message: "failed the update" 192 | }) 193 | } 194 | ) 195 | 196 | } 197 | 198 | export function getRoomByCategory(req, res) { 199 | 200 | const category = req.params.category; 201 | 202 | Room.find({ category: category }).then( 203 | (category) => { 204 | console.log("inside category : " + category) 205 | if (category.length == 0) { 206 | return res.status(404).json({ 207 | message: "Category Not Found" 208 | }) 209 | } 210 | return res.json({ 211 | message: "category found", 212 | category: category 213 | }) 214 | } 215 | ).catch( 216 | (err) => { 217 | return res.status(500).json({ 218 | message: "Find Failed", 219 | details: err.message 220 | }) 221 | } 222 | ) 223 | } 224 | 225 | -------------------------------------------------------------------------------- /controller/userController.js: -------------------------------------------------------------------------------- 1 | 2 | import User from '../model/user.js' 3 | import jwt from 'jsonwebtoken' 4 | import bcrypt from 'bcryptjs' 5 | import dotenv from 'dotenv' 6 | dotenv.config() 7 | 8 | export function postUser(req, res) { 9 | const user = req.body; 10 | console.log("User data received:", user); 11 | 12 | const saltRounds = 10; 13 | const plainTextPassword = user.password; 14 | 15 | // Hash the password and wait for it to finish 16 | bcrypt.hash(plainTextPassword, saltRounds, (err, hashedPassword) => { 17 | if (err) { 18 | console.error("Error hashing password:", err); 19 | return res.status(500).json({ 20 | message: "Password hashing failed", 21 | details: err.message 22 | }); 23 | } 24 | 25 | // Set the hashed password 26 | user.password = hashedPassword; 27 | console.log("Hashed password:", user.password); 28 | 29 | // Create a new user with the hashed password 30 | const newUser = new User(user); 31 | 32 | // Save the new user to the database 33 | newUser.save() 34 | .then(() => { 35 | console.log("User created successfully:", user.email); 36 | res.json({ 37 | message: "User created successfully", 38 | res: newUser 39 | }); 40 | }) 41 | .catch((error) => { 42 | console.error("Error saving user:", error); 43 | res.status(500).json({ 44 | message: "User creation failed", 45 | details: error.message 46 | }); 47 | }); 48 | }); 49 | } 50 | 51 | export function loginUser(req, res) { 52 | 53 | const credential = req.body 54 | 55 | console.log("credential password : " + credential.password) 56 | 57 | 58 | 59 | User.findOne({ email: credential.email }).then( 60 | (user) => { 61 | if (user == null) { 62 | 63 | res.status(404).json( 64 | { 65 | message: "User not found" 66 | } 67 | ) 68 | } else { 69 | 70 | console.log("user password in db : ", user.password) 71 | 72 | const isPasswordValid = bcrypt.compare(credential.password, user.password); //methanadi api dena password eka automa hash karagena user password ekth ekka compaire karala harida balayi 73 | 74 | if (!isPasswordValid) { 75 | 76 | return res.status(403).json({ 77 | message: "Incorrect Password" 78 | }) 79 | } else { 80 | const payload = { 81 | id: user._id, 82 | email: user.email, 83 | firstName: user.firstName, 84 | lastName: user.lastName, 85 | type: user.type 86 | } 87 | 88 | const token = jwt.sign(payload, process.env.JWT_KEY, { expiresIn: "744h" }); 89 | 90 | return res.json({ 91 | message: "user found", 92 | user: user, 93 | token: token 94 | }) 95 | 96 | } 97 | 98 | } 99 | 100 | 101 | } 102 | ) 103 | } 104 | 105 | export function getUser(req, res) { 106 | 107 | const validUser = isAdmin(req,res); 108 | 109 | if(!validUser){ 110 | return 111 | } 112 | 113 | console.log("get user") 114 | User.find().then( 115 | (userList) => { 116 | 117 | res.json({ 118 | list: userList 119 | }) 120 | } 121 | ).catch( 122 | (error) => { 123 | res.json({ 124 | message: "get failed", 125 | details: error.message 126 | }) 127 | } 128 | ) 129 | } 130 | 131 | export function getUserByEmail(req, res) { 132 | 133 | const validUser = isUserValid(req,res); 134 | 135 | if(!validUser){ 136 | return 137 | } 138 | 139 | const email = req.body.email; 140 | 141 | User.findOne({ email: email }).then( 142 | (user) => { 143 | if (!user) { 144 | return res.status(404).json({ 145 | message: "user Not found" 146 | }) 147 | } 148 | return res.json({ 149 | message: "user found", 150 | user: user 151 | }) 152 | 153 | } 154 | ) 155 | } 156 | 157 | 158 | export function updateUser(req, res) { 159 | 160 | const validUser = isUserValid(req,res); 161 | 162 | if(!validUser){ 163 | return 164 | } 165 | 166 | const email = req.body.email; 167 | const updatedData = req.body; // The data to update 168 | 169 | // Find the user by email and update fields provided in updatedData 170 | User.findOneAndUpdate({ email: email }, updatedData, { new: true, runValidators: true }) 171 | .then((updatedUser) => { 172 | if (!updatedUser) { 173 | return res.status(404).json({ 174 | message: "User not found", 175 | }); 176 | } 177 | res.json({ 178 | message: "User updated successfully", 179 | user: updatedUser 180 | }); 181 | }) 182 | .catch((error) => { 183 | console.error("Error updating user:", error); 184 | res.status(500).json({ 185 | message: "User update failed", 186 | details: error.message 187 | }); 188 | }); 189 | } 190 | 191 | 192 | export function deleteUser(req, res) { 193 | 194 | const validAdmin = isAdmin(req,res); 195 | 196 | if(!validAdmin){ 197 | return 198 | } 199 | 200 | const email = req.body.email; 201 | 202 | User.deleteOne({ email: email }).then( 203 | () => { 204 | res.json( 205 | { 206 | message: "user delete successfully" 207 | 208 | } 209 | ) 210 | } 211 | ).catch( 212 | () => { 213 | res.json({ 214 | message: "user delete failed" 215 | }) 216 | } 217 | ) 218 | } 219 | 220 | export function isUserValid(req, res) { 221 | 222 | const user = req.user; 223 | 224 | if (!user) { 225 | res.status(401).json({ 226 | message: "Authentication required" 227 | }) 228 | return false 229 | } 230 | return true 231 | } 232 | 233 | export function isAdmin(req, res) { 234 | 235 | const userValid = isUserValid(req, res); 236 | 237 | if (!userValid) { 238 | return false 239 | } 240 | 241 | 242 | if (req.user.type != "admin") { 243 | res.status(403).json({ 244 | message: "Only Admin can doing this task" 245 | }) 246 | return false 247 | } 248 | return true 249 | } 250 | 251 | export function isCustomer(req, res) { 252 | 253 | 254 | const userValid = isUserValid(req, res); 255 | 256 | if (!userValid) { 257 | return false 258 | } 259 | 260 | 261 | if (req.user.type != "customer") { 262 | res.status(403).json({ 263 | message: "Only customer can doing this task" 264 | }) 265 | return false 266 | } 267 | return true 268 | } --------------------------------------------------------------------------------