db set up, enroll api built
This commit is contained in:
10
.bruno/bruno.json
Executable file
10
.bruno/bruno.json
Executable file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"version": "1",
|
||||||
|
"name": "XIVPlan+DB",
|
||||||
|
"type": "collection",
|
||||||
|
"ignore": [".git"],
|
||||||
|
"presets": {
|
||||||
|
"requestType": "http",
|
||||||
|
"requestUrl": "http://localhost:14014/api"
|
||||||
|
}
|
||||||
|
}
|
||||||
3
.bruno/collection.bru
Executable file
3
.bruno/collection.bru
Executable file
@@ -0,0 +1,3 @@
|
|||||||
|
auth {
|
||||||
|
mode: none
|
||||||
|
}
|
||||||
24
.bruno/enroll user.bru
Normal file
24
.bruno/enroll user.bru
Normal file
@@ -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
|
||||||
|
}
|
||||||
@@ -9,7 +9,7 @@ export const config = {
|
|||||||
port: 3306,
|
port: 3306,
|
||||||
username: '',
|
username: '',
|
||||||
password: '',
|
password: '',
|
||||||
name: 'xivplan-db',
|
name: 'xivplan',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,8 @@ await dbClient.execute(`
|
|||||||
email varchar(255) NULL,
|
email varchar(255) NULL,
|
||||||
deleteCode varchar(20) NULL,
|
deleteCode varchar(20) NULL,
|
||||||
PRIMARY KEY (id),
|
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;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||||
`);
|
`);
|
||||||
console.log('Table created');
|
console.log('Table created');
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
"@mysql": "https://deno.land/x/mysql@v2.12.1/mod.ts",
|
"@mysql": "https://deno.land/x/mysql@v2.12.1/mod.ts",
|
||||||
"@nanoid": "https://deno.land/x/nanoid@v3.0.0/mod.ts",
|
"@nanoid": "https://deno.land/x/nanoid@v3.0.0/mod.ts",
|
||||||
"@std/http": "jsr:@std/http@1.0.15",
|
"@std/http": "jsr:@std/http@1.0.15",
|
||||||
"~config": "./config.ts"
|
"~config": "./config.ts",
|
||||||
|
"db/": "./db/"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
37
mod.ts
37
mod.ts
@@ -3,6 +3,8 @@ import { STATUS_CODE, STATUS_TEXT, StatusCode } from '@std/http';
|
|||||||
|
|
||||||
import config from '~config';
|
import config from '~config';
|
||||||
|
|
||||||
|
import dbClient from 'db/client.ts';
|
||||||
|
|
||||||
// Using custom alphabet to exclude - and _
|
// Using custom alphabet to exclude - and _
|
||||||
const alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
|
const alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
|
||||||
const nanoid = customAlphabet(alphabet, 20);
|
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] });
|
new Response(customText || STATUS_TEXT[status], { status: status, statusText: STATUS_TEXT[status] });
|
||||||
|
|
||||||
Deno.serve({ port: config.api.port }, async (req) => {
|
Deno.serve({ port: config.api.port }, async (req) => {
|
||||||
const [urlPath, tempQ] = req.url.split('?');
|
const urlPath = req.url.split('?')[0] ?? '';
|
||||||
const path = (urlPath.split('api/')[1] ?? '').toLowerCase().trim();
|
const path = (urlPath.split('api')[1] ?? '').toLowerCase().trim();
|
||||||
console.log(urlPath, path);
|
console.log(urlPath, path);
|
||||||
|
|
||||||
if (req.method === 'GET') {
|
if (req.method === 'GET') {
|
||||||
// handle all gets
|
// handle all gets
|
||||||
} else if (path === '/auth') {
|
} else if (req.method === 'POST' && path === '/enroll') {
|
||||||
return new Response(nanoid());
|
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 {
|
} else {
|
||||||
// handle auth then all other shiz
|
// handle auth then all other shiz
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user