Adding asset sorting to endpoint.

This commit is contained in:
David Jay
2016-12-16 15:35:15 -05:00
parent 1154b65c91
commit d4880ff183
3 changed files with 67 additions and 12 deletions
@@ -12,21 +12,24 @@ import {
TableHeader
} from 'react-mdl';
const limit = 25;
class Streams extends Component {
state = {
searchTerm: '',
sortBy: 'newest',
search: '',
sort: 'desc',
statusFilter: 'all',
statusMenus: {}
}
componentDidMount () {
this.props.fetchAssets(0, 25, '', 'desc');
this.props.fetchAssets(0, limit, '', this.state.sortBy);
}
onSettingChange = (setting) => (e) => {
this.setState({[setting]: e.target.value});
this.props.fetchAssets(0, limit, this.state.search, this.state.sort);
}
renderDate = (date) => {
@@ -104,8 +107,8 @@ class Streams extends Component {
value={sortBy}
childContainer='div'
onChange={this.onSettingChange('sortBy')}>
<Radio value='newest'>{lang.t('streams.newest')}</Radio>
<Radio value='oldest'>{lang.t('streams.oldest')}</Radio>
<Radio value='desc'>{lang.t('streams.newest')}</Radio>
<Radio value='asc'>{lang.t('streams.oldest')}</Radio>
</RadioGroup>
</div>
+23 -5
View File
@@ -12,22 +12,40 @@ router.get('/', (req, res, next) => {
skip = 0,
sort = 'asc',
field = 'created_at',
filter = 'all',
search = ''
} = req.query;
const assets = (filter, search) => {
switch(filter) {
case 'open':
return Asset.search(search)
.find({
$or: [
{closedAt: null},
{closedAt: {$gt: Date.now()}}
]
});
case 'closed':
return Asset.search(search)
.find({
closedAt: {$lt: Date.now()}
});
default:
return Asset.search(search);
}
};
// Find all the assets.
Promise.all([
Asset
.search(search)
assets(filter, search)
.sort({[field]: (sort === 'asc') ? 1 : -1})
.skip(parseInt(skip))
.limit(parseInt(limit)),
Asset
.search(search)
assets(filter, search)
.count()
])
.then(([result, count]) => {
// Send back the asset data.
res.json({
result,
+36 -2
View File
@@ -18,12 +18,14 @@ describe('/api/v1/assets', () => {
url: 'https://coralproject.net/news/asset1',
title: 'Asset 1',
description: 'term1',
id: '1'
id: '1',
closedAt: Date.now()
},
{
url: 'https://coralproject.net/news/asset2',
title: 'Asset 2',
description: 'term2'
description: 'term2',
closedAt: null
}
]);
});
@@ -81,6 +83,38 @@ describe('/api/v1/assets', () => {
});
});
it('should return only closed assets', () => {
return chai.request(app)
.get('/api/v1/assets?filter=closed')
.set(passport.inject({roles: ['admin']}))
.then((res) => {
const body = res.body;
expect(body).to.have.property('count', 1);
expect(body).to.have.property('result');
const assets = body.result;
expect(assets[0]).to.have.property('title', 'Asset 1');
});
});
it('should return only opened assets', () => {
return chai.request(app)
.get('/api/v1/assets?filter=open')
.set(passport.inject({roles: ['admin']}))
.then((res) => {
const body = res.body;
expect(body).to.have.property('count', 1);
expect(body).to.have.property('result');
const assets = body.result;
expect(assets[0]).to.have.property('title', 'Asset 2');
});
});
});
describe('#put', () => {