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: 1 addition & 0 deletions Learning-Resources
Submodule Learning-Resources added at e03827
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: 52 additions & 10 deletions task-1/cocktail.js
Original file line number Diff line number Diff line change
@@ -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;
}

Expand All @@ -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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Here should be error handling

if (data.drinks === null) {
    console.error("No cocktails found with that name.");
    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}`;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The colon should be outside the bold marker
const category = Category: ${drink.strCategory};
const alcoholic = Alcoholic: ${drink.strAlcoholic};
and there should be a blank line between them

const alcoholic = `**Alcoholic:** ${drink.strAlcoholic}`;
const instructions = `### Instructions\n${drink.strInstructions}`;
const glass = `**Glass:** ${drink.strGlass}`;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This should be serve in
const glass = Serve in: ${drink.strGlass};

let ingredients = "### 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.

Lists in markdown should have blank lines before and after, so another \n after ingredients

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");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The file should start with #Coctail Recipes at the first line
const content = "# Cocktail Recipes\n\n" + 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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

the commented-out code should be deleted

}

// Do not change the code below
if (!process.env.VITEST) {
main();
}
17 changes: 17 additions & 0 deletions task-1/output/margarita.md
Original file line number Diff line number Diff line change
@@ -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
104 changes: 92 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,18 @@ const getHello = async () => {
* Response: { user: string }
*/
const getMe = async () => {
// TODO
const response = await fetch(`${BASE_URL}/users/me`, {
method: "Get",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Get works, but the convention is 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();
};

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

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

/**
Expand All @@ -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",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Get works, but the convention is 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();
};

/**
Expand All @@ -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`, {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

the id should be added as a variable : ${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();
};

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

/**
Expand All @@ -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 `, {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You should add the parameter as a variable to the url: ${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();
};

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