Skip to content

Commit

Permalink
fix data
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasB25 committed Sep 11, 2024
1 parent 563fc9c commit 3d653c3
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 102 deletions.
79 changes: 42 additions & 37 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,72 @@
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "debian-openssl-3.0.x"]
provider = "prisma-client-js"
binaryTargets = ["native", "debian-openssl-3.0.x"]
}

datasource db {
provider = "sqlite"
url = "file:./lavamusic.db"
provider = "sqlite"
url = "file:./lavamusic.db"
}

model Bot {
botId String @unique
totalPlaySong Int
}

model Guild {
guildId String @id
prefix String
language String?
stay Stay?
dj Dj?
roles Role[]
setup Setup?
guildId String @id
prefix String
language String? @default("EnglishUS")
stay Stay?
dj Dj?
roles Role[]
setup Setup?
}

model Stay {
guildId String @id
textId String
voiceId String
Guild Guild @relation(fields: [guildId], references: [guildId])
guildId String @id
textId String
voiceId String
Guild Guild @relation(fields: [guildId], references: [guildId])
}

model Dj {
guildId String @id
mode Boolean
Guild Guild @relation(fields: [guildId], references: [guildId])
guildId String @id
mode Boolean
Guild Guild @relation(fields: [guildId], references: [guildId])
}

model Role {
guildId String
roleId String
Guild Guild @relation(fields: [guildId], references: [guildId])
guildId String
roleId String
Guild Guild @relation(fields: [guildId], references: [guildId])
@@unique([guildId, roleId])
@@unique([guildId, roleId])
}

model Playlist {
id String @id @default(uuid())
userId String
name String
songs Song[]
id String @id @default(uuid())
userId String
name String
songs Song[]
@@unique([userId, name])
@@unique([userId, name])
}

model Song {
id String @id @default(uuid())
track String
playlistId String
playlist Playlist @relation(fields: [playlistId], references: [id])
id String @id @default(uuid())
track String
playlistId String
playlist Playlist @relation(fields: [playlistId], references: [id])
@@unique([track, playlistId])
@@unique([track, playlistId])
}

model Setup {
guildId String @id
textId String
messageId String
Guild Guild @relation(fields: [guildId], references: [guildId])
}
guildId String @id
textId String
messageId String
Guild Guild @relation(fields: [guildId], references: [guildId])
}
120 changes: 55 additions & 65 deletions src/database/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Dj, type Guild, type Playlist, PrismaClient, type Role, type Setup, type Song, type Stay } from "@prisma/client";
import { type Dj, type Guild, type Playlist, PrismaClient, type Role, type Song, type Setup, type Stay } from "@prisma/client";
import config from "../config.js";

export default class ServerData {
Expand All @@ -9,16 +9,15 @@ export default class ServerData {
}

public async get(guildId: string): Promise<Guild | null> {
return (
(await this.prisma.guild.findUnique({
where: { guildId },
})) ?? this.createGuild(guildId)
);
return (await this.prisma.guild.findUnique({ where: { guildId } })) ?? this.createGuild(guildId);
}

private async createGuild(guildId: string): Promise<Guild> {
return await this.prisma.guild.create({
data: { guildId, prefix: config.prefix },
data: {
guildId,
prefix: config.prefix,
},
});
}

Expand All @@ -30,6 +29,39 @@ export default class ServerData {
});
}

public async getPrefix(guildId: string): Promise<string> {
const guild = await this.get(guildId);
return guild?.prefix ?? config.prefix;
}

public async updateLanguage(guildId: string, language: string): Promise<void> {
await this.prisma.guild.update({
where: { guildId },
data: { language },
});
}

public async getLanguage(guildId: string): Promise<string> {
const guild = await this.get(guildId);
return guild?.language ?? config.defaultLanguage;
}

public async getSetup(guildId: string): Promise<Setup | null> {
return await this.prisma.setup.findUnique({ where: { guildId } });
}

public async setSetup(guildId: string, textId: string, messageId: string): Promise<void> {
await this.prisma.setup.upsert({
where: { guildId },
update: { textId, messageId },
create: { guildId, textId, messageId },
});
}

public async deleteSetup(guildId: string): Promise<void> {
await this.prisma.setup.delete({ where: { guildId } });
}

public async set_247(guildId: string, textId: string, voiceId: string): Promise<void> {
await this.prisma.stay.upsert({
where: { guildId },
Expand All @@ -42,6 +74,13 @@ export default class ServerData {
await this.prisma.stay.delete({ where: { guildId } });
}

public async get_247(guildId?: string): Promise<Stay | Stay[]> {
if (guildId) {
return await this.prisma.stay.findUnique({ where: { guildId } });
}
return this.prisma.stay.findMany();
}

public async setDj(guildId: string, mode: boolean): Promise<void> {
await this.prisma.dj.upsert({
where: { guildId },
Expand All @@ -50,13 +89,6 @@ export default class ServerData {
});
}

public async get_247(guildId?: string): Promise<Stay | Stay[]> {
if (guildId) {
return await this.prisma.stay.findUnique({ where: { guildId } });
}
return this.prisma.stay.findMany();
}

public async getDj(guildId: string): Promise<Dj | null> {
return await this.prisma.dj.findUnique({ where: { guildId } });
}
Expand All @@ -77,36 +109,22 @@ export default class ServerData {
await this.prisma.role.deleteMany({ where: { guildId } });
}

public async getSetup(guildId: string): Promise<Setup | null> {
return await this.prisma.setup.findUnique({ where: { guildId } });
}

public async setSetup(guildId: string, textId: string, messageId: string): Promise<void> {
await this.prisma.setup.upsert({
where: { guildId },
update: { textId, messageId },
create: { guildId, textId, messageId },
});
}

public async deleteSetup(guildId: string): Promise<void> {
await this.prisma.setup.delete({ where: { guildId } });
}

public async getPlaylist(userId: string, name: string): Promise<Playlist | null> {
return await this.prisma.playlist.findUnique({
where: { userId_name: { userId, name } },
});
}

public async getUserPlaylists(userId: string) {
public async getUserPlaylists(userId: string): Promise<Playlist[]> {
return await this.prisma.playlist.findMany({
where: {
userId: userId,
},
where: { userId },
});
}

public async createPlaylist(userId: string, name: string): Promise<void> {
await this.prisma.playlist.create({ data: { userId, name } });
}

public async createPlaylistWithSongs(userId: string, name: string, songs: any[]): Promise<void> {
await this.prisma.playlist.create({
data: {
Expand All @@ -119,10 +137,6 @@ export default class ServerData {
});
}

public async createPlaylist(userId: string, name: string): Promise<void> {
await this.prisma.playlist.create({ data: { userId, name } });
}

public async deletePlaylist(userId: string, name: string): Promise<void> {
await this.prisma.playlist.delete({
where: { userId_name: { userId, name } },
Expand Down Expand Up @@ -161,9 +175,7 @@ export default class ServerData {
await this.prisma.song.deleteMany({
where: {
playlistId: playlist.id,
track: {
contains: encodedSong,
},
track: { contains: encodedSong },
},
});
}
Expand All @@ -172,19 +184,15 @@ export default class ServerData {
public async getSongs(userId: string, name: string): Promise<Song[]> {
const playlist = await this.getPlaylist(userId, name);
if (playlist) {
return this.prisma.song.findMany({
where: { playlistId: playlist.id },
});
return this.prisma.song.findMany({ where: { playlistId: playlist.id } });
}
return [];
}

public async clearPlaylist(userId: string, name: string): Promise<void> {
const playlist = await this.getPlaylist(userId, name);
if (playlist) {
await this.prisma.song.deleteMany({
where: { playlistId: playlist.id },
});
await this.prisma.song.deleteMany({ where: { playlistId: playlist.id } });
}
}

Expand All @@ -199,24 +207,6 @@ export default class ServerData {
public async clearAllSongs(): Promise<void> {
await this.prisma.song.deleteMany();
}

public async updateLanguage(guildId: string, language: string): Promise<void> {
const guild = await this.get(guildId);
if (guild) {
await this.prisma.guild.update({
where: { guildId },
data: { language },
});
} else {
await this.createGuild(guildId);
await this.updateLanguage(guildId, language);
}
}

public async getLanguage(guildId: string): Promise<string> {
const guild = await this.get(guildId);
return guild?.language ?? config.defaultLanguage;
}
}

/**
Expand Down

0 comments on commit 3d653c3

Please sign in to comment.