Skip to content

Commit

Permalink
modified class components from to functions, and adapted the logic an…
Browse files Browse the repository at this point in the history
…d handles acordingly b00tc4mp#173
  • Loading branch information
angelzrc committed Nov 7, 2024
1 parent ee4cb88 commit da14eaf
Show file tree
Hide file tree
Showing 47 changed files with 1,319 additions and 1,858 deletions.
84 changes: 46 additions & 38 deletions staff/angelzrc/unsocial/api/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import express, { json } from 'express'
import logic from './logic/index.js'
import cors from 'cors'

const server = express()

const jsonBodyParser = express.json()
server.use(cors())

const jsonBodyParser = json()

server.get('/', (_, res) => res.send('Hello, API!'))

server.post('/authenticate', jsonBodyParser, (req, res) => {
const { username, password } = req.body

try {
const userId = logic.authenticateUser(username, password)

res.json(userId)

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

Expand All @@ -22,9 +25,9 @@ server.post('/authenticate', jsonBodyParser, (req, res) => {
})

server.post('/register', jsonBodyParser, (req, res) => {
const { name, email, username, password, 'password-repeat': passwordRepeat } = req.body

try {
const { name, email, username, password, 'password-repeat': passwordRepeat } = req.body

logic.registerUser(name, email, username, password, passwordRepeat)

res.status(201).send()
Expand All @@ -35,132 +38,137 @@ server.post('/register', jsonBodyParser, (req, res) => {
}
})

server.get('/users/:userId/name', (req, res) => {
const { userId } = req.params
server.get('/users/:targetUserId/name', (req, res) => {
const userId = req.headers.authorization.slice(6)

const { targetUserId } = req.params

try {
const name = logic.getUserName(userId)
const name = logic.getUserName(userId, targetUserId)

res.json(name)

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

console.error(error)
}
})

server.get('/posts', (req, res) => {
server.post('/posts', jsonBodyParser, (req, res) => {
const userId = req.headers.authorization.slice(6)

const { image, text } = req.body

try {
const posts = logic.getPosts(userId)
logic.createPost(userId, image, text)

res.json(posts)
res.status(201).send()
} catch (error) {
res.status(400).json({ error: error.constructor.name, message: error.message })

console.error(error)
}
})

server.get('/posts/:postId/comments', (req, res) => {
const { postId } = req.params
server.get('/posts', (req, res) => {
const userId = req.headers.authorization.slice(6)

try {
const comments = logic.getComments(postId)
const posts = logic.getPosts(userId)

res.json(comments)
res.json(posts)
} catch (error) {
res.status(400).json({ error: error.constructor.name, message: error.message })

console.error(error)
}
})

server.post('/posts', jsonBodyParser, (req, res) => {
server.delete('/posts/:postId', (req, res) => {
const userId = req.headers.authorization.slice(6)

const { image, text } = req.body
const { postId } = req.params

try {
logic.createPost(userId, image, text)
logic.deletePost(userId, postId)

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

console.error(error)
}
})

server.patch('/posts/:postId', (req, res) => {
const { postId } = req.params
server.patch('/posts/:postId/likes', (req, res) => {
const userId = req.headers.authorization.slice(6)

const { postId } = req.params

try {
logic.toggleLikePost(postId, userId)
logic.toggleLikePost(userId, postId)

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

console.error(error)
}
})

server.post('/posts/:postId/comments', jsonBodyParser, (req, res) => {
const userId = req.headers.authorization.slice(6)

// const { postId } = req.params

server.post('/posts/:postId/comments', jsonBodyParser, (req, res) => {
const { postId } = req.params
const userId = req.headers.authorization.slice(6) // 'Basic asdfasdfas'
const { text } = req.body
// const { text } = req.body

const { params: { postId }, body: { text } } = req

try {
logic.addComment(userId, postId, text)

res.status(201).send()
} catch (error) {

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

console.error(error)
}


})

server.delete('posts/:postId/comments/:commentId', (req, res) => {
const { postId, commentId } = req.params
server.delete('/posts/:postId/comments/:commentId', (req, res) => {
const userId = req.headers.authorization.slice(6)

const { postId, commentId } = req.params

try {
logic.removeComment(postId, userId, commentId)
logic.removeComment(userId, postId, commentId)

res.status(200).send()
res.status(204).send()
} catch (error) {

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

console.error(error)
}
})

server.delete('/posts/:postId', (req, res) => {
server.get('/posts/:postId/comments', (req, res) => {
const userId = req.headers.authorization.slice(6)

const { postId } = req.params

try {
logic.deletePost(postId, userId)
const comments = logic.getComments(userId, postId)

res.status(200).send()
res.json(comments)
} catch (error) {
res.status(400).json({ error: error.constructor.name, message: error.message })

console.error(error)
}
})


server.listen(8080, () => console.log('api is up'))

// TODO use cookies for session management (RTFM cookies + express)
31 changes: 12 additions & 19 deletions staff/angelzrc/unsocial/api/logic/addComment.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { validate } from './helpers/index.js'
import { validate } from 'com'
import { storage, uuid } from '../data/index.js'

export default (userId, postId, text) => {
Expand All @@ -9,28 +9,21 @@ export default (userId, postId, text) => {
const { users, posts } = storage

const found = users.some(({ id }) => id === userId)
const postFound = posts.some(({ id }) => id === postId)

if (!found) throw new Error('user not found')

if (!found) {
throw new Error('user nor found')
} else if (!postFound) {
throw new Error('post not found')
} else {
const post = posts.find(({ id }) => id === postId)

const comment = {
id: uuid(),
author: userId,
text,
date: new Date
}
post.comments.push(comment)

storage.posts = posts
}
const post = posts.find(({ id }) => id === postId)

if (!post) throw new Error('post not found')

const { comments } = post

comments.push({
id: uuid(),
author: userId,
text,
date: new Date().toISOString()
})

storage.posts = posts
}
3 changes: 2 additions & 1 deletion staff/angelzrc/unsocial/api/logic/authenticateUser.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { storage } from '../data/index.js'
import validate from './helpers/validate.js'
import { validate } from 'com'

export default (username, password) => {
validate.username(username)
validate.password(password)

const { users } = storage

const user = users.find(user => user.username === username && user.password === password)

if (user === undefined)
Expand Down
2 changes: 1 addition & 1 deletion staff/angelzrc/unsocial/api/logic/createPost.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { validate } from './helpers/index.js'
import { validate } from 'com'

import { storage, uuid } from '../data/index.js'

Expand Down
16 changes: 10 additions & 6 deletions staff/angelzrc/unsocial/api/logic/deletePost.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { validate } from './helpers/index.js'
import { validate } from 'com'
import { storage } from '../data/index.js'


export default (postId, userId) => {
validate.id(postId, 'postId')
export default (userId, postId) => {
validate.id(userId, 'userId')
validate.id(postId, 'postId')

const { users, posts } = storage

const found = users.some(({ id }) => id === userId)

if (!found) throw new Error('user not found')

const { posts } = storage
const index = posts.findIndex(({ id }) => id === postId)

if (index < 0) throw new Error('post not fouind')
if (index < 0) throw new Error('post not found')

const post = posts[index]

Expand Down
13 changes: 9 additions & 4 deletions staff/angelzrc/unsocial/api/logic/getComments.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { validate } from './helpers/index.js'
import { storage } from '../data/index.js'
import { validate } from 'com'

export default postId => {
export default (userId, postId) => {
validate.id(userId, 'userId')
validate.id(postId, 'postId')

const { posts, users } = storage
const { users, posts } = storage

const found = users.some(({ id }) => id === userId)

if (!found) throw new Error('user not found')

const post = posts.find(({ id }) => id === postId)

Expand All @@ -18,7 +23,7 @@ export default postId => {
const { username } = users.find(({ id }) => id === authorId)

comment.author = { id: authorId, username }
});
})

return comments
}
13 changes: 10 additions & 3 deletions staff/angelzrc/unsocial/api/logic/getPosts.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { storage } from '../data/index.js'
import { validate } from 'com'

export default userId => {
const { posts, users } = storage
validate.id(userId, 'userId')

const { users, posts } = storage

const found = users.some(({ id }) => id === userId)

if (!found) throw new Error('user not found')

posts.forEach(post => {
const { author: authorId } = post
Expand All @@ -10,10 +17,10 @@ export default userId => {

post.author = { id: authorId, username }

post.liked = post.likedBy.includes(userId)
post.liked = post.likes.includes(userId)

post.comments = post.comments.length
});
})

return posts.toReversed()
}
16 changes: 11 additions & 5 deletions staff/angelzrc/unsocial/api/logic/getUserName.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { storage } from '../data/index.js'
import validate from './helpers/validate.js'
import { validate } from 'com'

export default userId => {
export default (userId, targetUserId) => {
validate.id(userId, 'userId')
validate.id(targetUserId, 'targetUserId')

const { users } = storage
const user = users.find(user => user.id === userId)

if (!user) throw new Error('user nor found')
const found = users.some(({ id }) => id === userId)

return user.name
if (!found) throw new Error('user not found')

const user = users.find(({ id }) => id === targetUserId)

if (!user) throw new Error('target user not found')

return user.name
}
Loading

0 comments on commit da14eaf

Please sign in to comment.