2019-05-06 07:31:26 -04:00
|
|
|
|
---
|
|
|
|
|
id: 5a8b073d06fa14fcfde687aa
|
2021-09-18 11:22:53 -07:00
|
|
|
|
title: 运动追踪器
|
2019-05-06 07:31:26 -04:00
|
|
|
|
challengeType: 4
|
2020-09-18 00:11:18 +08:00
|
|
|
|
forumTopicId: 301505
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: exercise-tracker
|
2019-05-06 07:31:26 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
|
构建一个 JavaScript 的全栈应用,在功能上与这个应用相似: <https://exercise-tracker.freecodecamp.rocks/>。 在这个项目中,你将使用以下方法之一编写你的代码:
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
2021-02-16 15:21:30 -07:00
|
|
|
|
- 克隆 [GitHub 仓库](https://github.com/freeCodeCamp/boilerplate-project-exercisetracker/) 并在本地完成你的项目。
|
2021-05-12 21:25:58 +05:30
|
|
|
|
- 使用[我们的 Replit 初始化项目](https://replit.com/github/freeCodeCamp/boilerplate-project-exercisetracker)来完成你的项目。
|
2021-09-18 11:22:53 -07:00
|
|
|
|
- 使用你选择的网站生成器来完成项目。 需要包含我们 GitHub 仓库的所有文件。
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
|
完成本项目后,请将一个正常运行的 demo(项目演示)托管在可以公开访问的平台。 然后在 `Solution Link` 字段中提交它的 URL。 此外,还可以将项目的源码提交到 `GitHub Link` 中。
|
|
|
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
|
|
|
|
|
你的答案应该有以下结构。
|
|
|
|
|
|
|
|
|
|
运动:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
{
|
2022-03-04 19:46:29 +05:30
|
|
|
|
username: "fcc_test",
|
2021-09-18 11:22:53 -07:00
|
|
|
|
description: "test",
|
|
|
|
|
duration: 60,
|
|
|
|
|
date: "Mon Jan 01 1990",
|
|
|
|
|
_id: "5fb5853f734231456ccb3b05"
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
用户:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
{
|
|
|
|
|
username: "fcc_test",
|
|
|
|
|
_id: "5fb5853f734231456ccb3b05"
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
日志:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
{
|
|
|
|
|
username: "fcc_test",
|
|
|
|
|
count: 1,
|
|
|
|
|
_id: "5fb5853f734231456ccb3b05",
|
|
|
|
|
log: [{
|
|
|
|
|
description: "test",
|
|
|
|
|
duration: 60,
|
|
|
|
|
date: "Mon Jan 01 1990",
|
|
|
|
|
}]
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**提示:** 对于 `date` 属性,`Date` API 的 `toDateString` 方法可以用于实现预期的输出。
|
2019-05-06 07:31:26 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
|
|
|
|
|
2021-02-16 13:01:20 -08:00
|
|
|
|
提交自己的项目,而不是示例的 URL。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
(getUserInput) => {
|
|
|
|
|
const url = getUserInput('url');
|
|
|
|
|
assert(
|
2021-02-06 04:42:36 +00:00
|
|
|
|
!/.*\/exercise-tracker\.freecodecamp\.rocks/.test(getUserInput('url'))
|
2020-12-16 00:37:30 -07:00
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
|
可以将表单里的 `username` 通过 `POST` 请求发送到 `/api/users`,以创建一个新的用户。
|
|
|
|
|
|
|
|
|
|
```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)
|
|
|
|
|
});
|
|
|
|
|
assert.isTrue(res.ok);
|
|
|
|
|
if(!res.ok) {
|
|
|
|
|
throw new Error(`${res.status} ${res.statusText}`)
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
`POST /api/users` 带有表单数据 `username` 对请求,返回的响应将是一个具有 `username` 和 `_id` 属性的对象.
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
async (getUserInput) => {
|
|
|
|
|
const url = getUserInput('url');
|
2021-05-12 21:25:58 +05:30
|
|
|
|
const res = await fetch(url + '/api/users', {
|
2020-12-16 00:37:30 -07:00
|
|
|
|
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}`);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
|
你可以向 `/api/users` 发出 `GET` 请求以获取所有用户的列表。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
2021-09-18 11:22:53 -07:00
|
|
|
|
async(getUserInput) => {
|
2020-12-16 00:37:30 -07:00
|
|
|
|
const url = getUserInput('url');
|
2021-05-12 21:25:58 +05:30
|
|
|
|
const res = await fetch(url + '/api/users');
|
2021-09-18 11:22:53 -07:00
|
|
|
|
assert.isTrue(res.ok);
|
|
|
|
|
if(!res.ok) {
|
|
|
|
|
throw new Error(`${res.status} ${res.statusText}`)
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
对 `/api/users` 的 `GET` 请求返回一个数组。
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
async(getUserInput) => {
|
|
|
|
|
const url = getUserInput('url');
|
|
|
|
|
const res = await fetch(url + '/api/users');
|
|
|
|
|
if(res.ok){
|
|
|
|
|
const users = await res.json();
|
|
|
|
|
assert.isArray(users);
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`${res.status} ${res.statusText}`);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
从 `GET /api/users` 返回的数组中的每个元素都是一个对象字面量,包含用户的 `username` 和 `_id`。
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
async(getUserInput) => {
|
|
|
|
|
const url = getUserInput('url');
|
|
|
|
|
const res = await fetch(url + '/api/users');
|
|
|
|
|
if(res.ok){
|
|
|
|
|
const users = await res.json();
|
|
|
|
|
const user = users[0];
|
|
|
|
|
assert.exists(user);
|
|
|
|
|
assert.exists(user.username);
|
|
|
|
|
assert.exists(user._id);
|
|
|
|
|
assert.isString(user.username);
|
|
|
|
|
assert.isString(user._id);
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`${res.status} ${res.statusText}`);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
你能用表单里的 `description`、`duration` 和 `date`(可选)发送 `POST` 请求到 `/api/users/:_id/exercises`。 如果没有传入 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)
|
|
|
|
|
});
|
2020-12-16 00:37:30 -07:00
|
|
|
|
if (res.ok) {
|
2021-09-18 11:22:53 -07:00
|
|
|
|
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`
|
|
|
|
|
});
|
|
|
|
|
assert.isTrue(addRes.ok);
|
|
|
|
|
if(!addRes.ok) {
|
|
|
|
|
throw new Error(`${addRes.status} ${addRes.statusText}`)
|
|
|
|
|
};
|
2020-12-16 00:37:30 -07:00
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`${res.status} ${res.statusText}`);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
|
从 `POST /api/users/:_id/exercises` 返回的响应将是添加了运动字段的用户对象。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
async (getUserInput) => {
|
|
|
|
|
const url = getUserInput('url');
|
2021-05-12 21:25:58 +05:30
|
|
|
|
const res = await fetch(url + '/api/users', {
|
2020-12-16 00:37:30 -07:00
|
|
|
|
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'
|
|
|
|
|
};
|
2021-05-12 21:25:58 +05:30
|
|
|
|
const addRes = await fetch(url + `/api/users/${_id}/exercises`, {
|
2020-12-16 00:37:30 -07:00
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
2021-05-12 21:25:58 +05:30
|
|
|
|
body: `description=${expected.description}&duration=${expected.duration}&date=1990-01-01`
|
2020-12-16 00:37:30 -07:00
|
|
|
|
});
|
|
|
|
|
if (addRes.ok) {
|
|
|
|
|
const actual = await addRes.json();
|
|
|
|
|
assert.deepEqual(actual, expected);
|
2021-09-18 11:22:53 -07:00
|
|
|
|
assert.isString(actual.description);
|
|
|
|
|
assert.isNumber(actual.duration);
|
|
|
|
|
assert.isString(actual.date);
|
2020-12-16 00:37:30 -07:00
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`${addRes.status} ${addRes.statusText}`);
|
2020-09-18 00:11:18 +08:00
|
|
|
|
}
|
2020-12-16 00:37:30 -07:00
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`${res.status} ${res.statusText}`);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
|
可以发送 `GET` 请求到 `/api/users/:_id/logs`,以获取任何用户的完整 exercise 日志。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
async (getUserInput) => {
|
|
|
|
|
const url = getUserInput('url');
|
2021-05-12 21:25:58 +05:30
|
|
|
|
const res = await fetch(url + '/api/users', {
|
2020-12-16 00:37:30 -07:00
|
|
|
|
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()
|
|
|
|
|
};
|
2021-05-12 21:25:58 +05:30
|
|
|
|
const addRes = await fetch(url + `/api/users/${_id}/exercises`, {
|
2020-12-16 00:37:30 -07:00
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
2021-05-12 21:25:58 +05:30
|
|
|
|
body: `description=${expected.description}&duration=${expected.duration}`
|
2020-12-16 00:37:30 -07:00
|
|
|
|
});
|
|
|
|
|
if (addRes.ok) {
|
2021-05-12 21:25:58 +05:30
|
|
|
|
const logRes = await fetch(url + `/api/users/${_id}/logs`);
|
2021-09-18 11:22:53 -07:00
|
|
|
|
assert.isTrue(logRes.ok);
|
|
|
|
|
if(!logRes.ok) {
|
|
|
|
|
throw new Error(`${logRes.status} ${logRes.statusText}`)
|
|
|
|
|
};
|
2020-12-16 00:37:30 -07:00
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`${addRes.status} ${addRes.statusText}`);
|
2020-09-18 00:11:18 +08:00
|
|
|
|
}
|
2020-12-16 00:37:30 -07:00
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`${res.status} ${res.statusText}`);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
|
对用户日志的请求 `GET /api/users/:_id/logs` 返回一个用户对象,该对象具有一个 `count` 属性,表示属于该用户的运动次数。
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
async (getUserInput) => {
|
|
|
|
|
const url = getUserInput('url');
|
2021-05-12 21:25:58 +05:30
|
|
|
|
const res = await fetch(url + '/api/users', {
|
2021-02-06 04:42:36 +00:00
|
|
|
|
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()
|
|
|
|
|
};
|
2021-05-12 21:25:58 +05:30
|
|
|
|
const addRes = await fetch(url + `/api/users/${_id}/exercises`, {
|
2021-02-06 04:42:36 +00:00
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
2021-05-12 21:25:58 +05:30
|
|
|
|
body: `description=${expected.description}&duration=${expected.duration}`
|
2021-02-06 04:42:36 +00:00
|
|
|
|
});
|
|
|
|
|
if (addRes.ok) {
|
2021-05-12 21:25:58 +05:30
|
|
|
|
const logRes = await fetch(url + `/api/users/${_id}/logs`);
|
2021-02-06 04:42:36 +00:00
|
|
|
|
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}`);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
|
对 `/api/users/:id/logs` 的 `GET` 请求将返回用户对象,其中包含添加的所有练习的 `log` 数组。
|
|
|
|
|
|
|
|
|
|
```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}`)
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
从 `GET /api/users/:id/logs` 返回的 `log` 数组中的每一项都是一个应该具有 `description` 的对象, `duration` 和 `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();
|
|
|
|
|
const exercise = log[0];
|
|
|
|
|
assert.exists(exercise);
|
|
|
|
|
assert.exists(exercise.description);
|
|
|
|
|
assert.exists(exercise.duration);
|
|
|
|
|
assert.exists(exercise.date);
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`${logRes.status} ${logRes.statusText}`);
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`${addRes.status} ${addRes.statusText}`);
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`${res.status} ${res.statusText}`)
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
从 `GET /api/users/:id/logs` 返回的 `log` 数组中任何对象的 `description` 属性都应该是一个字符串。
|
|
|
|
|
|
|
|
|
|
```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();
|
|
|
|
|
const exercise = log[0];
|
|
|
|
|
assert.isString(exercise.description);
|
|
|
|
|
assert.equal(exercise.description, expected.description);
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`${logRes.status} ${logRes.statusText}`);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`${addRes.status} ${addRes.statusText}`);
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`${res.status} ${res.statusText}`);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
从 `GET /api/users/:id/logs` 返回的 `log` 数组中任何对象的 `duration` 属性都应该是一个数字。
|
|
|
|
|
|
|
|
|
|
```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();
|
|
|
|
|
const exercise = log[0];
|
|
|
|
|
assert.isNumber(exercise.duration);
|
|
|
|
|
assert.equal(exercise.duration, expected.duration);
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`${logRes.status} ${logRes.statusText}`);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`${addRes.status} ${addRes.statusText}`);
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`${res.status} ${res.statusText}`);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
从 `GET /api/users/:id/logs` 返回的 `log` 数组中任何对象的 `date` 属性应该是一个字符串。 使用 `Date` API 的 `dateString` 格式。
|
|
|
|
|
|
|
|
|
|
```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();
|
|
|
|
|
const exercise = log[0];
|
|
|
|
|
assert.isString(exercise.date);
|
|
|
|
|
assert.equal(exercise.date, expected.date);
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`${logRes.status} ${logRes.statusText}`);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`${addRes.status} ${addRes.statusText}`);
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`${res.status} ${res.statusText}`);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
你可以将 `from`、`to` 和 `limit` 参数添加到 `GET /api/users/:_id/logs` 请求检索任何用户的部分日志。 `from` 和 `to` 是 `yyyy-mm-dd` 形式的日期, `limit` 是希望返回的 log 数量。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
async (getUserInput) => {
|
|
|
|
|
const url = getUserInput('url');
|
2021-05-12 21:25:58 +05:30
|
|
|
|
const res = await fetch(url + '/api/users', {
|
2020-12-16 00:37:30 -07:00
|
|
|
|
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()
|
|
|
|
|
};
|
2021-05-12 21:25:58 +05:30
|
|
|
|
const addExerciseRes = await fetch(url + `/api/users/${_id}/exercises`, {
|
2020-12-16 00:37:30 -07:00
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
2021-05-12 21:25:58 +05:30
|
|
|
|
body: `description=${expected.description}&duration=${expected.duration}&date=1990-01-01`
|
2020-12-16 00:37:30 -07:00
|
|
|
|
});
|
2021-05-12 21:25:58 +05:30
|
|
|
|
const addExerciseTwoRes = await fetch(url + `/api/users/${_id}/exercises`, {
|
2020-12-16 00:37:30 -07:00
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
2022-02-12 21:01:07 +05:30
|
|
|
|
body: `description=${expected.description}&duration=${expected.duration}&date=1990-01-03`
|
2020-12-16 00:37:30 -07:00
|
|
|
|
});
|
|
|
|
|
if (addExerciseRes.ok && addExerciseTwoRes.ok) {
|
|
|
|
|
const logRes = await fetch(
|
2022-02-12 21:01:07 +05:30
|
|
|
|
url + `/api/users/${_id}/logs?from=1989-12-31&to=1990-01-04`
|
2020-12-16 00:37:30 -07:00
|
|
|
|
);
|
|
|
|
|
if (logRes.ok) {
|
|
|
|
|
const { log } = await logRes.json();
|
|
|
|
|
assert.isArray(log);
|
|
|
|
|
assert.equal(2, log.length);
|
2020-09-18 00:11:18 +08:00
|
|
|
|
} else {
|
2020-12-16 00:37:30 -07:00
|
|
|
|
throw new Error(`${logRes.status} ${logRes.statusText}`);
|
2020-09-18 00:11:18 +08:00
|
|
|
|
}
|
2020-12-16 00:37:30 -07:00
|
|
|
|
const limitRes = await fetch(
|
2021-05-12 21:25:58 +05:30
|
|
|
|
url + `/api/users/${_id}/logs?limit=1`
|
2020-12-16 00:37:30 -07:00
|
|
|
|
);
|
|
|
|
|
if (limitRes.ok) {
|
|
|
|
|
const { log } = await limitRes.json();
|
|
|
|
|
assert.isArray(log);
|
|
|
|
|
assert.equal(1, log.length);
|
2020-09-18 00:11:18 +08:00
|
|
|
|
} else {
|
2020-12-16 00:37:30 -07:00
|
|
|
|
throw new Error(`${limitRes.status} ${limitRes.statusText}`);
|
2020-09-18 00:11:18 +08:00
|
|
|
|
}
|
2022-02-12 21:01:07 +05:30
|
|
|
|
const filterDateBeforeLimitRes = await fetch(
|
|
|
|
|
url + `/api/users/${_id}/logs?from=1990-01-02&to=1990-01-04&limit=1`
|
|
|
|
|
);
|
|
|
|
|
if (filterDateBeforeLimitRes.ok) {
|
|
|
|
|
const { log } = await filterDateBeforeLimitRes.json();
|
|
|
|
|
assert.isArray(log);
|
|
|
|
|
assert.equal(1, log.length);
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`${filterDateBeforeLimitRes.status} ${filterDateBeforeLimitRes.statusText}`);
|
|
|
|
|
}
|
2020-12-16 00:37:30 -07:00
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`${res.status} ${res.statusText}`);
|
2020-09-18 00:11:18 +08:00
|
|
|
|
}
|
2020-12-16 00:37:30 -07:00
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`${res.status} ${res.statusText}`);
|
|
|
|
|
}
|
|
|
|
|
};
|
2019-05-06 07:31:26 -04:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
2020-08-13 17:24:35 +02:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```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.
|
|
|
|
|
*/
|
|
|
|
|
```
|