This repository has been archived by the owner on Jul 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b379d8a
commit a0d6279
Showing
6 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
packages/api/db/migrations/20200707125043_create_texts_table.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default interface Text { | ||
id: number; | ||
text: string; | ||
ordered: boolean; | ||
} |