-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpressrouter.js
More file actions
145 lines (140 loc) · 3.91 KB
/
expressrouter.js
File metadata and controls
145 lines (140 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const express =require('express');
const routs=express.Router();
const db=require('./db');
const passport=require('passport');
const Localpassport=require('passport-local').Strategy;
const person=require('./models/person');
const { route } = require('./menuerouts');
const {middleware,generatetoken}=require('./jwt');
const bctypt=require('bcrypt');
const { u } = require('tar');
routs.get('/profile',middleware,async function(req,res){
try{
const userdata=req.user;
const userId=userdata.id;
const user=await person.findById(userId);
res.status(200).json({user});
}catch(err){
res.status(500).json({err:"Profile error"});
}
});
routs.get('/', middleware,async (req,res)=>{
try{
const response= await person.find();
console.log("data feched")
res.status(200).json(response);
}
catch(err){
console.log("err : "+err);
res.status(500).json({error:"finished"});
}
})
routs.get('/:worktype',async function(req,res){
try{
const worktype=req.params.worktype;
if(worktype=='cheif'||worktype=='waiter'||worktype=='manager'){
const response=await person.find({work:worktype});
console.log('responce feched');
res.status(200).json(response);
}else{
res.status(404).json({erroe:'Invalid site'});
}
}
catch(err){
console.log("err "+err);
res.status(500).json({error:'Inter nal server error'});
}
})
routs.get('/', async (req,res)=>{
try{
const response= await person.find();
console.log("data feched")
res.status(200).json(response);
}
catch(err){
console.log("err : "+err);
res.status(500).json({error:"finished"});
}
})
routs.post('/login',async function(req,res){
try{
const {username,passward}=req.body;
const responce=await person.findOne({username:username});
if(!responce||!(await bctypt.compare(passward,responce.passward))){
// console.log(passward+" "+responce.passward);
res.status(404).json({err:"Invalid name"});
}
//generste tokens
const pyload={
id:responce.id,
name:responce.username
}
const token=generatetoken(pyload);
res.json({token});
// if(responce.passward!=passward)
}catch(err){
console.log("ERR "+err);
res.status(500).json({error:"catch error"});
}
})
// FOR TESTING PERPOSE
routs.post('/',async function(req,res){
try{
const data=req.body;
const newperson=new person(data);
const response= await newperson.save();
console.log('data saved');
const pyload={
id:response.id,
na:response.username
}
const token=generatetoken(pyload);
console.log("the token : "+JSON.stringify(pyload));
res.status(200).json({response:response,token:token});
}catch(err){
console.log("error "+err);
res.status(500).json({error:"Invalise operations"});
}
});
// routs.delete('/:id',async function(req,res){
// try{
// const id=req.params.id;
// const responce=await person.findByIdAndDelete(id);
// if(!responce){
// console.log("id not found");
// res.status(404).json({error:"Not found id"});
// }
// console.log("id is removed");
// res.status(200).json(responce);
// }catch(err){
// console.log("error : "+err);
// res.status(500).json({err:"invalid "})
// }
// });
routs.put('/:id',async function(req,res){
try{
const personid=req.params.id;
const changedata=req.body;
const responce=await person.findByIdAndUpdate(personid,changedata,{
new:true,
runValidators:true,
});
if(!responce){
res.status(404).json({error:"person not found"});
}
console.log('data updated');
res.status(200).json(responce);
}catch(err){
console.log("error -- "+err);
res.status(500).json({er:" Invalid type"});
}
});
// const comparepassword=async function(candidatepass){
// try{
// const ismatch=await bctypt.compare(candidatepass,this.passward);
// return ismatch;
// }catch(err){
// throw err;
// }
// }
module.exports=routs;