Skip to content

Commit

Permalink
Merge branch 'master' into sqlite-overhaul
Browse files Browse the repository at this point in the history
Nathaniel Moschkin committed Jun 18, 2024
2 parents cac1c63 + 0f92d19 commit ffbf24f
Showing 3 changed files with 57 additions and 1 deletion.
18 changes: 18 additions & 0 deletions app/controllers/api.controller.ts
Original file line number Diff line number Diff line change
@@ -36,6 +36,24 @@ router.post('/post_profile', async (req: Request, res: Response, next) => {
}
});

router.get('/profile', async (req: Request, res: Response, next) => {
if (!req.query || !req.query.dbid) {
res.status(400).send('Whaat?');
return;
}

const short_crew = !!req.query.short_crew && req.query.short_crew === '1'

try {
let apiResult = await DataCoreAPI.getProfile(req.query.dbid.toString(), short_crew);
res.status(apiResult.Status).send(apiResult.Body);
} catch (e) {
next(e);
}
});



router.post('/login', async (req: Request, res: Response, next) => {
if (!req.body || !req.body.user || !req.body.password) {
res.status(400).send('Whaat?');
35 changes: 34 additions & 1 deletion app/logic/api.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import fetch, { Response } from 'node-fetch';
import { sign, verify } from 'jsonwebtoken';

import { Logger, LogData } from './logger';
import { loadProfileCache, loginUser, getDBIDbyDiscord, uploadProfile, getProfile, getProfileByHash } from './profiletools';
import { loadProfileCache, loginUser, getDBIDbyDiscord, uploadProfile, getProfile, getProfileByHash, loadProfile } from './profiletools';
import { loadCommentsDB, saveCommentDB } from './commenttools';
import { recordTelemetryDB, getTelemetryDB, createStats } from './telemetry';
import { getSTTToken } from './stttools';
@@ -150,6 +150,39 @@ export class ApiClass {
}
}

async getProfile(dbid: string, short_crew?: boolean) {
let profile = await loadProfile(dbid);
if (profile) {
if (short_crew) {
return {
Status: 200,
Body: {
lastUpdate: profile.lastUpdate,
shortCrewList: profile.shortCrewList
}
}
}
else if (fs.existsSync(`${process.env.PROFILE_DATA_PATH}/${dbid}`)) {
return {
Status: 200,
Body: JSON.parse(fs.readFileSync(`${process.env.PROFILE_DATA_PATH}/${dbid}`, 'utf-8'))
}
}
else {
return {
Status: 404,
Body: 'Profile file not found'
}
}
}
else {
return {
Status: 404,
Body: 'Profile record not found'
}
}
}

async loadGauntletStatus(logData: LogData): Promise<ApiResult> {
Logger.info('Load gauntlet status', { logData });

5 changes: 5 additions & 0 deletions app/logic/profiletools.ts
Original file line number Diff line number Diff line change
@@ -99,6 +99,11 @@ export async function loadProfileCache() {
return player_data;
}

export async function loadProfile(dbid: string) {
let res = await Profile.findOne({ where: { dbid }});
return res;
}

export async function loginUser(loginUserName: string, password: string) {
let res = await User.findOne({ where: { loginUserName } });

0 comments on commit ffbf24f

Please sign in to comment.