From d562514762445cb685f125e739bf35b3ef1edae8 Mon Sep 17 00:00:00 2001 From: xWafl <73sampleperson@gmail.com> Date: Wed, 15 Jul 2020 08:33:17 -0400 Subject: [PATCH] feat: editing texts --- .../api/src/modules/texts/actions/editText.ts | 19 +++++++++++++++++++ packages/api/src/modules/texts/router.ts | 8 ++++++++ 2 files changed, 27 insertions(+) create mode 100644 packages/api/src/modules/texts/actions/editText.ts diff --git a/packages/api/src/modules/texts/actions/editText.ts b/packages/api/src/modules/texts/actions/editText.ts new file mode 100644 index 0000000..c14599f --- /dev/null +++ b/packages/api/src/modules/texts/actions/editText.ts @@ -0,0 +1,19 @@ +import knex from "../../../../db/knex"; + +import Text from "../types/Text"; + +export default async ( + property: T, + id: number, + newValue: Text[T] +) => { + if (!property || !id || !newValue) return null; + const result = await knex("texts") + .select() + .first() + .where({ id }) + .update({ [property]: newValue }); + if (!result) return null; + + return result; +}; diff --git a/packages/api/src/modules/texts/router.ts b/packages/api/src/modules/texts/router.ts index 18a5c8c..f351dcf 100644 --- a/packages/api/src/modules/texts/router.ts +++ b/packages/api/src/modules/texts/router.ts @@ -6,6 +6,7 @@ import getRandomText from "./actions/getRandomText"; import { addTextBody } from "./schema/addTextBody"; import { validateSchema } from "../schema/middleware/validateSchema"; import deleteText from "./actions/deleteText"; +import editText from "./actions/editText"; const router = new Router({ prefix: "/texts" }); @@ -25,6 +26,13 @@ router.post( } ); +// TODO: Add schema validation for this +router.patch("/editUser", requireAdmin(), async (ctx, next) => { + const { property, id, newValue } = ctx.request.body; + await editText(property, id, newValue); + await next(); +}); + router.delete("/deleteText", requireAdmin(), async (ctx, next) => { const { id } = ctx.request.body; await deleteText(id);