Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions controllers/bulk.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ const bulkCreate = async function (req, res, next) {
if (!Array.isArray(documents)) {
err.message = "The request body must be an array of objects."
err.status = 400
next(utils.createExpressError(err))
return
return next(utils.createExpressError(err))
}
if (documents.length === 0) {
err.message = "No action on an empty array."
err.status = 400
next(utils.createExpressError(err))
return
return next(utils.createExpressError(err))
}
const gatekeep = documents.filter(d=> {
// Each item must be valid JSON, but can't be an array.
Expand All @@ -42,12 +40,11 @@ const bulkCreate = async function (req, res, next) {
// Items must not have an @id, and in some cases same for id.
const idcheck = _contextid(d["@context"]) ? (d.id ?? d["@id"]) : d["@id"]
if(idcheck) return d
})
})
if (gatekeep.length > 0) {
err.message = "All objects in the body of a `/bulkCreate` must be JSON and must not contain a declared identifier property."
err.status = 400
next(utils.createExpressError(err))
return
return next(utils.createExpressError(err))
}

// TODO: bulkWrite SLUGS? Maybe assign an id to each document and then use that to create the slug?
Expand All @@ -66,6 +63,7 @@ const bulkCreate = async function (req, res, next) {
// unordered bulkWrite() operations have better performance metrics.
let bulkOps = []
const generatorAgent = getAgentClaim(req, next)
if (!generatorAgent) return
for(let d of documents) {
// Do not create empty {}s
if(Object.keys(d).length === 0) continue
Expand All @@ -92,7 +90,7 @@ const bulkCreate = async function (req, res, next) {
}
catch (error) {
//MongoServerError from the client has the following properties: index, code, keyPattern, keyValue
next(utils.createExpressError(error))
return next(utils.createExpressError(error))
}
}

Expand All @@ -111,14 +109,12 @@ const bulkUpdate = async function (req, res, next) {
if (!Array.isArray(documents)) {
err.message = "The request body must be an array of objects."
err.status = 400
next(utils.createExpressError(err))
return
return next(utils.createExpressError(err))
}
if (documents.length === 0) {
err.message = "No action on an empty array."
err.status = 400
next(utils.createExpressError(err))
return
return next(utils.createExpressError(err))
}
const gatekeep = documents.filter(d => {
// Each item must be valid JSON, but can't be an array.
Expand All @@ -136,12 +132,12 @@ const bulkUpdate = async function (req, res, next) {
if (gatekeep.length > 0) {
err.message = "All objects in the body of a `/bulkUpdate` must be JSON and must contain a declared identifier property."
err.status = 400
next(utils.createExpressError(err))
return
return next(utils.createExpressError(err))
}
// unordered bulkWrite() operations have better performance metrics.
let bulkOps = []
const generatorAgent = getAgentClaim(req, next)
if (!generatorAgent) return
for(const objectReceived of documents){
// We know it has an id
const idReceived = objectReceived["@id"] ?? objectReceived.id
Expand All @@ -154,8 +150,7 @@ const bulkUpdate = async function (req, res, next) {
try {
originalObject = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]})
} catch (error) {
next(utils.createExpressError(error))
return
return next(utils.createExpressError(error))
}
if (null === originalObject) continue
if (utils.isDeleted(originalObject)) continue
Expand Down Expand Up @@ -196,7 +191,7 @@ const bulkUpdate = async function (req, res, next) {
}
catch (error) {
//MongoServerError from the client has the following properties: index, code, keyPattern, keyValue
next(utils.createExpressError(error))
return next(utils.createExpressError(error))
}
}

Expand Down
25 changes: 16 additions & 9 deletions controllers/crud.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,27 @@ import { _contextid, idNegotiation, generateSlugId, ObjectID, getAgentClaim, par
* */
const create = async function (req, res, next) {
res.set("Content-Type", "application/json; charset=utf-8")
let props = req.body
if (!props || Object.keys(props).length === 0) {
let err = {
message: "Detected empty JSON object. You must provide at least one property in the /create request body JSON.",
status: 400
}
return next(utils.createExpressError(err))
}
let slug
if(req.get("Slug")){
let slug_json = await generateSlugId(req.get("Slug"), next)
if(slug_json.code){
next(utils.createExpressError(slug_json))
return
return next(utils.createExpressError(slug_json))
}
else{
slug = slug_json.slug_id
}
}

let generatorAgent = getAgentClaim(req, next)
if (!generatorAgent) return
let context = req.body["@context"] ? { "@context": req.body["@context"] } : {}
let provided = JSON.parse(JSON.stringify(req.body))
let rerumProp = { "__rerum": utils.configureRerumOptions(generatorAgent, provided, false, false)["__rerum"] }
Expand All @@ -54,7 +62,7 @@ const create = async function (req, res, next) {
}
catch (error) {
//MongoServerError from the client has the following properties: index, code, keyPattern, keyValue
next(utils.createExpressError(error))
return next(utils.createExpressError(error))
}
}

Expand All @@ -68,22 +76,21 @@ const query = async function (req, res, next) {
let props = req.body
const limit = parseInt(req.query.limit ?? 100)
const skip = parseInt(req.query.skip ?? 0)
if (Object.keys(props).length === 0) {
if (!props || Object.keys(props).length === 0) {
//Hey now, don't ask for everything...this can happen by accident. Don't allow it.
let err = {
message: "Detected empty JSON object. You must provide at least one property in the /query request body JSON.",
status: 400
}
next(utils.createExpressError(err))
return
return next(utils.createExpressError(err))
}
try {
let matches = await db.find(props).limit(limit).skip(skip).toArray()
matches = matches.map(o => idNegotiation(o))
res.set(utils.configureLDHeadersFor(matches))
res.json(matches)
} catch (error) {
next(utils.createExpressError(error))
return next(utils.createExpressError(error))
}
}

Expand Down Expand Up @@ -115,9 +122,9 @@ const id = async function (req, res, next) {
"message": `No RERUM object with id '${id}'`,
"status": 404
}
next(utils.createExpressError(err))
return next(utils.createExpressError(err))
} catch (error) {
next(utils.createExpressError(error))
return next(utils.createExpressError(error))
}
}

Expand Down
21 changes: 8 additions & 13 deletions controllers/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@ const deleteObj = async function(req, res, next) {
try {
id = req.params["_id"] ?? parseDocumentID(JSON.parse(JSON.stringify(req.body))["@id"]) ?? parseDocumentID(JSON.parse(JSON.stringify(req.body))["id"])
} catch(error){
next(utils.createExpressError(error))
return
return next(utils.createExpressError(error))
}
let agentRequestingDelete = getAgentClaim(req, next)
if (!agentRequestingDelete) return
let originalObject
try {
originalObject = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]})
} catch (error) {
next(utils.createExpressError(error))
return
return next(utils.createExpressError(error))
}
if (null !== originalObject) {
let safe_original = JSON.parse(JSON.stringify(originalObject))
Expand All @@ -58,8 +57,7 @@ const deleteObj = async function(req, res, next) {
})
}
if (err.status) {
next(utils.createExpressError(err))
return
return next(utils.createExpressError(err))
}
let preserveID = safe_original["@id"]
let deletedFlag = {} //The __deleted flag is a JSONObject
Expand All @@ -76,15 +74,13 @@ const deleteObj = async function(req, res, next) {
try {
result = await db.replaceOne({ "_id": originalObject["_id"] }, deletedObject)
} catch (error) {
next(utils.createExpressError(error))
return
return next(utils.createExpressError(error))
}
if (result.modifiedCount === 0) {
//result didn't error out, the action was not performed. Sometimes, this is a neutral thing. Sometimes it is indicative of an error.
err.message = "The original object was not replaced with the deleted object in the database."
err.status = 500
next(utils.createExpressError(err))
return
return next(utils.createExpressError(err))
}
//204 to say it is deleted and there is nothing in the body
console.log("Object deleted: " + preserveID)
Expand All @@ -94,12 +90,11 @@ const deleteObj = async function(req, res, next) {
//Not sure we can get here, as healHistoryTree might throw and error.
err.message = "The history tree for the object being deleted could not be mended."
err.status = 500
next(utils.createExpressError(err))
return
return next(utils.createExpressError(err))
}
err.message = "No object with this id could be found in RERUM. Cannot delete."
err.status = 404
next(utils.createExpressError(err))
return next(utils.createExpressError(err))
}

/**
Expand Down
12 changes: 6 additions & 6 deletions controllers/gog.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { _contextid, ObjectID, getAgentClaim, parseDocumentID, idNegotiation } f
const _gog_fragments_from_manuscript = async function (req, res, next) {
res.set("Content-Type", "application/json; charset=utf-8")
const agent = getAgentClaim(req, next)
if (!agent) return
const agentID = agent.split("/").pop()
const manID = req.body["ManuscriptWitness"]
const limit = parseInt(req.query.limit ?? 50)
Expand All @@ -44,8 +45,7 @@ const _gog_fragments_from_manuscript = async function (req, res, next) {
})
}
if (err.status) {
next(utils.createExpressError(err))
return
return next(utils.createExpressError(err))
}
try {
let matches = []
Expand Down Expand Up @@ -138,7 +138,7 @@ const _gog_fragments_from_manuscript = async function (req, res, next) {
}
catch (error) {
console.error(error)
next(utils.createExpressError(error))
return next(utils.createExpressError(error))
}
}

Expand All @@ -156,6 +156,7 @@ const _gog_fragments_from_manuscript = async function (req, res, next) {
const _gog_glosses_from_manuscript = async function (req, res, next) {
res.set("Content-Type", "application/json; charset=utf-8")
const agent = getAgentClaim(req, next)
if (!agent) return
const agentID = agent.split("/").pop()
const manID = req.body["ManuscriptWitness"]
const limit = parseInt(req.query.limit ?? 50)
Expand All @@ -176,8 +177,7 @@ const _gog_glosses_from_manuscript = async function (req, res, next) {
})
}
if (err.status) {
next(utils.createExpressError(err))
return
return next(utils.createExpressError(err))
}
try {
let matches = []
Expand Down Expand Up @@ -300,7 +300,7 @@ const _gog_glosses_from_manuscript = async function (req, res, next) {
}
catch (error) {
console.error(error)
next(utils.createExpressError(error))
return next(utils.createExpressError(error))
}
}

Expand Down
32 changes: 12 additions & 20 deletions controllers/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@ const since = async function (req, res, next) {
try {
obj = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]})
} catch (error) {
next(utils.createExpressError(error))
return
return next(utils.createExpressError(error))
}
if (null === obj) {
let err = {
message: `Cannot produce a history. There is no object in the database with id '${id}'. Check the URL.`,
status: 404
}
next(utils.createExpressError(err))
return
return next(utils.createExpressError(err))
}
let all = await getAllVersions(obj)
.catch(error => {
Expand Down Expand Up @@ -60,16 +58,14 @@ const history = async function (req, res, next) {
try {
obj = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]})
} catch (error) {
next(utils.createExpressError(error))
return
return next(utils.createExpressError(error))
}
if (null === obj) {
let err = {
message: `Cannot produce a history. There is no object in the database with id '${id}'. Check the URL.`,
status: 404
}
next(utils.createExpressError(err))
return
return next(utils.createExpressError(err))
}
let all = await getAllVersions(obj)
.catch(error => {
Expand Down Expand Up @@ -103,9 +99,9 @@ const idHeadRequest = async function (req, res, next) {
"message": `No RERUM object with id '${id}'`,
"status": 404
}
next(utils.createExpressError(err))
return next(utils.createExpressError(err))
} catch (error) {
next(utils.createExpressError(error))
return next(utils.createExpressError(error))
}
}

Expand All @@ -128,9 +124,9 @@ const queryHeadRequest = async function (req, res, next) {
"message": `There are no objects in the database matching the query. Check the request body.`,
"status": 404
}
next(utils.createExpressError(err))
return next(utils.createExpressError(err))
} catch (error) {
next(utils.createExpressError(error))
return next(utils.createExpressError(error))
}
}

Expand All @@ -145,16 +141,14 @@ const sinceHeadRequest = async function (req, res, next) {
try {
obj = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]})
} catch (error) {
next(utils.createExpressError(error))
return
return next(utils.createExpressError(error))
}
if (null === obj) {
let err = {
message: `Cannot produce a history. There is no object in the database with id '${id}'. Check the URL.`,
status: 404
}
next(utils.createExpressError(err))
return
return next(utils.createExpressError(err))
}
let all = await getAllVersions(obj)
.catch(error => {
Expand Down Expand Up @@ -183,16 +177,14 @@ const historyHeadRequest = async function (req, res, next) {
try {
obj = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]})
} catch (error) {
next(utils.createExpressError(error))
return
return next(utils.createExpressError(error))
}
if (null === obj) {
let err = {
message: "Cannot produce a history. There is no object in the database with this id. Check the URL.",
status: 404
}
next(utils.createExpressError(err))
return
return next(utils.createExpressError(err))
}
let all = await getAllVersions(obj)
.catch(error => {
Expand Down
Loading