-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
212 lines (207 loc) · 5.11 KB
/
index.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
const express=require("express");
const cors=require("cors");
const {User,Question,db}=require("./config.js");
const {doc,getDoc}=require("firebase/firestore");
const app=express();
// const cors = require('cors')
app.use(cors({origin: '*'}))
app.use(express.json());
// app.use(cors());
const query = `
query getUserProfile($username: String!) {
allQuestionsCount {
difficulty
count
}
userContestRanking(username: $username) {
attendedContestsCount
rating
globalRanking
totalParticipants
topPercentage
badge {
name
}
}
matchedUser(username: $username) {
contestBadge {
name
expired
hoverText
icon
}
username
githubUrl
twitterUrl
linkedinUrl
profile {
ranking
userAvatar
realName
aboutMe
school
websites
countryName
company
jobTitle
skillTags
postViewCount
postViewCountDiff
reputation
reputationDiff
solutionCount
solutionCountDiff
categoryDiscussCount
categoryDiscussCountDiff
}
badges {
id
name
shortName
displayName
icon
hoverText
medal {
slug
config {
iconGif
iconGifBackground
}
}
creationDate
category
}
upcomingBadges {
name
icon
progress
}
}
recentSubmissionList(username: $username) {
title
titleSlug
timestamp
statusDisplay
lang
__typename
}
matchedUserStats: matchedUser(username: $username) {
submitStats: submitStatsGlobal {
acSubmissionNum {
difficulty
count
submissions
__typename
}
totalSubmissionNum {
difficulty
count
submissions
__typename
}
__typename
}
}
}
`;
const fetchData=async()=>{
const snapshot=await User.get();
// console.log(snapshot.docs);
// const list=snapshot.docs.map((doc)=>doc.data());
const list=[];
const data = snapshot.docs;
for(const val of data){
const obj=val.data();
obj.id=val.id;
list.push(obj);
}
// console.log("Data:");
// console.log(data);
return list;
}
const fetchQuestions=async()=>{
const snapshot=await Question.get();
const list=snapshot.docs.map((doc)=>doc.data());
return list;
}
app.get("/fetchQuestions",async(req,res)=>{
const list=await fetchQuestions();
res.json({questions:list});
})
app.post("/createQuestions",async(req,res)=>{
const data=req.body;
Question.add(data);
const updatedList=await fetchQuestions();
res.json({ questions: updatedList });
// res.send({msg:"User added"});
})
app.post("/create",async(req,res)=>{
const data=req.body;
const list=await fetchData();
let flag=0;
for(const user of list){
if(user.username==data.username && user.password==data.password){
res.json({msg:"Exists"});
flag=1;
break;
}
}
if(flag==0){
User.add(data);
const updatedList=await fetchData();
res.json({users:updatedList});
}
// res.send({msg:"User added"});
})
app.post("/fetchUserProfile",async(req,res)=>{
const reqBody=req.body;
const username=reqBody["username"];
const resBody=await fetch('https://leetcode.com/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Referer': 'https://leetcode.com'
},
body: JSON.stringify({query: query, variables: {username: username}}),
})
const data=await resBody.json();
console.log(data);
res.json({data:data});
})
app.post("/favs",async(req,res)=>{
console.log("received");
const data=req.body;
const id=data["id"];
const favs=data["favs"];
console.log(data);
const docRef=User.doc(id);
const snap=await docRef.get();
console.log(snap.data());
const user=snap.data();
console.log("user:");
console.log(user);
user["favorites"]=favs;
const delRes = await User.doc(id).delete();
const addRes = await User.add(user);
console.log(delRes);
console.log(addRes.id);
const responseData={};
responseData["id"]=addRes.id;
const userDetailsSnap=await User.doc(addRes.id).get();
const userDetails=userDetailsSnap.data();
userDetails.id=addRes.id;
const userList=await fetchData();
res.send({user:userDetails,items:userList});
// console.log(docSnap);
// console.log(docSnap.data());
})
app.get("/create",(req,res)=>{
res.send({msg:"abc"});
})
app.get("/fetchUsers",async(req,res)=>{
const list=await fetchData();
res.json({users:list});
})
app.listen(4000,()=>{
console.log("Server is running on port 4000.");
})