add getUserID and tests

This commit is contained in:
Michael C
2021-06-25 03:33:41 -04:00
parent e7fed0f3cf
commit d4695f0192
3 changed files with 142 additions and 0 deletions

View File

@@ -32,6 +32,7 @@ import {endpoint as getSegmentInfo} from './routes/getSegmentInfo';
import {postClearCache} from './routes/postClearCache';
import { addUnlistedVideo } from './routes/addUnlistedVideo';
import {postPurgeAllSegments} from './routes/postPurgeAllSegments';
import {getUserID} from './routes/getUserID';
export function createServer(callback: () => void) {
// Create a service (the app object is just a callback).
@@ -146,6 +147,9 @@ function setupRoutes(app: Express) {
app.post('/api/purgeAllSegments', postPurgeAllSegments);
app.post('/api/unlistedVideo', addUnlistedVideo);
// get userID from username
app.get('/api/userID', getUserID);
if (config.postgres) {
app.get('/database', (req, res) => dumpDatabase(req, res, true));

29
src/routes/getUserID.ts Normal file
View File

@@ -0,0 +1,29 @@
import {db} from '../databases/databases';
import {Logger} from '../utils/logger';
import {Request, Response} from 'express';
export async function getUserID(req: Request, res: Response) {
let username = req.query.username as string;
if (username == undefined) {
//invalid request
res.sendStatus(400);
return;
}
// add wildcard to variable
username = `%${username}%`
try {
let rows = await db.prepare('all', `SELECT "userName", "userID" FROM "userNames" WHERE "userName" LIKE ?`, [username]);
if (rows.length === 0) {
res.sendStatus(404);
return;
} else {
res.send(rows);
}
} catch (err) {
Logger.error(err);
res.sendStatus(500);
return;
}
}