-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add patch functionality to lists #691
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fe24346
add list endpoints to add and delete from list
jpope19 63c9ce2
use patch
jpope19 1b6ed84
add tests
jpope19 267c797
add integration test
jpope19 ee01613
Merge branch 'master' of github.com:clay/amphora into modify-lists
jpope19 6db3456
remove error throw
jpope19 17a2f6e
Merge branch 'master' of github.com:clay/amphora into modify-lists
jpope19 e3c7c5d
7.9.0-0
jpope19 23aa4c8
Revert "7.9.0-0"
jpope19 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
'use strict'; | ||
|
||
const _isEqual = require('lodash/isEqual'); | ||
|
||
let db = require('./db'); | ||
|
||
/** | ||
* Appends an entry to a list and returns the updated list. | ||
* @param {Array<Object>} list | ||
* @param {Array<Object>} data | ||
* @returns {Array<Object>} | ||
*/ | ||
function addToList(list, data) { | ||
return list.concat(data); | ||
} | ||
|
||
/** | ||
* Removes an entry from a list and returns the updated list. | ||
* @param {Array<Object>} list | ||
* @param {Array<Object>} data | ||
* @returns {Array<Object>} | ||
*/ | ||
function removeFromList(list, data) { | ||
for (const deletion of data) { | ||
list = list.filter(entry => !_isEqual(entry, deletion)); | ||
} | ||
|
||
return list; | ||
} | ||
|
||
/** | ||
* Add or Remove an item from a list | ||
* @param {string} uri | ||
* @param {Array<Object>} data | ||
* @returns {Promise} | ||
*/ | ||
function patchList(uri, data) { | ||
if (!Array.isArray(data.add) && !Array.isArray(data.remove)) { | ||
throw new Error('Bad Request. List PATCH requires `add` or `remove` to be an array.'); | ||
} | ||
|
||
return db.get(uri).then(list => { | ||
if (Array.isArray(data.add)) { | ||
list = addToList(list, data.add); | ||
} | ||
|
||
if (Array.isArray(data.remove)) { | ||
list = removeFromList(list, data.remove); | ||
} | ||
|
||
// db.put wraps result in an object `{ _value: list }`, return list only | ||
// hopefully db.put never does anything to the data bc we're just returning list | ||
return db.put(uri, list).then(() => list); | ||
}); | ||
} | ||
|
||
module.exports.patchList = patchList; | ||
|
||
// For testing | ||
module.exports.setDb = mock => db = mock; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'), | ||
filename = __filename.split('/').pop().split('.').shift(), | ||
lib = require('./' + filename), | ||
sinon = require('sinon'), | ||
expect = require('chai').expect, | ||
storage = require('../../test/fixtures/mocks/storage'); | ||
|
||
describe(_.startCase(filename), function () { | ||
let sandbox, db; | ||
|
||
beforeEach(function () { | ||
sandbox = sinon.sandbox.create(); | ||
|
||
db = storage(); | ||
lib.setDb(db); | ||
}); | ||
|
||
afterEach(function () { | ||
sandbox.restore(); | ||
}); | ||
|
||
describe('patchList', function () { | ||
const fn = lib.patchList; | ||
|
||
it('will throw if bad request body', function () { | ||
try { | ||
fn('domain.com/_lists/test', {}); | ||
} catch (e) { | ||
expect(e.message).to.eql('Bad Request. List PATCH requires `add` or `remove` to be an array.'); | ||
} | ||
}); | ||
|
||
it('adds to existing lists if has add property', function () { | ||
db.get.resolves([]); | ||
db.put.callsFake((uri, list) => Promise.resolve({ _value: list })); | ||
|
||
return fn('domain.com/_lists/test', { add: [ 'hello' ] }).then(list => { | ||
expect(list).to.eql([ 'hello' ]); | ||
}); | ||
}); | ||
|
||
it('removes from existing lists if has remove property', function () { | ||
db.get.resolves([ 'hello' ]); | ||
db.put.callsFake((uri, list) => Promise.resolve({ _value: list })); | ||
|
||
return fn('domain.com/_lists/test', { remove: [ 'hello' ] }).then(list => { | ||
expect(list).to.eql([]); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'), | ||
apiAccepts = require('../../fixtures/api-accepts'), | ||
endpointName = _.startCase(__dirname.split('/').pop()), | ||
filename = _.startCase(__filename.split('/').pop().split('.')[0]), | ||
sinon = require('sinon'); | ||
|
||
describe(endpointName, function () { | ||
describe(filename, function () { | ||
let sandbox, | ||
hostname = 'localhost.example.com', | ||
acceptsJsonBody = apiAccepts.acceptsJsonBody(_.camelCase(filename)), | ||
start = ['item1', 'item2'], | ||
data = { | ||
add: ['item3', 'item4'], | ||
remove: ['item1'] | ||
}, | ||
end = ['item2', 'item3', 'item4']; | ||
|
||
beforeEach(function () { | ||
sandbox = sinon.sandbox.create(); | ||
return apiAccepts.beforeEachTest({ sandbox, hostname, pathsAndData: {'/_lists/valid': start} }); | ||
}); | ||
|
||
afterEach(function () { | ||
sandbox.restore(); | ||
}); | ||
|
||
describe('/_lists/:name', function () { | ||
const path = this.title; | ||
|
||
// overrides existing data | ||
acceptsJsonBody(path, {name: 'valid'}, data, 200, end); | ||
acceptsJsonBody(path, {name: 'missing'}, data, 404); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this might be the first time I've seen PATCH implemented correctly haha