-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #951 from SynBioHub/edit-types
Add/remove fields
- Loading branch information
Showing
12 changed files
with
438 additions
and
80 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
const config = require('../config') | ||
const getUrisFromReq = require('../getUrisFromReq') | ||
const loadTemplate = require('../loadTemplate') | ||
const sparql = require('../sparql/sparql') | ||
const lookupRole = require('../role') | ||
const lookupType = require('../type') | ||
|
||
const PREDICATES = { | ||
'role': 'http://sbols.org/v2#role', | ||
'type': 'http://sbols.org/v2#type' | ||
} | ||
|
||
function subjectIsValid (subject) { | ||
let defaultGraph = config.get('triplestore').defaultGraph | ||
|
||
return !subject.startsWith(defaultGraph) | ||
} | ||
|
||
function predicateIsValid (predicate) { | ||
return predicate !== undefined | ||
} | ||
|
||
function objectIsValid (object) { | ||
return object && object !== '' | ||
} | ||
|
||
function tripleIsValid (subject, predicate, object) { | ||
if (predicate === PREDICATES['role']) { | ||
// this needs to actually validate roles | ||
return true | ||
} | ||
|
||
return true | ||
} | ||
|
||
function getDisplayString (type, object) { | ||
switch (type) { | ||
case 'role': | ||
let role = lookupRole(object) | ||
return role.description.name || role.term || role.uri | ||
case 'type': | ||
let type = lookupType(object) | ||
return type.description.name || type.term || type.uri | ||
default: | ||
return object | ||
} | ||
} | ||
|
||
function formatObject (object) { | ||
if (object.startsWith('http')) { | ||
return `<${object}>` | ||
} else { | ||
return `"${object}"` | ||
} | ||
} | ||
|
||
function serve (req, res) { | ||
let { graphUri, uri } = getUrisFromReq(req, res) | ||
let field = req.params.field | ||
|
||
let d = new Date() | ||
let modified = d.toISOString() | ||
modified = modified.substring(0, modified.indexOf('.')) | ||
modified = JSON.stringify(modified) | ||
|
||
let subject = uri | ||
let predicate = PREDICATES[field] | ||
let object = req.body.object || '' | ||
|
||
if (!subjectIsValid(subject)) { | ||
res.sendStatus(403).end() | ||
return | ||
} else if (!predicateIsValid(predicate)) { | ||
res.sendStatus(404).end() | ||
return | ||
} else if (!objectIsValid(object)) { | ||
res.sendStatus(400).end() | ||
return | ||
} else if (!tripleIsValid(subject, predicate, object)) { | ||
res.sendStatus(400).end() | ||
return | ||
} | ||
|
||
const params = { | ||
subject: subject, | ||
predicate: predicate, | ||
object: formatObject(object), | ||
modified: modified | ||
} | ||
|
||
const query = loadTemplate('./sparql/AddTriple.sparql', params) | ||
console.log(query) | ||
|
||
sparql.updateQueryJson(query, graphUri).then(result => { | ||
res.send(getDisplayString(field, object)) | ||
}).catch(error => { | ||
console.error(error) | ||
res.sendStatus(500).end() | ||
}) | ||
} | ||
|
||
module.exports = serve |
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
Oops, something went wrong.