Skip to content

Commit 3a0927f

Browse files
crhallbergChris HallbergGeoffscdemiankatzgithub-actions[bot]
authored
Create Object tool on Main Menu (#72)
* Create Object tool on Main Menu. * Use res to make linting happy. * Fix merge error. * More complete, less broken. * Modernize code. * Make form processing React-controlled. * Fix key. * Pass values to API successfully. * Update snapshots. * Progress on object creation. * Add support for creating top-level objects. * Update snapshot. * Improve tests. * Improved property validation and tests. * Improved property validation and tests. * Add submit test. * Add no-parent test. * Expand test coverage. * Simplify with optional chaining. * Optimize to use map. * Code simplification; improved error handling. * Eliminate pointless noParent parameter. * More targeted validation. * Work in progress updates -- CURRENTLY BROKEN. * Improve parameter validation. * Fix request sending. * Commit from GitHub Actions (Lint Pull Requests) * More fixes. * Progress on tests. * Progress on tests (but still not working). * Improved error tolerance. * Progress on test suite (working, but with warnings). * Fix timing issues. * Integrate object creator into editor tool. * Change post-submit behavior. Co-authored-by: Chris Hallberg <[email protected]> Co-authored-by: Geoffsc <[email protected]> Co-authored-by: Demian Katz <[email protected]> Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent a43f286 commit 3a0927f

14 files changed

+4066
-8823
lines changed

api/src/routes/edit.ts

+55-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,68 @@
11
import express = require("express");
2+
import bodyParser = require("body-parser");
23
import Config from "../models/Config";
3-
const edit = express.Router();
4+
import Fedora from "../services/Fedora";
5+
import FedoraObjectFactory from "../services/FedoraObjectFactory";
6+
import MetadataExtractor from "../services/MetadataExtractor";
47
import { requireToken } from "./auth";
58
import { pidSanitizer } from "./sanitize";
69
import Solr from "../services/Solr";
10+
const edit = express.Router();
711

812
edit.get("/models", requireToken, function (req, res) {
913
res.json({ CollectionModels: Config.getInstance().collectionModels, DataModels: Config.getInstance().dataModels });
1014
});
1115

16+
edit.post("/object/new", requireToken, bodyParser.json(), async function (req, res) {
17+
let parentPid = req?.body?.parent;
18+
if (parentPid !== null && !parentPid?.length) {
19+
parentPid = null;
20+
}
21+
const model = req.body?.model;
22+
if (!model) {
23+
res.status(400).send("Missing model parameter.");
24+
return;
25+
}
26+
const title = req.body?.title;
27+
if (!title) {
28+
res.status(400).send("Missing title parameter.");
29+
return;
30+
}
31+
const state = req.body?.state;
32+
if (!state) {
33+
res.status(400).send("Missing state parameter.");
34+
return;
35+
}
36+
37+
// Validate parent PID, if set:
38+
if (parentPid !== null) {
39+
const fedora = Fedora.getInstance();
40+
const extractor = MetadataExtractor.getInstance();
41+
let relsExt: string;
42+
try {
43+
relsExt = await fedora.getDatastreamAsString(parentPid, "RELS-EXT");
44+
} catch (e) {
45+
res.status(404).send("Error loading parent PID: " + parentPid);
46+
return;
47+
}
48+
const models = extractor.extractRelations(relsExt).hasModel ?? [];
49+
50+
// Parents must be collections; validate!
51+
if (!models.includes("info:fedora/vudl-system:CollectionModel")) {
52+
res.status(400).send("Illegal parent " + parentPid + "; not a collection!");
53+
return;
54+
}
55+
}
56+
const factory = FedoraObjectFactory.getInstance();
57+
try {
58+
const newObject = await factory.build(model.replace("vudl-system:", ""), title, state, parentPid);
59+
res.status(200).send(newObject.pid);
60+
} catch (e) {
61+
console.error(e);
62+
res.status(400).send(e.message);
63+
}
64+
});
65+
1266
async function getChildren(req, res) {
1367
const query =
1468
(req.params.pid ?? "").length > 0

0 commit comments

Comments
 (0)