Skip to content

Commit

Permalink
create like and fix errors b00tc4mp#140
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianGonzalo committed Jul 1, 2024
1 parent 1408e1f commit 7114488
Show file tree
Hide file tree
Showing 25 changed files with 509 additions and 240 deletions.
74 changes: 0 additions & 74 deletions staff/adrian-martin/socialcode/api/data/posts.json

This file was deleted.

9 changes: 0 additions & 9 deletions staff/adrian-martin/socialcode/api/data/users.json

This file was deleted.

67 changes: 51 additions & 16 deletions staff/adrian-martin/socialcode/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ client.connect()
.then(connection => {
const db = connection.db('test')

const users =db.collection('users')
const users = db.collection('users')
const posts = db.collection('posts')

data.users = users
data.posts = posts

const { JsonWebTokenError, TokenExpiredError } = jwt

Expand Down Expand Up @@ -83,6 +85,10 @@ client.connect()

jwt.verify(token, JWT_SECRET, (error, payload) => {
if (error) {
if (error instanceof JsonWebTokenError || error instanceof TokenExpiredError)
res.status(500).json({ error: SystemError.name, message: error.message })
else

res.status(500).json({ error: error.constructor.name, message: error.message })

return
Expand All @@ -102,10 +108,7 @@ client.connect()
})

} catch (error) {
if (error instanceof JsonWebTokenError || error instanceof TokenExpiredError)
res.status(500).json({ error: SystemError.name, message: error.message })
else
res.status(500).json({ error: error.constructor.name, message: error.message })
res.status(500).json({ error: error.constructor.name, message: error.message })
}
})

Expand All @@ -117,6 +120,9 @@ client.connect()

jwt.verify(token, JWT_SECRET, (error, payload) => {
if (error) {
if (error instanceof JsonWebTokenError || error instanceof TokenExpiredError)
res.status(500).json({ error: SystemError.name, message: error.message })
else
res.status(500).json({ error: error.constructor.name, message: error.message })

return
Expand All @@ -134,9 +140,6 @@ client.connect()
})

} catch (error) {
if (error instanceof JsonWebTokenError || error instanceof TokenExpiredError)
res.status(500).json({ error: SystemError.name, message: error.message })
else
res.status(500).json({ error: error.constructor.name, message: error.message })
}
})
Expand All @@ -149,6 +152,9 @@ client.connect()

jwt.verify(token, JWT_SECRET, (error, payload) => {
if (error) {
if (error instanceof JsonWebTokenError || error instanceof TokenExpiredError)
res.status(500).json({ error: SystemError.name, message: error.message })
else
res.status(500).json({ error: error.constructor.name, message: error.message })

return
Expand All @@ -168,9 +174,6 @@ client.connect()
})
})
} catch (error) {
if (error instanceof JsonWebTokenError || error instanceof TokenExpiredError)
res.status(500).json({ error: SystemError.name, message: error.message })
else
res.status(500).json({ error: error.constructor.name, message: error.message })

}
Expand All @@ -183,6 +186,9 @@ client.connect()

jwt.verify(token, JWT_SECRET, (error, payload) => {
if (error) {
if (error instanceof JsonWebTokenError || error instanceof TokenExpiredError)
res.status(500).json({ error: SystemError.name, message: error.message })
else
res.status(500).json({ error: error.constructor.name, message: error.message })

return
Expand All @@ -202,14 +208,43 @@ client.connect()
})
})
} catch (error) {
if (error instanceof JsonWebTokenError || error instanceof TokenExpiredError)
res.status(500).json({ error: SystemError.name, message: error.message })
else
res.status(500).json({ error: error.constructor.name, message: error.message })
res.status(500).json({ error: error.constructor.name, message: error.message })
}
})

api.patch('/posts/:postId/likes', (req, res) => {
try {
const token = req.headers.authorization.slice(7)

jwt.verify(token, JWT_SECRET, (error, payload) => {
if (error) {
if (error instanceof JsonWebTokenError || error instanceof TokenExpiredError)
res.status(500).json({ error: SystemError.name, message: error.message })
else
res.status(500).json({ error: error.constructor.name, message: error.message })

return
}
const { sub: username } = payload

const { postId } = req.params

logic.toggleLikePost(username, postId, error => {
if (error) {
res.status(500).json({ error: error.constructor.name, message: error.message })

return
}

res.status(204).send()
})
})
} catch (error) {
res.status(500).json({ error: error.constructor.name, message: error.message })
}
})

api.listen(PORT, () => console.log('api is up'))
api.listen(PORT, () => console.log(`API running on PORT ${PORT}`))
})
.catch(error => console.error(error))

Expand Down
71 changes: 47 additions & 24 deletions staff/adrian-martin/socialcode/api/logic/createPost.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,60 @@ const createPost = (username, title, image, description, callback) => {
validate.text(description, 'description', 200)
validate.callback(callback)

data.findUser(user => user.username === username, (error, user) => {
if (error) {
callback(error)
data.users.findOne({ username })
.then(user => {
if (!user) {
callback(new MatchError('user not found'))

return
}
return
}

if (!user) {
callback(new MatchError('user not found'))
const post = {
author: username,
title,
image,
description,
date: new Date,
likes: []
}

return
}
data.posts.insertOne(post)
.then(() => callback(null))
.catch(error => callback(error))
})
.catch(error => console.error(error))

const post = {
author: username,
title,
image,
description,
date: new Date().toISOString()
}
// data.findUser(user => user.username === username, (error, user) => {
// if (error) {
// callback(error)

data.insertPost(post, error => {
if (error) {
callback(error)
// return
// }

return
}
// if (!user) {
// callback(new MatchError('user not found'))

callback(null)
})
})
// return
// }

// const post = {
// author: username,
// title,
// image,
// description,
// date: new Date().toISOString()
// }

// data.insertPost(post, error => {
// if (error) {
// callback(error)

// return
// }

// callback(null)
// })
// })

}

Expand Down
38 changes: 28 additions & 10 deletions staff/adrian-martin/socialcode/api/logic/createPost.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
import 'dotenv/config'
import logic from './index.js'
import { MongoClient } from 'mongodb'
import data from '../data/index.js'

try {
logic.createPost('AdrianGon', 'smile4', 'https://imgs.search.brave.com/rY4vd7ChrTffot87xezWVyJZcsjp10UPNHx2EQMRCfs/rs:fit:860:0:0/g:ce/aHR0cHM6Ly9pLnBp/bmltZy5jb20vb3Jp/Z2luYWxzLzFiL2Qx/L2I2LzFiZDFiNjE1/ZTZkYTcwZGQ3MWRj/ODRmZDJmNDdjODBk/LmpwZw', 'hi 2', error => {
if (error) {
console.error(error)
const { MONGODB_URL } = process.env
const client = new MongoClient(MONGODB_URL)

return
}
client.connect()
.then(connection => {
const db = connection.db('test')

const users = db.collection('users')
const posts = db.collection('posts')

data.users = users
data.posts = posts

console.log('posts created')
try {
logic.createPost('AdrianGon', 'smile4', 'https://imgs.search.brave.com/rY4vd7ChrTffot87xezWVyJZcsjp10UPNHx2EQMRCfs/rs:fit:860:0:0/g:ce/aHR0cHM6Ly9pLnBp/bmltZy5jb20vb3Jp/Z2luYWxzLzFiL2Qx/L2I2LzFiZDFiNjE1/ZTZkYTcwZGQ3MWRj/ODRmZDJmNDdjODBk/LmpwZw', 'hi 2', error => {
if (error) {
console.error(error)

return
}

console.log('posts created')
})
} catch (error) {
console.error(error)
}
})
} catch (error) {
console.error(error)
}
.catch(error => console.error(error))
Loading

0 comments on commit 7114488

Please sign in to comment.