Skip to content

Commit

Permalink
make every client logic promised b00tc4mp#149
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown authored and unknown committed Jul 9, 2024
1 parent 15ba427 commit 9a680b8
Show file tree
Hide file tree
Showing 18 changed files with 149 additions and 119 deletions.
2 changes: 1 addition & 1 deletion staff/mateo-gomez/socialcode/api_mongoose/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ mongoose.connect(MONGODB_URL)
}


res.status(200).send()
res.status(204).send()
})
})
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const getUserName = (userId, targetUserId, callback) => {
validate.username(targetUserId, 'targetUserId')
validate.callback(callback)

User.findById({ userId }).lean()
User.findById(userId).lean()
.then(user => {
if (!user) {
callback(new MatchError('user not found'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { MONGODB_URL } = process.env
mongoose.connect(MONGODB_URL)
.then(() => {
try {
getUserName('668a739a50df84d483367be9', '668a78423a0226d1ab93fe55', (error, name) => {
getUserName('668bb1b3a8796a3c7044e7ca', '668bb1b3a8796a3c7044e7ca', (error, name) => {
if (error) {
console.error(error)

Expand Down
15 changes: 7 additions & 8 deletions staff/mateo-gomez/socialcode/app/src/logic/createPost.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import errors from 'com/errors'
import validate from 'com/validate.js'


const createPost = (title, image, description, callback) => {
const createPost = (title, image, description) => {
validate.text(title, 'title', 50)
validate.url(image, 'image')
validate.text(description, 'description', 300)
validate.callback(callback)




Expand Down Expand Up @@ -44,33 +44,32 @@ const createPost = (title, image, description, callback) => {
*/

fetch(`${import.meta.env.VITE_API_URL}/posts`, {
return fetch(`${import.meta.env.VITE_API_URL}/posts`, {
method: 'POST',
headers: {
Authorization: `Bearer ${sessionStorage.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ title, image, description })
})

.catch(error => { throw new SystemError(error.message) })
.then(response => {
if (resonse.status === 201) {
callback(null)

return
}

return response.json()
.catch(error => { throw new SystemError(error.message) })
.then(body => {
const { error, message } = body

const constructor = errors[error]

callback(new constructor(message))
{ throw new constructor(message) }
})
.catch(error => callback(new SystemError(error.message)))

})
.catch(error => callback(new SystemError(error.message)))
}


Expand Down
15 changes: 7 additions & 8 deletions staff/mateo-gomez/socialcode/app/src/logic/deletePost.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import errors, { SystemError } from 'com/errors'
import validate from 'com/validate'


const deletePost = (postId, callback) => {
const deletePost = (postId) => {
validate.id(postId, 'postId')
validate.callback(callback)



/*
Expand Down Expand Up @@ -34,32 +34,31 @@ const deletePost = (postId, callback) => {
*/

fetch(`${import.meta.env.VITE_API_URL}/posts/${postId}`, {
return fetch(`${import.meta.env.VITE_API_URL}/posts/${postId}`, {
method: 'DELETE',
headers: {
Authorization: `Bearer ${sessionStorage.token}`
}
})

.catch(error => { throw new SystemError(error.message) })
.then(response => {
if (response.status === 204) {
callback(null)

return
}

return response.json()
.catch(error => { throw new SystemError(error.message) })
.then(body => {
const { error, message } = body

const constructor = errors[error]

callback(new constructor(message))
throw new constructor(message)
})
.catch(error => callback(new SystemError(error.message)))

})
.catch(error => callback(new SystemError(error.message)))

}

export default deletePost
17 changes: 9 additions & 8 deletions staff/mateo-gomez/socialcode/app/src/logic/getAllPosts.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import errors, { SystemError } from 'com/errors'
import validate from 'com/validate'


const getAllPosts = callback => {
validate.callback(callback)
const getAllPosts = () => {


/*
Expand Down Expand Up @@ -31,31 +29,34 @@ const getAllPosts = callback => {
xhr.send()
*/

fetch(`${import.meta.env.VITE_API_URL}/posts`, {
return fetch(`${import.meta.env.VITE_API_URL}/posts`, {
headers: {
method: 'GET',
Authorization: `Bearer ${sessionStorage.token}`

}
})
.catch(error => { throw new SystemError(error.message) })
.then(response => {
if (response.status === 200) {

return response.json()
.then(posts => callback(null, posts))
.catch(error => { throw new SystemError(error.message) })
.then(posts => { return posts })

}

return response.json()
.catch(error => { throw new SystemError(error.message) })
.then(body => {
const { error, message } = body

const constructor = errors[error]

callback(new constructor(message))
throw new constructor(message)
})
.catch(error => callback(new SystemError(error.message)))

})
.catch(error => callback(new SystemError(error.message)))
}


Expand Down
18 changes: 9 additions & 9 deletions staff/mateo-gomez/socialcode/app/src/logic/getUserName.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import validate from 'com/validate.js'
import extractPayloadFromJWT from '../utils/extractPayloadFromJWT.js'


const getUserName = callback => {
const getUserName = () => {
/* const user = data.findUser(user =>
user.username === sessionStorage.username)
return user.name*/
validate.callback(callback)


const { sub: userId } = extractPayloadFromJWT(sessionStorage.token)

Expand Down Expand Up @@ -39,36 +39,36 @@ const getUserName = callback => {
*/


fetch(`${import.meta.env.VITE_API_URL}/users/${userId}`, {
return fetch(`${import.meta.env.VITE_API_URL}/users/${userId}`, {
method: 'GET',
headers: {
Authorization: `Bearer ${sessionStorage.token}` //Authorization no hace falta que vayta entre comillas porque no tiene guion en medio
}
})
.catch(error => { throw new SystemError(error.message) })
.then(response => { // respuesta de la api
if (response.status === 200) {
callback(null)


return response.json() //La respuesta la convertmos de json a objeto, es decir, a JavaScript
.catch(error => { throw new SystemError(error.message) })
.then(name => { //Esperamos como respuesta el name

callback(null, name)
{ return name }
})

}


return response.json()
.catch(error => { throw new SystemError(error.message) }) // si la respuesta response.json() falla
.then(body => { // respuesta en forma de objeto porque lo transformamos a json(). (({error es el nombre de la constructora de error y message es el mensaje de error}))
const { error, message } = body

const constructor = errors[error] //A partir del nombre de la constructora traemos la constructora a const constructor puesto que errors[error] es un objeto en el que están todas las constructoras de error

callback(new constructor(message)) // reconstruyo el error con el mensaje que me viene de la api
throw new constructor(message) // reconstruyo el error con el mensaje que me viene de la api
})
.catch(error => callback(new SystemError(error.message))) // si la respuesta response.json() falla
})
.catch(error => callback(new SystemError(error.message)))
}


Expand Down
18 changes: 9 additions & 9 deletions staff/mateo-gomez/socialcode/app/src/logic/loginUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import validate from 'com/validate'



const loginUser = (username, password, callback) => {
const loginUser = (username, password) => {
validate.username(username)
validate.password(password)
validate.callback(callback)


/*
const xhr = new XMLHttpRequest
Expand Down Expand Up @@ -41,39 +39,41 @@ const loginUser = (username, password, callback) => {
xhr.setRequestHeader('Content-Type', 'application/json')
xhr.send(json)*/

fetch(`${import.meta.env.VITE_API_URL}/users/auth`, {
return fetch(`${import.meta.env.VITE_API_URL}/users/auth`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',

},
body: JSON.stringify({ username, password })
})
.catch(() => { throw new SystemError('connection error') })
.then(response => { // respuesta de la api
if (response.status === 200) {
callback(null)

return response.json() //La respuesta la convertmos de json a objeto, es decir, a JavaScript
.catch(error => { throw new SystemError(error.message) })
.then(token => {
sessionStorage.token = token

callback(null)
return token
})

}


return response.json()
.catch(error => { throw new SystemError(error.message) }) // si la respuesta response.json() falla
.then(body => { // respuesta en forma de objeto porque lo transformamos a json(). (({error es el nombre de la constructora de error y message es el mensaje de error}))
const { error, message } = body

const constructor = errors[error] //A partir del nombre de la constructora traemos la constructora a const constructor puesto que errors[error] es un objeto en el que están todas las constructoras de error

callback(new constructor(message)) // reconstruyo el error con el mensaje que me viene de la api
throw new constructor(message) // reconstruyo el error con el mensaje que me viene de la api
})
.catch(error => callback(new SystemError(error.message))) // si la respuesta response.json() falla

})
.catch(error => callback(new SystemError(error.message)))

}


Expand Down
16 changes: 8 additions & 8 deletions staff/mateo-gomez/socialcode/app/src/logic/registerUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import errors, { SystemError } from 'com/errors'
import validate from 'com/validate'


const registerUser = (name, surname, email, username, password, passwordRepeat, callback) => {
const registerUser = (name, surname, email, username, password, passwordRepeat) => {
//Input validation
validate.name(name)
validate.surname(surname)
validate.email(email)
validate.username(username)
validate.password(password)
validate.passwordMatch(password, passwordRepeat)
validate.callback(callback)


//Aquí lo haciamos con xhr pero lo cambiamos a getch() para poder hacer uso de las promesas
/*
Expand Down Expand Up @@ -54,33 +54,33 @@ const registerUser = (name, surname, email, username, password, passwordRepeat,

//Ahora utilizamso la lógica con fetch() para utilizar las promesas

fetch(`${import.meta.env.VITE_API_URL}/users`, {
return fetch(`${import.meta.env.VITE_API_URL}/users`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',

},
body: JSON.stringify({ name, surname, email, username, password, passwordRepeat })
})
.catch(() => { throw new SystemError('connection error') })
.then(response => { // respuesta de la api
if (response.status === 201) {
callback(null)

return
}

return response.json()
.catch(error => { throw new SystemError(error.message) }) // si la respuesta response.json() falla
.then(body => { // respuesta en forma de objeto porque lo transformamos a json(). (({error es el nombre de la constructora de error y message es el mensaje de error}))
const { error, message } = body

const constructor = errors[error] //A partir del nombre de la constructora traemos la constructora a const constructor puesto que errors[error] es un objeto en el que están todas las constructoras de error

callback(new constructor(message)) // reconstruyo el error con el mensaje que me viene de la api
throw new constructor(message)
})
.catch(error => callback(new SystemError(error.message))) // si la respuesta response.json() falla

})
.catch(error => callback(new SystemError(error.message)))
}

}

export default registerUser
Loading

0 comments on commit 9a680b8

Please sign in to comment.