diff --git a/task-1/cocktail.js b/task-1/cocktail.js index 6fd5a62..62e6027 100644 --- a/task-1/cocktail.js +++ b/task-1/cocktail.js @@ -1,10 +1,39 @@ // API documentation: https://www.thecocktaildb.com/api.php import path from 'path'; +import fs from 'fs/promises'; const BASE_URL = 'https://www.thecocktaildb.com/api/json/v1/1'; -// Add helper functions as needed here +function generateMarkdown(drinks) { + let markdown = ''; + + for (const drink of drinks) { + markdown += `## ${drink.strDrink}\n\n`; + + markdown += `![${drink.strDrink}](${drink.strDrinkThumb}/medium)\n\n`; + + markdown += `**Category:** ${drink.strCategory}\n\n`; + markdown += `**Alcoholic:** ${drink.strAlcoholic}\n\n`; + + markdown += `**Ingredients:**\n`; + + for (let i = 1; i <= 15; i++) { + const ingredient = drink[`strIngredient${i}`]; + const measure = drink[`strMeasure${i}`]; + + if (!ingredient) break; + + markdown += `- ${measure || ''} ${ingredient}\n`; + } + + markdown += `\n**Instructions:** ${drink.strInstructions}\n\n`; + markdown += `**Glass:** ${drink.strGlass}\n\n`; + markdown += `---\n\n`; + } + + return markdown; +} export async function main() { if (process.argv.length < 3) { @@ -19,15 +48,27 @@ 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); + + if (!response.ok) { + throw new Error('Failed to fetch data from API'); + } + + const data = await response.json(); + + if (!data.drinks) { + throw new Error('No cocktails found'); + } + + const markdown = generateMarkdown(data.drinks); + + await fs.writeFile(outPath, markdown); + } catch (error) { - // 4. Handle errors + console.error('Error:', error.message); } } -// Do not change the code below if (!process.env.VITEST) { main(); -} +} \ No newline at end of file diff --git a/task-2/post-cli/package-lock.json b/task-2/post-cli/package-lock.json index 8c03ed4..776be27 100644 --- a/task-2/post-cli/package-lock.json +++ b/task-2/post-cli/package-lock.json @@ -7,8 +7,7 @@ "": { "name": "post-central-cli", "version": "1.0.0", - "hasInstallScript": true, - "license": "ISC", + "license": "CC BY-NC-SA 4.0", "dependencies": { "@inquirer/prompts": "^7.0.0", "chalk": "^5.6.2", diff --git a/task-2/post-cli/src/services.js b/task-2/post-cli/src/services.js index 2601f57..b02af8e 100644 --- a/task-2/post-cli/src/services.js +++ b/task-2/post-cli/src/services.js @@ -53,7 +53,22 @@ const getHello = async () => { * Response: { user: string } */ const getMe = async () => { - // TODO + const token = getToken(); + + const response = await fetch(`${BASE_URL}/users/me`, { + method: 'GET', + headers: { + Authorization: `Bearer ${token}`, + }, + }); + + if (!response.ok) { + throw new Error( + `Failed to get user: HTTP ${response.status} ${response.statusText}` + ); + } + + return await response.json(); }; // ============================================================================ @@ -89,8 +104,22 @@ 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(); +}; // ============================================================================ // STAGE 3: More CRUD Operations @@ -103,7 +132,24 @@ const loginUser = async (name, password) => { * Response: { id: number, text: string, user: string } */ const createPost = async (text) => { - // TODO + const token = getToken(); + + const response = await fetch(`${BASE_URL}/posts`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}`, + }, + 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 +158,22 @@ const createPost = async (text) => { * Response: Array of { id, text, user } */ const getPosts = async () => { - // TODO + const token = getToken(); + + const response = await fetch(`${BASE_URL}/posts/me`, { + method: 'GET', + headers: { + Authorization: `Bearer ${token}`, + }, + }); + + if (!response.ok) { + throw new Error( + `Failed to get posts: HTTP ${response.status} ${response.statusText}` + ); + } + + return await response.json(); }; /** @@ -122,7 +183,24 @@ const getPosts = async () => { * Response: { id: number, text: string } */ const updatePost = async (id, text) => { - // TODO + const token = getToken(); + + const response = await fetch(`${BASE_URL}/posts/${id}`, { + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}`, + }, + 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 +209,22 @@ const updatePost = async (id, text) => { * Response: { user: string, message: string } */ const deleteUser = async () => { - // TODO + const token = getToken(); + + const response = await fetch(`${BASE_URL}/users/me`, { + method: 'DELETE', + headers: { + Authorization: `Bearer ${token}`, + }, + }); + + if (!response.ok) { + throw new Error( + `Failed to delete user: HTTP ${response.status} ${response.statusText}` + ); + } + + return await response.json(); }; /** @@ -140,7 +233,22 @@ const deleteUser = async () => { * Response: { id: number, text: string, message: string } */ const deletePost = async (id) => { - // TODO + const token = getToken(); + + const response = await fetch(`${BASE_URL}/posts/${id}`, { + method: 'DELETE', + headers: { + Authorization: `Bearer ${token}`, + }, + }); + + if (!response.ok) { + throw new Error( + `Failed to delete post: HTTP ${response.status} ${response.statusText}` + ); + } + + return await response.json(); }; // ============================================================================