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

Commit

Permalink
feat: texts routes and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonHowe committed Jul 7, 2020
1 parent b379d8a commit a0d6279
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/api/db/migrations/20200707125043_create_texts_table.ts
Original file line number Diff line number Diff line change
@@ -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");
};
6 changes: 6 additions & 0 deletions packages/api/src/modules/texts/actions/addText.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import knex from "../../../../db/knex";
import Text from "../types/Text";

export default async (text: string, ordered = true) => {
await knex<Text>("texts").insert({ text, ordered });
};
6 changes: 6 additions & 0 deletions packages/api/src/modules/texts/actions/getAllTexts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Text from "../types/Text";
import knex from "../../../../db/knex";

export default async () => {
return await knex<Text>("texts");
};
10 changes: 10 additions & 0 deletions packages/api/src/modules/texts/actions/getRandomText.ts
Original file line number Diff line number Diff line change
@@ -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<Text>("texts").where({ ordered });
} else {
return await knex<Text>("texts");
}
};
31 changes: 31 additions & 0 deletions packages/api/src/modules/texts/router.ts
Original file line number Diff line number Diff line change
@@ -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();
5 changes: 5 additions & 0 deletions packages/api/src/modules/texts/types/Text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default interface Text {
id: number;
text: string;
ordered: boolean;
}

0 comments on commit a0d6279

Please sign in to comment.