Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Commit

Permalink
feat: editing texts
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonHowe committed Jul 15, 2020
1 parent 28c6244 commit d562514
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/api/src/modules/texts/actions/editText.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import knex from "../../../../db/knex";

import Text from "../types/Text";

export default async <T extends keyof Text>(
property: T,
id: number,
newValue: Text[T]
) => {
if (!property || !id || !newValue) return null;
const result = await knex<Text>("texts")
.select()
.first()
.where({ id })
.update({ [property]: newValue });
if (!result) return null;

return result;
};
8 changes: 8 additions & 0 deletions packages/api/src/modules/texts/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });

Expand All @@ -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);
Expand Down

0 comments on commit d562514

Please sign in to comment.