Skip to content

Commit

Permalink
Sort by/direction is added to children listing request
Browse files Browse the repository at this point in the history
  • Loading branch information
amourzenkov-sc committed Nov 13, 2017
1 parent 8e96db9 commit 4633a86
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 20 deletions.
25 changes: 14 additions & 11 deletions server-nodejs/docs/api-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,21 @@ If successful, this method returns an empty response body.
* URL: `api/files/:id/children`
* Method: GET

### Request
### Query Parameters

```javascript
{
orderBy: <string>, // one of 'createdDate', 'folder', 'modifiedDate', 'quotaBytesUsed', 'name'.
orderDirection: <string>, // ASC/DESC
maxResults: <number>, // TODO in v2
pageToken: <string>, // TODO in v2
searchQuery: <string>, // TODO in v2
searchRecursively: <bool> // TODO in v2
}
```
All query paramaters are optional

---|---|---
Name | Possible Values | Default
orderBy | name<br />modifiedTime | name
orderDirection | ASC<br />DESC | ASC

TODO in v2:

* maxResults: <number>,
* pageToken: <string>,
* searchQuery: <string>,
* searchRecursively: <bool>

### Response

Expand Down
54 changes: 45 additions & 9 deletions server-nodejs/router/listChildren.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,58 @@ const {

const { TYPE_DIR } = require('../constants');

const
ORDER_BY_NAME = 'name',
ORDER_BY_MODIFIED_TIME = 'modifiedTime',
DEFAULT_ORDER_BY = ORDER_BY_NAME,

ORDER_DIRECTION_ASC = 'ASC',
ORDER_DIRECTION_DESC = 'DESC',
DEFAULT_ORDER_DIRECTION = ORDER_DIRECTION_ASC;

const sortItems = ({
orderBy = DEFAULT_ORDER_BY,
orderDirection = DEFAULT_ORDER_DIRECTION
}) => {
const sorter = orderBy === ORDER_BY_NAME ?
(itemA, itemB) => itemA.name.toLowerCase().localeCompare(itemB.name.toLowerCase()) :
(itemA, itemB) => itemA.modifiedTime - itemB.modifiedTime;

return items => items.sort((itemA, itemB) =>
(itemB.type === TYPE_DIR) - (itemA.type === TYPE_DIR) ||
sorter(...(
orderDirection === ORDER_DIRECTION_DESC ?
[itemB, itemA] :
[itemA, itemB]
))
);
};

module.exports = ({
options,
req,
res,
handleError,
path: userPath
}) => {
if (req.query.orderDirection && ![ORDER_DIRECTION_ASC, ORDER_DIRECTION_DESC].includes(req.query.orderDirection)) {
return handleError(Object.assign(
new Error(`Invalid order direction ${req.query.orderDirection}`),
{ httpCode: 400 }
));
}

if (req.query.orderBy && ![ORDER_BY_NAME, ORDER_BY_MODIFIED_TIME].includes(req.query.orderBy)) {
return handleError(Object.assign(
new Error(`Invalid order by ${req.query.orderBy}`),
{ httpCode: 400 }
));
}

const absPath = path.join(options.fsRoot, userPath);
options.logger.info(`Children for ${absPath} requested by ${getClientIp(req)}`);

fs.readdir(absPath).
return fs.readdir(absPath).
then(basenames =>
Promise.all(basenames.map(basename => fs.stat(path.join(absPath, basename)))).
then(stats => stats.reduce(
Expand All @@ -40,14 +81,9 @@ module.exports = ({
return rez;
},
[]
)).
then(items =>
items.sort((itemA, itemB) =>
(itemB.type === TYPE_DIR) - (itemA.type === TYPE_DIR) ||
itemA.name.toLowerCase().localeCompare(itemB.name.toLowerCase())
// TODO: add user locale as 2nd argument to the above localeCompare() function.
)).
then(items => res.json({ items }))
))
).
then(sortItems(req.query)).
then(items => res.json({ items })).
catch(handleError);
};

0 comments on commit 4633a86

Please sign in to comment.