diff --git a/.bruno/bruno.json b/.bruno/bruno.json new file mode 100755 index 0000000..a0be828 --- /dev/null +++ b/.bruno/bruno.json @@ -0,0 +1,10 @@ +{ + "version": "1", + "name": "XIVPlan+DB", + "type": "collection", + "ignore": [".git"], + "presets": { + "requestType": "http", + "requestUrl": "http://localhost:14014/api" + } +} diff --git a/.bruno/collection.bru b/.bruno/collection.bru new file mode 100755 index 0000000..732ab0c --- /dev/null +++ b/.bruno/collection.bru @@ -0,0 +1,3 @@ +auth { + mode: none +} diff --git a/.bruno/enroll user.bru b/.bruno/enroll user.bru new file mode 100644 index 0000000..cee808c --- /dev/null +++ b/.bruno/enroll user.bru @@ -0,0 +1,24 @@ +meta { + name: enroll user + type: http + seq: 1 +} + +post { + url: http://localhost:14014/api/enroll + body: json + auth: inherit +} + +body:json { + { + "name": "test", + "pin": "1234", + "email": "" + } +} + +settings { + encodeUrl: true + timeout: 0 +} diff --git a/config.example.ts b/config.example.ts index 4d857f0..f2d31bf 100644 --- a/config.example.ts +++ b/config.example.ts @@ -9,7 +9,7 @@ export const config = { port: 3306, username: '', password: '', - name: 'xivplan-db', + name: 'xivplan', }, }; diff --git a/db/initialize.ts b/db/initialize.ts index ad3e8fa..32c0cbc 100644 --- a/db/initialize.ts +++ b/db/initialize.ts @@ -22,7 +22,8 @@ await dbClient.execute(` email varchar(255) NULL, deleteCode varchar(20) NULL, PRIMARY KEY (id), - UNIQUE KEY users_id_UNIQUE (id) + UNIQUE KEY users_id_UNIQUE (id), + UNIQUE KEY users_name_UNIQUE (name) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; `); console.log('Table created'); diff --git a/deno.json b/deno.json index ae3136a..2d5c7b9 100755 --- a/deno.json +++ b/deno.json @@ -25,6 +25,7 @@ "@mysql": "https://deno.land/x/mysql@v2.12.1/mod.ts", "@nanoid": "https://deno.land/x/nanoid@v3.0.0/mod.ts", "@std/http": "jsr:@std/http@1.0.15", - "~config": "./config.ts" + "~config": "./config.ts", + "db/": "./db/" } } diff --git a/mod.ts b/mod.ts index 722390e..2938c87 100644 --- a/mod.ts +++ b/mod.ts @@ -3,6 +3,8 @@ import { STATUS_CODE, STATUS_TEXT, StatusCode } from '@std/http'; import config from '~config'; +import dbClient from 'db/client.ts'; + // Using custom alphabet to exclude - and _ const alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; const nanoid = customAlphabet(alphabet, 20); @@ -11,14 +13,41 @@ const genericResponse = (status: StatusCode, customText = '') => new Response(customText || STATUS_TEXT[status], { status: status, statusText: STATUS_TEXT[status] }); Deno.serve({ port: config.api.port }, async (req) => { - const [urlPath, tempQ] = req.url.split('?'); - const path = (urlPath.split('api/')[1] ?? '').toLowerCase().trim(); + const urlPath = req.url.split('?')[0] ?? ''; + const path = (urlPath.split('api')[1] ?? '').toLowerCase().trim(); console.log(urlPath, path); if (req.method === 'GET') { // handle all gets - } else if (path === '/auth') { - return new Response(nanoid()); + } else if (req.method === 'POST' && path === '/enroll') { + const body = await req.json(); + + let readFailure = false; + const userNameMatches = await dbClient.query('SELECT name FROM users WHERE name = ?', [body.name]).catch(() => { + readFailure = true; + }); + if (readFailure) return genericResponse(STATUS_CODE.InternalServerError, "Couldn't read DB."); + + if (userNameMatches.length === 0) { + if (body.name.length < 4 || body.name.length > 20) return genericResponse(STATUS_CODE.BadRequest, `Name too ${body.name.length < 4 ? 'short' : 'long'}.`); + if (body.pin.length < 4 || body.pin.length > 20) return genericResponse(STATUS_CODE.BadRequest, `PIN too ${body.pin.length < 4 ? 'short' : 'long'}.`); + if (body.email.length > 20) return genericResponse(STATUS_CODE.BadRequest, `Email too long.`); + + const userId = nanoid(); + + let writeFailure = false; + await dbClient.execute('INSERT INTO users(id,name,pin,email) values(?,?,?,?)', [userId, body.name, body.pin, body.email]).catch(() => { + writeFailure = true; + }); + + if (writeFailure) { + return genericResponse(STATUS_CODE.InternalServerError, "Couldn't write DB."); + } else { + return genericResponse(STATUS_CODE.OK, JSON.stringify({ userId })); + } + } else { + return genericResponse(STATUS_CODE.BadRequest, 'Username Taken.'); + } } else { // handle auth then all other shiz }