Skip to content

Commit

Permalink
add base code b00tc4mp#68
Browse files Browse the repository at this point in the history
  • Loading branch information
NerinaHctz committed Jul 14, 2024
1 parent 87e1ef9 commit e275c2a
Show file tree
Hide file tree
Showing 17 changed files with 123 additions and 23 deletions.
24 changes: 24 additions & 0 deletions staff/nerina-castillo/ponies/api/demo1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import express from 'express'
import findUser from './data/findUser.js'

const app = express()

app.use(express.json())

app.get('/users/:username', (req, res) => {
const username = req.params.username

try{
const user = findUser(user => user.username === username)
if(!user){
return res.status(404).send('user not found')
}
res.json(user)
} catch(error){
res.status(500).send(error.message)
}
})

app.listen(8080, () => console.log('server running at http://localhost:8080/users/{username}'))


22 changes: 22 additions & 0 deletions staff/nerina-castillo/ponies/api/demo2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import express from 'express'
import findPost from './data/findPost.js'

const app = express() //es la instancia de express que se usará para definir rutas y middleware

app.use(express.json()) //le dice a la aplicación que use el middleware 'express.json()' para parsear las peticiones

app.get('/posts/:postId', (req, res) => { //se define una ruta GET en el endpoint '/posts/:postId
const postId = req.params.postId //extrae el valor del parámetro de la URL 'postId'

try{
const post = findPost(post => post.id === postId)
if(!post){
return res.status(404).send('post not found')
}
res.json(post)
} catch(error){
res.status(500).send(error.message)
}
})

app.listen(8080, () => console.log('server running at http://localhost:8080/posts/{postId}')) //el sevidor comienza a esuchar en el puerto 8080
16 changes: 16 additions & 0 deletions staff/nerina-castillo/ponies/api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import express from 'express' //importamos el módulo express

const api = express() //creamos una instancia de express y la guardamos en la constante api

api.get('/hello', (req, res) => { //efinimos una ruta HTTP GET en '/hello'

res.send('Hello, world')

//req: request, contiene información sobre la solicitud HTTP
//res: response, objeto de respuesta que se usa para enviar una respuesta al cliente

})

api.listen(8080, () => console.log('server up'))

//iniciamos el servidor para que escuche en el puerto 8080
2 changes: 1 addition & 1 deletion staff/nerina-castillo/ponies/api/logic/authenticateUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const authenticateUser = (username, password) => {
validate.username(username)
validate.password(password)

if (!user_regex.test(username))
if (!USER_REGEX.test(username))
throw new Error('invalid username')

if (password.trim().length < 8)
Expand Down
2 changes: 1 addition & 1 deletion staff/nerina-castillo/ponies/api/logic/createPost.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const createPost = (username, image, caption) => {
const user = data.findUser(user => user.username == username)

if (user === null)
throw new Error('User not found')
throw new Error('user not found')
if (!image.startsWith("http")) throw new Error("invalid image");

const post = {
Expand Down
2 changes: 1 addition & 1 deletion staff/nerina-castillo/ponies/api/logic/deletePost.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const deletePost = (username, postId) => {

const user = data.findUser(user => user.username === username)
if (user === null)
throw new Error('User not found')
throw new Error('user not found')

if (postId.trim().length === 0) throw new Error("invalid postId");

Expand Down
2 changes: 1 addition & 1 deletion staff/nerina-castillo/ponies/api/logic/getAllFavPosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const getAllFavPosts = username => {
const user = data.findUser(user => user.username === username)

if (user === null)
throw new Error('User not found')
throw new Error('user not found')

const posts = data.findPosts(post => user.favs.includes(post.id))

Expand Down
2 changes: 1 addition & 1 deletion staff/nerina-castillo/ponies/api/logic/getAllPosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const getAllPosts = username => {
(user) => user.username === username
);

if (user === null) throw new Error("User not found");
if (user === null) throw new Error("user not found");

const posts = data.findPosts(() => true);

Expand Down
16 changes: 8 additions & 8 deletions staff/nerina-castillo/ponies/api/logic/registerUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import data from "../data/index.js";

import validate from "../validate.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 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,
Expand All @@ -19,15 +19,15 @@ const registerUser = (
validate.email(email)
validate.username(username)
validate.password(password)
validate.passwordRepeat(passwordRepeat)
validate.password(passwordRepeat)

if (!name_regex.test(name.trim())) throw new Error("invalid name");
if (!NAME_REGEX.test(name.trim())) throw new Error("invalid name");

if (!name_regex.test(surname.trim())) throw new Error("ivalid surname");
if (!NAME_REGEX.test(surname.trim())) throw new Error("ivalid surname");

if (!email_regex.test(email)) throw new Error("invalid email");
if (!EMAIL_REGEX.test(email)) throw new Error("invalid email");

if (!user_regex.test(username)) throw new Error("invalid username");
if (!USER_REGEX.test(username)) throw new Error("invalid username");

if (password.trim().length < 8) throw new Error("invalid password");

Expand Down
2 changes: 1 addition & 1 deletion staff/nerina-castillo/ponies/api/logic/toggleLikePost.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function toggleLikePost(username, postId) {

const user = data.findUser(user => user.username === username)

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

if (postId.trim().length === 0) throw new Error("invalid postId");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const updatePostCaption = (username, postId, newCaption) => {
validate.postId(postId, 'postId')
const user = data.findUser(user => user.username === username)

if (user === null) throw new Error('User not found')
if (user === null) throw new Error('user not found')

if (postId.trim().length === 0) throw new Error("invalid postId");

Expand Down
1 change: 1 addition & 0 deletions staff/nerina-castillo/ponies/api/test/findPost.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -X GET http://localhost:8080/posts/onlstpoafkw
1 change: 1 addition & 0 deletions staff/nerina-castillo/ponies/api/test/findUser.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -X POST 'http://localhost:8080/users/julitoCamelas '
1 change: 1 addition & 0 deletions staff/nerina-castillo/ponies/api/test/hello.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl http://localhost:8080/hello -v
42 changes: 42 additions & 0 deletions staff/nerina-castillo/ponies/api/test/posts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
curl -X POST jttp://localhost:8080/posts -H 'Content-Type: application/json' -d '[
{
"image": "https://media.giphy.com/media/g5R9dok94mrIvplmZd/giphy.gif?cid=790b7611t0zs44n98uqyevqcf7813v2y1by0mmk3mejeu6mw&ep=v1_gifs_trending&rid=giphy.gif&ct=g",
"author": "maxPower",
"id": "onlstpoafkw",
"date": "2024-07-12T09:04:37.515Z",
"caption": "yay",
"likes": [],
"follows": [],
"favs": []
},
{
"image": "https://media.giphy.com/media/mLZ6kvGkH31z0BAKUX/giphy.gif?cid=790b7611t0zs44n98uqyevqcf7813v2y1by0mmk3mejeu6mw&ep=v1_gifs_trending&rid=giphy.gif&ct=g",
"author": "cauliFlower",
"id": "2cxgeu12zsw0",
"date": "2024-07-12T09:03:37.515Z",
"caption": "oh",
"likes": [],
"follows": [],
"favs": []
},
{
"image": "https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExdDB6czQ0bjk4dXF5ZXZxY2Y3ODEzdjJ5MWJ5MG1tazNtZWpldTZtdyZlcD12MV9naWZzX3RyZW5kaW5nJmN0PWc/2bJWG8k0HAktq/giphy.gif",
"author": "julitoCamelas",
"id": "3fswobxum9q0",
"date": "2024-07-12T09:03:50.515Z",
"caption": "hey",
"likes": [],
"follows": [],
"favs": []
},
{
"id": "ce4fdb4238o",
"image": "https//nlknvliver",
"caption": "yass",
"author": "cauliFlower",
"date": "2024-07-12T09:01:57.515Z",
"likes": [],
"follows": [],
"favs": []
}
]'
1 change: 1 addition & 0 deletions staff/nerina-castillo/ponies/api/test/users.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -X POST http://localhost:8080/users -H 'Content-Type: application/json' -d '[{"name":"Julito","surname":"Camelas","email":"[email protected]","username":"julitoCamelas","password":"julito123","post":"https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExdDB6czQ0bjk4dXF5ZXZxY2Y3ODEzdjJ5MWJ5MG1tazNtZWpldTZtdyZlcD12MV9naWZzX3RyZW5kaW5nJmN0PWc/2bJWG8k0HAktq/giphy.gif","following":[],"favs":[]},{"name":"Cauli","surname":"Flower","email":"[email protected]","username":"cauliFlower","password":"cauliflower4","post":"https://media.giphy.com/media/mLZ6kvGkH31z0BAKUX/giphy.gif?cid=790b7611t0zs44n98uqyevqcf7813v2y1by0mmk3mejeu6mw&ep=v1_gifs_trending&rid=giphy.gif&ct=g","following":[],"favs":[]},{"name":"Max","surname":"Power","email":"[email protected]","username":"maxPower","password":"maxpower1","post":"https://media.giphy.com/media/g5R9dok94mrIvplmZd/giphy.gif?cid=790b7611t0zs44n98uqyevqcf7813v2y1by0mmk3mejeu6mw&ep=v1_gifs_trending&rid=giphy.gif&ct=g","following":[],"favs":[]},{"name":"janfry","surname":"topera","email":"[email protected]","username":"janfryTopera","password":"janfrytopera1","favs":[],"following":[],"avatar":"https://c8.alamy.com/comp/2EDB67T/cute-horse-avatar-cute-farm-animal-hand-drawn-illustration-isolated-vector-illustration-2EDB67T.jpg"}]'
8 changes: 0 additions & 8 deletions staff/nerina-castillo/ponies/api/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ function validatePassword(password) {
if (password.includes(' ')) throw new SyntaxError('password has empty spaces')
}

function validateRepeatPassword(passwordRepeat){
validateString(passwordRepeat, 'repeat password')
if(passwordRepeat.trim().length < 8) throw new RangeError('password length is lower than 8 characters')
if(passwordRepeat.includes(' ')) throw new SyntaxError('password has empty spaces')
if(passwordRepeat !== password) throw new SyntaxError('passwords do not match')
}

function validateName(name, explain = 'name') {
validateString(name, explain)
if (!NAME_REGEX.test(name)) throw new SyntaxError(`invalid ${explain}`)
Expand Down Expand Up @@ -63,7 +56,6 @@ const validate = {
object: validateObject,
username: validateUsername,
password: validatePassword,
passwordRepeat: validateRepeatPassword,
name: validateName,
surname: validateSurname,
email: validateEmail,
Expand Down

0 comments on commit e275c2a

Please sign in to comment.