diff --git a/server/src/infrastructure/database.ts b/server/src/infrastructure/database.ts index df0549a..c657808 100644 --- a/server/src/infrastructure/database.ts +++ b/server/src/infrastructure/database.ts @@ -1,4 +1,4 @@ -import { Connection, Pool, createPool } from 'mariadb'; +import { PoolConnection, Pool, createPool } from 'mariadb'; type ConnectionConfig = { host: string, @@ -30,7 +30,7 @@ export default class Database { return Database.instance; } - public async getConnection(): Promise { + public async getConnection(): Promise { return this.pool.getConnection(); } } diff --git a/server/src/middleware/form-to-json.ts b/server/src/middleware/form-to-json.ts new file mode 100644 index 0000000..46cc37c --- /dev/null +++ b/server/src/middleware/form-to-json.ts @@ -0,0 +1,13 @@ +import { Context } from 'hono'; + +export default async function parsedForm(c: Context, next) { + if (c.req.method == 'POST') { + if (c.req.header('Content-Type') == 'application/json') { + next(); + } else { + + } + } + + +} diff --git a/server/src/routes/user.ts b/server/src/routes/user.ts index 64ba7c7..801712f 100644 --- a/server/src/routes/user.ts +++ b/server/src/routes/user.ts @@ -1,28 +1,28 @@ import { Hono, Context } from 'hono'; -import { Connection } from 'mariadb'; -import User from '@slovo/models/User'; -import Database from '@slovo/infrastructure/database'; +import { PoolConnection } from 'mariadb'; +import User from 'slovo/models/User'; +import Database from 'slovo/infrastructure/database'; const app = new Hono(); // add routes and methods here app.post('/register', async (c: Context) => { - // check if form (from the web app) or JSON (from a blank API call); - let user: User; - if (c.req.header('Content-Type') == 'application/json') { - user = await c.req.json(); - } else { - user = await c.req.formData(); - } + const user: User = await c.req.json(); - - const conn: Connection = await Database.getInstance().getConnection(); - const query = `INSERT INTO users (username, password) VALUES (?, ?)`; + const conn: PoolConnection = await Database.getInstance().getConnection(); + const query = `INSERT INTO users (username, name, password) VALUES (?, ?)`; try { - const results = await conn.query(query, [ user.username, user.password ]); + const results = await conn.query(query, [ + user.username, + user.name, + user.password + ]); console.log(results); + + await conn.release(); + return c.json({ message: "Registration successful.", }); diff --git a/server/tsconfig.json b/server/tsconfig.json index 98da342..4751702 100644 --- a/server/tsconfig.json +++ b/server/tsconfig.json @@ -2,9 +2,10 @@ "compilerOptions": { "strict": true, "jsx": "react-jsx", - "jsxImportSource": "hono/jsx" - }, - "paths": { - "@slovo/*": [ "./src/*" ] + "jsxImportSource": "hono/jsx", + "baseUrl": "./src", + "paths": { + "slovo/*": [ "*" ] + } } }