298 lines
9.5 KiB
Markdown
298 lines
9.5 KiB
Markdown
---
|
|
id: 5a8b073d06fa14fcfde687aa
|
|
title: Registrador de exercícios
|
|
challengeType: 4
|
|
forumTopicId: 301505
|
|
dashedName: exercise-tracker
|
|
---
|
|
|
|
# --description--
|
|
|
|
Crie um aplicativo full stack em JavaScript que seja funcionalmente semelhante a este: <https://exercise-tracker.freecodecamp.rocks/>. Trabalhar nesse projeto vai fazer com que você escreva seu código usando um dos seguintes métodos:
|
|
|
|
- Clone [este repositório do GitHub](https://github.com/freeCodeCamp/boilerplate-project-exercisetracker/) e complete o projeto localmente.
|
|
- Use [nosso projeto inicial do Replit](https://replit.com/github/freeCodeCamp/boilerplate-project-exercisetracker) para completar o projeto.
|
|
- Use um construtor de site de sua escolha para completar o projeto. Certifique-se de incorporar todos os arquivos do nosso repositório no GitHub.
|
|
|
|
Quando terminar, certifique-se de que uma demonstração funcional do seu projeto está hospedada em algum lugar público. Em seguida, envie o URL para ela no campo `Solution Link`. Como opção, envie também um link para o código-fonte do projeto no campo `GitHub Link`.
|
|
|
|
# --instructions--
|
|
|
|
Suas respostas devem ter as seguintes estruturas.
|
|
|
|
Exercício:
|
|
|
|
```js
|
|
{
|
|
username: "fcc_test"
|
|
description: "test",
|
|
duration: 60,
|
|
date: "Mon Jan 01 1990",
|
|
_id: "5fb5853f734231456ccb3b05"
|
|
}
|
|
```
|
|
|
|
Usuário:
|
|
|
|
```js
|
|
{
|
|
username: "fcc_test",
|
|
_id: "5fb5853f734231456ccb3b05"
|
|
}
|
|
```
|
|
|
|
Log:
|
|
|
|
```js
|
|
{
|
|
username: "fcc_test",
|
|
count: 1,
|
|
_id: "5fd01a41c5b5cf05d080502f",
|
|
log: [{
|
|
description: "test",
|
|
duration: 60,
|
|
date: "Mon Jan 01 1990",
|
|
}]
|
|
}
|
|
```
|
|
|
|
**Dica:** para a propriedade `date`, o método `toDateString` da API `Date` pode ser usado para conseguir o resultado esperado.
|
|
|
|
# --hints--
|
|
|
|
Você deve fornecer seu próprio projeto, não o exemplo de URL.
|
|
|
|
```js
|
|
(getUserInput) => {
|
|
const url = getUserInput('url');
|
|
assert(
|
|
!/.*\/exercise-tracker\.freecodecamp\.rocks/.test(getUserInput('url'))
|
|
);
|
|
};
|
|
```
|
|
|
|
Você pode fazer a solicitação de `POST` para `/api/users` com dados do formulário `username` para criar um novo usuário. A resposta retornada será um objeto com as propriedades `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}`);
|
|
}
|
|
};
|
|
```
|
|
|
|
Você pode fazer uma solicitação de `GET` a `/api/users` para obter um array de todos os usuários. Cada elemento no array é um objeto que contém `username` e `_id` dos usuários.
|
|
|
|
```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}`);
|
|
}
|
|
};
|
|
```
|
|
|
|
Você pode fazer a solicitação de `POST` para `/api/users/:_id/exercises` com os dados do formulário `description`, `duration` e, como opção, `date`. Se nenhuma data for fornecida, a data atual será utilizada. A resposta retornada será o objeto do usuário com os campos de exercício adicionados.
|
|
|
|
```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}`);
|
|
}
|
|
};
|
|
```
|
|
|
|
Você pode fazer uma solicitação de `GET` a `/api/users/:_id/logs` para obter um registro completo dos exercícios de qualquer usuário. A resposta retornada será o objeto de usuário com um array de `log` de todos os exercícios adicionados. Cada item do registro (log) terá as propriedades `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}`);
|
|
}
|
|
};
|
|
```
|
|
|
|
Uma solicitação para o registro (log) de um usuário (`/api/users/:_id/logs`) retornará um objeto com uma propriedade `count`, que representa o número de exercícios retornados.
|
|
|
|
```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}`);
|
|
}
|
|
};
|
|
```
|
|
|
|
Você pode adicionar os parâmetros `from`, `to` e `limit` a uma solicitação para `/api/users/:_id/logs` para recuperar parte do registro de qualquer usuário. `from` e `to` são datas no formato `yyyy-mm-dd`. `limit` é um número inteiro de quantos registros devem ser enviados de volta.
|
|
|
|
```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.
|
|
*/
|
|
```
|