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
1 change: 0 additions & 1 deletion package-lock.json

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

62 changes: 62 additions & 0 deletions task-1/cocktail.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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}`);
}
}

Expand Down
22 changes: 22 additions & 0 deletions task-1/output/margarita.md
Original file line number Diff line number Diff line change
@@ -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
124 changes: 112 additions & 12 deletions task-2/post-cli/src/services.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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();
};

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

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

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

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

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

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

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