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
207 changes: 103 additions & 104 deletions package-lock.json

Large diffs are not rendered by default.

75 changes: 72 additions & 3 deletions task-1/cocktail.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// API documentation: https://www.thecocktaildb.com/api.php

import path from 'path';
import { error } from "console";
import path from "path";
import { writeFile } from "fs/promises";

const BASE_URL = 'https://www.thecocktaildb.com/api/json/v1/1';
const BASE_URL = "https://www.thecocktaildb.com/api/json/v1/1";

// Add helper functions as needed here

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 @@ -20,10 +22,77 @@ export async function main() {

try {
// 1. Fetch data from the API at the given URL
const response = await fetch(
`https://www.thecocktaildb.com/api/json/v1/1/search.php?s=${cocktailName}`,
);

const data = await response.json();

// 2. Generate markdown content to match the examples

let markdown = `# Cocktail Recipes\n\n`;

if (!data.drinks || data.drinks.length === 0) {
throw new Error("No cocktails found with that name.");
} else {
for (const drink of data.drinks) {
markdown += `## ${drink.strDrink}\n\n`;

// Use strDrinkThumb medium
if (drink.strDrinkThumb) {
const thumbUrl = `${drink.strDrinkThumb}/medium`;
markdown += `![${drink.strDrink}](${thumbUrl})\n\n`;
}

// Category & Alcoholic
markdown += `**Category**: ${drink.strCategory || "Unknown"}\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.

Here is missing an \n


let alcoholicText = "—";
if (drink.strAlcoholic === "Alcoholic") {
alcoholicText = "Yes";
} else if (
drink.strAlcoholic === "Non alcoholic" ||
drink.strAlcoholic === "Non-Alcoholic"
) {
alcoholicText = "No";
}
markdown += `**Alcoholic**: ${alcoholicText}\n\n`;

// Ingredients
markdown += `### Ingredients\n\n`;
let ingredientCount = 0;

for (let i = 1; i <= 15; i++) {
const ingredient = drink[`strIngredient${i}`];
if (ingredient && ingredient.trim()) {
ingredientCount++;
const measure = drink[`strMeasure${i}`]?.trim() || "";
const line = measure ? `${measure} ${ingredient}` : ingredient;
markdown += `- ${line}\n`;
}
}

if (ingredientCount === 0) {
markdown += `- No ingredients listed\n`;
}
markdown += `\n`;

// Instructions
markdown += `### Instructions\n${drink.strInstructions || "No instructions provided."}\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.

here is missing \n


// Glass
markdown += `Serve in: ${drink.strGlass || "Unknown"}\n\n`;

markdown += `---\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.

Bravo, I like that you introduced a line separator between recipes, this line could be even longer to make sure the user knows that this is the end.

}
}

// 3. Write the generated content to a markdown file as given by outPath
await writeFile(outPath, markdown, "utf-8");
console.log(`Successfully wrote to ${outPath}`);
} catch (error) {
// 4. Handle errors
console.error(error.message);
}
}

Expand Down
23 changes: 23 additions & 0 deletions task-1/output/margarita.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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

---

3 changes: 3 additions & 0 deletions task-1/output/nonexistentcocktail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Cocktail Recipes

No cocktails found for "nonexistentcocktail".
110 changes: 105 additions & 5 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 @@ -54,6 +54,22 @@ const getHello = async () => {
*/
const getMe = async () => {
// TODO
const response = await fetch(`${BASE_URL}/users/me`, {
/**
*
*/
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${getToken()}`,
},
});
if (!response.ok) {
throw new Error(
`Failed to get user: HTTP ${response.status} ${response.statusText}`,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

see my main reviewed comment please about this line

);
}
return await response.json();
};

// ============================================================================
Expand All @@ -68,15 +84,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 @@ -90,6 +106,19 @@ const createUser = async (name, password) => {
*/
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 create user: HTTP ${response.status} ${response.statusText}`,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

see my main reviewed comment please about this line

);
}
return await response.json();
};

// ============================================================================
Expand All @@ -104,6 +133,20 @@ const loginUser = async (name, password) => {
*/
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 get user: HTTP ${response.status} ${response.statusText}`,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

see my main reviewed comment please about this line

);

return await response.json();
};

/**
Expand All @@ -113,6 +156,22 @@ const createPost = async (text) => {
*/
const getPosts = async () => {
// TODO
const response = await fetch(`${BASE_URL}/posts/me`, {
/**
*
*/
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${getToken()}`,
},
});
if (!response.ok) {
throw new Error(
`Failed to get user: HTTP ${response.status} ${response.statusText}`,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

see my main reviewed comment please about this line

);
}
return await response.json();
};

/**
Expand All @@ -123,6 +182,21 @@ const getPosts = async () => {
*/
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 get user: HTTP ${response.status} ${response.statusText}`,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

see my main reviewed comment please about this line

);
}
return await response.json();
};

/**
Expand All @@ -132,6 +206,19 @@ const updatePost = async (id, text) => {
*/
const deleteUser = async () => {
// TODO
const response = await fetch(`${BASE_URL}/users/me`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${getToken()}`,
},
});
if (!response.ok) {
throw new Error(
`Failed to get user: HTTP ${response.status} ${response.statusText}`,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

see my main reviewed comment please about this line

);
}
return await response.json();
};

/**
Expand All @@ -141,6 +228,19 @@ const deleteUser = async () => {
*/
const deletePost = async (id) => {
// TODO
const response = await fetch(`${BASE_URL}/posts/${id}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${getToken()}`,
},
});
if (!response.ok) {
throw new Error(
`Failed to delete post: HTTP ${response.status} ${response.statusText}`,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

see my main reviewed comment please about this line

);
}
return await response.json();
};

// ============================================================================
Expand Down
6 changes: 5 additions & 1 deletion task-2/post-cli/tests/test-crud.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ describe('Complete CRUD Operations', () => {
fetchMock.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({ id: 1, text: 'Updated post text!', message: 'Post deleted' }),
});

// DELETE: Remove the post
Expand All @@ -170,6 +171,7 @@ describe('Complete CRUD Operations', () => {
fetchMock.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({ user: 'Alice', message: 'User deleted' }),
});

// DELETE: Remove the user
Expand Down Expand Up @@ -218,6 +220,7 @@ describe('Complete CRUD Operations', () => {
fetchMock.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({ user: 'mock-user', message: 'User deleted' }),
});

await deleteUser();
Expand All @@ -240,6 +243,7 @@ describe('Complete CRUD Operations', () => {
fetchMock.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({ id: 5, text: 'some text', message: 'Post deleted' }),
});

await deletePost(5);
Expand Down Expand Up @@ -292,4 +296,4 @@ describe('Complete CRUD Operations', () => {
// Verify any function throws error on API failure
await expect(createUser('Test', 'pass')).rejects.toThrow();
});
});
});