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 prisma, { Prisma } from "@server/prisma";
|
||||
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) => {
|
||||
logger.info(`Getting style: ${request.query.id}`);
|
||||
|
||||
if (!request.query.id) {
|
||||
response.status(400).json({ error: "missing 'id' query" });
|
||||
return;
|
||||
@ -22,121 +26,95 @@ export const get = async (request: Request, response: Response) => {
|
||||
};
|
||||
|
||||
export const post = async (request: Request, response: Response) => {
|
||||
const guildId = request.params.guildId;
|
||||
const {
|
||||
name,
|
||||
show_author,
|
||||
show_image,
|
||||
show_thumbnail,
|
||||
show_footer,
|
||||
show_timestamp,
|
||||
colour,
|
||||
title_mutator,
|
||||
description_mutator
|
||||
} = request.body;
|
||||
logger.info(`Posting style: ${request.body.colour} - ${request.params.guildId}`);
|
||||
|
||||
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 {
|
||||
style = await prisma.messageStyle.create({
|
||||
data: {
|
||||
name: name,
|
||||
guild_id: guildId,
|
||||
show_author: show_author === "on",
|
||||
show_image: show_image === "on",
|
||||
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
|
||||
}
|
||||
});
|
||||
const createResponse = await prisma.messageStyle.create({ data: createInputData });
|
||||
response.status(201).json(createResponse);
|
||||
} 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(201).json(style);
|
||||
};
|
||||
|
||||
export const patch = async (request: Request, response: Response) => {
|
||||
const guildId = request.params.guildId;
|
||||
const {
|
||||
id,
|
||||
name,
|
||||
show_author,
|
||||
show_image,
|
||||
show_thumbnail,
|
||||
show_footer,
|
||||
show_timestamp,
|
||||
colour,
|
||||
title_mutator,
|
||||
description_mutator
|
||||
} = request.body;
|
||||
logger.info(`Patching style: ${request.body.id} - ${request.params.guildId}`);
|
||||
|
||||
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 {
|
||||
style = await prisma.messageStyle.update({
|
||||
where: { id: Number(id) },
|
||||
data: {
|
||||
name: name,
|
||||
guild_id: guildId,
|
||||
show_author: show_author === "on",
|
||||
show_image: show_image === "on",
|
||||
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
|
||||
}
|
||||
});
|
||||
const updateArgs = { where: { id: Number(body.id) }, data: updateInputData };
|
||||
const updateResponse = prisma.messageStyle.update(updateArgs);
|
||||
response.status(200).json(updateResponse);
|
||||
} 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(201).json(style);
|
||||
};
|
||||
|
||||
export const del = async (request: Request, response: Response) => {
|
||||
let { ids } = request.body;
|
||||
const guildId = request.params.guildId;
|
||||
logger.info(`Deleting style(s): ${request.body.ids} - ${request.params.guildId}`)
|
||||
|
||||
if (!ids || !Array.isArray(ids)) {
|
||||
response.status(400).json({ error: "invalid request body" });
|
||||
const ids = request.body.ids?.map((id: string) => Number(id));
|
||||
|
||||
if (!ids) {
|
||||
response.status(400).json({ error: "Couldn't parse ID's from request body" });
|
||||
return;
|
||||
}
|
||||
|
||||
ids = ids.map(id => Number(id));
|
||||
|
||||
try {
|
||||
await prisma.messageStyle.deleteMany({ where: {
|
||||
id: { in: ids },
|
||||
guild_id: guildId
|
||||
}});
|
||||
const deleteArgs = { where: { guild_id: request.params.guildId, id: { in: ids } } };
|
||||
await prisma.messageStyle.deleteMany(deleteArgs);
|
||||
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) => {
|
||||
@ -151,13 +129,10 @@ export const datatable = 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({
|
||||
where: {
|
||||
guild_id: guildId,
|
||||
name: { contains: `${search}` }
|
||||
guild_id: request.params.guildId,
|
||||
name: { contains: `${request.query.search}` }
|
||||
}
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user