diff --git a/staff/lucas-orts/ponies/app/home.html b/staff/lucas-orts/ponies/app/home.html deleted file mode 100644 index e69de29bb..000000000 diff --git a/staff/lucas-orts/ponies/app/logic/createPost.js b/staff/lucas-orts/ponies/app/logic/createPost.js new file mode 100644 index 000000000..8458c7a03 --- /dev/null +++ b/staff/lucas-orts/ponies/app/logic/createPost.js @@ -0,0 +1,19 @@ +function createPost(image, caption) { + if (!image.startsWith('http')) { + throw new Error('invalid image') + } + + var posts = localStorage.posts !== undefined ? JSON.parse(localStorage.posts) : [] + + var post = { + image: image, + caption: caption, + author: sessionStorage.username, + date: new Date().toISOString(), + id: generateRandomId() + } + + posts.push(post) + + localStorage.posts = JSON.stringify(posts) +} \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/logic/deletePost.js b/staff/lucas-orts/ponies/app/logic/deletePost.js new file mode 100644 index 000000000..4eda5ad83 --- /dev/null +++ b/staff/lucas-orts/ponies/app/logic/deletePost.js @@ -0,0 +1,14 @@ +function deletePost(id) { + + var posts = getAllPosts() + var index = posts.findIndex(function (element) { + + return element.id === id + + }) + + posts.splice(index, 1) + + localStorage.posts = JSON.stringify(posts) + +} \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/logic/getAllPosts.js b/staff/lucas-orts/ponies/app/logic/getAllPosts.js new file mode 100644 index 000000000..fa27f6e8d --- /dev/null +++ b/staff/lucas-orts/ponies/app/logic/getAllPosts.js @@ -0,0 +1,5 @@ +function getAllPosts() { + var posts = localStorage.posts !== undefined ? JSON.parse(localStorage.posts) : [] + + return posts.reverse() +} \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/logic/getUserName.js b/staff/lucas-orts/ponies/app/logic/getUserName.js new file mode 100644 index 000000000..3aec79a50 --- /dev/null +++ b/staff/lucas-orts/ponies/app/logic/getUserName.js @@ -0,0 +1,13 @@ +function getUserName() { + var users = localStorage.users !== undefined ? JSON.parse(localStorage.users) : [] + + var user = users.find(function (user) { + return user.username === sessionStorage.username + }) + + if (user === undefined) { + throw new Error('user not found') + } + + return user.name +} \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/logic/getUserUsername.js b/staff/lucas-orts/ponies/app/logic/getUserUsername.js new file mode 100644 index 000000000..660002cf1 --- /dev/null +++ b/staff/lucas-orts/ponies/app/logic/getUserUsername.js @@ -0,0 +1,3 @@ +function getUserUsername() { + return sessionStorage.username +} \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/logic/loginUser.js b/staff/lucas-orts/ponies/app/logic/loginUser.js new file mode 100644 index 000000000..ff8ebe07f --- /dev/null +++ b/staff/lucas-orts/ponies/app/logic/loginUser.js @@ -0,0 +1,27 @@ +var USER_REGEX = /^(?!.*\s{2})[a-zA-Z0-9._-]{4,16}$/ + +function loginUser(username, password) { + if (!USER_REGEX.test(username)) { + throw new Error('invalid username') + } + + if (password.trim().length < 8) { + throw new Error('invalid password') + } + + var users = localStorage.users !== undefined ? JSON.parse(localStorage.users) : [] + + var user = users.find(function (user) { + return user.username === username + }) + + if (user === undefined) { + throw new Error('username does not exist') + } + + if (user.password !== password) { + throw new Error('wrong password') + } + + sessionStorage.username = username +} \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/logic/logoutUser.js b/staff/lucas-orts/ponies/app/logic/logoutUser.js new file mode 100644 index 000000000..1307bfdaf --- /dev/null +++ b/staff/lucas-orts/ponies/app/logic/logoutUser.js @@ -0,0 +1,3 @@ +function logoutUser() { + delete sessionStorage.username +} \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/logic/registerUser.js b/staff/lucas-orts/ponies/app/logic/registerUser.js new file mode 100644 index 000000000..3e1573fdb --- /dev/null +++ b/staff/lucas-orts/ponies/app/logic/registerUser.js @@ -0,0 +1,59 @@ +var EMAIL_REGEX = /^[a-z0-9._]+@[a-z0-9.-]{3,63}\.[a-z]{2,10}$/ +var NAME_REGEX = /^(?!.*\s{2})[a-zA-Z ]{3,16}$/ +var USER_REGEX = /^(?!.*\s{2})[a-zA-Z0-9._-]{4,16}$/ + +function 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') + } + + var users = localStorage.users !== undefined ? JSON.parse(localStorage.users) : [] + + var user = users.find(function (user) { + return user.email === email + }) + + if (user !== undefined) { + throw new Error('email already exists') + } + + var user = users.find(function (user) { + return user.username === username + }) + + if (user !== undefined) { + throw new Error('username already exists') + } + + user = { + name: name, + surname: surname, + email: email, + username: username, + password: password + } + + users.push(user) + + localStorage.users = JSON.stringify(users) +} \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/login.html b/staff/lucas-orts/ponies/app/login.html deleted file mode 100644 index e69de29bb..000000000 diff --git a/staff/lucas-orts/ponies/app/register.html b/staff/lucas-orts/ponies/app/register.html deleted file mode 100644 index e69de29bb..000000000 diff --git a/staff/lucas-orts/ponies/app/utils/formatTime.js b/staff/lucas-orts/ponies/app/utils/formatTime.js new file mode 100644 index 000000000..c54838bf7 --- /dev/null +++ b/staff/lucas-orts/ponies/app/utils/formatTime.js @@ -0,0 +1,41 @@ +function formatTime(date) { + var seconds = Math.round((Date.now() - date.getTime()) / 1000) + + if (seconds < 60) { + return seconds + ' second' + (seconds === 1 ? '' : 's') + } + + var minutes = Math.round(seconds / 60) + + if (minutes < 60) { + return minutes + ' minute' + (minutes === 1 ? '' : 's') + } + + var hours = Math.round(minutes / 60) + + if (hours < 24) { + return hours + ' hour' + (hours === 1 ? '' : 's') + } + + var days = Math.round(hours / 24) + + if (days < 7) { + return days + ' day' + (days === 1 ? '' : 's') + } + + var weeks = Math.round(days / 7) + + if (weeks < 4) { + return weeks + ' week' + (weeks === 1 ? '' : 's') + } + + var months = Math.round(weeks / 4) + + if (months < 12) { + return months + ' month' + (months === 1 ? '' : 's') + } + + var years = Math.round(months / 12) + + return years + ' year' + (years === 1 ? '' : 's') +} \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/utils/generateRandomId.js b/staff/lucas-orts/ponies/app/utils/generateRandomId.js new file mode 100644 index 000000000..6e3866900 --- /dev/null +++ b/staff/lucas-orts/ponies/app/utils/generateRandomId.js @@ -0,0 +1,3 @@ +function generateRandomId() { + return Math.random().toString() + Date.now().toString() +} \ No newline at end of file diff --git a/staff/lucas-orts/ponies/app/views/home/index.html b/staff/lucas-orts/ponies/app/views/home/index.html new file mode 100644 index 000000000..3345b80cb --- /dev/null +++ b/staff/lucas-orts/ponies/app/views/home/index.html @@ -0,0 +1,32 @@ + + + +
+ + +