mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-07-12 21:47:20 +00:00
feat: add tag aliases support
This commit is contained in:
parent
bc830cf301
commit
5846012a0c
10 changed files with 162 additions and 30 deletions
66
backend/src/data/GuildTagAliases.ts
Normal file
66
backend/src/data/GuildTagAliases.ts
Normal file
|
@ -0,0 +1,66 @@
|
|||
import { getRepository, Repository } from "typeorm";
|
||||
import { BaseGuildRepository } from "./BaseGuildRepository";
|
||||
import { TagAlias } from "./entities/TagAlias";
|
||||
|
||||
export class GuildTagAliases extends BaseGuildRepository {
|
||||
private tagAliases: Repository<TagAlias>;
|
||||
|
||||
constructor(guildId) {
|
||||
super(guildId);
|
||||
this.tagAliases = getRepository(TagAlias);
|
||||
}
|
||||
|
||||
async all(): Promise<TagAlias[]> {
|
||||
return this.tagAliases.find({
|
||||
where: {
|
||||
guild_id: this.guildId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async find(alias): Promise<TagAlias | undefined> {
|
||||
return this.tagAliases.findOne({
|
||||
where: {
|
||||
guild_id: this.guildId,
|
||||
alias,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async findAllWithTag(tag): Promise<TagAlias[] | undefined> {
|
||||
const all = await this.all();
|
||||
const aliases = all.filter((a) => a.tag === tag);
|
||||
return aliases.length > 0 ? aliases : undefined;
|
||||
}
|
||||
|
||||
async createOrUpdate(alias, tag, userId) {
|
||||
const existingTagAlias = await this.find(alias);
|
||||
if (existingTagAlias) {
|
||||
await this.tagAliases
|
||||
.createQueryBuilder()
|
||||
.update()
|
||||
.set({
|
||||
tag,
|
||||
user_id: userId,
|
||||
created_at: () => "NOW()",
|
||||
})
|
||||
.where("guild_id = :guildId", { guildId: this.guildId })
|
||||
.andWhere("alias = :alias", { alias })
|
||||
.execute();
|
||||
} else {
|
||||
await this.tagAliases.insert({
|
||||
guild_id: this.guildId,
|
||||
user_id: userId,
|
||||
alias,
|
||||
tag,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async delete(alias) {
|
||||
await this.tagAliases.delete({
|
||||
guild_id: this.guildId,
|
||||
alias,
|
||||
});
|
||||
}
|
||||
}
|
20
backend/src/data/entities/TagAlias.ts
Normal file
20
backend/src/data/entities/TagAlias.ts
Normal file
|
@ -0,0 +1,20 @@
|
|||
import { Column, Entity, PrimaryColumn } from "typeorm";
|
||||
|
||||
@Entity("tag_aliases")
|
||||
export class TagAlias {
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
guild_id: string;
|
||||
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
alias: string;
|
||||
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
tag: string;
|
||||
|
||||
@Column() user_id: string;
|
||||
|
||||
@Column() created_at: string;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue