forked from b00tc4mp/isdi-parttime-202309
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement toggle fav api and app b00tc4mp#407
- Loading branch information
Showing
10 changed files
with
156 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
staff/belen-ivars/project/api/handlers/toggleFavRecipeHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import logic from "../logic/index.js" | ||
import jwt from 'jsonwebtoken' | ||
import { errors } from 'com' | ||
const { NotFoundError, ContentError } = errors | ||
|
||
|
||
const toggleFavRecipeHandler = async (req, res) => { | ||
|
||
const token = req.headers.authorization.substring(7) | ||
|
||
const { sub: userId } = jwt.verify(token, process.env.JWT_SECRET) | ||
|
||
const recipeId = req.params.recipeId | ||
|
||
try { | ||
await logic.toggleFavRecipe(userId, recipeId) | ||
|
||
res.status(204).send() | ||
|
||
} catch (error) { | ||
let status = 500 | ||
|
||
if (error instanceof NotFoundError) | ||
status = 404 | ||
if (error instanceof ContentError || error instanceof TypeError) | ||
status = 500 | ||
res.status(status).json({ error: error.constructor.name, message: error.message }) | ||
} | ||
|
||
} | ||
|
||
export default toggleFavRecipeHandler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
staff/belen-ivars/project/api/test/toggle-fav-recipe.test.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
source pepetest.sh | ||
|
||
TEST 'toggle-fav-recipe' | ||
|
||
CASE 'success on current user' | ||
|
||
curl 'http://localhost:9000/users/66229b4f6c6c99938fab1a5b' \ | ||
-H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NWQ2NDJlOTY5NWNlMDFiNTM1ODVhODUiLCJpYXQiOjE3MTM2Mjg2NjgsImV4cCI6MTcxMzYzMjI2OH0.dJe5OIV7Un3fU6wcBTA2uhRhxAr0AmdPq8k_37X458w' \ | ||
-v | ||
|
||
# > POST /recipes HTTP/1.1 | ||
# > Host: localhost:9000 | ||
# > User-Agent: curl/8.4.0 | ||
# > Accept: */* | ||
# > Content-Type: application/json | ||
# > Content-Length: 187 | ||
# > | ||
# < HTTP/1.1 201 Created | ||
# < X-Powered-By: Express | ||
# < Access-Control-Allow-Origin: * | ||
# < Date: Thu, 21 Mar 2024 17:45:59 GMT | ||
# < Connection: keep-alive | ||
# < Keep-Alive: timeout=5 | ||
# < Content-Length: 0 | ||
|
||
# CASE "fail on non existing user" | ||
|
||
# curl 'http://localhost:9000/recipes' \ | ||
# -H 'Content-Type: application/json' \ | ||
# -d '{"author": "65d655fac1dd88f9aee917d7", "title": "Colors", "description": "Persona con pintura corporal", "image":"https://www.pexels.com/es-es/foto/persona-con-pintura-corporal-1209843/"}' \ | ||
# -v | ||
|
||
# > POST /recipes HTTP/1.1 | ||
# > Host: localhost:9000 | ||
# > User-Agent: curl/8.4.0 | ||
# > Accept: */* | ||
# > Content-Type: application/json | ||
# > Content-Length: 187 | ||
# > | ||
# < HTTP/1.1 404 Not Found | ||
# < X-Powered-By: Express | ||
# < Access-Control-Allow-Origin: * | ||
# < Content-Type: application/json; charset=utf-8 | ||
# < Content-Length: 52 | ||
# < ETag: W/"34-Cs2INrsYwSHLSHCKVUFPEWh9NjI" | ||
# < Date: Thu, 21 Mar 2024 17:45:59 GMT | ||
# < Connection: keep-alive | ||
# < Keep-Alive: timeout=5 | ||
# < | ||
# * Connection #0 to host localhost left intact | ||
# {"error":"NotFoundError","message":"user not found"}% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
staff/belen-ivars/project/app/src/logic/toggleFavRecipe.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { API_URL } from "../utils/constants" | ||
import { errors } from 'com' | ||
import session from "./session" | ||
|
||
const { SystemError } = errors | ||
|
||
export default function toggleFavRecipe(recipeId) { | ||
return (async () => { | ||
const req = { | ||
method: 'PATCH', | ||
headers: { | ||
Authorization: `Bearer ${session.token}` | ||
} | ||
} | ||
|
||
let res | ||
|
||
try { | ||
res = await fetch(`${API_URL}/users/${recipeId}`, req) | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
|
||
if (!res.ok) { | ||
let body | ||
|
||
try { | ||
body = await res.json() | ||
} catch (error) { | ||
throw new SystemError(errors.message) | ||
} | ||
throw new errors[body.error](body.message) | ||
} | ||
|
||
})() | ||
} |