Skip to content

Commit

Permalink
Create Object tool on Main Menu (#72)
Browse files Browse the repository at this point in the history
* 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 <crhallberg@gmail.com>
Co-authored-by: Geoffsc <geoffrey.scholl@villanova.edu>
Co-authored-by: Demian Katz <demian.katz@villanova.edu>
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
5 people committed Nov 17, 2021
1 parent a43f286 commit 3a0927f
Show file tree
Hide file tree
Showing 14 changed files with 4,066 additions and 8,823 deletions.
56 changes: 55 additions & 1 deletion api/src/routes/edit.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,68 @@
import express = require("express");
import bodyParser = require("body-parser");
import Config from "../models/Config";
const edit = express.Router();
import Fedora from "../services/Fedora";
import FedoraObjectFactory from "../services/FedoraObjectFactory";
import MetadataExtractor from "../services/MetadataExtractor";
import { requireToken } from "./auth";
import { pidSanitizer } from "./sanitize";
import Solr from "../services/Solr";
const edit = express.Router();

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

edit.post("/object/new", requireToken, bodyParser.json(), async function (req, res) {
let parentPid = req?.body?.parent;
if (parentPid !== null && !parentPid?.length) {
parentPid = null;
}
const model = req.body?.model;
if (!model) {
res.status(400).send("Missing model parameter.");
return;
}
const title = req.body?.title;
if (!title) {
res.status(400).send("Missing title parameter.");
return;
}
const state = req.body?.state;
if (!state) {
res.status(400).send("Missing state parameter.");
return;
}

// Validate parent PID, if set:
if (parentPid !== null) {
const fedora = Fedora.getInstance();
const extractor = MetadataExtractor.getInstance();
let relsExt: string;
try {
relsExt = await fedora.getDatastreamAsString(parentPid, "RELS-EXT");
} catch (e) {
res.status(404).send("Error loading parent PID: " + parentPid);
return;
}
const models = extractor.extractRelations(relsExt).hasModel ?? [];

// Parents must be collections; validate!
if (!models.includes("info:fedora/vudl-system:CollectionModel")) {
res.status(400).send("Illegal parent " + parentPid + "; not a collection!");
return;
}
}
const factory = FedoraObjectFactory.getInstance();
try {
const newObject = await factory.build(model.replace("vudl-system:", ""), title, state, parentPid);
res.status(200).send(newObject.pid);
} catch (e) {
console.error(e);
res.status(400).send(e.message);
}
});

async function getChildren(req, res) {
const query =
(req.params.pid ?? "").length > 0
Expand Down

0 comments on commit 3a0927f

Please sign in to comment.