From a0d6279dd5b7a1d1a43d5bc7fd35cacaabe9dbdf Mon Sep 17 00:00:00 2001 From: xWafl <73sampleperson@gmail.com> Date: Tue, 7 Jul 2020 13:24:31 -0400 Subject: [PATCH] feat: texts routes and methods --- .../20200707125043_create_texts_table.ts | 13 ++++++++ .../api/src/modules/texts/actions/addText.ts | 6 ++++ .../src/modules/texts/actions/getAllTexts.ts | 6 ++++ .../modules/texts/actions/getRandomText.ts | 10 ++++++ packages/api/src/modules/texts/router.ts | 31 +++++++++++++++++++ packages/api/src/modules/texts/types/Text.ts | 5 +++ 6 files changed, 71 insertions(+) create mode 100644 packages/api/db/migrations/20200707125043_create_texts_table.ts create mode 100644 packages/api/src/modules/texts/actions/addText.ts create mode 100644 packages/api/src/modules/texts/actions/getAllTexts.ts create mode 100644 packages/api/src/modules/texts/actions/getRandomText.ts create mode 100644 packages/api/src/modules/texts/router.ts create mode 100644 packages/api/src/modules/texts/types/Text.ts diff --git a/packages/api/db/migrations/20200707125043_create_texts_table.ts b/packages/api/db/migrations/20200707125043_create_texts_table.ts new file mode 100644 index 0000000..e0acdc1 --- /dev/null +++ b/packages/api/db/migrations/20200707125043_create_texts_table.ts @@ -0,0 +1,13 @@ +import Knex from "knex"; + +export const up = async (knex: Knex) => { + return knex.schema.createTable("texts", table => { + table.increments("id"); + table.string("text").notNullable(); + table.boolean("ordered"); + }); +}; + +export const down = async (knex: Knex) => { + return knex.schema.dropTable("texts"); +}; diff --git a/packages/api/src/modules/texts/actions/addText.ts b/packages/api/src/modules/texts/actions/addText.ts new file mode 100644 index 0000000..e1ae1cd --- /dev/null +++ b/packages/api/src/modules/texts/actions/addText.ts @@ -0,0 +1,6 @@ +import knex from "../../../../db/knex"; +import Text from "../types/Text"; + +export default async (text: string, ordered = true) => { + await knex("texts").insert({ text, ordered }); +}; diff --git a/packages/api/src/modules/texts/actions/getAllTexts.ts b/packages/api/src/modules/texts/actions/getAllTexts.ts new file mode 100644 index 0000000..3f19bb9 --- /dev/null +++ b/packages/api/src/modules/texts/actions/getAllTexts.ts @@ -0,0 +1,6 @@ +import Text from "../types/Text"; +import knex from "../../../../db/knex"; + +export default async () => { + return await knex("texts"); +}; diff --git a/packages/api/src/modules/texts/actions/getRandomText.ts b/packages/api/src/modules/texts/actions/getRandomText.ts new file mode 100644 index 0000000..2764b76 --- /dev/null +++ b/packages/api/src/modules/texts/actions/getRandomText.ts @@ -0,0 +1,10 @@ +import Text from "../types/Text"; +import knex from "../../../../db/knex"; + +export default async (typed = false, ordered = false) => { + if (typed) { + return await knex("texts").where({ ordered }); + } else { + return await knex("texts"); + } +}; diff --git a/packages/api/src/modules/texts/router.ts b/packages/api/src/modules/texts/router.ts new file mode 100644 index 0000000..ba26c8e --- /dev/null +++ b/packages/api/src/modules/texts/router.ts @@ -0,0 +1,31 @@ +import Router from "../Router"; +import { requireAdmin } from "../auth/middleware/requireAdmin"; +import addText from "./actions/addText"; +import getAllTexts from "./actions/getAllTexts"; +import getRandomText from "./actions/getRandomText"; + +const router = new Router({ prefix: "/texts" }); + +router.post("/addText", requireAdmin(), async (ctx, next) => { + const { text, ordered } = ctx.request.body; + await addText(text, ordered); + ctx.status = 201; + ctx.body = "Successfully added text"; + await next(); +}); + +router.post("/getAllTexts", async (ctx, next) => { + const texts = await getAllTexts(); + ctx.status = 200; + ctx.body = texts; + await next(); +}); + +router.post("/getRandomText", async (ctx, next) => { + const text = await getRandomText(); + ctx.status = 200; + ctx.body = text; + await next(); +}); + +export default router.routes(); diff --git a/packages/api/src/modules/texts/types/Text.ts b/packages/api/src/modules/texts/types/Text.ts new file mode 100644 index 0000000..d20cd7d --- /dev/null +++ b/packages/api/src/modules/texts/types/Text.ts @@ -0,0 +1,5 @@ +export default interface Text { + id: number; + text: string; + ordered: boolean; +}