parent
c8a39c9224
commit
ff800978d1
12 changed files with 197 additions and 11 deletions
@ -1,5 +1,5 @@ |
|||||||
import * as serverApi from '../../api'; |
import * as serverApi from '../../api'; |
||||||
import { QueryElem, toApiQuery } from './Query'; |
import { QueryElem, toApiQuery } from '../query/Query'; |
||||||
|
|
||||||
export interface QueryArgs { |
export interface QueryArgs { |
||||||
query?: QueryElem, |
query?: QueryElem, |
@ -0,0 +1,45 @@ |
|||||||
|
import * as serverApi from '../../api'; |
||||||
|
|
||||||
|
export async function createTag(details: serverApi.CreateTagRequest) { |
||||||
|
const requestOpts = { |
||||||
|
method: 'POST', |
||||||
|
headers: { 'Content-Type': 'application/json' }, |
||||||
|
body: JSON.stringify(details), |
||||||
|
}; |
||||||
|
|
||||||
|
const response = await fetch((process.env.REACT_APP_BACKEND || "") + serverApi.CreateTagEndpoint, requestOpts) |
||||||
|
if (!response.ok) { |
||||||
|
throw new Error("Response to tag creation not OK: " + JSON.stringify(response)); |
||||||
|
} |
||||||
|
return await response.json(); |
||||||
|
} |
||||||
|
|
||||||
|
export async function modifyTag(id: number, details: serverApi.ModifyTagRequest) { |
||||||
|
const requestOpts = { |
||||||
|
method: 'PUT', |
||||||
|
headers: { 'Content-Type': 'application/json' }, |
||||||
|
body: JSON.stringify(details), |
||||||
|
}; |
||||||
|
|
||||||
|
const response = await fetch( |
||||||
|
(process.env.REACT_APP_BACKEND || "") + serverApi.ModifyTagEndpoint.replace(':id', id.toString()), |
||||||
|
requestOpts |
||||||
|
); |
||||||
|
if (!response.ok) { |
||||||
|
throw new Error("Response to tag modification not OK: " + JSON.stringify(response)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export async function deleteTag(id: number) { |
||||||
|
const requestOpts = { |
||||||
|
method: 'DELETE', |
||||||
|
}; |
||||||
|
|
||||||
|
const response = await fetch( |
||||||
|
(process.env.REACT_APP_BACKEND || "") + serverApi.DeleteTagEndpoint.replace(':id', id.toString()), |
||||||
|
requestOpts |
||||||
|
); |
||||||
|
if (!response.ok) { |
||||||
|
throw new Error("Response to tag deletion not OK: " + JSON.stringify(response)); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,64 @@ |
|||||||
|
import * as api from '../../client/src/api'; |
||||||
|
import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types'; |
||||||
|
import Knex from 'knex'; |
||||||
|
|
||||||
|
export const DeleteTagEndpointHandler: EndpointHandler = async (req: any, res: any, knex: Knex) => { |
||||||
|
if (!api.checkDeleteTagRequest(req)) { |
||||||
|
const e: EndpointError = { |
||||||
|
internalMessage: 'Invalid DeleteTag request: ' + JSON.stringify(req.body), |
||||||
|
httpStatus: 400 |
||||||
|
}; |
||||||
|
throw e; |
||||||
|
} |
||||||
|
const reqObject: api.DeleteTagRequest = req.body; |
||||||
|
|
||||||
|
console.log("Delete Tag:", reqObject); |
||||||
|
|
||||||
|
await knex.transaction(async (trx) => { |
||||||
|
try { |
||||||
|
// Start retrieving any child tags.
|
||||||
|
const childTagsPromise = trx.select('id') |
||||||
|
.from('tags') |
||||||
|
.where({ 'parentId': req.params.id }); |
||||||
|
|
||||||
|
// Start retrieving the tag itself.
|
||||||
|
const tagPromise = trx.select('id') |
||||||
|
.from('tags') |
||||||
|
.where({ id: req.params.id }) |
||||||
|
.then((r: any) => (r && r[0]) ? r[0]['id'] : undefined) |
||||||
|
|
||||||
|
// Wait for the requests to finish.
|
||||||
|
var [tag, children] = await Promise.all([tagPromise, childTagsPromise]); |
||||||
|
|
||||||
|
// If there are any children, refuse the deletion
|
||||||
|
if(children && children.length > 0) { |
||||||
|
const e: EndpointError = { |
||||||
|
internalMessage: 'Invalid DeleteTag request: tag ' + req.params.id.toString() + " has children", |
||||||
|
httpStatus: 400 |
||||||
|
}; |
||||||
|
throw e; |
||||||
|
} |
||||||
|
|
||||||
|
// Check that we found all objects we need.
|
||||||
|
if (!tag) { |
||||||
|
const e: EndpointError = { |
||||||
|
internalMessage: 'Tag or parent does not exist for DeleteTag request: ' + JSON.stringify(req.body), |
||||||
|
httpStatus: 400 |
||||||
|
}; |
||||||
|
throw e; |
||||||
|
} |
||||||
|
|
||||||
|
// Delete the tag.
|
||||||
|
await trx('tags') |
||||||
|
.where({ 'id': req.params.id }) |
||||||
|
.del(); |
||||||
|
|
||||||
|
// Respond to the request.
|
||||||
|
res.status(200).send(); |
||||||
|
|
||||||
|
} catch (e) { |
||||||
|
catchUnhandledErrors(e); |
||||||
|
trx.rollback(); |
||||||
|
} |
||||||
|
}) |
||||||
|
} |
Loading…
Reference in new issue