Skip to content

Commit

Permalink
jsonifier
Browse files Browse the repository at this point in the history
  • Loading branch information
LarmuseauNiels committed Mar 31, 2023
1 parent f7228be commit a508a74
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions modules/WebApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,47 @@ export class WebApi {
}

setLegacyEndPoints() {
let jsonify = this.jsonify;
app.get("/activity", async function (req, res) {
let results = await globalThis.client.prisma
.$queryRaw`select timestamp,count(*) as online from VoiceConnected WHERE VoiceConnected.TimeStamp >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY group by timestamp`;
res.send(JSON.stringify(results));
res.send(jsonify(results));
});

app.get("/activityFromDate/:date", async function (req, res) {
var date = req.params["date"];
let results = await globalThis.client.prisma
.$queryRaw`select timestamp,count(*) as online from VoiceConnected WHERE date(TimeStamp) = ${date} group by timestamp `;
res.send(JSON.stringify(results));
res.send(jsonify(results));
});

app.get("/userActivityDate/:date", async function (req, res) {
var date = req.params["date"];
let results = await globalThis.client.prisma
.$queryRaw`SELECT Members.DisplayName as name, count(*) as y FROM VoiceConnected LEFT JOIN Members ON VoiceConnected.ID = Members.ID
WHERE date(TimeStamp) = ${date} GROUP BY VoiceConnected.ID order by y desc`;
res.send(JSON.stringify(results));
res.send(jsonify(results));
});

app.get("/channelActivity", async function (req, res) {
let results = await globalThis.client.prisma
.$queryRaw`select ChannelName as name, count(*) as y from VoiceConnected
WHERE VoiceConnected.TimeStamp >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY group by ChannelName`;
res.send(JSON.stringify(results));
res.send(jsonify(results));
});

app.get("/userActivity", async function (req, res) {
let results = await globalThis.client.prisma
.$queryRaw`SELECT Members.DisplayName as name, count(*) as y FROM VoiceConnected LEFT JOIN Members ON VoiceConnected.ID = Members.ID
WHERE VoiceConnected.TimeStamp >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY GROUP BY VoiceConnected.ID order by y desc`;
res.send(JSON.stringify(results));
res.send(jsonify(results));
});

app.get("/userActivityAll", async function (req, res) {
let results = await globalThis.client.prisma
.$queryRaw`SELECT Members.DisplayName as name, count(*) as y FROM VoiceConnected LEFT JOIN Members ON VoiceConnected.ID = Members.ID
GROUP BY VoiceConnected.ID order by y desc`;
res.send(JSON.stringify(results));
res.send(jsonify(results));
});

app.get("/userOnlineTimes/:userId", async function (req, res) {
Expand All @@ -61,43 +62,49 @@ export class WebApi {
.$queryRaw`SELECT timestamp, 1 as online FROM VoiceConnected
JOIN Channel on Channel.ID = VoiceConnected.ChannelID
WHERE VoiceConnected.TimeStamp >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY AND VoiceConnected.ID = ${userId}`;
res.send(JSON.stringify(results));
res.send(jsonify(results));
});

app.get("/userInfo/:userId", async function (req, res) {
var userId = req.params["userId"];
let results = await globalThis.client.prisma
.$queryRaw`SELECT * FROM Members where ID = ${userId}`;
res.send(JSON.stringify(results));
res.send(jsonify(results));
});

app.get("/PossibleYears", async function (req, res) {
let results = await globalThis.client.prisma
.$queryRaw`select DISTINCT YEAR(timestamp) from VoiceConnected`;
res.send(JSON.stringify(results));
res.send(jsonify(results));
});

app.get("/YearActivity/:year", async function (req, res) {
var year = req.params["year"];
let results = await globalThis.client.prisma
.$queryRaw`select MONTH(timestamp) as month, DAY(timestamp) as day ,YEAR(timestamp) as year, count(*) as online from VoiceConnected
Where YEAR(timestamp) = ${year} group by year,month, day`;
res.send(JSON.stringify(results));
res.send(jsonify(results));
});

app.get("/events", function (req, res) {
if (globalThis.client.events == null) {
client.guilds.fetch("530537522355240961").then((guild) => {
guild.scheduledEvents.fetch().then((events) => {
res.send(JSON.stringify(events));
res.send(jsonify(events));
});
});
} else {
res.send(JSON.stringify(globalThis.client.events));
res.send(jsonify(globalThis.client.events));
}
});
}

jsonify(obj) {
return JSON.stringify(obj, (key, value) =>
typeof value === "bigint" ? value.toString() : value
);
}

load() {
app.listen(3000, () => {
console.log("WebApi listening on port 3000!");
Expand Down

0 comments on commit a508a74

Please sign in to comment.