mirror of
https://github.com/detleph/server.git
synced 2026-09-04 08:36:06 +02:00
de-duplicated (un-)linkMedia
This commit is contained in:
@@ -286,61 +286,3 @@ export const deleteDiscipline = async (req: Request<DeleteDisciplineQueryParams>
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
interface visualParams {
|
||||
disciplinePid: string;
|
||||
}
|
||||
|
||||
interface visualBody {
|
||||
mediaPid: string;
|
||||
}
|
||||
|
||||
export const addVisual = async (req: Request<visualParams, {}, visualBody>, res: Response) => {
|
||||
if (req.auth?.permission_level != "ELEVATED") {
|
||||
res.status(403).json(createInsufficientPermissionsError());
|
||||
}
|
||||
|
||||
const { disciplinePid } = req.params;
|
||||
|
||||
const discipline = await prisma.discipline.update({
|
||||
where: { pid: disciplinePid },
|
||||
data: {
|
||||
visual: { connect: { pid: req.body.mediaPid } },
|
||||
},
|
||||
});
|
||||
|
||||
if (!discipline) {
|
||||
throw new NotFoundError("discipline", disciplinePid);
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
type: "success",
|
||||
payload: {},
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteVisual = async (req: Request<visualParams & { pid: string }>, res: Response) => {
|
||||
if (req.auth?.permission_level != "ELEVATED") {
|
||||
res.status(403).json(createInsufficientPermissionsError());
|
||||
}
|
||||
|
||||
const { disciplinePid, pid } = req.params;
|
||||
|
||||
try {
|
||||
await prisma.discipline.update({
|
||||
where: {
|
||||
pid: disciplinePid,
|
||||
},
|
||||
data: {
|
||||
visual: { disconnect: { pid } },
|
||||
},
|
||||
});
|
||||
return res.status(204).end();
|
||||
} catch (e) {
|
||||
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
|
||||
throw new NotFoundError("discipline", disciplinePid);
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -272,67 +272,3 @@ export const deleteEvent = async (req: Request<DeleteEventQueryParams>, res: Res
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
// REVIEW: This code **will** need to be de-duplicated
|
||||
|
||||
interface visualParams {
|
||||
eventPid: string;
|
||||
}
|
||||
|
||||
interface visualBody {
|
||||
mediaPid: string;
|
||||
}
|
||||
|
||||
export const addVisual = async (req: Request<visualParams, {}, visualBody>, res: Response) => {
|
||||
if (req.auth?.permission_level != "ELEVATED") {
|
||||
res.status(403).json(createInsufficientPermissionsError());
|
||||
}
|
||||
|
||||
const { eventPid } = req.params;
|
||||
|
||||
if (typeof req.body.mediaPid !== "string") {
|
||||
res.status(400).json(generateInvalidBodyError({ mediaPid: DataType.STRING }));
|
||||
}
|
||||
|
||||
const event = await prisma.event.update({
|
||||
where: { pid: eventPid },
|
||||
data: {
|
||||
visual: { connect: { pid: req.body.mediaPid } },
|
||||
},
|
||||
});
|
||||
|
||||
if (!event) {
|
||||
throw new NotFoundError("event", eventPid);
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
type: "success",
|
||||
payload: {},
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteVisual = async (req: Request<visualParams & { pid: string }>, res: Response) => {
|
||||
if (req.auth?.permission_level != "ELEVATED") {
|
||||
res.status(403).json(createInsufficientPermissionsError());
|
||||
}
|
||||
|
||||
const { eventPid, pid } = req.params;
|
||||
|
||||
try {
|
||||
await prisma.event.update({
|
||||
where: {
|
||||
pid: eventPid,
|
||||
},
|
||||
data: {
|
||||
visual: { disconnect: { pid } },
|
||||
},
|
||||
});
|
||||
return res.status(204).end();
|
||||
} catch (e) {
|
||||
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
|
||||
throw new NotFoundError("discipline", eventPid);
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -12,9 +12,18 @@ import { type } from "os";
|
||||
import { unlink } from "fs/promises";
|
||||
import ForwardableError from "../Middleware/error/ForwardableError";
|
||||
import SchemaError from "../Middleware/error/SchemaError";
|
||||
import { z } from "zod";
|
||||
import { updateEvent } from "./event.controller";
|
||||
|
||||
require("express-async-errors");
|
||||
|
||||
const linkMediaBody = z.object({
|
||||
tableToUpdate: z.enum(["EVENT", "ROLE_SCHEMA", "DISCIPLINE"]),
|
||||
mediaPid: z.string(),
|
||||
});
|
||||
|
||||
const unlinkMediaBody = linkMediaBody.omit({ mediaPid: true, });
|
||||
|
||||
function createMediaLinks(fileName: string) {
|
||||
return [{ rel: "self", type: "GET", href: `/api/media/${fileName}` }];
|
||||
}
|
||||
@@ -189,3 +198,89 @@ export const deleteMedia = async (req: Request, res: Response) => {
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
//TODO: maybe create a function that adds the tableToUpdate based on path
|
||||
// and call it before calling (un)linkMedia
|
||||
|
||||
export const linkMedia = async (
|
||||
req: Request<{ pid: string }, {}, { mediaPid: string, tableToUpdate: string }>,
|
||||
res: Response) => {
|
||||
if (req.auth?.permission_level != "ELEVATED") {
|
||||
res.status(403).json(createInsufficientPermissionsError());
|
||||
}
|
||||
|
||||
const zBody = linkMediaBody.safeParse(req.body);
|
||||
|
||||
if(zBody.success === false) {
|
||||
return res.status(400).json(
|
||||
generateInvalidBodyError({
|
||||
mediaPid: DataType.UUID,
|
||||
tableToUpdate: DataType.STRING,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
const { pid } = req.params;
|
||||
const { mediaPid, tableToUpdate } = zBody.data;
|
||||
|
||||
const updatedRec = await getPrismaUpdateFKT(tableToUpdate)({
|
||||
where: { pid },
|
||||
data: {
|
||||
visual: { connect: { pid: mediaPid } },
|
||||
},
|
||||
});
|
||||
|
||||
if (!updatedRec) {
|
||||
throw new NotFoundError(tableToUpdate, pid);
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
type: "success",
|
||||
payload: {},
|
||||
});
|
||||
};
|
||||
|
||||
export const unlinkMedia = async (
|
||||
req: Request<{ pid: string, mediaPid: string }, {}, { tableToUpdate: string }>,
|
||||
res: Response) => {
|
||||
if (req.auth?.permission_level != "ELEVATED") {
|
||||
res.status(403).json(createInsufficientPermissionsError());
|
||||
}
|
||||
|
||||
const zBody = linkMediaBody.safeParse(req.body);
|
||||
|
||||
if(zBody.success === false) {
|
||||
return res.status(400).json(
|
||||
generateInvalidBodyError({
|
||||
mediaPid: DataType.UUID,
|
||||
tableToUpdate: DataType.STRING,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
const { pid } = req.params;
|
||||
const { mediaPid, tableToUpdate } = zBody.data;
|
||||
|
||||
try {
|
||||
await getPrismaUpdateFKT(tableToUpdate)({
|
||||
where: pid,
|
||||
data: { visual: { disconnect: { pid: mediaPid } },
|
||||
},
|
||||
});
|
||||
return res.status(204).end();
|
||||
} catch (e) {
|
||||
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
|
||||
throw new NotFoundError(tableToUpdate, pid);
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
function getPrismaUpdateFKT( tableToUpdate: string ): Function {
|
||||
switch(tableToUpdate) {
|
||||
case "EVENT": return prisma.event.update;
|
||||
case "DISCIPLINE": return prisma.discipline.update;
|
||||
default: return prisma.roleSchema.update;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,61 +123,3 @@ export const createRoleSchema = async (
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
interface visualParams {
|
||||
schemaPid: string;
|
||||
}
|
||||
|
||||
interface visualBody {
|
||||
mediaPid: string;
|
||||
}
|
||||
|
||||
export const addVisual = async (req: Request<visualParams, {}, visualBody>, res: Response) => {
|
||||
if (req.auth?.permission_level != "ELEVATED") {
|
||||
res.status(403).json(createInsufficientPermissionsError());
|
||||
}
|
||||
|
||||
const { schemaPid } = req.params;
|
||||
|
||||
const schema = await prisma.roleSchema.update({
|
||||
where: { pid: schemaPid },
|
||||
data: {
|
||||
visual: { connect: { pid: req.body.mediaPid } },
|
||||
},
|
||||
});
|
||||
|
||||
if (!schema) {
|
||||
throw new NotFoundError("role_schema", schemaPid);
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
type: "success",
|
||||
payload: {},
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteVisual = async (req: Request<visualParams & { pid: string }>, res: Response) => {
|
||||
if (req.auth?.permission_level != "ELEVATED") {
|
||||
res.status(403).json(createInsufficientPermissionsError());
|
||||
}
|
||||
|
||||
const { schemaPid, pid } = req.params;
|
||||
|
||||
try {
|
||||
await prisma.roleSchema.update({
|
||||
where: {
|
||||
pid: schemaPid,
|
||||
},
|
||||
data: {
|
||||
visual: { disconnect: { pid } },
|
||||
},
|
||||
});
|
||||
return res.status(204).end();
|
||||
} catch (e) {
|
||||
if (e instanceof PrismaClientKnownRequestError && e.code === "P2025") {
|
||||
throw new NotFoundError("role_schema", schemaPid);
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user