Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 48 additions & 7 deletions task-1/cocktail.js
Original file line number Diff line number Diff line change
@@ -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`;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under the Ingredients there is a list, and lists should be surrounded in blank lines. This means that another new line will solve the problem markdown += Ingredients:\n\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) {
Expand All @@ -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();
}
}
3 changes: 1 addition & 2 deletions task-2/post-cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 116 additions & 8 deletions task-2/post-cli/src/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};

// ============================================================================
Expand Down Expand Up @@ -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
Expand All @@ -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();
};

/**
Expand All @@ -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();
};

/**
Expand All @@ -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();
};

/**
Expand All @@ -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();
};

/**
Expand All @@ -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();
};

// ============================================================================
Expand Down