vibe coded v1
This commit is contained in:
@@ -5,16 +5,18 @@ import {
|
||||
APIException,
|
||||
type APIResponse,
|
||||
type Dump,
|
||||
isCreateDumpRequest,
|
||||
isCreateUrlDumpRequest,
|
||||
isUpdateDumpRequest,
|
||||
} from "../model/interfaces.ts";
|
||||
|
||||
import { authMiddleware } from "../middleware/auth.ts";
|
||||
import {
|
||||
createDump,
|
||||
createFileDump,
|
||||
createUrlDump,
|
||||
deleteDump,
|
||||
getDump,
|
||||
listDumps,
|
||||
replaceFileDump,
|
||||
updateDump,
|
||||
} from "../services/dump-service.ts";
|
||||
|
||||
@@ -24,106 +26,119 @@ router.post(
|
||||
"/",
|
||||
authMiddleware,
|
||||
async (ctx) => {
|
||||
const createDumpRequest = await ctx.request.body.json();
|
||||
const userId = ctx.state.user.userId;
|
||||
const contentType = ctx.request.headers.get("content-type") ?? "";
|
||||
|
||||
if (!isCreateDumpRequest(createDumpRequest)) {
|
||||
throw new APIException(
|
||||
APIErrorCode.VALIDATION_ERROR,
|
||||
400,
|
||||
"Invalid dump data",
|
||||
let dump: Dump;
|
||||
|
||||
if (contentType.includes("multipart/form-data")) {
|
||||
const formData = await ctx.request.body.formData();
|
||||
const file = formData.get("file");
|
||||
const comment = formData.get("comment");
|
||||
|
||||
if (!(file instanceof File)) {
|
||||
throw new APIException(
|
||||
APIErrorCode.VALIDATION_ERROR,
|
||||
400,
|
||||
"A file is required",
|
||||
);
|
||||
}
|
||||
|
||||
dump = await createFileDump(
|
||||
file,
|
||||
typeof comment === "string" && comment ? comment : undefined,
|
||||
userId,
|
||||
);
|
||||
} else {
|
||||
const body = await ctx.request.body.json();
|
||||
|
||||
if (!isCreateUrlDumpRequest(body)) {
|
||||
throw new APIException(
|
||||
APIErrorCode.VALIDATION_ERROR,
|
||||
400,
|
||||
"Invalid dump data",
|
||||
);
|
||||
}
|
||||
|
||||
dump = await createUrlDump(body, userId);
|
||||
}
|
||||
|
||||
const userId = ctx.state.user.userId;
|
||||
const dump = createDump(createDumpRequest, userId);
|
||||
|
||||
const responseBody: APIResponse<Dump> = {
|
||||
success: true,
|
||||
data: dump,
|
||||
};
|
||||
|
||||
const responseBody: APIResponse<Dump> = { success: true, data: dump };
|
||||
ctx.response.status = 201;
|
||||
ctx.response.body = responseBody;
|
||||
},
|
||||
);
|
||||
|
||||
router.get("/:dumpId", (ctx) => {
|
||||
const dumpId = ctx.params.dumpId;
|
||||
const dump = getDump(dumpId);
|
||||
|
||||
const responseBody: APIResponse<Dump> = {
|
||||
success: true,
|
||||
data: dump,
|
||||
};
|
||||
|
||||
const dump = getDump(ctx.params.dumpId);
|
||||
const responseBody: APIResponse<Dump> = { success: true, data: dump };
|
||||
ctx.response.body = responseBody;
|
||||
});
|
||||
|
||||
router.get("/", (ctx) => {
|
||||
const dumps = listDumps();
|
||||
const responseBody: APIResponse<Dump[]> = { success: true, data: dumps };
|
||||
ctx.response.body = responseBody;
|
||||
});
|
||||
|
||||
const responseBody: APIResponse<Dump[]> = {
|
||||
success: true,
|
||||
data: dumps,
|
||||
};
|
||||
router.put("/:dumpId/file", authMiddleware, async (ctx) => {
|
||||
const dumpId = ctx.params.dumpId;
|
||||
const userId = ctx.state.user?.userId;
|
||||
|
||||
const dump = getDump(dumpId);
|
||||
if (userId !== dump.userId) {
|
||||
throw new APIException(APIErrorCode.UNAUTHORIZED, 401, "Not authorized to update dump");
|
||||
}
|
||||
|
||||
const formData = await ctx.request.body.formData();
|
||||
const file = formData.get("file");
|
||||
const comment = formData.get("comment");
|
||||
|
||||
if (!(file instanceof File)) {
|
||||
throw new APIException(APIErrorCode.VALIDATION_ERROR, 400, "A file is required");
|
||||
}
|
||||
|
||||
const updatedDump = await replaceFileDump(
|
||||
dumpId,
|
||||
file,
|
||||
typeof comment === "string" && comment ? comment : undefined,
|
||||
);
|
||||
const responseBody: APIResponse<Dump> = { success: true, data: updatedDump };
|
||||
ctx.response.body = responseBody;
|
||||
});
|
||||
|
||||
router.put("/:dumpId", authMiddleware, async (ctx) => {
|
||||
const dumpId = ctx.params.dumpId;
|
||||
const userId = ctx.state.user?.userId;
|
||||
const updateDumpRequest = await ctx.request.body.json();
|
||||
const body = await ctx.request.body.json();
|
||||
|
||||
if (!isUpdateDumpRequest(updateDumpRequest)) {
|
||||
throw new APIException(
|
||||
APIErrorCode.VALIDATION_ERROR,
|
||||
422,
|
||||
"Erroneous user input",
|
||||
);
|
||||
if (!isUpdateDumpRequest(body)) {
|
||||
throw new APIException(APIErrorCode.VALIDATION_ERROR, 422, "Erroneous user input");
|
||||
}
|
||||
|
||||
const dump = getDump(dumpId);
|
||||
|
||||
if (userId !== dump.userId) {
|
||||
throw new APIException(
|
||||
APIErrorCode.UNAUTHORIZED,
|
||||
401,
|
||||
"Not authorized to update dump",
|
||||
);
|
||||
throw new APIException(APIErrorCode.UNAUTHORIZED, 401, "Not authorized to update dump");
|
||||
}
|
||||
|
||||
const updatedDump = updateDump(dumpId, updateDumpRequest);
|
||||
|
||||
const responseBody: APIResponse<Dump> = {
|
||||
success: true,
|
||||
data: updatedDump,
|
||||
};
|
||||
|
||||
const updatedDump = await updateDump(dumpId, body);
|
||||
const responseBody: APIResponse<Dump> = { success: true, data: updatedDump };
|
||||
ctx.response.body = responseBody;
|
||||
});
|
||||
|
||||
router.delete("/:dumpId", authMiddleware, (ctx) => {
|
||||
router.delete("/:dumpId", authMiddleware, async (ctx) => {
|
||||
const dumpId = ctx.params.dumpId;
|
||||
const userId = ctx.state.user?.userId;
|
||||
|
||||
const dump = getDump(dumpId);
|
||||
|
||||
if (userId !== dump.userId) {
|
||||
throw new APIException(
|
||||
APIErrorCode.UNAUTHORIZED,
|
||||
401,
|
||||
"Not authorized to update dump",
|
||||
);
|
||||
throw new APIException(APIErrorCode.UNAUTHORIZED, 401, "Not authorized to delete dump");
|
||||
}
|
||||
|
||||
deleteDump(dumpId);
|
||||
|
||||
const responseBody: APIResponse<null> = {
|
||||
success: true,
|
||||
data: null,
|
||||
};
|
||||
await deleteDump(dumpId);
|
||||
|
||||
const responseBody: APIResponse<null> = { success: true, data: null };
|
||||
ctx.response.body = responseBody;
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user