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;
|
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;
|
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 { unlink } from "fs/promises";
|
||||||
import ForwardableError from "../Middleware/error/ForwardableError";
|
import ForwardableError from "../Middleware/error/ForwardableError";
|
||||||
import SchemaError from "../Middleware/error/SchemaError";
|
import SchemaError from "../Middleware/error/SchemaError";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { updateEvent } from "./event.controller";
|
||||||
|
|
||||||
require("express-async-errors");
|
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) {
|
function createMediaLinks(fileName: string) {
|
||||||
return [{ rel: "self", type: "GET", href: `/api/media/${fileName}` }];
|
return [{ rel: "self", type: "GET", href: `/api/media/${fileName}` }];
|
||||||
}
|
}
|
||||||
@@ -189,3 +198,89 @@ export const deleteMedia = async (req: Request, res: Response) => {
|
|||||||
throw e;
|
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;
|
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;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import eventRouter from "./event.routes";
|
import eventRouter from "./event.routes";
|
||||||
import {
|
import {
|
||||||
addVisual,
|
|
||||||
createDiscipline,
|
createDiscipline,
|
||||||
deleteDiscipline,
|
deleteDiscipline,
|
||||||
deleteVisual,
|
|
||||||
getAllDisciplines,
|
getAllDisciplines,
|
||||||
getDiscipline,
|
getDiscipline,
|
||||||
} from "../Controllers/discipline.controller";
|
} from "../Controllers/discipline.controller";
|
||||||
@@ -18,18 +16,6 @@ router.get("/:pid", getDiscipline);
|
|||||||
|
|
||||||
router.delete<"/:pid", { pid: string }>("/:pid", requireAuthentication, deleteDiscipline);
|
router.delete<"/:pid", { pid: string }>("/:pid", requireAuthentication, deleteDiscipline);
|
||||||
|
|
||||||
router.post<"/:disciplinePid/images", { disciplinePid: string }>(
|
|
||||||
"/:disciplinePid/images",
|
|
||||||
requireAuthentication,
|
|
||||||
addVisual
|
|
||||||
);
|
|
||||||
|
|
||||||
router.delete<"/:disciplinePid/images/:pid", { disciplinePid: string; pid: string }>(
|
|
||||||
"/:disciplinePid/images/:pid",
|
|
||||||
requireAuthentication,
|
|
||||||
deleteVisual
|
|
||||||
);
|
|
||||||
|
|
||||||
eventRouter.post("/:eventPid/disciplines", requireAuthentication, createDiscipline);
|
eventRouter.post("/:eventPid/disciplines", requireAuthentication, createDiscipline);
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
@@ -2,9 +2,7 @@ import Express from "express";
|
|||||||
import { string } from "zod";
|
import { string } from "zod";
|
||||||
import {
|
import {
|
||||||
addEvent,
|
addEvent,
|
||||||
addVisual,
|
|
||||||
deleteEvent,
|
deleteEvent,
|
||||||
deleteVisual,
|
|
||||||
getAllEvents,
|
getAllEvents,
|
||||||
getEvent,
|
getEvent,
|
||||||
updateEvent,
|
updateEvent,
|
||||||
@@ -22,12 +20,4 @@ router.patch<"/:pid/", { pid: string }>("/:pid/", requireAuthentication, updateE
|
|||||||
|
|
||||||
router.delete<"/:pid/", { pid: string }>("/:pid/", requireAuthentication, deleteEvent);
|
router.delete<"/:pid/", { pid: string }>("/:pid/", requireAuthentication, deleteEvent);
|
||||||
|
|
||||||
router.post<"/:eventPid/media", { eventPid: string }>("/:eventPid/media", requireAuthentication, addVisual);
|
|
||||||
|
|
||||||
router.delete<"/:eventPid/media/:pid", { eventPid: string; pid: string }>(
|
|
||||||
"/:eventPid/media/:pid",
|
|
||||||
requireAuthentication,
|
|
||||||
deleteVisual
|
|
||||||
);
|
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import fileUpload from "express-fileupload";
|
import fileUpload from "express-fileupload";
|
||||||
import { requireAuthentication } from "../Middleware/auth/auth";
|
import { requireAuthentication } from "../Middleware/auth/auth";
|
||||||
import { deleteMedia, getAllMedia, getMediaMeta, uploadImage } from "../Controllers/media.controller";
|
import { deleteMedia, getAllMedia, getMediaMeta, linkMedia, unlinkMedia, uploadImage } from "../Controllers/media.controller";
|
||||||
import eventRouter from "./event.routes";
|
import eventRouter from "./event.routes";
|
||||||
import disciplineRouter from "./discipline.routes";
|
import disciplineRouter from "./discipline.routes";
|
||||||
import roleSchemaRouter from "./role_schema.routes";
|
import roleSchemaRouter from "./role_schema.routes";
|
||||||
@@ -19,4 +19,28 @@ router.get("/:pid/meta", getMediaMeta);
|
|||||||
|
|
||||||
router.delete("/:pid", requireAuthentication, deleteMedia);
|
router.delete("/:pid", requireAuthentication, deleteMedia);
|
||||||
|
|
||||||
|
eventRouter.post<"/:pid/media", { pid: string }>(
|
||||||
|
"/:pid/media", requireAuthentication, linkMedia
|
||||||
|
);
|
||||||
|
|
||||||
|
eventRouter.delete<"/:pid/media/:mediaPid", { pid: string, mediaPid: string }>(
|
||||||
|
"/:pid/media/:mediaPid", requireAuthentication, unlinkMedia
|
||||||
|
);
|
||||||
|
|
||||||
|
disciplineRouter.post<"/:pid/media", { pid: string }>(
|
||||||
|
"/:pid/media", requireAuthentication, linkMedia
|
||||||
|
);
|
||||||
|
|
||||||
|
disciplineRouter.delete<"/:pid/media/:mediaPid", { pid: string, mediaPid: string }>(
|
||||||
|
"/:pid/media/:mediaPid", requireAuthentication, unlinkMedia
|
||||||
|
);
|
||||||
|
|
||||||
|
roleSchemaRouter.post<"/:pid/media", { pid: string }>(
|
||||||
|
"/:pid/media", requireAuthentication, linkMedia
|
||||||
|
);
|
||||||
|
|
||||||
|
roleSchemaRouter.delete<"/:pid/media/:mediaPid", { pid: string, mediaPid: string }>(
|
||||||
|
"/:pid/media/:mediaPid", requireAuthentication, unlinkMedia
|
||||||
|
);
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import disciplineRouter from "./discipline.routes";
|
import disciplineRouter from "./discipline.routes";
|
||||||
import {
|
import {
|
||||||
addVisual,
|
|
||||||
createRoleSchema,
|
createRoleSchema,
|
||||||
deleteVisual,
|
|
||||||
getAllRoleSchemas,
|
getAllRoleSchemas,
|
||||||
getAllRoleSchemasWithParam,
|
getAllRoleSchemasWithParam,
|
||||||
getRoleSchema,
|
getRoleSchema,
|
||||||
@@ -20,12 +18,4 @@ disciplineRouter.get("/:disciplinePid/role-schemas", getAllRoleSchemasWithParam)
|
|||||||
|
|
||||||
disciplineRouter.post("/:disciplinePid/role-schemas", requireAuthentication, createRoleSchema);
|
disciplineRouter.post("/:disciplinePid/role-schemas", requireAuthentication, createRoleSchema);
|
||||||
|
|
||||||
router.post<"/:schemaPid/images", { schemaPid: string }>("/:schemaPid/images", requireAuthentication, addVisual);
|
|
||||||
|
|
||||||
router.delete<"/:schemaPid/images/:pid", { schemaPid: string; pid: string }>(
|
|
||||||
"/:schemaPid/images/:pid",
|
|
||||||
requireAuthentication,
|
|
||||||
deleteVisual
|
|
||||||
);
|
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
Reference in New Issue
Block a user