Skip to content
Open

Muna #20

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
216 changes: 108 additions & 108 deletions package-lock.json

Large diffs are not rendered by default.

15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "core-assignment-week-9",
"version": "1.0.0",
"description": "",
"description": "Install dependencies (run this once from the repository root):",
"main": "index.js",
"scripts": {
"start": "node task-1/server/server.js",
Expand All @@ -21,5 +21,16 @@
"chalk": "^5.6.2",
"express": "^5.2.1",
"morgan": "^1.10.1"
}
},
"directories": {
"test": "tests"
},
"repository": {
"type": "git",
"url": "git+https://github.com/HackYourAssignment/c55-core-week-9.git"
},
"bugs": {
"url": "https://github.com/HackYourAssignment/c55-core-week-9/issues"
},
"homepage": "https://github.com/HackYourAssignment/c55-core-week-9#readme"
}
5 changes: 5 additions & 0 deletions task-1/delete.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

USER_ID=11

curl -X DELETE http://localhost:3000/users/$USER_ID \
-H "Content-Type: application/json"
5 changes: 5 additions & 0 deletions task-1/get.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

USER_ID=11

curl -X GET http://localhost:3000/users/$USER_ID \
-H "Content-Type: application/json"
10 changes: 10 additions & 0 deletions task-1/patch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@



USER_ID=11

curl -X PATCH http://localhost:3000/users/$USER_ID \
-H "Content-Type: application/json" \
-d '{
"email": "johndoe@example.com"
}'
12 changes: 12 additions & 0 deletions task-1/post.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

# add new user
curl -X POST http://localhost:3000/users \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"email": "john.doe@example.com",
"password": "secret123",
"role": "user",
"active": true,
"department": "Engineering"
}'
45 changes: 19 additions & 26 deletions task-2/fetcher.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
/**
* Fetch data from a given URL and handle success and error via callbacks.
* @param {string} url The URL to fetch data from
* @param {Function} onSuccess Called with data for successful fetch
* @param {Function} onError Called with an Error object for fetch errors
*/
export function fetchData(url, onSuccess, onError) {
fetch(url)
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then((data) => {
if (onSuccess) {
console.log(data);
onSuccess(data);
}
})
.catch((error) => {
if (onError) {
console.log('Error fetching data:', error);
onError(error);
}
});


export async function fetchNobelPrizes({ year = 'all', category = 'all', offset = 0, limit = 20, sort = 'desc' } = {}) {
const url = new URL('https://api.nobelprize.org/2.1/nobelPrizes');

if (year !== 'all') url.searchParams.set('nobelPrizeYear', year);
if (category !== 'all') url.searchParams.set('nobelPrizeCategory', category);

url.searchParams.set('offset', offset);
url.searchParams.set('limit', limit);
url.searchParams.set('sort', sort);

const response = await fetch(url.toString());
return response.json();
}




// Temporary change to activate PR
19 changes: 15 additions & 4 deletions task-2/services.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Nobel Prize API Documentation: https://www.nobelprize.org/about/developer-zone-2/

import { fetchData } from './fetcher.js';

const API_BASE_URL = 'https://api.nobelprize.org/2.1';

/**
Expand All @@ -13,9 +12,21 @@ const API_BASE_URL = 'https://api.nobelprize.org/2.1';
* @param {number} filters.limit - Number of results per page (default: 10)
* @param {Function} onSuccess - Callback for successful fetch
* @param {Function} onError - Callback for fetch errors
*
*/

import { fetchNobelPrizes as fetcher } from './fetcher.js';

/**
* Wrapper function to use callbacks with fetchNobelPrizes
* @param {Object} filters - Filtering options
* @param {Function} onSuccess - Success callback
* @param {Function} onError - Error callback
*/
export function fetchNobelPrizes(filters = {}, onSuccess, onError) {
let url = ''; // TODO Construct the full URL with query parameters;

fetchData(url, onSuccess, onError);

export function fetchNobelPrizes(filters = {}, onSuccess, onError) {
fetcher(filters)
.then(onSuccess)
.catch(onError);
}
11 changes: 11 additions & 0 deletions task-2/testService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { fetchNobelPrizes } from './services.js';

fetchNobelPrizes({},
data => console.log('All Prizes:', data),
error => console.error('Error:', error)
);

fetchNobelPrizes({ year: '2020', category: 'phy' },
data => console.log('Physics Prizes 2020:', data),
error => console.error('Error:', error)
);
8 changes: 5 additions & 3 deletions task-2/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ export default class UI {
* Populate year select dropdown with years from 1901 to current year
*/
populateYearSelect() {
const currentYear = new Date().getFullYear();
const startYear = 1901;

const currentYear = new Date().getFullYear();
const startYear = 1901;

// Add 'All' option first
const allOption = document.createElement('option');
Expand Down Expand Up @@ -108,7 +109,8 @@ export default class UI {

// Get filter values
const category = this.dom.categorySelect.value;
const year = this.dom.yearSelect.value;
const year = this.dom.yearSelect.value;


// Update state
this.state.filters.category = category;
Expand Down