backend fixes with api routes and datatables route
This commit is contained in:
parent
c53435027c
commit
443b1cb223
@ -1,7 +1,7 @@
|
|||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import { buildDatatableQuery } from "@utils/datatable";
|
import { buildDatatableQuery } from "@utils/datatable";
|
||||||
import { db } from "@db/db";
|
import { db } from "@db/db";
|
||||||
import { client as bot } from "@bot/bot";
|
import { Subscription, Channel } from "@db/models/subs.model";
|
||||||
|
|
||||||
const isPostgres = db.client.config.client === "pg";
|
const isPostgres = db.client.config.client === "pg";
|
||||||
const TABLE = "subscriptions";
|
const TABLE = "subscriptions";
|
||||||
@ -10,34 +10,29 @@ export const datatable = async (request: Request, response: Response) => {
|
|||||||
try {
|
try {
|
||||||
|
|
||||||
let query = db(TABLE).where({ guild_id: request.params.guildId });
|
let query = db(TABLE).where({ guild_id: request.params.guildId });
|
||||||
// query.select("subscriptions.*")
|
|
||||||
// .leftJoin("channels", "subscriptions.id", "=", "channels.subscription_id")
|
|
||||||
// .select(db.raw(isPostgres
|
|
||||||
// ? "json_agg(channels.channel_id) as channels"
|
|
||||||
// :"JSON_GROUP_ARRAY(channels.channel_id) as channels"
|
|
||||||
// ))
|
|
||||||
// .groupBy("subscriptions.id")
|
|
||||||
|
|
||||||
const datatableQuery = await buildDatatableQuery(request as any, query, TABLE);
|
const datatableQuery = await buildDatatableQuery(request as any, query, TABLE);
|
||||||
const { recordsTotal, recordsFiltered } = datatableQuery;
|
const { recordsTotal, recordsFiltered } = datatableQuery;
|
||||||
query = datatableQuery.query;
|
query = datatableQuery.query;
|
||||||
|
|
||||||
|
query.select("subscriptions.*")
|
||||||
|
.leftJoin("channels", "subscriptions.id", "=", "channels.subscription_id")
|
||||||
|
.select(db.raw(isPostgres
|
||||||
|
? "json_agg(channels.channel_id) as channels"
|
||||||
|
:"JSON_GROUP_ARRAY(channels.channel_id) as channels"
|
||||||
|
))
|
||||||
|
.groupBy("subscriptions.id")
|
||||||
|
|
||||||
|
|
||||||
const data = await query;
|
const data = await query;
|
||||||
|
|
||||||
console.log(`total: ${recordsTotal} filtered: ${recordsFiltered} filtered+paged: ${data.length}`);
|
console.log(`total: ${recordsTotal} filtered: ${recordsFiltered} filtered+paged: ${data.length}`);
|
||||||
|
|
||||||
// data.forEach((item: any) => {
|
data.forEach((item: any) => {
|
||||||
// item.channels = item.channels === "[null]"
|
item.channels = item.channels === "[null]"
|
||||||
// ? []
|
? []
|
||||||
// : JSON.parse(item.channels);
|
: JSON.parse(item.channels);
|
||||||
|
});
|
||||||
// // item.channels.forEach((channel: any) => {
|
|
||||||
// // channel = {
|
|
||||||
// // "channel_id": channel,
|
|
||||||
// // "name": bot.channels.cache.filter(a => a.id === id).first().name;
|
|
||||||
// // }
|
|
||||||
// // })
|
|
||||||
// });
|
|
||||||
|
|
||||||
response.json({
|
response.json({
|
||||||
data,
|
data,
|
||||||
@ -54,7 +49,7 @@ export const datatable = async (request: Request, response: Response) => {
|
|||||||
export const get = async (request: Request, response: Response) => {
|
export const get = async (request: Request, response: Response) => {
|
||||||
try {
|
try {
|
||||||
if (!request.query.id) {
|
if (!request.query.id) {
|
||||||
response.status(400).json({ message: "missing 'id' query" });
|
response.status(400).json({ error: "missing 'id' query" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,11 +81,41 @@ export const get = async (request: Request, response: Response) => {
|
|||||||
|
|
||||||
export const post = async (request: Request, response: Response) => {
|
export const post = async (request: Request, response: Response) => {
|
||||||
try {
|
try {
|
||||||
|
console.debug(JSON.stringify(request.body, null, 4));
|
||||||
|
|
||||||
|
const guild_id = request.params.guildId;
|
||||||
|
const { name, url, message_style, published_threshold, channels, filters, active } = request.body;
|
||||||
|
|
||||||
|
if (!name || !url || !message_style || !published_threshold) {
|
||||||
|
response.status(400).json({ error: "Missing required fields" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const [subscription] = await db<Subscription>(TABLE)
|
||||||
|
.insert({
|
||||||
|
name,
|
||||||
|
url,
|
||||||
|
guild_id,
|
||||||
|
active: active ?? true,
|
||||||
|
created_at: new Date(),
|
||||||
|
updated_at: new Date()
|
||||||
|
})
|
||||||
|
.returning("*");
|
||||||
|
|
||||||
|
if (channels && channels.length) {
|
||||||
|
const channelData = channels.map((channel_id: string) => ({
|
||||||
|
channel_id,
|
||||||
|
subscription_id: subscription.id
|
||||||
|
}));
|
||||||
|
|
||||||
|
await db<Channel>("channels").insert(channelData);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//
|
response.status(201).json({
|
||||||
console.log(JSON.stringify(request.body, null, 4));
|
...subscription,
|
||||||
response.json({ message: "default response" });
|
channels: channels ?? []
|
||||||
|
});
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
@ -98,4 +123,26 @@ export const post = async (request: Request, response: Response) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default { datatable, get, post }
|
export const del = async (request: Request, response: Response) => {
|
||||||
|
try {
|
||||||
|
let ids: any = request.query.id;
|
||||||
|
|
||||||
|
if (!ids) {
|
||||||
|
response.status(400).json({ error: "missing 'id' query" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(ids)) {
|
||||||
|
ids = [ids];
|
||||||
|
}
|
||||||
|
|
||||||
|
await db(TABLE).whereIn("id", ids).delete();
|
||||||
|
response.status(204).json(null);
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
response.status(500).json({ error: "Failed to delete subscription" })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default { datatable, get, post, del }
|
@ -10,6 +10,8 @@ export const get = async (request: Request, response: Response) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(guild.toJSON());
|
||||||
|
|
||||||
response.render("guild/subscriptions", {
|
response.render("guild/subscriptions", {
|
||||||
title: `${guild.name} - Relay`,
|
title: `${guild.name} - Relay`,
|
||||||
guild: guild,
|
guild: guild,
|
||||||
|
@ -6,5 +6,6 @@ const router = Router();
|
|||||||
router.get("/:guildId/subscriptions/api/datatable", subApiController.datatable);
|
router.get("/:guildId/subscriptions/api/datatable", subApiController.datatable);
|
||||||
router.get("/:guildId/subscriptions/api", subApiController.get);
|
router.get("/:guildId/subscriptions/api", subApiController.get);
|
||||||
router.post("/:guildId/subscriptions/api", subApiController.post);
|
router.post("/:guildId/subscriptions/api", subApiController.post);
|
||||||
|
router.delete("/:guildId/subscriptions/api", subApiController.del);
|
||||||
|
|
||||||
export default router;
|
export default router;
|
@ -30,8 +30,11 @@ export const buildDatatableQuery = async (request: { query: RequestQuery }, quer
|
|||||||
const direction: string = (request.query.order && request.query.order[0].dir) || "asc";
|
const direction: string = (request.query.order && request.query.order[0].dir) || "asc";
|
||||||
const search: string = request.query.search.value;
|
const search: string = request.query.search.value;
|
||||||
|
|
||||||
|
const recordsTotalResult = await query.clone().count("* as count").first();
|
||||||
|
const recordsTotal = recordsTotalResult ? recordsTotalResult.count : 0;
|
||||||
|
|
||||||
const filterConfig: FilterConfig = {
|
const filterConfig: FilterConfig = {
|
||||||
active: (value: number) => query.where('active', '=', value),
|
active: (value: string) => query.where('active', '=', value),
|
||||||
};
|
};
|
||||||
|
|
||||||
if (request.query.filters) {
|
if (request.query.filters) {
|
||||||
@ -57,15 +60,9 @@ export const buildDatatableQuery = async (request: { query: RequestQuery }, quer
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const recordsTotalResult = await db(tableName).count("* as count").first();
|
|
||||||
const recordsTotal = recordsTotalResult ? recordsTotalResult.count : 0;
|
|
||||||
|
|
||||||
const recordsFilteredResult = await query.clone().count("* as count").first();
|
const recordsFilteredResult = await query.clone().count("* as count").first();
|
||||||
const recordsFiltered = recordsFilteredResult ? recordsFilteredResult.count : 0;
|
const recordsFiltered = recordsFilteredResult ? recordsFilteredResult.count : 0;
|
||||||
|
|
||||||
console.log(query.toSQL())
|
|
||||||
console.log(query.clone().count("* as count").toSQL())
|
|
||||||
|
|
||||||
query = query.orderBy(`${tableName}.${order}`, direction).limit(size).offset(start);
|
query = query.orderBy(`${tableName}.${order}`, direction).limit(size).offset(start);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user