Skip to content

Commit

Permalink
implement zzz folder b00tc4mp#68
Browse files Browse the repository at this point in the history
  • Loading branch information
NerinaHctz committed Jul 12, 2024
1 parent 4280b5b commit 87e1ef9
Show file tree
Hide file tree
Showing 53 changed files with 2,014 additions and 0 deletions.
14 changes: 14 additions & 0 deletions staff/nerina-castillo/ponies/zzz/app1/data/deletePost.mjs
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;
10 changes: 10 additions & 0 deletions staff/nerina-castillo/ponies/zzz/app1/data/findPost.mjs
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;
10 changes: 10 additions & 0 deletions staff/nerina-castillo/ponies/zzz/app1/data/findPosts.mjs
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;
10 changes: 10 additions & 0 deletions staff/nerina-castillo/ponies/zzz/app1/data/findUser.mjs
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;
21 changes: 21 additions & 0 deletions staff/nerina-castillo/ponies/zzz/app1/data/index.mjs
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;
10 changes: 10 additions & 0 deletions staff/nerina-castillo/ponies/zzz/app1/data/insertPost.mjs
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;
10 changes: 10 additions & 0 deletions staff/nerina-castillo/ponies/zzz/app1/data/insertUser.mjs
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;
14 changes: 14 additions & 0 deletions staff/nerina-castillo/ponies/zzz/app1/data/updatePost.mjs
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;
14 changes: 14 additions & 0 deletions staff/nerina-castillo/ponies/zzz/app1/data/updateUser.mjs
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 staff/nerina-castillo/ponies/zzz/app1/logic/createPost.mjs
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 staff/nerina-castillo/ponies/zzz/app1/logic/deletePost.mjs
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 staff/nerina-castillo/ponies/zzz/app1/logic/getAllFavPosts.mjs
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;
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);
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 staff/nerina-castillo/ponies/zzz/app1/logic/getAllPosts.mjs
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 staff/nerina-castillo/ponies/zzz/app1/logic/getUserName.mjs
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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const getUserUsername = () => sessionStorage.username;

export default getUserUsername;
33 changes: 33 additions & 0 deletions staff/nerina-castillo/ponies/zzz/app1/logic/index.mjs
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;
17 changes: 17 additions & 0 deletions staff/nerina-castillo/ponies/zzz/app1/logic/loginUser.mjs
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;
3 changes: 3 additions & 0 deletions staff/nerina-castillo/ponies/zzz/app1/logic/logoutUser.mjs
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 staff/nerina-castillo/ponies/zzz/app1/logic/registerUser.mjs
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;
12 changes: 12 additions & 0 deletions staff/nerina-castillo/ponies/zzz/app1/logic/test.html
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 staff/nerina-castillo/ponies/zzz/app1/logic/toggleFavPost.mjs
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;
Loading

0 comments on commit 87e1ef9

Please sign in to comment.