diff --git a/Learning-Resources b/Learning-Resources new file mode 160000 index 0000000..e03827a --- /dev/null +++ b/Learning-Resources @@ -0,0 +1 @@ +Subproject commit e03827aeef38e3733a6d5392074a28ad59364799 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..8ba6666 100644 --- a/task-1/cocktail.js +++ b/task-1/cocktail.js @@ -1,14 +1,13 @@ // API documentation: https://www.thecocktaildb.com/api.php -import path from 'path'; +import path from "path"; +import { writeFile } from "fs/promises"; -const BASE_URL = 'https://www.thecocktaildb.com/api/json/v1/1'; - -// Add helper functions as needed here +const BASE_URL = "https://www.thecocktaildb.com/api/json/v1/1"; export async function main() { if (process.argv.length < 3) { - console.error('Please provide a cocktail name as a command line argument.'); + console.error("Please provide a cocktail name as a command line argument."); return; } @@ -19,15 +18,58 @@ export async function main() { const outPath = path.join(__dirname, `./output/${cocktailName}.md`); try { - // 1. Fetch data from the API at the given URL - // 2. Generate markdown content to match the examples - // 3. Write the generated content to a markdown file as given by outPath + const response = await fetch(url, { + method: "GET", + }); + const data = await response.json(); + if (!response.ok) { + const error = new Error(data.error || response.statusText); + error.status = response.status; + throw error; + } + if (data.drinks === null) { + return; + } + const markdown = []; + for (const drink of data.drinks) { + const name = `## ${drink.strDrink}`; + const image = `![${drink.strDrink}](${drink.strDrinkThumb}/medium)`; + const category = `**Category:** ${drink.strCategory}`; + const alcoholic = `**Alcoholic:** ${drink.strAlcoholic}`; + const instructions = `### Instructions\n${drink.strInstructions}`; + const glass = `**Glass:** ${drink.strGlass}`; + let ingredients = "### Ingredients\n"; + for (let i = 1; i <= 15; i++) { + const ingredient = drink["strIngredient" + i]; + const measure = drink["strMeasure" + i]; + if (ingredient) { + ingredients += `- ${measure ? measure : ""}${measure ? " " : ""}${ingredient}\n`; + } + } + const block = + name + + "\n\n" + + image + + "\n\n" + + category + + "\n" + + alcoholic + + "\n\n" + + ingredients + + "\n" + + instructions + + "\n\n" + + glass; + markdown.push(block); + } + const content = markdown.join("\n\n"); + await writeFile(outPath, content); } catch (error) { - // 4. Handle errors + console.error("Failed to get or to write cocktail data:", error.message); } + //return data; } -// Do not change the code below if (!process.env.VITEST) { main(); } diff --git a/task-1/output/margarita.md b/task-1/output/margarita.md new file mode 100644 index 0000000..32bf105 --- /dev/null +++ b/task-1/output/margarita.md @@ -0,0 +1,17 @@ +## Margarita + +![Margarita](https://www.thecocktaildb.com/images/media/drink/5noda61589575158.jpg/medium) + +**Category:** Ordinary Drink +**Alcoholic:** Alcoholic + +### Ingredients +- 1 1/2 oz Tequila +- 1/2 oz Triple sec +- 1 oz Lime juice +- Salt + +### Instructions +Rub the rim of the glass. + +**Glass:** 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..dfa03e5 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,18 @@ const getHello = async () => { * Response: { user: string } */ const getMe = async () => { - // TODO + const response = await fetch(`${BASE_URL}/users/me`, { + method: "Get", + headers: { + Authorization: `Bearer ${getToken()}`, + }, + }); + if (!response.ok) { + throw new Error( + `Failed to get user information: HTTP ${response.status} ${response.statusText}`, + ); + } + return await response.json(); }; // ============================================================================ @@ -68,15 +79,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 +100,17 @@ 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 user: HTTP ${response.status} ${response.statusText}`, + ); + } + return await response.json(); }; // ============================================================================ @@ -103,7 +124,20 @@ 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 a post: HTTP ${response.status} ${response.statusText}`, + ); + } + return await response.json(); }; /** @@ -112,7 +146,18 @@ const createPost = async (text) => { * Response: Array of { id, text, user } */ const getPosts = async () => { - // TODO + const response = await fetch(`${BASE_URL}/posts/me`, { + method: "Get", + headers: { + Authorization: `Bearer ${getToken()}`, + }, + }); + if (!response.ok) { + throw new Error( + `Failed to get a post: HTTP ${response.status} ${response.statusText}`, + ); + } + return await response.json(); }; /** @@ -122,7 +167,20 @@ 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 a post: HTTP ${response.status} ${response.statusText}`, + ); + } + return await response.json(); }; /** @@ -131,7 +189,18 @@ 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 a user: HTTP ${response.status} ${response.statusText}`, + ); + } + return await response.json(); }; /** @@ -140,7 +209,18 @@ 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 a post: HTTP ${response.status} ${response.statusText}`, + ); + } + return await response.json(); }; // ============================================================================