Skip to content

Commit bbf504b

Browse files
committed
added the visits function
1 parent 89c2603 commit bbf504b

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

client/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ async function weather(city) {
133133
headers: {'Content-Type': 'application/json'},
134134
method: 'GET'
135135
})
136-
return response.json()
136+
return response
137137
}
138138

139139
// Visits
@@ -171,8 +171,13 @@ nowFetch().then(response => {
171171
console.log(shaRESPONSE)
172172
findCount(HASH).then(countRes => {
173173
console.log(countRes)
174+
}).catch(e => {
175+
console.log(e)
174176
})
177+
}).catch(e => {
178+
console.log(e)
175179
})
180+
176181
console.log("HASH:" + HASH)
177182
const hashPhrase = `Your identity HASH: ${HASH}`
178183
const ipPhrase = `Your IP address is ${data.ip}.`;

index.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,28 +77,44 @@ app.get('/generate/:data', async (req, res) => { // Because URL PARAMS ARE COOLE
7777
})
7878
})
7979

80-
app.get('/add-sha/:sha', async (req, res) => {
81-
const encoded = Base64.encode(1);
82-
await Axios.put("https://api.github.com/repos/ujjwal-kr-data/ip-data/contents/" + req.params.sha, {
80+
app.get('/add-sha/:sha', (req, res) => {
81+
const encoded = Base64.encode(1)
82+
Axios.put("https://api.github.com/repos/ujjwal-kr-data/ip-data/contents/" + req.params.sha, {
8383
message: "Added a document",
8484
content: encoded
8585
}, {
8686
headers: { 'Authorization': "token " + KEY }
87-
}).then(res => {
88-
return res.json({ message: "created" })
87+
}).then(result => {
88+
res.json({ message: "created" })
8989
}).catch(e => {
90-
return res.json({ message: "exists" })
90+
Axios.get("https://api.github.com/repos/ujjwal-kr-data/ip-data/contents/" + req.params.sha, {
91+
headers: { 'Authorization': "token " + KEY }
92+
}).then(result => {
93+
const contentSHA = result.data.sha;
94+
let visits = Base64.decode(result.data.content)
95+
let newVisit = Base64.encode(parseInt(visits) + 1)
96+
Axios.put("https://api.github.com/repos/ujjwal-kr-data/ip-data/contents/"+ req.params.sha, {
97+
message: "UPDATED COUNT",
98+
sha: contentSHA,
99+
content: newVisit
100+
}, {headers: { 'Authorization': "token " + KEY } }).then(done => {
101+
console.log(done.data)
102+
res.json({ visits: Base64.decode(newVisit) })
103+
})
104+
.catch(e => console.log(e))
105+
})
106+
.catch(e => {console.log(e)})
91107
})
92108
})
93109

94110
app.get("/get-count/:sha", async (req, res) => {
95-
const sha = req.params.sha;
111+
const sha = req.params.sha
96112
await Axios.get("https://api.github.com/repos/ujjwal-kr-data/ip-data/contents/"+sha, {
97113
headers: {'Authorization': "token "+KEY}
98114
}).then(res => {
99-
console.log(res)
115+
console.log(Base64.decode(res.data.content))
100116
}).catch(e => {
101-
res.json({message: "ERRRR"})
117+
res.status(400).json({message: "ERRRR"})
102118
})
103119
})
104120

0 commit comments

Comments
 (0)