Skip to content

Commit a1a352f

Browse files
committed
add more one endpoint
1 parent 3b00000 commit a1a352f

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

backend/handlers/apikey.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function CreateApiKeyHandler (db) {
2+
async function checkIfApiKeyExists (ctx) {
3+
const { apikey } = ctx.query
4+
if (!apikey) {
5+
ctx.status = 400
6+
ctx.body = { error: 'apikey query param not provided' }
7+
return
8+
}
9+
const users = await db.readAll('users')
10+
11+
const apiKeyExists = users.map((user) => {
12+
return user.apiKey === apikey
13+
})
14+
15+
if (apiKeyExists.includes(true)) {
16+
ctx.status = 200
17+
return
18+
}
19+
20+
ctx.status = 404
21+
}
22+
23+
return {
24+
checkIfApiKeyExists
25+
}
26+
}
27+
28+
module.exports = CreateApiKeyHandler

backend/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const database = require('./database')
88
const CreateUserHandler = require('./handlers/users')
99
const CreateAuthHandler = require('./handlers/auth')
1010
const CreateFeedbackHandler = require('./handlers/feedbacks')
11+
const CreateApiKeyHandler = require('./handlers/apikey')
1112

1213
const app = new Koa()
1314
const router = new Router()
@@ -23,11 +24,13 @@ app.use(cors())
2324
const feedbacksHandler = CreateFeedbackHandler(database)
2425
const usersHandler = CreateUserHandler(database)
2526
const authHandler = CreateAuthHandler(database)
27+
const apiKeyHandler = CreateApiKeyHandler(database)
2628

2729
router.get('/', (ctx) => {
2830
ctx.status = 200
2931
ctx.body = { message: new Date() }
3032
})
33+
router.head('/apikey/exists', apiKeyHandler.checkIfApiKeyExists)
3134
router.post('/auth/register', usersHandler.create)
3235
router.post('/auth/login', authHandler.login)
3336
router.get('/users/me', authMiddleware, usersHandler.getLoggerUser)

backend/vuejs_brasil_feedbacker.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,37 @@
4242
}
4343
]
4444
},
45+
{
46+
"name": "Apikey",
47+
"item": [
48+
{
49+
"name": "Checar se a apikey existe",
50+
"request": {
51+
"method": "HEAD",
52+
"header": [],
53+
"url": {
54+
"raw": "http://localhost:3000/apikey/exists?apikey=fcd5015c-10d3-4e9c-b395-ec7ed8850165",
55+
"protocol": "http",
56+
"host": [
57+
"localhost"
58+
],
59+
"port": "3000",
60+
"path": [
61+
"apikey",
62+
"exists"
63+
],
64+
"query": [
65+
{
66+
"key": "apikey",
67+
"value": "fcd5015c-10d3-4e9c-b395-ec7ed8850165"
68+
}
69+
]
70+
}
71+
},
72+
"response": []
73+
}
74+
]
75+
},
4576
{
4677
"name": "Auth",
4778
"item": [

0 commit comments

Comments
 (0)