Skip to content

Commit

Permalink
move all logic of cor and api to promises b00tc4mp#68
Browse files Browse the repository at this point in the history
  • Loading branch information
NerinaHctz committed Aug 2, 2024
1 parent 2ba943d commit 4684858
Show file tree
Hide file tree
Showing 49 changed files with 836 additions and 1,613 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,19 @@ export default (req, res, next) => {
const { username, password } = req.body

try {
logic.authenticateUser(username, password, error => {
if (error) {
next(error)
logic.authenticateUser(username, password)
.then(() =>
jwt.sign({ sub: username }, process.env.JWT_SECRET, (error, token) => {
if (error) {
next(new SessionError(error.message))

return
}
return
}

jwt.sign({ sub: username }, process.env.JWT_SECRET, (error, token) => {
if (error) {
next(new SessionError(error.message))

return
}

res.json(token)
})
})
res.json(token)
})
)
.catch(error => next(error))
} catch (error) {
next(error)
}
Expand Down
13 changes: 3 additions & 10 deletions staff/nerina-castillo/ponies/api/handlers/createPostHandler.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import { logic } from '../../cor/index.js'

export default (req, res, next) => {

const { username } = req

const { image, caption } = req.body

try {
logic.createPost(username, image, caption, error => {
if (error) {
next(error)

return
}

res.status(201).send()
})
logic.createPost(username, image, caption)
.then(() => res.status(201).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
Expand Down
15 changes: 4 additions & 11 deletions staff/nerina-castillo/ponies/api/handlers/deletePostHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,10 @@ export default (req, res, next) => {
const { postId } = req.params

try {
logic.deletePost(username, postId, error => {
if (error) {
next(error)

return
}

res.status(204).send()
})
logic.deletePost(username, postId)
.then(() => res.status(204).send())
.catch(error => next(error))
} catch (error) {
netx(error)

next(error)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ export default (req, res, next) => {
const { username } = req

try {
logic.getAllFavPosts(username, (error, posts) => {
if (error) {
next(error)

return
}

res.json(posts)
})
logic.getAllFavPosts(username)
.then(posts => res.json(posts))
.catch(error => next(error))
} catch (error) {
next(error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,10 @@ export default (req, res, next) => {
const { username } = req

try {
logic.getAllFollowingUserPosts(username, (error, posts) => {
if (error) {
next(error)

return
}

res.json(posts)
})

logic.getAllFollowingUserPosts(username)
.then(posts => res.json(posts))
.catch(error => next(error))
} catch (error) {
next(error)

}
}
16 changes: 5 additions & 11 deletions staff/nerina-castillo/ponies/api/handlers/getAllPostsHandler.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
export default (req, res) => {
const { username } = req

try {
logic.getAllPosts(username, (error, posts) => {
if (error) {
next(error)

return
}

res.json(posts)
})
logic.getAllPosts(username)
.then(posts => res.json(posts))
.catch(error => next(error))
} catch (error) {
next(error)
nexct(error)
}
}
12 changes: 3 additions & 9 deletions staff/nerina-castillo/ponies/api/handlers/getUserNameHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@ export default (req, res, next) => {
const { targetUsername } = req.params

try {
logic.getUserName(username, targetUsername, (error, name) => {
if (error) {
next(error)

return
}

res.json(name)
})
logic.getUserName(username, targetUsername)
.then(name => res.json(name))
.catch(error => next(error))
} catch (error) {
next(error)
}
Expand Down
12 changes: 3 additions & 9 deletions staff/nerina-castillo/ponies/api/handlers/registerUserHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ export default (req, res, next) => {
const { name, surname, email, username, password, passwordRepeat } = req.body

try {
logic.registerUser(name, surname, email, username, password, passwordRepeat, error => {
if (error) {
next(error)

return
}

res.status(201).send()
})
logic.registerUser(name, surname, email, username, password, passwordRepeat)
.then(() => res.status(201).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@ export default (req, res, next) => {
const { postId } = req.params

try {
logic.toggleFavPost(username, postId, error => {
if (error) {
next(error)

return
}

res.status(204).send()
})
logic.toggleFavPost(username, postId)
.then(() => res.status(204).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@ export default (req, res, next) => {
const { targetUsername } = req.params

try {
logic.toggleFollowUser(username, targetUsername, error => {
if (error) {
next(error)

return
}

res.status(204).send()
})
logic.toggleFollowUser(username, targetUsername)
.then(() => res.status(204).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@ export default (req, res, next) => {
const { postId } = req.params

try {
logic.toggleLikePost(username, postId, error => {
if (error) {
next(error)

return
}

res.status(204).send()
})
logic.toggleLikePost(username, postId)
.then(() => res.status(204).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,9 @@ export default (req, res, next) => {
const { caption } = req.body

try {
logic.updatePostCaption(username, postId, caption, error => {
if (error) {
next(error)

return
}

res.status(204).send()
})
logic.updatePostCaption(username, postId, caption)
.then(() => res.status(204).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
Expand Down
2 changes: 1 addition & 1 deletion staff/nerina-castillo/ponies/api/test/toggleFav-post.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
curl -v http://localhost:8080/posts/3fswobxum974/favs -X PATCH -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJqYW5mcnlUb3BlcmEiLCJpYXQiOjE3MjIzMzAzNzl9.psSpv8cj_SrZSNGn2alTkkJ8-Aq7FTTkCGDsl9VEI-I"
curl -v http://localhost:8080/posts/66ab7e7385156518ecdcd558/favs -X PATCH -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJqYW5mcnlUb3BlcmEiLCJpYXQiOjE3MjIzMzAzNzl9.psSpv8cj_SrZSNGn2alTkkJ8-Aq7FTTkCGDsl9VEI-I"
27 changes: 9 additions & 18 deletions staff/nerina-castillo/ponies/cor/logic/authenticateUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,21 @@ import { validate, errors } from '../../com/index.js'

const { NotFoundError, CredentialsError, SystemError } = errors

export default (username, password, callback) => {
export default (username, password) => {
validate.username(username)
validate.password(password)
validate.callback(callback)

User.findOne({ username }).lean()
return User.findOne({ username }).lean()
.catch(error => { throw new SystemError(error.message) })
.then(user => {
if (!user) {
callback(new NotFoundError('user not found'))
if (!user)
throw new NotFoundError('user not found')

return
}

bcrypt.compare(password, user.password)
return bcrypt.compare(password, user.password)
.catch(error => { throw new SystemError(error.message) })
.then(match => {
if (!match) {
callback(new CredentialsError('wrong password'))

return
}

callback(null)
if (!match)
throw new CredentialsError('wrong password')
})
.catch(error => callback(new SystemError(error.message)))
})
.catch(error => callback(new SystemError(error.message)))
}
Loading

0 comments on commit 4684858

Please sign in to comment.