class implementation with fixes and prisma schema changes
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
const { lvl, randomExp } = require("./tools");
|
||||
const { PrismaClient } = require("@prisma/client");
|
||||
class Postgres {
|
||||
constructor() {
|
||||
this.client = new PrismaClient();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
messageCount: async function (connect, userID) {
|
||||
const update = await connect.users.update({
|
||||
async messageCount(userID) {
|
||||
return await this.client.users.update({
|
||||
where: {
|
||||
user_id: userID,
|
||||
},
|
||||
@@ -12,49 +16,59 @@ module.exports = {
|
||||
},
|
||||
},
|
||||
});
|
||||
return update;
|
||||
},
|
||||
profile: async function (connect, userID) {
|
||||
return await connect.users.findUnique({
|
||||
}
|
||||
|
||||
async profile(userID) {
|
||||
return await this.client.users.findUnique({
|
||||
where: {
|
||||
user_id: userID,
|
||||
},
|
||||
});
|
||||
},
|
||||
addExp: async function (connect, userID, min, max) {
|
||||
const rExp = randomExp(min, max);
|
||||
return await connect.users.upsert({
|
||||
where: {
|
||||
user_id: userID,
|
||||
},
|
||||
create: {
|
||||
user_id: userID,
|
||||
msg_count: 1,
|
||||
exp: randomExp(min, max),
|
||||
lvl: 0,
|
||||
},
|
||||
update: {
|
||||
msg_count: {
|
||||
increment: 1,
|
||||
}
|
||||
|
||||
async addExp(userID, min, max) {
|
||||
await this.#ifExist(userID).then(async () => {
|
||||
const rExp = randomExp(min, max);
|
||||
return await this.client.users.update({
|
||||
where: {
|
||||
user_id: userID,
|
||||
},
|
||||
exp: {
|
||||
increment: rExp,
|
||||
data: {
|
||||
msg_count: {
|
||||
increment: 1,
|
||||
},
|
||||
exp: {
|
||||
increment: rExp,
|
||||
},
|
||||
lvl: lvl(
|
||||
(
|
||||
await this.client.users.findUnique({
|
||||
where: {
|
||||
user_id: userID,
|
||||
},
|
||||
select: {
|
||||
exp: true,
|
||||
},
|
||||
})
|
||||
).exp
|
||||
),
|
||||
},
|
||||
lvl: lvl(
|
||||
(
|
||||
await connect.users.findUnique({
|
||||
where: {
|
||||
user_id: userID,
|
||||
},
|
||||
select: {
|
||||
exp: true,
|
||||
},
|
||||
})
|
||||
).exp + rExp
|
||||
),
|
||||
},
|
||||
});
|
||||
});
|
||||
},
|
||||
addBadge: async function (args) {},
|
||||
giveBadge: async function (args) {},
|
||||
};
|
||||
}
|
||||
async #ifExist(userID) {
|
||||
if ((await !this.profile(userID)) == null) {
|
||||
await this.client.users.create({
|
||||
data: {
|
||||
user_id: userID,
|
||||
msg_count: 0,
|
||||
lvl: 0,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
//addBadge: async function (args) {},
|
||||
//giveBadge: async function (args) {},
|
||||
}
|
||||
module.exports.Postgres = Postgres;
|
||||
|
||||
Reference in New Issue
Block a user