forked from b00tc4mp/isdi-bootcamp-202405
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4280b5b
commit 87e1ef9
Showing
53 changed files
with
2,014 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
function deletePost(condition) { | ||
const posts = | ||
localStorage.posts !== undefined ? JSON.parse(localStorage.posts) : []; | ||
|
||
const postIndex = posts.findIndex(condition); | ||
|
||
if (postIndex > -1) { | ||
posts.splice(postIndex, 1); | ||
|
||
localStorage.posts = JSON.stringify(posts); | ||
} | ||
} | ||
|
||
export default deletePost; |
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,10 @@ | ||
function findPost(condition) { | ||
const posts = | ||
localStorage.posts !== undefined ? JSON.parse(localStorage.posts) : []; | ||
|
||
const post = posts.find(condition); | ||
|
||
return post || null; | ||
} | ||
|
||
export default findPost; |
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,10 @@ | ||
function findPosts(condition) { | ||
const posts = | ||
localStorage.posts !== undefined ? JSON.parse(localStorage.posts) : []; | ||
|
||
const foundPosts = posts.filter(condition); | ||
|
||
return foundPosts; | ||
} | ||
|
||
export default findPosts; |
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,10 @@ | ||
function findUser(condition) { | ||
const users = | ||
localStorage.users !== undefined ? JSON.parse(localStorage.users) : []; | ||
|
||
const user = users.find(condition); | ||
|
||
return user || null; | ||
} | ||
|
||
export default findUser; |
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,21 @@ | ||
import deletePost from "./deletePost.js"; | ||
import findPost from "./findPost.js"; | ||
import findPosts from "./findPosts.js"; | ||
import findUser from "./findUser.js"; | ||
import insertPost from "./insertPost.js"; | ||
import insertUser from "./insertUser.js"; | ||
import updatePost from "./updatePost.js"; | ||
import updateUser from "./updateUser.js"; | ||
|
||
const data = { | ||
deletePost, | ||
findPost, | ||
findPosts, | ||
findUser, | ||
insertPost, | ||
insertUser, | ||
updatePost, | ||
updateUser, | ||
}; | ||
|
||
export default data; |
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,10 @@ | ||
function insertPost(post) { | ||
const posts = | ||
localStorage.posts !== undefined ? JSON.parse(localStorage.posts) : []; | ||
|
||
posts.push(post); | ||
|
||
localStorage.posts = JSON.stringify(posts); | ||
} | ||
|
||
export default insertPost; |
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,10 @@ | ||
function insertUser(user) { | ||
const users = | ||
localStorage.users !== undefined ? JSON.parse(localStorage.users) : []; | ||
|
||
users.push(user); | ||
|
||
localStorage.users = JSON.stringify(users); | ||
} | ||
|
||
export default insertUser; |
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,14 @@ | ||
function updatePost(condition, post) { | ||
const posts = | ||
localStorage.posts !== undefined ? JSON.parse(localStorage.posts) : []; | ||
|
||
const postIndex = posts.findIndex(condition); | ||
|
||
if (postIndex > -1) { | ||
posts.splice(postIndex, 1, post); | ||
|
||
localStorage.posts = JSON.stringify(posts); | ||
} | ||
} | ||
|
||
export default updatePost; |
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,14 @@ | ||
function updateUser(condition, user) { | ||
const users = | ||
localStorage.users !== undefined ? JSON.parse(localStorage.users) : []; | ||
|
||
const index = users.findIndex(condition); | ||
|
||
if (index > -1) { | ||
users.splice(index, 1, user); | ||
|
||
localStorage.users = JSON.stringify(users); | ||
} | ||
} | ||
|
||
export default updateUser; |
20 changes: 20 additions & 0 deletions
20
staff/nerina-castillo/ponies/zzz/app1/logic/createPost.mjs
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,20 @@ | ||
import data from "../data/index.js"; | ||
|
||
import generateId from "../util/generateId.js"; | ||
|
||
const createPost = (image, caption) => { | ||
if (!image.startsWith("http")) throw new Error("invalid image"); | ||
|
||
const post = { | ||
id: generateId(), | ||
image: image, | ||
caption: caption, | ||
author: sessionStorage.username, | ||
date: new Date().toISOString(), | ||
likes: [], | ||
}; | ||
|
||
data.insertPost(post); | ||
}; | ||
|
||
export default createPost; |
13 changes: 13 additions & 0 deletions
13
staff/nerina-castillo/ponies/zzz/app1/logic/deletePost.mjs
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,13 @@ | ||
import data from "../data/index.js"; | ||
|
||
const deletePost = (postId) => { | ||
if (postId.trim().length === 0) throw new Error("invalid postId"); | ||
|
||
const post = data.findPost((post) => post.id === postId); | ||
|
||
if (post === null) throw new Error("post not found"); | ||
|
||
data.deletePost((post) => post.id === postId); | ||
}; | ||
|
||
export default deletePost; |
21 changes: 21 additions & 0 deletions
21
staff/nerina-castillo/ponies/zzz/app1/logic/getAllFavPosts.mjs
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,21 @@ | ||
import data from "../data/index.js"; | ||
|
||
const getAllFavPosts = () => { | ||
const user = data.findUser( | ||
(user) => user.username === sessionStorage.username | ||
); | ||
|
||
if (user === null) throw new Error("user not found"); | ||
|
||
const posts = data.findPosts((post) => user.favs.includes(post.id)); | ||
|
||
posts.forEach((post) => { | ||
post.fav = user.favs.includes(post.id); | ||
post.like = post.likes.includes(sessionStorage.username); | ||
post.following = user.following.includes(post.author); | ||
}); | ||
|
||
return posts.reverse(); | ||
}; | ||
|
||
export default getAllFavPosts; |
11 changes: 11 additions & 0 deletions
11
staff/nerina-castillo/ponies/zzz/app1/logic/getAllFavPosts.test.mjs
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 getAllFavPosts from "./getAllFavPosts.js"; | ||
|
||
console.info("TEST getAllFavPosts"); | ||
|
||
console.info("CASE get all fav posts from mamoracho"); | ||
|
||
sessionStorage.username = "caulifFlower"; | ||
|
||
const favs = getAllFavPosts(); | ||
|
||
console.log(favs); |
21 changes: 21 additions & 0 deletions
21
staff/nerina-castillo/ponies/zzz/app1/logic/getAllFollowingUsersPosts.mjs
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,21 @@ | ||
import data from "../data/index.js"; | ||
|
||
const getAllFollowingUserPosts = () => { | ||
const user = data.findUser( | ||
(user) => user.username === sessionStorage.username | ||
); | ||
|
||
if (user === null) throw new Error("user not found"); | ||
|
||
const posts = data.findPosts((post) => user.following.includes(post.author)); | ||
|
||
posts.forEach((post) => { | ||
post.fav = user.favs.includes(post.id); | ||
post.like = post.likes.includes(sessionStorage.username); | ||
post.following = user.following.includes(post.author); | ||
}); | ||
|
||
return posts.reverse(); | ||
}; | ||
|
||
export default getAllFollowingUserPosts; |
21 changes: 21 additions & 0 deletions
21
staff/nerina-castillo/ponies/zzz/app1/logic/getAllPosts.mjs
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,21 @@ | ||
import data from "../data/index.js"; | ||
|
||
const getAllPosts = () => { | ||
const user = data.findUser( | ||
(user) => user.username === sessionStorage.username | ||
); | ||
|
||
if (user === null) throw new Error("User not found"); | ||
|
||
const posts = data.findPosts(() => true); | ||
|
||
posts.forEach((post) => { | ||
post.fav = user.favs.includes(post.id); | ||
post.like = post.likes.includes(sessionStorage.username); | ||
post.following = user.following.includes(post.author); | ||
}); | ||
|
||
return posts.reverse(); | ||
}; | ||
|
||
export default getAllPosts; |
13 changes: 13 additions & 0 deletions
13
staff/nerina-castillo/ponies/zzz/app1/logic/getUserName.mjs
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,13 @@ | ||
import data from "../data/index.js"; | ||
|
||
const getUserName = () => { | ||
const user = data.findUser( | ||
(user) => user.username === sessionStorage.username | ||
); | ||
|
||
if (user === null) throw new Error("user not found"); | ||
|
||
return user.name; | ||
}; | ||
|
||
export default getUserName; |
3 changes: 3 additions & 0 deletions
3
staff/nerina-castillo/ponies/zzz/app1/logic/getUserUsername.mjs
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,3 @@ | ||
const getUserUsername = () => sessionStorage.username; | ||
|
||
export default getUserUsername; |
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,33 @@ | ||
import getAllPosts from "./getAllPosts.js"; | ||
import getUserName from "./getUserName.js"; | ||
import getUserUsername from "./getUserUsername.js"; | ||
import loginUser from "./loginUser.js"; | ||
import logoutUser from "./logoutUser.js"; | ||
import registerUser from "./registerUser.js"; | ||
import toggleLikePost from "./toggleLikePost.js"; | ||
import updatePostCaption from "./updatePostCaption.js"; | ||
import createPost from "./createPost.js"; | ||
import deletePost from "./deletePost.js"; | ||
import toggleFavPost from "./toggleFavPost.js"; | ||
import getAllFavPosts from "./getAllFavPosts.js"; | ||
import toggleFollowUser from "./toggleFollowUser.js"; | ||
import getAllFollowingUserPosts from "./getAllFollowingUserPosts.js"; | ||
|
||
const logic = { | ||
getAllPosts, | ||
getUserName, | ||
getUserUsername, | ||
loginUser, | ||
logoutUser, | ||
registerUser, | ||
toggleLikePost, | ||
updatePostCaption, | ||
createPost, | ||
deletePost, | ||
toggleFavPost, | ||
getAllFavPosts, | ||
toggleFollowUser, | ||
getAllFollowingUserPosts, | ||
}; | ||
|
||
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,17 @@ | ||
import data from "../data/index.js"; | ||
|
||
const loginUser = (username, password) => { | ||
if (username.trim().length < 4) throw new Error("invalid username"); | ||
|
||
if (password.trim().length < 8) throw new Error("invalid password"); | ||
|
||
const user = data.findUser((user) => user.username === username); | ||
|
||
if (user === null) throw new Error("username does not exist"); | ||
|
||
if (user.password !== password) throw new Error("wrong password"); | ||
|
||
sessionStorage.username = username; | ||
}; | ||
|
||
export default loginUser; |
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,3 @@ | ||
const logoutUser = () => delete sessionStorage.username; | ||
|
||
export default logoutUser; |
48 changes: 48 additions & 0 deletions
48
staff/nerina-castillo/ponies/zzz/app1/logic/registerUser.mjs
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,48 @@ | ||
import data from "../data/index.js"; | ||
|
||
const email_regex = /^[a-z0-9._]+@[a-z0-9.-]{3,63}\.[a-z]{2,10}$/; | ||
const name_regex = /^(?!.*\s{2})[a-zA-Z ]{3,16}$/; | ||
const user_regex = /^(?!.*\s{2})[a-zA-Z0-9._-]{4,16}$/; | ||
|
||
const registerUser = ( | ||
name, | ||
surname, | ||
email, | ||
username, | ||
password, | ||
passwordRepeat | ||
) => { | ||
if (!name_regex.test(name.trim())) throw new Error("invalid name"); | ||
|
||
if (!name_regex.test(surname.trim())) throw new Error("ivalid surname"); | ||
|
||
if (!email_regex.test(email)) throw new Error("invalid email"); | ||
|
||
if (!user_regex.test(username)) throw new Error("invalid username"); | ||
|
||
if (password.trim().length < 8) throw new Error("invalid password"); | ||
|
||
if (password !== passwordRepeat) throw new Error("passwords do not match"); | ||
|
||
let user = data.findUser((user) => user.email === email); | ||
|
||
if (user !== null) throw new Error("email already exists"); | ||
|
||
user = data.findUser((user) => user.username === username); | ||
|
||
if (user !== null) throw new Error("username already exists"); | ||
|
||
user = { | ||
name: name, | ||
surname: surname, | ||
email: email, | ||
username: username, | ||
password: password, | ||
favs: [], | ||
following: [], | ||
}; | ||
|
||
data.insertUser(user); | ||
}; | ||
|
||
export default registerUser; |
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,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>TEST logic</title> | ||
</head> | ||
<body> | ||
<h1>TEST logic</h1> | ||
<script type="module" src="getAllFavPosts.test.js"></script> | ||
</body> | ||
</html> |
24 changes: 24 additions & 0 deletions
24
staff/nerina-castillo/ponies/zzz/app1/logic/toggleFavPost.mjs
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 data from "../data/index.js"; | ||
|
||
function toggleFavPost(postId) { | ||
if (postId.trim().length === 0) throw new Error("invalid postId"); | ||
|
||
const user = data.findUser( | ||
(user) => user.username === sessionStorage.username | ||
); | ||
|
||
if (user === null) throw new Error("user not found"); | ||
|
||
const post = data.findPost((post) => post.id === postId); | ||
|
||
if (post === null) throw new Error("post not found"); | ||
|
||
const index = user.favs.indexOf(postId); | ||
|
||
if (index < 0) user.favs.push(postId); | ||
else user.favs.splice(index, 1); | ||
|
||
data.updateUser((user) => user.username === sessionStorage.username, user); | ||
} | ||
|
||
export default toggleFavPost; |
Oops, something went wrong.