├── .gitignore ├── Schema.cjs ├── package.json └── server.cjs /.gitignore: -------------------------------------------------------------------------------- 1 | /package-lock.json 2 | /node_modules -------------------------------------------------------------------------------- /Schema.cjs: -------------------------------------------------------------------------------- 1 | const mongoose=require('mongoose') 2 | 3 | const RestaurantSchema=new mongoose.Schema({ 4 | areaName:{ 5 | type:String 6 | }, 7 | avgRating:{ 8 | type:Number 9 | }, 10 | costForTwo:{ 11 | type:String 12 | }, 13 | cuisines:{ 14 | type:Array 15 | }, 16 | imageLink:{ 17 | type:String 18 | }, 19 | name:{ 20 | type:String 21 | } 22 | }) 23 | 24 | const userSchema=new mongoose.Schema({ 25 | userName:{ 26 | type:String 27 | }, 28 | email:{ 29 | type:String 30 | }, 31 | password:{ 32 | type:String 33 | }, 34 | contact:{ 35 | type:Number 36 | } 37 | }) 38 | const Users =mongoose.model('User-Details',userSchema) 39 | const Restaurant =mongoose.model('RestaurantDetails',RestaurantSchema) 40 | 41 | module.exports={Restaurant,Users} -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "swigi_backend", 3 | "version": "1.0.0", 4 | "main": "index.js", 5 | "scripts": { 6 | "start":"node server.cjs", 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "body-parser": "^1.20.2", 14 | "cors": "^2.8.5", 15 | "express": "^4.18.2", 16 | "mongoose": "^8.1.2" 17 | }, 18 | "devDependencies": { 19 | "nodemon": "^3.0.3" 20 | }, 21 | "description": "" 22 | } 23 | -------------------------------------------------------------------------------- /server.cjs: -------------------------------------------------------------------------------- 1 | const express=require('express') 2 | const bodyparser= require('body-parser') 3 | const cors =require('cors') 4 | const mongoose= require('mongoose') 5 | 6 | 7 | const {Restaurant,Users}=require('./Schema.cjs') 8 | 9 | const app=express() 10 | 11 | app.use(bodyparser.json()) 12 | app.use(cors()) 13 | 14 | const port =process.env.PORT || 8000 15 | 16 | async function connectToDb() { 17 | try{ 18 | await mongoose.connect('mongodb+srv://krish:gobal@cluster0.3akbqm2.mongodb.net/Swigi?retryWrites=true&w=majority') 19 | 20 | app.listen(port,function () { 21 | console.log('Listening on port 8000... ') 22 | }) 23 | } 24 | catch(error){ 25 | console.log(error) 26 | } 27 | } 28 | connectToDb() 29 | //add-res:post 30 | //get-res-details:get 31 | //update-res:patech 32 | //delete-res:delete 33 | //creat-new -user:post 34 | //validate-user:post 35 | app.post('/add-restaturant',async function(req,res){ 36 | try{ 37 | await Restaurant.create({ 38 | "areaName":req.body.areaName, 39 | "avgRating" :req.body.avgRating, 40 | "costForTwo":req.body.costForTwo, 41 | "cuisines":req.body.cuisines, 42 | "imageLink":req.body.imageLink, 43 | "name":req.body.name 44 | }) 45 | 46 | res.status(201).json({"Satus":"data set created"}) 47 | 48 | }catch(errror){ 49 | res.status(500).json({"status":"not created"}) 50 | console.log(error) 51 | } 52 | }) 53 | 54 | app.get('/get-res-details',async function (req,res) { 55 | try { 56 | const resDetails = await Restaurant.find() 57 | res.status(200).json(resDetails) 58 | } catch (error) { 59 | res.status(500).json({ 60 | "satus":"no details found", 61 | "error":error 62 | }) 63 | } 64 | }) 65 | 66 | 67 | app.delete('/delete-restaurant/:id',async function(req,res) { 68 | try { 69 | const restaurant = await Restaurant.findById(req.params.id) 70 | if(restaurant){ 71 | await restaurant.findByIdAndDelete(req.params.id) 72 | res.status(201).json({"Message":"Successfully Deleted"}) 73 | } 74 | else{ 75 | res.status(404).json({ 76 | "status":"failure", 77 | "messge":"Give valid id" 78 | }) 79 | } 80 | } catch (error) { 81 | res.status(500).json({ 82 | "Status":"can not be Delete", 83 | "error":error 84 | }) 85 | } 86 | }) 87 | 88 | app.post('/add-user',async function (req,res) { 89 | try { 90 | await Users.create({ 91 | "userName":req.body.userName, 92 | "email":req.body.emial, 93 | "password":req.body.password, 94 | "contact":req.body.contact 95 | }) 96 | 97 | res.status(201).json({"Status":"user created"}) 98 | } 99 | catch (error) { 100 | res.status(500).json({"Status":"User Not created"}) 101 | } 102 | }) 103 | 104 | app.get('/valid-user',async function (req,res) { 105 | try { 106 | const user = await Users.findOne({ 107 | "email":req.body.email, 108 | "password":req.body.password 109 | }) 110 | if (user) { 111 | res.status(201).json({"Status":"user is avilable"}) 112 | } 113 | else{ 114 | res.status(401).json({"Status":"Invalid user"}) 115 | } 116 | } 117 | catch (error) { 118 | res.status(500).json({"Status":"No User"}) 119 | console.log(error) 120 | } 121 | }) --------------------------------------------------------------------------------