Skip to content

Commit

Permalink
add all logics and tests in api b00tc4mp#168
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier Romera Claros authored and Javier Romera Claros committed Nov 1, 2024
1 parent 5a43564 commit 1320ad4
Show file tree
Hide file tree
Showing 38 changed files with 426 additions and 27 deletions.
29 changes: 27 additions & 2 deletions staff/javier-romera/unsocial/api/data/posts.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
[
{
"id": "m2w6jhmsr0b",
"id": "m2yjwe5wdfs",
"image": "http://invented-image.com",
"text": "hello world",
"author": "m2w6ch7dkh",
"date": "2024-10-30T17:57:59.428Z",
"date": "2024-11-01T09:47:28.820Z",
"likedBy": [],
"comments": [
{
"id": "m2ynlgm6cz",
"author": "m2w6ch7dkh",
"text": "holaaaa",
"date": "2024-11-01T11:30:57.246Z"
}
]
},
{
"id": "m2ykyrjdnuq",
"image": "https://media.giphy.com/media/QLiqUx7aHg10bl5FVj/giphy.gif?cid=790b7611k9bnssjixy10hu88u0buneurbrjffxxhlxderris&ep=v1_gifs_search&rid=giphy.gif&ct=g",
"text": "hello world2",
"author": "m2x99dua3tc",
"date": "2024-11-01T10:17:19.081Z",
"likedBy": [],
"comments": []
},
{
"id": "m2yl1cp5wie",
"image": "https://media.giphy.com/media/QLiqUx7aHg10bl5FVj/giphy.gif?cid=790b7611k9bnssjixy10hu88u0buneurbrjffxxhlxderris&ep=v1_gifs_search&rid=giphy.gif&ct=g",
"text": "hello world3",
"author": "m2x99dua3tc",
"date": "2024-11-01T10:19:19.817Z",
"likedBy": [],
"comments": []
}
Expand Down
7 changes: 7 additions & 0 deletions staff/javier-romera/unsocial/api/data/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,12 @@
"email": "[email protected]",
"username": "pepitogrillo",
"password": "123123123"
},
{
"id": "m2x99dua3tc",
"name": "Pin Otto",
"email": "[email protected]",
"username": "pinotto",
"password": "123123123"
}
]
104 changes: 97 additions & 7 deletions staff/javier-romera/unsocial/api/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import express, { json } from 'express'
import logic from './logic/index.js'

const server = express()
const server = express() // creamos un server express

const jsonBodyParser = express.json()
const jsonBodyParser = express.json() // parsea todo el contenido a json

server.use(express.static('public'))
server.get('/', (_, res) => res.send('Hello API!')) // endpoint, una ruta y un método

server.post('/authenticate', jsonBodyParser, (req, res) => {
const { username, password } = req.body
Expand Down Expand Up @@ -35,7 +35,7 @@ server.post('/register', jsonBodyParser, (req, res) => {
}
})

server.get('/users/:userId/name', (req, res) => {
server.get('/users/:userId/name', (req, res) => { // : un parámetro que puede cambiar ==> dinámico
const { userId } = req.params

try {
Expand All @@ -49,8 +49,22 @@ server.get('/users/:userId/name', (req, res) => {
}
})

server.get('/users/:userId/username', (req, res) => { // : un parámetro que puede cambiar ==> dinámico
const { userId } = req.params

try {
const username = logic.getUserUsername(userId)

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

console.error(error)
}
})

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

const { image, text } = req.body

Expand All @@ -65,11 +79,87 @@ server.post('/posts', jsonBodyParser, (req, res) => {
}
})

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

const { postId } = req.params

try {
logic.deletePost(postId, userId)

res.status(200).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
const userId = req.headers.authorization.slice(6)

try {
logic.toggleLikePost(postId, userId)

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

console.error(error)
}
})

server.get('/posts/:userId', (req, res) => {
const { userId } = req.params

try {
console.log(logic.getPosts(userId))
const posts = logic.getPosts(userId)

res.json(posts)
} 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
const { text } = req.body

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

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

try {
const comments = logic.getComments(postId)

res.json(comments)
} 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
const userId = req.headers.authorization.slice(6)

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

res.status(200).send()
} catch (error) {
Expand Down
24 changes: 24 additions & 0 deletions staff/javier-romera/unsocial/api/logic/addComment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import validate from './helpers/validate.js'
import { storage, uuid } from '../data/index.js'

export default (postId, text, userId) => {
validate.id(postId, 'postId')
validate.text(text)
if (text.length < 5) throw new Error('Comment is too short')
if (text.length > 100) throw new Error('Comment is too long')

const { posts } = storage

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

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

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

storage.posts = posts
}
7 changes: 7 additions & 0 deletions staff/javier-romera/unsocial/api/logic/addComment.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import addComment from './addComment.js'

try {
addComment('m2yjwe5wdfs', 'holaaa', 'm2w6ch7dkh')
} catch (error) {
console.error(error)
}
2 changes: 1 addition & 1 deletion staff/javier-romera/unsocial/api/logic/createPost.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default (userId, image, text) => {
text: text,
author: userId,
date: new Date,
likes: [],
likedBy: [],
comments: []
}

Expand Down
24 changes: 24 additions & 0 deletions staff/javier-romera/unsocial/api/logic/deletePost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { validate } from './helpers/index.js'

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

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

const { posts } = storage

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

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

const post = posts[index]

const { author } = post

if (author !== userId) throw new Error('user is not author of post')

posts.splice(index, 1)

storage.posts = posts
}
7 changes: 7 additions & 0 deletions staff/javier-romera/unsocial/api/logic/deletePost.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import deletePost from './deletePost.js';

try {
deletePost('m2yjwe5wdfs', 'm2w6ch7dkh')
} catch (error) {
console.error(error)
}
24 changes: 24 additions & 0 deletions staff/javier-romera/unsocial/api/logic/getComments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { validate } from './helpers/index.js'
import { storage } from '../data/index.js'

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

const { posts, users } = storage

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

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

const { comments } = post

comments.forEach(comment => {
const { author: authorId } = comment

const { username } = users.find(({ id }) => id === authorId)

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

return comments
}
7 changes: 7 additions & 0 deletions staff/javier-romera/unsocial/api/logic/getComments.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import getComments from './getComments.js'

try {
console.log(getComments())
} catch (error) {
console.error(error)
}
11 changes: 11 additions & 0 deletions staff/javier-romera/unsocial/api/logic/getUserUsername.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { storage } from '../data/index.js'

export default userId => {
const { users } = storage

const user = users.find(user => user.id === userId)

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

return user.username
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import getUserUsername from './getUserUsername.js';

try {
console.log(getUserUsername('m2w6ch7dkh'))
} catch (error) {
console.error(error)
}
21 changes: 18 additions & 3 deletions staff/javier-romera/unsocial/api/logic/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import authenticateUser from './authenticateUser.js';
import authenticateUser from './authenticateUser.js'
import registerUser from './registerUser.js'
import getUserName from './getUserName.js'
import getUserUsername from './getUserUsername.js'

import createPost from './createPost.js'
import getPosts from './getPosts.js';
import getPosts from './getPosts.js'
import deletePost from './deletePost.js'
import toggleLikePost from './toggleLikePost.js'

import addComment from './addComment.js'
import removeComment from './removeComment.js'
import getComments from './getComments.js'

const logic = {
authenticateUser,
registerUser,
getUserName,
getUserUsername,

createPost,
getPosts
deletePost,
getPosts,
toggleLikePost,

addComment,
removeComment,
getComments
}

export default logic
25 changes: 25 additions & 0 deletions staff/javier-romera/unsocial/api/logic/removeComment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { validate } from './helpers/index.js'
import { storage } from '../data/index.js'

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

const { posts } = storage

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

const { comments } = post

const index = comments.findIndex(({ id }) => id === commentId)

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

const { author } = comments[index]

if (author !== userId) throw new Error('user is not author of comment')

comments.splice(index, 1)

storage.posts = posts
}
7 changes: 7 additions & 0 deletions staff/javier-romera/unsocial/api/logic/removeComment.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import removeComment from './removeComment.js'

try {
removeComment('m2yjwe5wdfs', 'm2ymh29xxfb', 'm2w6ch7dkh')
} catch (error) {
console.error(error)
}
Loading

0 comments on commit 1320ad4

Please sign in to comment.