Page MenuHomeDevCentral

D3182.id8138.diff
No OneTemporary

D3182.id8138.diff

diff --git a/backend/README.md b/backend/README.md
new file mode 100644
--- /dev/null
+++ b/backend/README.md
@@ -0,0 +1,8 @@
+## Code conventions
+
+The project adheres to the following code conventions:
+[https://agora.nasqueron.org/Code_conventions](https://agora.nasqueron.org/Code_conventions)
+
+In addition:
+- the project uses single quotes
+- both variables and functions names are in camelCase
diff --git a/backend/servpulse-backend/.env.dist b/backend/servpulse-backend/.env.dist
new file mode 100644
--- /dev/null
+++ b/backend/servpulse-backend/.env.dist
@@ -0,0 +1,2 @@
+POSTGRES_CONNECTION_STRING="postgresql://postgres:password@localhost:5432/postgres"
+EXPRESS_PORT=3000
diff --git a/backend/servpulse-backend/app.js b/backend/servpulse-backend/app.js
new file mode 100644
--- /dev/null
+++ b/backend/servpulse-backend/app.js
@@ -0,0 +1,30 @@
+// -------------------------------------------------------------
+// ServPulse :: app
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+// Project: Nasqueron
+// Description: Bootstrap the application
+// License: BSD-2-Clause
+// -------------------------------------------------------------
+
+require('dotenv').config(); // Set up .env file
+require('./config/database.js'); // Set up database connection
+
+const express = require('express');
+const app = express();
+
+// Middleware to parse JSON requests
+app.use(express.json());
+
+// Import routes
+const serviceRoutes = require('./routes/serviceRoutes.js');
+const incidentRoutes = require('./routes/incidentRoutes.js');
+
+// Use the routes
+app.use('/api', serviceRoutes);
+app.use('/api', incidentRoutes);
+
+// Start the server
+const PORT = process.env.EXPRESS_PORT || 3000;
+app.listen(PORT, () => {
+ console.log(`Server running on port ${PORT}`);
+});
diff --git a/backend/servpulse-backend/config/database.js b/backend/servpulse-backend/config/database.js
new file mode 100644
--- /dev/null
+++ b/backend/servpulse-backend/config/database.js
@@ -0,0 +1,8 @@
+require('dotenv').config({ path: '../.env' });
+const { Pool } = require('pg');
+
+const pool = new Pool({
+ connectionString: process.env.POSTGRES_CONNECTION_STRING,
+});
+
+module.exports = pool;
diff --git a/backend/servpulse-backend/controllers/incidentController.js b/backend/servpulse-backend/controllers/incidentController.js
new file mode 100644
--- /dev/null
+++ b/backend/servpulse-backend/controllers/incidentController.js
@@ -0,0 +1,15 @@
+const incidentModel = require('../models/incidentModel.js');
+const incidentServiceModel = require('../models/incidentServiceModel.js');
+const incidentPostModel = require('../models/incidentPostModel.js');
+const incidentPostStatusModel = require('../models/incidentPostStatusModel.js');
+
+const getIncidents = async (req, res) => {
+ try {
+ const incidents = await incidentModel.getIncidents();
+ res.status(200).json(incidents);
+ } catch (error) {
+ res.status(500).json({ message: 'Error fetching services', error: error.message });
+ }
+};
+
+module.exports = { getIncidents };
diff --git a/backend/servpulse-backend/controllers/serviceController.js b/backend/servpulse-backend/controllers/serviceController.js
new file mode 100644
--- /dev/null
+++ b/backend/servpulse-backend/controllers/serviceController.js
@@ -0,0 +1,23 @@
+const serviceModel = require('../models/serviceModel.js');
+
+const addService = async (req, res) => {
+ try {
+ const serviceData = req.body;
+ const result = await serviceModel.addService(serviceData);
+
+ res.status(201).json(result);
+ } catch (error) {
+ res.status(500).json({ message: 'Error creating service', error: error.message });
+ }
+};
+
+const getServices = async (req, res) => {
+ try {
+ const services = await serviceModel.getServices();
+ res.status(200).json(services);
+ } catch (error) {
+ res.status(500).json({ message: 'Error fetching services', error: error.message });
+ }
+};
+
+module.exports = { addService, getServices };
diff --git a/backend/servpulse-backend/index.js b/backend/servpulse-backend/index.js
deleted file mode 100644
--- a/backend/servpulse-backend/index.js
+++ /dev/null
@@ -1,11 +0,0 @@
-const express = require('express')
-const app = express()
-const port = 3000
-
-app.get('/', (req, res) => {
- res.send('Hello World!')
-})
-
-app.listen(port, () => {
- console.log(`Example app listening on port ${port}`)
-})
diff --git a/backend/servpulse-backend/models/incidentModel.js b/backend/servpulse-backend/models/incidentModel.js
new file mode 100644
--- /dev/null
+++ b/backend/servpulse-backend/models/incidentModel.js
@@ -0,0 +1,17 @@
+const pool = require('../config/database.js');
+
+const createIncident = async (data) => {
+ return await pool.query(`
+ INSERT INTO incident (title, start_date, update_date, type_id, status)
+ VALUES (\$1, \$2, \$3, \$4, \$5)
+ RETURNING *
+ `, [data.title, data.start_date, data.update_date, data.type_id, data.status]);
+ };
+
+const getIncidents = async () => {
+ return await pool.query(`
+ SELECT * FROM incident;
+ `);
+};
+
+module.exports = { createIncident, getIncidents };
diff --git a/backend/servpulse-backend/models/serviceModel.js b/backend/servpulse-backend/models/serviceModel.js
new file mode 100644
--- /dev/null
+++ b/backend/servpulse-backend/models/serviceModel.js
@@ -0,0 +1,18 @@
+const pool = require('../config/database.js');
+
+const addService = async (data) => {
+ return await pool.query(`
+ INSERT INTO service
+ (name, "group", description, status)
+ VALUES (\$1, \$2, \$3, \$4)
+ RETURNING *
+ `, [data.name, data.group, data.description, data.status]);
+};
+
+const getServices = async () => {
+ return await pool.query(`
+ SELECT * FROM service;
+ `);
+};
+
+module.exports = { addService, getServices };
diff --git a/backend/servpulse-backend/package.json b/backend/servpulse-backend/package.json
--- a/backend/servpulse-backend/package.json
+++ b/backend/servpulse-backend/package.json
@@ -1,14 +1,17 @@
{
- "name": "servpulse-backend",
- "version": "1.0.0",
- "description": "Backend API for the ServPulse frontend.",
- "main": "index.js",
- "scripts": {
- "test": "echo \"Error: no test specified\" && exit 1"
- },
- "author": "",
- "license": "ISC",
- "dependencies": {
- "express": "^4.18.2"
- }
+ "name": "servpulse-backend",
+ "version": "1.0.0",
+ "description": "Backend API for the ServPulse frontend.",
+ "main": "index.js",
+ "scripts": {
+ "test": "echo \"Error: no test specified\" && exit 1"
+ },
+ "author": "",
+ "license": "BSD-2-Clause",
+ "dependencies": {
+ "dotenv": "^16.0.3",
+ "express": "^4.18.2",
+ "pg": "^8.11.0",
+ "sequelize": "^6.31.1"
+ }
}
diff --git a/backend/servpulse-backend/routes/incidentRoutes.js b/backend/servpulse-backend/routes/incidentRoutes.js
new file mode 100644
--- /dev/null
+++ b/backend/servpulse-backend/routes/incidentRoutes.js
@@ -0,0 +1,7 @@
+const express = require('express');
+const router = express.Router();
+const incidentController = require('../controllers/incidentController.js');
+
+router.get('/incident/getAll', incidentController.getIncidents);
+
+module.exports = router;
diff --git a/backend/servpulse-backend/routes/serviceRoutes.js b/backend/servpulse-backend/routes/serviceRoutes.js
new file mode 100644
--- /dev/null
+++ b/backend/servpulse-backend/routes/serviceRoutes.js
@@ -0,0 +1,8 @@
+const express = require('express');
+const router = express.Router();
+const serviceController = require('../controllers/serviceController.js');
+
+router.get('/service/getAll', serviceController.getServices);
+router.post('/service', serviceController.addService);
+
+module.exports = router;
diff --git a/database/README.md b/database/README.md
new file mode 100644
--- /dev/null
+++ b/database/README.md
@@ -0,0 +1,7 @@
+## Run the database
+```bash
+docker run --name servpulse_db -p 5432:5432 -e POSTGRES_PASSWORD="yourpassword" -d postgres
+```
+
+## Diagram design project
+https://dbdiagram.io/d/643dab996b31947051bf53e7

File Metadata

Mime Type
text/plain
Expires
Mon, Nov 18, 17:04 (21 h, 43 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2250151
Default Alt Text
D3182.id8138.diff (7 KB)

Event Timeline