You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

125 lines
3.7 KiB

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;
console.log("User ", userId, ": Delete Tag ", req.params.id);
try {
await deleteTag(userId, req.params.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;
try {
let tag = await getTag(req.params.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;
console.log("User ", userId, ": Put Tag ", reqObject);
try {
await modifyTag(userId, req.params.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;
console.log("User ", userId, ": Patch Tag ", reqObject);
try {
await modifyTag(userId, req.params.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;
console.log("User ", userId, ": Merge Tag ", req.params.id, req.params.toId);
const fromId = req.params.id;
const toId = 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 ],
];