From 8a463e875ddd3637d9fbd3b79f063e92e4684ccc Mon Sep 17 00:00:00 2001 From: Grace H Date: Wed, 20 Sep 2023 23:44:12 -0400 Subject: [PATCH] add user concept and add todos for exercise --- public/util.ts | 8 +++++++- src/app.ts | 4 +++- src/concepts/user.ts | 23 +++++++++++++++++++++++ src/concepts/websession.ts | 4 +++- src/routes.ts | 8 +++++++- 5 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 src/concepts/user.ts diff --git a/public/util.ts b/public/util.ts index 04514b9..5945856 100644 --- a/public/util.ts +++ b/public/util.ts @@ -11,7 +11,13 @@ const operations = [ endpoint: "/api/logout", method: "POST", fields: {}, - } + }, + { + name: "Register", + endpoint: "/api/register", + method: "POST", + fields: { username: "input" }, + }, ]; const API_URL = "http://localhost:3000"; diff --git a/src/app.ts b/src/app.ts index d64841b..f67b7e4 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,4 +1,6 @@ +import UserConcept from "./concepts/user"; import WebSessionConcept from "./concepts/websession"; // App Definition using concepts. Right now, we only have our WebSession concept implemented. -export const WebSession = new WebSessionConcept(); \ No newline at end of file +export const WebSession = new WebSessionConcept(); +export const User = new UserConcept(); diff --git a/src/concepts/user.ts b/src/concepts/user.ts new file mode 100644 index 0000000..9f1ce26 --- /dev/null +++ b/src/concepts/user.ts @@ -0,0 +1,23 @@ +import { NotAllowedError } from "./errors"; + +export interface UserDoc { + username: string; +} + +export default class UserConcept { + // Hardcoded users just for recitation purposes + public readonly users: UserDoc[] = [{ username: "testAccount" }, { username: "grace" }]; + + register(username: string) { + this.isUsernameAvailable(username); + this.users.push({ username }); + } + + isUsernameAvailable(username: string) { + if (this.users.filter((user) => user.username === username).length !== 0) { + throw new NotAllowedError("Username already taken!"); + } + } + + // TODO: Write code to make sure the username exists +} diff --git a/src/concepts/websession.ts b/src/concepts/websession.ts index b549cea..b25b339 100644 --- a/src/concepts/websession.ts +++ b/src/concepts/websession.ts @@ -1,5 +1,5 @@ // This is (some of) the code for the WebSession concept which was introduced in lecture on 9/18. -// We're storing the user (in the form of the username string for now) when the user logs in, and we +// We're storing the user (in the form of the username string for now) when the user logs in, and we // reset the session's user when the user logs out. import { SessionData } from "express-session"; @@ -28,6 +28,8 @@ export default class WebSessionConcept { // Hint: Take a look at how the "end" function makes sure the user is logged in. Keep in mind that a // synchronization like starting a session should just consist of a series of actions that may throw // exceptions and should not have its own control flow. + + // TODO: make sure user is registered before trying to log the user in session.user = username; } diff --git a/src/routes.ts b/src/routes.ts index 91575f9..8ab342e 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -1,6 +1,6 @@ import { Router, getExpressRouter } from "./framework/router"; -import { WebSession } from "./app"; +import { User, WebSession } from "./app"; import { WebSessionDoc } from "./concepts/websession"; class Routes { @@ -16,6 +16,12 @@ class Routes { return { msg: "Logged out!" }; } + @Router.post("/register") + async register(username: string) { + User.register(username); + return { msg: "Username registered!", user: username }; + } + // If we had more concepts working, we'd add routes for those here too! }