Skip to content

Commit

Permalink
added 409 Conflict to POST if already listed
Browse files Browse the repository at this point in the history
  • Loading branch information
elf Pavlik committed Nov 10, 2014
1 parent c630610 commit 46f55ec
Showing 1 changed file with 56 additions and 36 deletions.
92 changes: 56 additions & 36 deletions daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function readFile(name){
}

var storage = {
getAll: function(uuid){
getAll: function(){
return new Promise(function(resolve, reject){
fs.readdir(config.dataPath, function(err, files){
if(err) reject(err);
Expand Down Expand Up @@ -79,46 +79,66 @@ function fetchProfile(uri){
});
}

function checkIfNew(uri){
console.log(uri);
return new Promise(function(resolve, reject){
storage.getAll().then(function(all){
var existing = _.find(all['@graph'], function(listing){ return listing.about['@id'] == uri; });
console.log(existing);
if(existing) {
reject();
} else {
resolve();
}
});
});
}

// listProfile: Receives the URI (probably from a plp-editor) and adds it to the list
daemon.post('/', function(req, res){

// Get the URI where the profile is stored from requesr
var profileUri = req.body['@id'];
var uuid = UUID.v4();
var uri = 'http://' + config.domain + '/' + uuid;
var path = 'data/'+ uuid;

fetchProfile(profileUri)
.then(function(profile){
// delete context FIXME
delete profile["@context"];

var listing = {
"@id": uri,
"@type": "Listing",
about: profile
};

storage.save(uuid, listing)
.then(function(data){
var min = {
"@context": "http://plp.hackers4peace.net/context.jsonld",
"@id": data["@id"],
"@type": "Listing"
};
res.type('application/ld+json');
res.send(min);
})
.catch(function(err){
// TODO add error reporting
res.send(500);
});

}).catch(function(){
console.log('failed fetching', profileUri);
// TODO add error reporting
res.send(500);
});

checkIfNew(profileUri).then(function(){
var uuid = UUID.v4();
var uri = 'http://' + config.domain + '/' + uuid;
var path = 'data/'+ uuid;

fetchProfile(profileUri)
.then(function(profile){
// delete context FIXME
delete profile["@context"];

var listing = {
"@id": uri,
"@type": "Listing",
about: profile
};

storage.save(uuid, listing)
.then(function(data){
var min = {
"@context": "http://plp.hackers4peace.net/context.jsonld",
"@id": data["@id"],
"@type": "Listing"
};
res.type('application/ld+json');
res.send(min);
})
.catch(function(err){
// TODO add error reporting
res.send(500);
});

}).catch(function(){
console.log('failed fetching', profileUri);
// TODO add error reporting
res.send(500);
});
}, function(){
res.send(409);
});
});

// getProfiles: Returns the profiles listed on this plp-directory
Expand Down

0 comments on commit 46f55ec

Please sign in to comment.