forked from b00tc4mp/isdi-bootcamp-202409
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add all logics and tests in api b00tc4mp#168
- Loading branch information
Javier Romera Claros
authored and
Javier Romera Claros
committed
Nov 1, 2024
1 parent
5a43564
commit 1320ad4
Showing
38 changed files
with
426 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,12 @@ | |
"email": "[email protected]", | ||
"username": "pepitogrillo", | ||
"password": "123123123" | ||
}, | ||
{ | ||
"id": "m2x99dua3tc", | ||
"name": "Pin Otto", | ||
"email": "[email protected]", | ||
"username": "pinotto", | ||
"password": "123123123" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
7 changes: 7 additions & 0 deletions
7
staff/javier-romera/unsocial/api/logic/getUserUsername.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.