feat(api): added message style api endpoints
This commit is contained in:
parent
6d1f4e6f7f
commit
a1bd362799
150
src/server/controllers/guild/api/style.controller.ts
Normal file
150
src/server/controllers/guild/api/style.controller.ts
Normal file
@ -0,0 +1,150 @@
|
||||
import { Request, Response } from "express";
|
||||
import prisma, { Prisma } from "@server/prisma";
|
||||
import { datatableRequest } from "@server/controllers/guild/api/dt.module";
|
||||
|
||||
export const get = async (request: Request, response: Response) => {
|
||||
if (!request.query.id) {
|
||||
response.status(400).json({ error: "missing 'id' query" });
|
||||
return;
|
||||
}
|
||||
|
||||
const style = await prisma.messageStyle.findUnique({
|
||||
where: { id: Number(request.query.id) },
|
||||
});
|
||||
|
||||
if (!style) {
|
||||
response.status(404).json({ message: "no result found" });
|
||||
return;
|
||||
}
|
||||
|
||||
response.json(style);
|
||||
};
|
||||
|
||||
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;
|
||||
|
||||
let style;
|
||||
|
||||
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,
|
||||
description_mutator: description_mutator
|
||||
}
|
||||
});
|
||||
}
|
||||
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;
|
||||
|
||||
let style;
|
||||
|
||||
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,
|
||||
description_mutator: description_mutator
|
||||
}
|
||||
});
|
||||
}
|
||||
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;
|
||||
|
||||
if (!ids || !Array.isArray(ids)) {
|
||||
response.status(400).json({ error: "invalid request body" });
|
||||
return;
|
||||
}
|
||||
|
||||
ids = ids.map(id => Number(id));
|
||||
|
||||
try {
|
||||
await prisma.messageStyle.deleteMany({ where: {
|
||||
id: { in: ids },
|
||||
guild_id: guildId
|
||||
}});
|
||||
}
|
||||
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) => {
|
||||
return await datatableRequest(
|
||||
request,
|
||||
response,
|
||||
prisma.messageStyle,
|
||||
[{ updated_at: "desc" }, { id: "asc" }],
|
||||
{ },
|
||||
{ guild_id: request.params.guildId } // TODO: verify authenticated user can access this guild
|
||||
);
|
||||
};
|
||||
|
||||
export default { get, post, patch, del, datatable };
|
@ -7,6 +7,7 @@ import contentController from "@server/controllers/guild/content.controller";
|
||||
|
||||
import feedApiController from "@server/controllers/guild/api/feed.controller";
|
||||
import filterApiController from "@server/controllers/guild/api/filter.controller";
|
||||
import styleApiController from "@server/controllers/guild/api/style.controller";
|
||||
|
||||
const router = Router();
|
||||
|
||||
@ -38,4 +39,10 @@ router.post("/:guildId/filters/api", filterApiController.post);
|
||||
router.patch("/:guildId/filters/api", filterApiController.patch);
|
||||
router.delete("/:guildId/filters/api", filterApiController.del);
|
||||
|
||||
router.post("/:guildId/styles/api/datatable", styleApiController.datatable);
|
||||
router.get("/:guildId/styles/api", styleApiController.get);
|
||||
router.post("/:guildId/styles/api", styleApiController.post);
|
||||
router.patch("/:guildId/styles/api", styleApiController.patch);
|
||||
router.delete("/:guildId/styles/api", styleApiController.del);
|
||||
|
||||
export default router;
|
Loading…
x
Reference in New Issue
Block a user