mirror of
https://github.com/karma-riuk/crab-webapp.git
synced 2025-07-05 06:08:13 +02:00
formatted file
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
import { Router } from 'express';
|
import { Router } from "express";
|
||||||
import multer from 'multer';
|
import multer from "multer";
|
||||||
import { InvalidJsonFormatError } from '../utils/errors.js';
|
import { InvalidJsonFormatError } from "../utils/errors.js";
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
@ -12,12 +12,12 @@ const upload = multer({
|
|||||||
},
|
},
|
||||||
fileFilter: (_req, file, cb) => {
|
fileFilter: (_req, file, cb) => {
|
||||||
// Accept only JSON files
|
// Accept only JSON files
|
||||||
if (file.mimetype === 'application/json') {
|
if (file.mimetype === "application/json") {
|
||||||
cb(null, true);
|
cb(null, true);
|
||||||
} else {
|
} else {
|
||||||
cb(new Error('Only JSON files are allowed'));
|
cb(new Error("Only JSON files are allowed"));
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Helper function to validate JSON format
|
// Helper function to validate JSON format
|
||||||
@ -25,26 +25,36 @@ const validateJsonFormat = (data) => {
|
|||||||
try {
|
try {
|
||||||
const parsed = JSON.parse(data);
|
const parsed = JSON.parse(data);
|
||||||
// Check if it's an object
|
// Check if it's an object
|
||||||
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
|
if (
|
||||||
throw new InvalidJsonFormatError("Submitted json doesn't contain an object");
|
typeof parsed !== "object" ||
|
||||||
|
parsed === null ||
|
||||||
|
Array.isArray(parsed)
|
||||||
|
) {
|
||||||
|
throw new InvalidJsonFormatError(
|
||||||
|
"Submitted json doesn't contain an object",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
// Check if all values are strings
|
// Check if all values are strings
|
||||||
if (!Object.values(parsed).every(value => typeof value === 'string')) {
|
if (
|
||||||
throw new InvalidJsonFormatError("Submitted json object must only be str -> str. Namely id -> comment");
|
!Object.values(parsed).every((value) => typeof value === "string")
|
||||||
|
) {
|
||||||
|
throw new InvalidJsonFormatError(
|
||||||
|
"Submitted json object must only be str -> str. Namely id -> comment",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return parsed;
|
return parsed;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof InvalidJsonFormatError) {
|
if (error instanceof InvalidJsonFormatError) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
throw new InvalidJsonFormatError('Invalid JSON format');
|
throw new InvalidJsonFormatError("Invalid JSON format");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
router.post('/submit/comments', upload.single('file'), async (req, res) => {
|
router.post("/submit/comments", upload.single("file"), async (req, res) => {
|
||||||
try {
|
try {
|
||||||
if (!req.file) {
|
if (!req.file) {
|
||||||
return res.status(400).json({ error: 'No file uploaded' });
|
return res.status(400).json({ error: "No file uploaded" });
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileContent = req.file.buffer.toString();
|
const fileContent = req.file.buffer.toString();
|
||||||
@ -55,8 +65,8 @@ router.post('/submit/comments', upload.single('file'), async (req, res) => {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof InvalidJsonFormatError) {
|
if (error instanceof InvalidJsonFormatError) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
error: 'Invalid JSON format',
|
error: "Invalid JSON format",
|
||||||
message: error.message
|
message: error.message,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
throw error;
|
throw error;
|
||||||
@ -65,20 +75,19 @@ router.post('/submit/comments', upload.single('file'), async (req, res) => {
|
|||||||
// TODO: Save the file or process it further
|
// TODO: Save the file or process it further
|
||||||
// For now, just return success
|
// For now, just return success
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
message: 'Answer submitted successfully',
|
message: "Answer submitted successfully",
|
||||||
data: validatedData
|
data: validatedData,
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error processing submission:', error);
|
console.error("Error processing submission:", error);
|
||||||
res.status(500).json({ error: 'Error processing submission' });
|
res.status(500).json({ error: "Error processing submission" });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post('/submit/refinement', upload.single('file'), async (req, res) => {
|
router.post("/submit/refinement", upload.single("file"), async (req, res) => {
|
||||||
try {
|
try {
|
||||||
if (!req.file) {
|
if (!req.file) {
|
||||||
return res.status(400).json({ error: 'No file uploaded' });
|
return res.status(400).json({ error: "No file uploaded" });
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileContent = req.file.buffer.toString();
|
const fileContent = req.file.buffer.toString();
|
||||||
@ -89,8 +98,8 @@ router.post('/submit/refinement', upload.single('file'), async (req, res) => {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof InvalidJsonFormatError) {
|
if (error instanceof InvalidJsonFormatError) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
error: 'Invalid JSON format',
|
error: "Invalid JSON format",
|
||||||
message: error.message
|
message: error.message,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
throw error;
|
throw error;
|
||||||
@ -99,13 +108,12 @@ router.post('/submit/refinement', upload.single('file'), async (req, res) => {
|
|||||||
// TODO: Save the file or process it further
|
// TODO: Save the file or process it further
|
||||||
// For now, just return success
|
// For now, just return success
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
message: 'Answer submitted successfully',
|
message: "Answer submitted successfully",
|
||||||
data: validatedData
|
data: validatedData,
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error processing submission:', error);
|
console.error("Error processing submission:", error);
|
||||||
res.status(500).json({ error: 'Error processing submission' });
|
res.status(500).json({ error: "Error processing submission" });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
import express, { json } from 'express';
|
import express, { json } from "express";
|
||||||
import cors from 'cors';
|
import cors from "cors";
|
||||||
import dotenv from 'dotenv';
|
import dotenv from "dotenv";
|
||||||
import routes from './routes/index.js';
|
import routes from "./routes/index.js";
|
||||||
|
import createSocketServer from "./socket.js";
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
@ -14,9 +15,11 @@ app.use(json());
|
|||||||
|
|
||||||
// Use routes
|
// Use routes
|
||||||
app.use(express.static("public"));
|
app.use(express.static("public"));
|
||||||
app.use('/', routes);
|
app.use("/", routes);
|
||||||
|
|
||||||
|
const server = createSocketServer(app);
|
||||||
|
|
||||||
// Start server
|
// Start server
|
||||||
app.listen(port, () => {
|
server.listen(port, () => {
|
||||||
console.log(`Server is running on port ${port}`);
|
console.log(`Server is running on port ${port}`);
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user