256 lines
8.8 KiB
Markdown
256 lines
8.8 KiB
Markdown
---
|
|
id: 5a8b073d06fa14fcfde687aa
|
|
title: Monitor degli esercizi
|
|
challengeType: 4
|
|
forumTopicId: 301505
|
|
dashedName: exercise-tracker
|
|
---
|
|
|
|
# --description--
|
|
|
|
Costruisci un'app JavaScript full-stack che sia funzionalmente simile a questa: <https://exercise-tracker.freecodecamp.rocks/>. Lavorare su questo progetto ti porterà a scrivere il tuo codice utilizzando uno dei seguenti metodi:
|
|
|
|
- Clonare [questa repository GitHub](https://github.com/freeCodeCamp/boilerplate-project-exercisetracker/) e completa il tuo progetto localmente.
|
|
- Usare [il nostro progetto di avvio Replit](https://replit.com/github/freeCodeCamp/boilerplate-project-exercisetracker) per completare il tuo progetto.
|
|
- Usare un costruttore di siti di tua scelta per completare il progetto. Assicurati di incorporare tutti i file della nostra repository GitHub.
|
|
|
|
Quando hai finito, assicurati che una demo funzionante del tuo progetto sia ospitata pubblicamente da qualche parte. Quindi invia l'URL nel campo `Solution Link`. Facoltativamente, invia anche un link al codice sorgente del tuo progetto nel campo `GitHub Link`.
|
|
|
|
# --hints--
|
|
|
|
Dovresti inviare il tuo progetto, non l'URL di esempio.
|
|
|
|
```js
|
|
(getUserInput) => {
|
|
const url = getUserInput('url');
|
|
assert(
|
|
!/.*\/exercise-tracker\.freecodecamp\.rocks/.test(getUserInput('url'))
|
|
);
|
|
};
|
|
```
|
|
|
|
Puoi fare una richiesta `POST` a `/api/users` con il dato `username` proveniente dal modulo per creare un nuovo utente. La risposta restituita sarà un oggetto con proprietà `username` e `_id`.
|
|
|
|
```js
|
|
async (getUserInput) => {
|
|
const url = getUserInput('url');
|
|
const res = await fetch(url + '/api/users', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
body: `username=fcc_test_${Date.now()}`.substr(0, 29)
|
|
});
|
|
if (res.ok) {
|
|
const { _id, username } = await res.json();
|
|
assert.exists(_id);
|
|
assert.exists(username);
|
|
} else {
|
|
throw new Error(`${res.status} ${res.statusText}`);
|
|
}
|
|
};
|
|
```
|
|
|
|
È possibile fare una richiesta `GET` a `/api/users` per ottenere un array di tutti gli utenti. Ogni elemento nell'array è un oggetto contenente lo `username` e l'`_id` dell'utente.
|
|
|
|
```js
|
|
async (getUserInput) => {
|
|
const url = getUserInput('url');
|
|
const res = await fetch(url + '/api/users');
|
|
if (res.ok) {
|
|
const data = await res.json();
|
|
assert.isArray(data);
|
|
assert.isString(data[0].username);
|
|
assert.isString(data[0]._id);
|
|
} else {
|
|
throw new Error(`${res.status} ${res.statusText}`);
|
|
}
|
|
};
|
|
```
|
|
|
|
È possibile fare una richiesta `POST` a `/api/users/:_id/exercises` con i dati del modulo `description`, `duration` e, facoltativamente `date`. Se non viene fornita alcuna data, verrà utilizzata la data corrente. La risposta restituita sarà l'oggetto utente con l'aggiunta dei campi dell'esercizio.
|
|
|
|
```js
|
|
async (getUserInput) => {
|
|
const url = getUserInput('url');
|
|
const res = await fetch(url + '/api/users', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
body: `username=fcc_test_${Date.now()}`.substr(0, 29)
|
|
});
|
|
if (res.ok) {
|
|
const { _id, username } = await res.json();
|
|
const expected = {
|
|
username,
|
|
description: 'test',
|
|
duration: 60,
|
|
_id,
|
|
date: 'Mon Jan 01 1990'
|
|
};
|
|
const addRes = await fetch(url + `/api/users/${_id}/exercises`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
body: `description=${expected.description}&duration=${expected.duration}&date=1990-01-01`
|
|
});
|
|
if (addRes.ok) {
|
|
const actual = await addRes.json();
|
|
assert.deepEqual(actual, expected);
|
|
} else {
|
|
throw new Error(`${addRes.status} ${addRes.statusText}`);
|
|
}
|
|
} else {
|
|
throw new Error(`${res.status} ${res.statusText}`);
|
|
}
|
|
};
|
|
```
|
|
|
|
È possibile fare una richiesta `GET` a `/api/users/:_id/logs` per recuperare il log completo degli esercizi di qualsiasi utente. La risposta restituita sarà l'oggetto utente con un array `log` di tutti gli esercizi aggiunti. Ogni elemento di log ha le proprietà `description`, `duration` e `date`.
|
|
|
|
```js
|
|
async (getUserInput) => {
|
|
const url = getUserInput('url');
|
|
const res = await fetch(url + '/api/users', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
body: `username=fcc_test_${Date.now()}`.substr(0, 29)
|
|
});
|
|
if (res.ok) {
|
|
const { _id, username } = await res.json();
|
|
const expected = {
|
|
username,
|
|
description: 'test',
|
|
duration: 60,
|
|
_id,
|
|
date: new Date().toDateString()
|
|
};
|
|
const addRes = await fetch(url + `/api/users/${_id}/exercises`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
body: `description=${expected.description}&duration=${expected.duration}`
|
|
});
|
|
if (addRes.ok) {
|
|
const logRes = await fetch(url + `/api/users/${_id}/logs`);
|
|
if (logRes.ok) {
|
|
const { log } = await logRes.json();
|
|
assert.isArray(log);
|
|
assert.equal(1, log.length);
|
|
} else {
|
|
throw new Error(`${logRes.status} ${logRes.statusText}`);
|
|
}
|
|
} else {
|
|
throw new Error(`${addRes.status} ${addRes.statusText}`);
|
|
}
|
|
} else {
|
|
throw new Error(`${res.status} ${res.statusText}`);
|
|
}
|
|
};
|
|
```
|
|
|
|
Una richiesta al log di un utente (`/api/users/:_id/logs`) restituisce un oggetto con una proprietà `count` che rappresenta il numero di esercizi restituiti.
|
|
|
|
```js
|
|
async (getUserInput) => {
|
|
const url = getUserInput('url');
|
|
const res = await fetch(url + '/api/users', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
body: `username=fcc_test_${Date.now()}`.substr(0, 29)
|
|
});
|
|
if (res.ok) {
|
|
const { _id, username } = await res.json();
|
|
const expected = {
|
|
username,
|
|
description: 'test',
|
|
duration: 60,
|
|
_id,
|
|
date: new Date().toDateString()
|
|
};
|
|
const addRes = await fetch(url + `/api/users/${_id}/exercises`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
body: `description=${expected.description}&duration=${expected.duration}`
|
|
});
|
|
if (addRes.ok) {
|
|
const logRes = await fetch(url + `/api/users/${_id}/logs`);
|
|
if (logRes.ok) {
|
|
const { count } = await logRes.json();
|
|
assert(count);
|
|
} else {
|
|
throw new Error(`${logRes.status} ${logRes.statusText}`);
|
|
}
|
|
} else {
|
|
throw new Error(`${addRes.status} ${addRes.statusText}`);
|
|
}
|
|
} else {
|
|
throw new Error(`${res.status} ${res.statusText}`);
|
|
}
|
|
};
|
|
```
|
|
|
|
È possibile aggiungere parametri `from`, `to` e`limit` alla richiesta `/api/users/:_id/logs` per ricevere parte del log di ogni utente. `from` e `to` sono date nel formato `yyyy-mm-dd`. `limit` è un numero intero che indica quanti log devono essere restituiti.
|
|
|
|
```js
|
|
async (getUserInput) => {
|
|
const url = getUserInput('url');
|
|
const res = await fetch(url + '/api/users', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
body: `username=fcc_test_${Date.now()}`.substr(0, 29)
|
|
});
|
|
if (res.ok) {
|
|
const { _id, username } = await res.json();
|
|
const expected = {
|
|
username,
|
|
description: 'test',
|
|
duration: 60,
|
|
_id,
|
|
date: new Date().toDateString()
|
|
};
|
|
const addExerciseRes = await fetch(url + `/api/users/${_id}/exercises`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
body: `description=${expected.description}&duration=${expected.duration}&date=1990-01-01`
|
|
});
|
|
const addExerciseTwoRes = await fetch(url + `/api/users/${_id}/exercises`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
body: `description=${expected.description}&duration=${expected.duration}&date=1990-01-02`
|
|
});
|
|
if (addExerciseRes.ok && addExerciseTwoRes.ok) {
|
|
const logRes = await fetch(
|
|
url + `/api/users/${_id}/logs?from=1989-12-31&to=1990-01-03`
|
|
);
|
|
if (logRes.ok) {
|
|
const { log } = await logRes.json();
|
|
assert.isArray(log);
|
|
assert.equal(2, log.length);
|
|
} else {
|
|
throw new Error(`${logRes.status} ${logRes.statusText}`);
|
|
}
|
|
const limitRes = await fetch(
|
|
url + `/api/users/${_id}/logs?limit=1`
|
|
);
|
|
if (limitRes.ok) {
|
|
const { log } = await limitRes.json();
|
|
assert.isArray(log);
|
|
assert.equal(1, log.length);
|
|
} else {
|
|
throw new Error(`${limitRes.status} ${limitRes.statusText}`);
|
|
}
|
|
} else {
|
|
throw new Error(`${res.status} ${res.statusText}`);
|
|
}
|
|
} else {
|
|
throw new Error(`${res.status} ${res.statusText}`);
|
|
}
|
|
};
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
/**
|
|
Backend challenges don't need solutions,
|
|
because they would need to be tested against a full working project.
|
|
Please check our contributing guidelines to learn more.
|
|
*/
|
|
```
|