import * as api from '../../client/src/api/api'; import { EndpointError, EndpointHandler, handleErrorsInEndpoint } from './types'; import Knex from 'knex'; import { createTag, deleteTag, getTag, mergeTag, modifyTag } from '../db/Tag'; import { getAllJSDocTagsOfKind } from 'typescript'; export const PostTag: EndpointHandler = async (req: any, res: any, knex: Knex) => { if (!api.checkPostTagRequest(req.body)) { const e: EndpointError = { name: "EndpointError", message: 'Invalid PostTag request' + JSON.stringify(req.body), httpStatus: 400 }; throw e; } const reqObject: api.PostTagRequest = req.body; const { id: userId } = req.user; console.log("User ", userId, ": Post Tag ", reqObject); try { // Respond to the request. const responseObject: api.PostTagResponse = { id: await createTag(userId, reqObject, knex) }; res.status(200).send(responseObject); } catch (e) { handleErrorsInEndpoint(e); } } export const DeleteTag: EndpointHandler = async (req: any, res: any, knex: Knex) => { const { id: userId } = req.user; let id = parseInt(req.params.id); console.log("User ", userId, ": Delete Tag ", id); try { await deleteTag(userId, id, knex); res.status(200).send(); } catch (e) { handleErrorsInEndpoint(e); } } export const GetTag: EndpointHandler = async (req: any, res: any, knex: Knex) => { const { id: userId } = req.user; let id = parseInt(req.params.id); try { let tag = await getTag(id, userId, knex); await res.status(200).send(tag); } catch (e) { handleErrorsInEndpoint(e) } } export const PutTag: EndpointHandler = async (req: any, res: any, knex: Knex) => { if (!api.checkPutTagRequest(req.body)) { const e: EndpointError = { name: "EndpointError", message: 'Invalid PutTag request', httpStatus: 400 }; throw e; } const reqObject: api.PutTagRequest = req.body; const { id: userId } = req.user; let id: number = parseInt(req.params.id); console.log("User ", userId, ": Put Tag ", reqObject); try { await modifyTag(userId, id, reqObject, knex); res.status(200).send(); } catch (e) { handleErrorsInEndpoint(e); } } export const PatchTag: EndpointHandler = async (req: any, res: any, knex: Knex) => { if (!api.checkPatchTagRequest(req.body)) { const e: EndpointError = { name: "EndpointError", message: 'Invalid PatchTag request', httpStatus: 400 }; throw e; } const reqObject: api.PatchTagRequest = req.body; const { id: userId } = req.user; let id = parseInt(req.params.id); console.log("User ", userId, ": Patch Tag ", reqObject); try { await modifyTag(userId, id, reqObject, knex); res.status(200).send(); } catch (e) { handleErrorsInEndpoint(e); } } export const MergeTag: EndpointHandler = async (req: any, res: any, knex: Knex) => { const { id: userId } = req.user; const fromId = parseInt(req.params.id); const toId = parseInt(req.params.toId); console.log("User ", userId, ": Merge Tag ", fromId, req.params.toId); try { await mergeTag(userId, fromId, toId, knex); res.status(200).send(); } catch (e) { handleErrorsInEndpoint(e); } } export const tagEndpoints: [ string, string, boolean, EndpointHandler ][] = [ [ api.PostTagEndpoint, 'post', true, PostTag ], [ api.GetTagEndpoint, 'get', true, GetTag ], [ api.PutTagEndpoint, 'put', true, PutTag ], [ api.PatchTagEndpoint, 'patch', true, PatchTag ], [ api.DeleteTagEndpoint, 'delete', true, DeleteTag ], [ api.MergeTagEndpoint, 'post', true, MergeTag ], ];