refactor(api): clean up style api endpoints in line with others, and add logging
This commit is contained in:
parent
e81275cf9f
commit
b209f03cd6
@ -1,9 +1,13 @@
|
|||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import prisma, { Prisma } from "@server/prisma";
|
import prisma, { Prisma } from "@server/prisma";
|
||||||
import { datatableRequest } from "@server/controllers/guild/api/dt.module";
|
import { datatableRequest } from "@server/controllers/guild/api/dt.module";
|
||||||
import { logger } from "@server/../log";
|
import { getLogger } from "@server/../log";
|
||||||
|
|
||||||
|
const logger = getLogger(__filename);
|
||||||
|
|
||||||
export const get = async (request: Request, response: Response) => {
|
export const get = async (request: Request, response: Response) => {
|
||||||
|
logger.info(`Getting style: ${request.query.id}`);
|
||||||
|
|
||||||
if (!request.query.id) {
|
if (!request.query.id) {
|
||||||
response.status(400).json({ error: "missing 'id' query" });
|
response.status(400).json({ error: "missing 'id' query" });
|
||||||
return;
|
return;
|
||||||
@ -22,121 +26,95 @@ export const get = async (request: Request, response: Response) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const post = async (request: Request, response: Response) => {
|
export const post = async (request: Request, response: Response) => {
|
||||||
const guildId = request.params.guildId;
|
logger.info(`Posting style: ${request.body.colour} - ${request.params.guildId}`);
|
||||||
const {
|
|
||||||
name,
|
|
||||||
show_author,
|
|
||||||
show_image,
|
|
||||||
show_thumbnail,
|
|
||||||
show_footer,
|
|
||||||
show_timestamp,
|
|
||||||
colour,
|
|
||||||
title_mutator,
|
|
||||||
description_mutator
|
|
||||||
} = request.body;
|
|
||||||
|
|
||||||
logger.debug("Style Post", request.body);
|
const body = {
|
||||||
|
...request.body,
|
||||||
|
show_author: request.body.show_author === "on",
|
||||||
|
show_image: request.body.show_image === "on",
|
||||||
|
show_thumbnail: request.body.show_thumbnail === "on",
|
||||||
|
show_footer: request.body.show_footer === "on",
|
||||||
|
show_timestamp: request.body.show_timestamp === "on"
|
||||||
|
};
|
||||||
|
|
||||||
let style;
|
const createInputData: Prisma.MessageStyleUncheckedCreateInput = {
|
||||||
|
guild_id: request.params.guildId,
|
||||||
|
name: body.name,
|
||||||
|
colour: body.colour,
|
||||||
|
show_author: body.show_author,
|
||||||
|
show_image: body.show_image,
|
||||||
|
show_thumbnail: body.show_thumbnail,
|
||||||
|
show_footer: body.show_footer,
|
||||||
|
show_timestamp: body.show_timestamp,
|
||||||
|
title_mutator: body.title_mutator || null,
|
||||||
|
description_mutator: body.description_mutator || null
|
||||||
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
style = await prisma.messageStyle.create({
|
const createResponse = await prisma.messageStyle.create({ data: createInputData });
|
||||||
data: {
|
response.status(201).json(createResponse);
|
||||||
name: name,
|
} catch (error) {
|
||||||
guild_id: guildId,
|
logger.error(error);
|
||||||
show_author: show_author === "on",
|
const isPrismaError = error instanceof Prisma.PrismaClientKnownRequestError;
|
||||||
show_image: show_image === "on",
|
response.status(500).json({ error: isPrismaError ? error.message : error });
|
||||||
show_thumbnail: show_thumbnail === "on",
|
|
||||||
show_footer: show_footer === "on",
|
|
||||||
show_timestamp: show_timestamp === "on",
|
|
||||||
colour: colour,
|
|
||||||
title_mutator: title_mutator || null,
|
|
||||||
description_mutator: description_mutator || null
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
|
||||||
response.status(500).json({ error: error.message });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
response.status(201).json(style);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const patch = async (request: Request, response: Response) => {
|
export const patch = async (request: Request, response: Response) => {
|
||||||
const guildId = request.params.guildId;
|
logger.info(`Patching style: ${request.body.id} - ${request.params.guildId}`);
|
||||||
const {
|
|
||||||
id,
|
|
||||||
name,
|
|
||||||
show_author,
|
|
||||||
show_image,
|
|
||||||
show_thumbnail,
|
|
||||||
show_footer,
|
|
||||||
show_timestamp,
|
|
||||||
colour,
|
|
||||||
title_mutator,
|
|
||||||
description_mutator
|
|
||||||
} = request.body;
|
|
||||||
|
|
||||||
let style;
|
const body = {
|
||||||
|
...request.body,
|
||||||
|
show_author: request.body.show_author === "on",
|
||||||
|
show_image: request.body.show_image === "on",
|
||||||
|
show_thumbnail: request.body.show_thumbnail === "on",
|
||||||
|
show_footer: request.body.show_footer === "on",
|
||||||
|
show_timestamp: request.body.show_timestamp === "on"
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateInputData: Prisma.MessageStyleUncheckedUpdateInput = {
|
||||||
|
guild_id: request.params.guildId,
|
||||||
|
name: body.name,
|
||||||
|
colour: body.colour,
|
||||||
|
show_author: body.show_author,
|
||||||
|
show_image: body.show_image,
|
||||||
|
show_thumbnail: body.show_thumbnail,
|
||||||
|
show_footer: body.show_footer,
|
||||||
|
show_timestamp: body.show_timestamp,
|
||||||
|
title_mutator: body.title_mutator || null,
|
||||||
|
description_mutator: body.description_mutator || null
|
||||||
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
style = await prisma.messageStyle.update({
|
const updateArgs = { where: { id: Number(body.id) }, data: updateInputData };
|
||||||
where: { id: Number(id) },
|
const updateResponse = prisma.messageStyle.update(updateArgs);
|
||||||
data: {
|
response.status(200).json(updateResponse);
|
||||||
name: name,
|
} catch (error) {
|
||||||
guild_id: guildId,
|
logger.error(error);
|
||||||
show_author: show_author === "on",
|
const isPrismaError = error instanceof Prisma.PrismaClientKnownRequestError;
|
||||||
show_image: show_image === "on",
|
response.status(500).json({ error: isPrismaError ? error.message : error });
|
||||||
show_thumbnail: show_thumbnail === "on",
|
|
||||||
show_footer: show_footer === "on",
|
|
||||||
show_timestamp: show_timestamp === "on",
|
|
||||||
colour: colour,
|
|
||||||
title_mutator: title_mutator || null,
|
|
||||||
description_mutator: description_mutator || null
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
|
||||||
response.status(500).json({ error: error.message });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
response.status(201).json(style);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const del = async (request: Request, response: Response) => {
|
export const del = async (request: Request, response: Response) => {
|
||||||
let { ids } = request.body;
|
logger.info(`Deleting style(s): ${request.body.ids} - ${request.params.guildId}`)
|
||||||
const guildId = request.params.guildId;
|
|
||||||
|
|
||||||
if (!ids || !Array.isArray(ids)) {
|
const ids = request.body.ids?.map((id: string) => Number(id));
|
||||||
response.status(400).json({ error: "invalid request body" });
|
|
||||||
|
if (!ids) {
|
||||||
|
response.status(400).json({ error: "Couldn't parse ID's from request body" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ids = ids.map(id => Number(id));
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await prisma.messageStyle.deleteMany({ where: {
|
const deleteArgs = { where: { guild_id: request.params.guildId, id: { in: ids } } };
|
||||||
id: { in: ids },
|
await prisma.messageStyle.deleteMany(deleteArgs);
|
||||||
guild_id: guildId
|
response.status(204).send();
|
||||||
}});
|
} catch (error) {
|
||||||
|
logger.error(error);
|
||||||
|
const isPrismaError = error instanceof Prisma.PrismaClientKnownRequestError;
|
||||||
|
response.status(500).json({ error: isPrismaError ? error.message : error });
|
||||||
}
|
}
|
||||||
catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
|
||||||
response.status(500).json({ error: error.message });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
response.status(204).json(null);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const datatable = async (request: Request, response: Response) => {
|
export const datatable = async (request: Request, response: Response) => {
|
||||||
@ -151,13 +129,10 @@ export const datatable = async (request: Request, response: Response) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const select = async (request: Request, response: Response) => {
|
export const select = async (request: Request, response: Response) => {
|
||||||
const guildId = request.params.guildId;
|
|
||||||
const { search } = request.query;
|
|
||||||
|
|
||||||
const data = await prisma.messageStyle.findMany({
|
const data = await prisma.messageStyle.findMany({
|
||||||
where: {
|
where: {
|
||||||
guild_id: guildId,
|
guild_id: request.params.guildId,
|
||||||
name: { contains: `${search}` }
|
name: { contains: `${request.query.search}` }
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user