Skip to content

Commit

Permalink
ItemEditor: Adjust how new URIs are minted (#166)
Browse files Browse the repository at this point in the history
This will make deleting old records possible without issues.
  • Loading branch information
stefandesu committed Oct 10, 2024
1 parent c65ccb0 commit 6919eb3
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions vue/components/ItemEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -419,20 +419,7 @@ export default {
return
}
const item = { ...this.item }
const method = item.uri ? "PUT" : "POST"
if (!item.uri) {
// Try to find an URI not taken yet.
// TODO: better use auto-increment at jskos-server?
const total = await fetch("/api/voc?limit=1").then(res => res.headers.get("x-total-count"))
item.uri = "http://bartoc.org/en/node/" + (17002 + 1 * total)
}
const body = JSON.stringify(this.cleanupItem(item), null, 2)
const headers = { "Content-Type": "application/json" }
if (this.auth) {
headers.Authorization = `Bearer ${this.auth.token}`
}
let body
const onError = (error, res) => {
const message = error.message || res.StatusText
const issue = "This JSKOS record could not be saved:\n\n~~~json\n" + body + "\n~~~\n" +
Expand All @@ -443,6 +430,26 @@ export default {
this.error = { message, status: res.status, html }
}
const item = { ...this.item }
const method = item.uri ? "PUT" : "POST"
if (!item.uri) {
const base = "http://bartoc.org/en/node/"
// Try to find an URI not taken yet.
try {
const latestUri = (await fetch("/api/voc?sort=counter&order=desc&limit=1").then(res => res.json()))[0].uri
const latestId = parseInt(latestUri.replace(base, ""))
item.uri = base + (latestId + 1)
} catch (error) {
onError(new Error("Could not determine URI for new record."), { status: "determining new URI" })
return
}
}
body = JSON.stringify(this.cleanupItem(item), null, 2)
const headers = { "Content-Type": "application/json" }
if (this.auth) {
headers.Authorization = `Bearer ${this.auth.token}`
}
fetch("/api/voc", { method, body, headers }).then(res => {
if (res.ok) {
window.location.href = "/vocabularies?uri=" + encodeURIComponent(item.uri)
Expand Down

0 comments on commit 6919eb3

Please sign in to comment.