Skip to content

Commit

Permalink
Get last Id path.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathaniel Moschkin committed Sep 14, 2024
1 parent fbf6994 commit 1dc61b7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
38 changes: 37 additions & 1 deletion app/abstract/voyagetracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,45 @@ export interface TrackerPostResult {

export abstract class VoyageTrackerBase {

async getLastTrackedId(dbid?: number) {
Logger.info("Last Tracked Id", { dbid });
let trackerId = 0;
if (!dbid)
return {
Status: 400,
Body: { result: "bad input" },
};

try {
trackerId = await this.getLastInsertId(dbid);
} catch (err) {
if (typeof err === "string") {
return {
Status: 500,
Body: err,
};
} else if (err instanceof Error) {
return {
Status: 500,
Body: err.toString(),
};
}
}

return {
Status: 200,
Body: {
dbid: dbid,
lastId: trackerId
},
};
}

async deleteTrackedVoyage(
dbid?: number,
trackerId?: number
): Promise<ApiResult> {
Logger.info("Tracked Voyage data", { dbid, trackerId });
Logger.info("Tracked Voyage Delete", { dbid, trackerId });

if (!dbid || !trackerId)
return {
Expand Down Expand Up @@ -407,5 +441,7 @@ export abstract class VoyageTrackerBase {
timeStamp?: Date
): Promise<number>;

protected abstract getLastInsertId(dbid: number): Promise<number>;

}

18 changes: 16 additions & 2 deletions app/controllers/api.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,22 @@ router.get('/getAssignments', async (req: Request, res: Response, next) => {
}
});


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

try {
let dbid = req.query?.dbid ? Number.parseInt(req.query.dbid.toString()) : undefined;
let apiResult = await VoyageTrackerAPI.getLastTrackedId(dbid);
res.status(apiResult.Status).send(apiResult.Body);
} catch (e) {
next(e);
}
});

router.get('/getTrackedData', async (req: Request, res: Response, next) => {
if (!req.query || (!req.query.dbid && !req.query.trackerId )) {
res.status(400).send('Whaat?');
Expand Down Expand Up @@ -408,8 +424,6 @@ router.delete('/deleteTrackedData', async (req: Request, res: Response, next) =>
}
});



router.post('/postBossBattle', async (req: Request, res: Response, next) => {
if (!req.body) {
res.status(400).send('Whaat?');
Expand Down
15 changes: 15 additions & 0 deletions app/logic/voyagetracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ import { TrackerPostResult, VoyageTrackerBase } from "../abstract/voyagetracker"

export class VoyageTracker extends VoyageTrackerBase {

protected async getLastInsertId(dbid: number): Promise<number> {
let res: TrackedVoyage[] | null = null;

const sql = await makeSql(dbid);
if (sql) {
const repo = sql.getRepository(TrackedVoyage);
let result = await repo.max('id');
return result as number || 0;
// sql?.close();
}

return 0;
}


protected async getVoyagesByDbid(dbid: number) {
let res: TrackedVoyage[] | null = null;

Expand Down

0 comments on commit 1dc61b7

Please sign in to comment.