fix(guild): use guild id for members endpoint

This commit is contained in:
Axel Cocat 2022-03-17 00:26:09 +01:00
parent 4af6e7e8d0
commit c251b8de27
5 changed files with 21 additions and 8 deletions

View file

@ -151,7 +151,7 @@ export class Armory {
const guildsController = new GuildController(this);
app.get("/guild/:realm/:name", this.wrapRoute(guildsController.guild.bind(guildsController)));
app.get("/guild/:realm/:name/members", this.wrapRoute(guildsController.members.bind(guildsController)));
app.get("/guild/:realm/:guild/members", this.wrapRoute(guildsController.members.bind(guildsController)));
app.use((err, req: express.Request, res: express.Response, next: express.NextFunction) => {
// Error handler

View file

@ -44,7 +44,7 @@ export class GuildController {
public async members(req: express.Request, res: express.Response, next: express.NextFunction): Promise<void> {
const realmName = req.params.realm;
const guildName = req.params.name;
const guildId = parseInt(req.params.guild);
const realm = this.armory.getRealm(realmName);
if (realm === undefined) {
@ -52,8 +52,7 @@ export class GuildController {
return next(404);
}
const guildId = await this.getGuildId(realm, guildName);
if (guildId === null) {
if (isNaN(guildId) || !(await this.guildExists(realm, guildId))) {
// Could not find guild
return next(404);
}
@ -155,6 +154,20 @@ export class GuildController {
return rows[0].guildid;
}
private async guildExists(realm: IRealmConfig, id: number): Promise<boolean> {
const db = this.armory.getCharactersDb(realm.name);
const [rows, fields] = await db.query({
sql: `
SELECT guildid
FROM guild WHERE guildid
`,
values: [id],
timeout: this.armory.config.dbQueryTimeout,
});
return (rows as RowDataPacket[]).length !== 0;
}
private async getGuildRanks(realm: IRealmConfig, id: number): Promise<IGuildRank[]> {
const db = this.armory.getCharactersDb(realm.name);
const [rows, fields] = await db.query({