|
|
|
class StructuredNode {
|
|
|
|
constructor(json) {
|
|
|
|
if (this.constructor === StructuredNode) {
|
|
|
|
throw new TypeError('Cannot instantiate object of abstract type StructuredNode!')
|
|
|
|
}
|
|
|
|
|
|
|
|
this.json = json
|
|
|
|
this.changes = {}
|
|
|
|
this.definition = StructuredNode.classes[this.constructor.name]
|
|
|
|
this.hooks = []
|
|
|
|
this.deleted = false
|
|
|
|
this.__name__ = this.constructor.name.toLowerCase()
|
|
|
|
}
|
|
|
|
|
|
|
|
check(name, type, new_value) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
_link_property(name, elm) {
|
|
|
|
const relation = this.definition.rels[name]
|
|
|
|
|
|
|
|
if (!relation) {
|
|
|
|
throw new ApiError(`Cannot find realation ${name} of object ${this.constructor.name}!`, 404, {obj: this, elm: elm})
|
|
|
|
}
|
|
|
|
const type = StructuredNode.classes[relation.target].klass
|
|
|
|
|
|
|
|
if (typeof elm == "string") {
|
|
|
|
elm = type.by_id(elm)
|
|
|
|
}
|
|
|
|
if (elm instanceof type) {
|
|
|
|
if (!elm.exists() || !this.exists()) {
|
|
|
|
throw new ApiError("Cannot connect object before it exists on the backend!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// make results available immediately
|
|
|
|
if (relation.cardinality.endsWith('OrMore')) {
|
|
|
|
if (!this.json[name].contains(elm)) {
|
|
|
|
this.json[name].push(elm)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.json[name] = elm
|
|
|
|
}
|
|
|
|
|
|
|
|
return Api.post(`/${this.__name__}/${this.json.uid}/${name}/${elm.json.uid}`).then(json => {
|
|
|
|
this.json = json
|
|
|
|
this.update('link')
|
|
|
|
return this
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_unlink_property(name, type, elm) {
|
|
|
|
return Api.delete(`/${this.__name__}/${this.json.uid}/${name}/${elm.json.uid}`).then(json => {
|
|
|
|
this.json = json
|
|
|
|
this.update('link')
|
|
|
|
return this
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
static by_id(uid) {
|
|
|
|
return Api.get('/' + this.name.toLowerCase() + '/' + uid).then(x => new this(x))
|
|
|
|
}
|
|
|
|
|
|
|
|
static find(attributes, settings) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
|
|
|
if (this.deleted) {
|
|
|
|
throw new ApiError("Cannot save deleted object!", {obj: this})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.json.uid) {
|
|
|
|
return Api.put(`/${this.__name__}/${this.json.uid}`, this.changes).then(json => {
|
|
|
|
this.json = json
|
|
|
|
this.changes = {}
|
|
|
|
this.update('save')
|
|
|
|
return this
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return Api.post('/' + this.__name__, this.json).then(json => {
|
|
|
|
this.json = json
|
|
|
|
this.changes = {}
|
|
|
|
this.update('created')
|
|
|
|
return this
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete() {
|
|
|
|
if (!this.exists()) {
|
|
|
|
throw new ApiError("Cannot delete object that does not exist yet!", {obj: this})
|
|
|
|
}
|
|
|
|
this.deleted = true
|
|
|
|
Api.delete(`/${this.__name__}/${this.json.uid}`).then(r => {
|
|
|
|
this.update('deleted')
|
|
|
|
return this
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
exists() {
|
|
|
|
return !this.deleted && !!this.json.uid
|
|
|
|
}
|
|
|
|
|
|
|
|
onupdate(callback) {
|
|
|
|
this.hooks.push(callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
update(event) {
|
|
|
|
this.hooks.forEach(cb => {
|
|
|
|
try {
|
|
|
|
cb(this, event)
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`Error while running update on ${this.constructor.name} update function ${cb}: ${e}`)
|
|
|
|
console.error(cb, event, e)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
static registerClassDefinition(klass, props, rels) {
|
|
|
|
console.log(klass, props, rels)
|
|
|
|
StructuredNode.classes[klass.name] = {klass, props, rels}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
StructuredNode.classes = {}
|
|
|
|
|
|
|
|
class Api {
|
|
|
|
static set_auth(token) {
|
|
|
|
Api.token = token;
|
|
|
|
}
|
|
|
|
|
|
|
|
static get(url) {
|
|
|
|
return fetch('/api' + url, {headers: Api.headers()}).then(r => {
|
|
|
|
if (r.ok) {
|
|
|
|
return r.json()
|
|
|
|
} else {
|
|
|
|
// evaluate response and raise error
|
|
|
|
return ApiError.fromResponse(r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
static post(url, body="") {
|
|
|
|
return fetch('/api' + url, {headers: Api.headers(), body: JSON.stringify(body), method: 'POST'}).then(r => {
|
|
|
|
if (r.ok) {
|
|
|
|
return r.json()
|
|
|
|
} else {
|
|
|
|
// evaluate response and raise error
|
|
|
|
return ApiError.fromResponse(r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
static put(url, body) {
|
|
|
|
return fetch('/api' + url, {headers: Api.headers(), body: JSON.stringify(body), method: 'PUT'}).then(r => {
|
|
|
|
if (r.ok) {
|
|
|
|
return r.json()
|
|
|
|
} else {
|
|
|
|
// evaluate response and raise error
|
|
|
|
return ApiError.fromResponse(r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
static delete(url) {
|
|
|
|
return fetch('/api' + url, {headers: Api.headers(), method: 'DELETE'}).then(r => {
|
|
|
|
if (r.ok) {
|
|
|
|
return r
|
|
|
|
} else {
|
|
|
|
// evaluate response and raise error
|
|
|
|
return ApiError.fromResponse(r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
static headers() {
|
|
|
|
return {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
// 'Authorization': 'Bearer ' + Api.token
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ApiError {
|
|
|
|
constructor(msg, code, data) {
|
|
|
|
this.msg = msg
|
|
|
|
this.code = code
|
|
|
|
this.data = data
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromResponse(r) {
|
|
|
|
if (r.headers.get('Content-Type') === 'application/json') {
|
|
|
|
return r.json().then(json => {
|
|
|
|
throw new ApiError(json.msg, r.status, json.data)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return r.text().then(text => {
|
|
|
|
throw new ApiError(text, r.status)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|