Skip to content

Commit

Permalink
Use new creator middleware in concept service (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefandesu committed Nov 13, 2020
1 parent 9ecc05b commit 8182f73
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
2 changes: 2 additions & 0 deletions routes/concepts.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ if (config.concepts.update) {
utils.wrappers.async(async (req) => {
return await conceptService.putConcept({
body: req.body,
existing: req.existing,
})
}),
utils.adjust,
Expand All @@ -58,6 +59,7 @@ if (config.concepts.delete) {
utils.wrappers.async(async (req) => {
return await conceptService.deleteConcept({
uri: req.query.uri,
existing: req.existing,
})
}),
(req, res) => res.sendStatus(204),
Expand Down
20 changes: 10 additions & 10 deletions services/concepts.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ module.exports = class ConceptService {
return isMultiple ? response : response[0]
}

async putConcept({ body }) {
async putConcept({ body, existing }) {
if (!body) {
throw new MalformedBodyError()
}
Expand All @@ -307,8 +307,13 @@ module.exports = class ConceptService {
}
concept = preparation.concepts[0]

// Override _id, uri, and created properties
concept._id = existing._id
concept.uri = existing.uri
concept.created = existing.created

// Write concept to database
const result = await Concept.replaceOne({ _id: concept.uri }, concept)
const result = await Concept.replaceOne({ _id: existing._id }, concept)
if (!result.ok) {
throw new DatabaseAccessError()
}
Expand All @@ -322,17 +327,12 @@ module.exports = class ConceptService {
return concept
}

async deleteConcept({ uri }) {
async deleteConcept({ uri, existing }) {
if (!uri) {
throw new MalformedRequestError()
}
const concept = await Concept.findById(uri).lean()

if (!concept) {
throw new EntityNotFoundError()
}

const result = await Concept.deleteOne({ _id: concept._id })
const result = await Concept.deleteOne({ _id: existing._id })
if (!result.ok) {
throw new DatabaseAccessError()
}
Expand All @@ -342,7 +342,7 @@ module.exports = class ConceptService {

await this.postAdjustmentsForConcepts({
// Adjust scheme in case it was its last concept
schemeUrisToAdjust: [_.get(concept, "inScheme[0].uri")],
schemeUrisToAdjust: [_.get(existing, "inScheme[0].uri")],
conceptUrisWithNarrower: [],
})
}
Expand Down

0 comments on commit 8182f73

Please sign in to comment.