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
5706dbd
commit aa4377a
Showing
18 changed files
with
198 additions
and
16 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
|
@@ -4,7 +4,8 @@ interface Users { | |
password: string; | ||
role?: string | null; | ||
description?: string | null; | ||
rank: number; | ||
exp: number; | ||
tutorials: number[]; | ||
} | ||
|
||
export default [ | ||
|
@@ -14,30 +15,34 @@ export default [ | |
password: "UserPass", | ||
role: "admin", | ||
description: null, | ||
rank: 8 | ||
exp: 8, | ||
tutorials: [] | ||
}, | ||
{ | ||
name: "MKGUN3", | ||
email: "[email protected]", | ||
password: "MaoGay", | ||
role: "member", | ||
description: null, | ||
rank: 4 | ||
exp: 4, | ||
tutorials: [] | ||
}, | ||
{ | ||
name: "NotAUser", | ||
email: "[email protected]", | ||
password: "nothing", | ||
role: "member", | ||
description: null, | ||
rank: 2 | ||
exp: 2, | ||
tutorials: [] | ||
}, | ||
{ | ||
name: "Guy2", | ||
email: "[email protected]", | ||
password: "JustAGuy", | ||
role: "member", | ||
description: "But hey, I have a description!", | ||
rank: 0 | ||
exp: 0, | ||
tutorials: [] | ||
} | ||
] as readonly Users[]; |
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
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
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 |
---|---|---|
@@ -1,6 +1,18 @@ | ||
import knex from "../../../../db/knex"; | ||
import Text from "../types/Text"; | ||
|
||
export default async (text: string, ordered = true) => { | ||
await knex<Text>("texts").insert({ text, ordered }); | ||
export default async ( | ||
text: string, | ||
difficulty: number, | ||
author: number, | ||
ordered = true, | ||
tutorial = false | ||
) => { | ||
await knex<Text>("texts").insert({ | ||
text, | ||
difficulty, | ||
author, | ||
ordered, | ||
tutorial | ||
}); | ||
}; |
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
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
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
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
25 changes: 25 additions & 0 deletions
25
packages/api/src/modules/tutorials/actions/completeTutorial.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,25 @@ | ||
import { Tutorial } from "../types/Tutorial"; | ||
import knex from "../../../../db/knex"; | ||
import User from "../../users/types/User"; | ||
|
||
export default async (tutorialid: number, userid: number) => { | ||
const tutorial = await knex<Tutorial>("texts") | ||
.where({ | ||
id: tutorialid, | ||
tutorial: true | ||
}) | ||
.first(); | ||
console.log(tutorial); | ||
const user = await knex<User>("users") | ||
.where({ | ||
id: userid | ||
}) | ||
.first(); | ||
if (!tutorial || !user) return false; | ||
await knex("users") | ||
.update({ | ||
tutorials: knex.raw("array_append(tutorials, ?)", [tutorialid]) | ||
}) | ||
.where({ id: userid }); | ||
return true; | ||
}; |
9 changes: 9 additions & 0 deletions
9
packages/api/src/modules/tutorials/actions/getRandomTutorial.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,9 @@ | ||
import Text from "../../texts/types/Text"; | ||
import knex from "../../../../db/knex"; | ||
|
||
export default async () => { | ||
const texts = await knex<Text>("texts").where({ | ||
tutorial: true | ||
}); | ||
return texts[Math.floor(Math.random() * texts.length)]; | ||
}; |
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,9 @@ | ||
import { Tutorial } from "../types/Tutorial"; | ||
import knex from "../../../../db/knex"; | ||
|
||
export default async (id: number) => { | ||
const tutorial = await knex<Tutorial>("texts") | ||
.where({ id, tutorial: true }) | ||
.first(); | ||
return tutorial || null; | ||
}; |
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,56 @@ | ||
import request from "supertest"; | ||
import { expect } from "chai"; | ||
|
||
import { server } from "../../index"; | ||
import addText from "../texts/actions/addText"; | ||
import User from "../users/types/User"; | ||
import knex from "../../../db/knex"; | ||
|
||
const agent = request.agent(server); | ||
|
||
describe("Tutorials routes", async () => { | ||
// We don't need to rerun migrations or seeds because we did in the auth route | ||
|
||
it("Completes a tutorial", async () => { | ||
await agent.post("/api/auth/login").send({ | ||
email: "[email protected]", | ||
password: "MaoGay" | ||
}); | ||
|
||
await addText("blah", 1, 1, false, true); | ||
const response = await agent | ||
.post(`/api/tutorials/completeTutorial/`) | ||
.send({ id: 3 }) | ||
.set("Accept", "application/text") | ||
.expect("Content-Type", /text/) | ||
.expect(200); | ||
|
||
expect(response.text).to.deep.equal("Successfully completed tutorial"); | ||
}); | ||
|
||
it("Cannot complete a non-existent tutorial", async () => { | ||
const response = await agent | ||
.post(`/api/tutorials/completeTutorial/`) | ||
.send({ id: 1 }) | ||
.set("Accept", "application/json") | ||
.expect("Content-Type", /json/) | ||
.expect(400); | ||
|
||
expect(response.body).to.deep.equal({ | ||
status: 400, | ||
message: "That tutorial does not exist!" | ||
}); | ||
}); | ||
|
||
it("Gets tutorials completed for the user", async () => { | ||
const user = await knex<User>("users") | ||
.where({ | ||
email: "[email protected]" | ||
}) | ||
.first(); | ||
|
||
console.log(user); | ||
|
||
expect(user?.tutorials).to.deep.equal([3]); | ||
}); | ||
}); |
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,42 @@ | ||
import Router from "../Router"; | ||
import { requireAuthenticated } from "../auth/middleware/requireAuthenticated"; | ||
import completeTutorial from "./actions/completeTutorial"; | ||
import { HttpError } from "../../common/error/classes/httpError"; | ||
import getTutorial from "./actions/getTutorial"; | ||
import getRandomTutorial from "./actions/getRandomTutorial"; | ||
|
||
const router = new Router({ prefix: "/tutorials" }); | ||
|
||
router.post("/completeTutorial", requireAuthenticated(), async (ctx, next) => { | ||
const { id } = ctx.request.body; | ||
const { user } = ctx.session!; | ||
const success = await completeTutorial(id, user); | ||
if (!success) { | ||
throw new HttpError(400, "That tutorial does not exist!"); | ||
} | ||
ctx.status = 200; | ||
ctx.body = "Successfully completed tutorial"; | ||
await next(); | ||
}); | ||
|
||
router.get("/:id", async (ctx, next) => { | ||
const { id } = ctx.params; | ||
console.log(`Received ${id}`); | ||
const tutorial = getTutorial(id); | ||
if (!tutorial) { | ||
throw new HttpError(400, "That tutorial does not exist!"); | ||
} | ||
console.log(tutorial); | ||
ctx.status = 200; | ||
ctx.body = tutorial; | ||
await next(); | ||
}); | ||
|
||
router.get("/random", async (ctx, next) => { | ||
const tutorial = getRandomTutorial(); | ||
ctx.status = 200; | ||
ctx.body = tutorial; | ||
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,3 @@ | ||
import Text from "../../texts/types/Text"; | ||
|
||
export type Tutorial = Text; |
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
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