Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions form-builder/backend/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,29 @@ let forms = [
}
];

let submissions = [];

function addSubmission(formId, data) {
submissions.push({ formId, data, submittedAt: new Date().toISOString() });
}

function getSubmissions(formId) {
return submissions.filter(s => String(s.formId) === String(formId));
}

function getSubmissionCount(formId) {
return getSubmissions(formId).length;
}

function getForms() {
// Return only form metadata (not full config)
return forms.map(({ id, name, description, createdAt }) => ({ id, name, description, createdAt }));
// Return only form metadata (not full config), plus filledCount
return forms.map(({ id, name, description, createdAt }) => ({
id,
name,
description,
createdAt,
filledCount: getSubmissionCount(id)
}));
}

function getFormById(id) {
Expand Down Expand Up @@ -59,5 +79,8 @@ module.exports = {
getFormById,
addForm,
updateFormById,
deleteFormById
deleteFormById,
addSubmission,
getSubmissions,
getSubmissionCount
};
18 changes: 17 additions & 1 deletion form-builder/backend/routes/formRoutes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require('express');
const router = express.Router();
const { getForms, getFormById, addForm, updateFormById } = require('../database');
const { getForms, getFormById, addForm, updateFormById, deleteFormById, addSubmission, getSubmissions } = require('../database');
const Form = require('../models/form');

// Get all forms (metadata only)
Expand Down Expand Up @@ -40,4 +40,20 @@ router.delete('/forms/:id', (req, res) => {
res.json({ success: true });
});

// Submit a filled form
router.post('/forms/:id/submit', (req, res) => {
const form = getFormById(req.params.id);
if (!form) return res.status(404).json({ error: 'Form not found' });
addSubmission(req.params.id, req.body);
res.status(201).json({ success: true });
});

// Get all submissions for a form
router.get('/forms/:id/submissions', (req, res) => {
const form = getFormById(req.params.id);
if (!form) return res.status(404).json({ error: 'Form not found' });
const submissions = getSubmissions(req.params.id);
res.json(submissions);
});

module.exports = router;
1 change: 1 addition & 0 deletions form-builder/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const App = () => {
<Router>
<Routes>
<Route path="/" element={<FormBuilder />} />
<Route path="/add" element={<FormBuilder />} />
<Route path="/form/:id" element={<FormBuilder />} />
<Route path="/form/:id/edit" element={<FormBuilder />} />
<Route path="/form/:id/view" element={<FormBuilder />} />
Expand Down
Loading