Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
fforres committed Jul 15, 2020
1 parent a350d0d commit abf1977
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"dependencies": {
"@babel/plugin-proposal-decorators": "^7.10.4",
"@f/elapsed-time": "^1.0.0",
"@types/pg": "^7.14.4",
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
Expand Down
26 changes: 24 additions & 2 deletions pages/api/favoritos/[id].ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-console */
import elapsed from '@f/elapsed-time'
import { dbConnection } from './../../../src/databaseConnection'
import { config } from './../../../src/config'
import { NextApiRequest, NextApiResponse } from 'next'
Expand All @@ -7,26 +9,33 @@ import { decode } from '../../../src/jwt'
import { UserEntity } from '../../../src/models'

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const time = elapsed()
await corsMiddleware(req, res)
res.statusCode = 200
console.log('Cors Middleware', time())
try {
const anId = req?.query?.id
if (!anId) {
apiError(res, 'no id present', 500)
return
}
const pokemonId = Number(anId)
console.log('pokemonid', time())
if (Number.isNaN(pokemonId)) {
apiError(res, 'id has to be a number', 500)
return
}
const token = req.headers?.[config.authentication.header]
const { id } = decode(token) as { id: string }
console.log('token', time())
const connection = await dbConnection()
const repository = connection.getRepository(UserEntity)
console.log('user repository', time())
if (req.method === 'POST') {
try {
console.log('POST - start', time())
const user = await repository.findOne({ id })
console.log('POST - find user', time())
if (!user) {
apiError(res, 'could not find user', 500)
return
Expand All @@ -35,45 +44,58 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
favorites.push(pokemonId)
const newFavorites = Array.from(new Set(favorites.map(Number)))
user.favorites = JSON.stringify(newFavorites)
repository.save(user)
console.log('POST - update favorites', time())
await repository.save(user)
console.log('POST - save user', time())
res.json({
success: true,
newFavorites,
})
console.log('POST - return response', time())
return
} catch (e) {
apiError(res, 'could not save favourites', 401)
return
}
} else if (req.method === 'GET') {
console.log('GET - start', time())
const user = await repository.findOne({ id })
console.log('GET - find user', time())
if (!user) {
apiError(res, 'could not find user', 500)
return
}
const favorites = (JSON.parse(user.favorites) || []) as number[]
const isFavorite = favorites.some((fav) => fav === pokemonId)
console.log('GET - modify data', time())
res.json({
success: true,
isFavorite,
})
console.log('GET - return response', time())
return
} else if (req.method === 'DELETE') {
console.log('DELETE - delete', time())
const user = await repository.findOne({ id })
console.log('DELETE - find user', time())
if (!user) {
apiError(res, 'could not find user', 500)
return
}
try {
console.log('DELETE - find user', time())
const favorites = ((JSON.parse(user.favorites) ||
[]) as number[]).filter((fav) => fav !== pokemonId)
const newFavorites = Array.from(new Set(favorites.map(Number)))
user.favorites = JSON.stringify(newFavorites)
repository.save(user)
console.log('DELETE - update favorites', time())
await repository.save(user)
console.log('DELETE - save user', time())
res.json({
success: true,
newFavorites,
})
console.log('DELETE - find user', time())
return
} catch (e) {
apiError(res, 'could not delete favourites', 401)
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,18 @@
exec-sh "^0.3.2"
minimist "^1.2.0"

"@f/elapsed-time@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@f/elapsed-time/-/elapsed-time-1.0.0.tgz#6a079a6104a87278bf5b4080444ef02d1b595449"
integrity sha1-ageaYQSocni/W0CARE7wLRtZVEk=
dependencies:
"@f/timestamp" "^1.0.0"

"@f/timestamp@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@f/timestamp/-/timestamp-1.0.0.tgz#32a9166e2516e5ccb9b0fcfdc89223819710e88c"
integrity sha1-MqkWbiUW5cy5sPz9yJIjgZcQ6Iw=

"@istanbuljs/load-nyc-config@^1.0.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz#fd3db1d59ecf7cf121e80650bb86712f9b55eced"
Expand Down

1 comment on commit abf1977

@vercel
Copy link

@vercel vercel bot commented on abf1977 Jul 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.