diff --git a/package-lock.json b/package-lock.json index 65aa212..da4f394 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,7 +7,6 @@ "": { "name": "core-assignment-week-10", "version": "1.0.0", - "hasInstallScript": true, "license": "CC BY-NC-SA 4.0", "dependencies": { "@inquirer/prompts": "^7.0.0", diff --git a/task-1/cocktail.js b/task-1/cocktail.js index 6fd5a62..29a8bea 100644 --- a/task-1/cocktail.js +++ b/task-1/cocktail.js @@ -1,10 +1,57 @@ // API documentation: https://www.thecocktaildb.com/api.php +import { writeFile } from 'fs/promises'; import path from 'path'; const BASE_URL = 'https://www.thecocktaildb.com/api/json/v1/1'; // Add helper functions as needed here +function getAlcoholicLabel(alcoholicValue) { + return alcoholicValue === 'Alcoholic' ? 'Yes' : 'No'; +} + +function getIngredients(drink) { + const ingredients = []; + + for (let i = 1; i <= 15; i += 1) { + const ingredient = drink[`strIngredient${i}`]?.trim(); + const measure = drink[`strMeasure${i}`]?.trim(); + + if (!ingredient) { + continue; + } + + ingredients.push(`- ${measure ? `${measure} ` : ''}${ingredient}`); + } + + return ingredients.join('\n'); +} + +function formatDrink(drink) { + return [ + `## ${drink.strDrink}`, + '', + `![${drink.strDrink}](${drink.strDrinkThumb}/medium)`, + '', + `**Category**: ${drink.strCategory}`, + '', + `**Alcoholic**: ${getAlcoholicLabel(drink.strAlcoholic)}`, + '', + '### Ingredients', + '', + getIngredients(drink), + '', + '### Instructions', + '', + drink.strInstructions, + '', + `Serve in: ${drink.strGlass}`, + ].join('\n'); +} + +function createMarkdown(drinks) { + return `# Cocktail Recipes\n\n${drinks.map(formatDrink).join('\n\n')}`; +} export async function main() { if (process.argv.length < 3) { @@ -20,10 +67,25 @@ export async function main() { try { // 1. Fetch data from the API at the given URL + const response = await fetch(url); + if (!response.ok) { + throw new Error(`Failed to fetch cocktail data. Status: ${response.status}`); + } + + const data = await response.json(); + + if (!data.drinks) { + throw new Error('No cocktails found with that name.'); + } + // 2. Generate markdown content to match the examples + const markdown = createMarkdown(data.drinks); + // 3. Write the generated content to a markdown file as given by outPath + await writeFile(outPath, markdown); } catch (error) { // 4. Handle errors + console.error(`Error: ${error.message}`); } } diff --git a/task-1/output/margarita.md b/task-1/output/margarita.md new file mode 100644 index 0000000..8011a1b --- /dev/null +++ b/task-1/output/margarita.md @@ -0,0 +1,22 @@ +# Cocktail Recipes + +## Margarita + +![Margarita](https://www.thecocktaildb.com/images/media/drink/5noda61589575158.jpg/medium) + +**Category**: Ordinary Drink + +**Alcoholic**: Yes + +### Ingredients + +- 1 1/2 oz Tequila +- 1/2 oz Triple sec +- 1 oz Lime juice +- Salt + +### Instructions + +Rub the rim of the glass. + +Serve in: Cocktail glass \ No newline at end of file diff --git a/task-2/post-cli/src/services.js b/task-2/post-cli/src/services.js index 2601f57..a5c4901 100644 --- a/task-2/post-cli/src/services.js +++ b/task-2/post-cli/src/services.js @@ -1,5 +1,5 @@ // Change base URL for API requests to the local IP of the Post Central API server -const BASE_URL = 'http://localhost:3000'; +const BASE_URL = "http://localhost:3000"; // ============================================================================ // AUTH TOKEN - Stored after login/register, sent with every request @@ -37,7 +37,7 @@ const getHello = async () => { const response = await fetch(`${BASE_URL}/posts/hello`); if (!response.ok) { throw new Error( - `Failed to get hello: HTTP ${response.status} ${response.statusText}` + `Failed to get hello: HTTP ${response.status} ${response.statusText}`, ); } return await response.json(); @@ -53,7 +53,19 @@ const getHello = async () => { * Response: { user: string } */ const getMe = async () => { - // TODO + const response = await fetch(`${BASE_URL}/users/me`, { + headers: { + Authorization: `Bearer ${getToken()}`, + }, + }); + + if (!response.ok) { + throw new Error( + `Failed to get user: HTTP ${response.status} ${response.statusText}`, + ); + } + + return await response.json(); }; // ============================================================================ @@ -68,15 +80,15 @@ const getMe = async () => { */ const createUser = async (name, password) => { const response = await fetch(`${BASE_URL}/users/register`, { - method: 'POST', + method: "POST", headers: { - 'Content-Type': 'application/json', + "Content-Type": "application/json", }, body: JSON.stringify({ name, password }), }); if (!response.ok) { throw new Error( - `Failed to create user: HTTP ${response.status} ${response.statusText}` + `Failed to create user: HTTP ${response.status} ${response.statusText}`, ); } return await response.json(); @@ -89,7 +101,21 @@ const createUser = async (name, password) => { * Response: { user: string, token: string } */ const loginUser = async (name, password) => { - // TODO + const response = await fetch(`${BASE_URL}/users/login`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ name, password }), + }); + + if (!response.ok) { + throw new Error( + `Failed to login: HTTP ${response.status} ${response.statusText}`, + ); + } + + return await response.json(); }; // ============================================================================ @@ -103,7 +129,22 @@ const loginUser = async (name, password) => { * Response: { id: number, text: string, user: string } */ const createPost = async (text) => { - // TODO + const response = await fetch(`${BASE_URL}/posts`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${getToken()}`, + }, + body: JSON.stringify({ text }), + }); + + if (!response.ok) { + throw new Error( + `Failed to create post: HTTP ${response.status} ${response.statusText}`, + ); + } + + return await response.json(); }; /** @@ -112,7 +153,19 @@ const createPost = async (text) => { * Response: Array of { id, text, user } */ const getPosts = async () => { - // TODO + const response = await fetch(`${BASE_URL}/posts/me`, { + headers: { + Authorization: `Bearer ${getToken()}`, + }, + }); + + if (!response.ok) { + throw new Error( + `Failed to get posts: HTTP ${response.status} ${response.statusText}`, + ); + } + + return await response.json(); }; /** @@ -122,7 +175,22 @@ const getPosts = async () => { * Response: { id: number, text: string } */ const updatePost = async (id, text) => { - // TODO + const response = await fetch(`${BASE_URL}/posts/${id}`, { + method: "PUT", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${getToken()}`, + }, + body: JSON.stringify({ text }), + }); + + if (!response.ok) { + throw new Error( + `Failed to update post: HTTP ${response.status} ${response.statusText}`, + ); + } + + return await response.json(); }; /** @@ -131,7 +199,23 @@ const updatePost = async (id, text) => { * Response: { user: string, message: string } */ const deleteUser = async () => { - // TODO + const response = await fetch(`${BASE_URL}/users/me`, { + method: "DELETE", + headers: { + Authorization: `Bearer ${getToken()}`, + }, + }); + + if (!response.ok) { + throw new Error( + `Failed to delete user: HTTP ${response.status} ${response.statusText}`, + ); + } + + // Some tests only mock status/ok and do not provide json() + if (response.json) { + return await response.json(); + } }; /** @@ -140,7 +224,23 @@ const deleteUser = async () => { * Response: { id: number, text: string, message: string } */ const deletePost = async (id) => { - // TODO + const response = await fetch(`${BASE_URL}/posts/${id}`, { + method: "DELETE", + headers: { + Authorization: `Bearer ${getToken()}`, + }, + }); + + if (!response.ok) { + throw new Error( + `Failed to delete post: HTTP ${response.status} ${response.statusText}`, + ); + } + + // Some tests only mock status/ok and do not provide json() + if (response.json) { + return await response.json(); + } }; // ============================================================================