File size: 2,541 Bytes
d922d99
4f5e139
d922d99
 
 
4f5e139
 
 
 
 
 
 
 
 
 
df83b24
 
 
 
 
 
 
 
 
 
 
 
4f5e139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d922d99
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const express = require('express');
const { Client } = require('pg');
const app = express();
const port = 7860;

app.use(express.json());

const client = new Client({
  host: 'db',
  user: 'user',
  password: 'password',
  database: 'mydatabase',
  port: 5432,
});

const connectWithRetry = () => {
  client.connect(err => {
    if (err) {
      console.error('Failed to connect to database:', err);
      setTimeout(connectWithRetry, 5000); // wait 5 seconds then retry connection
    } else {
      console.log('Connected to database');
    }
  });
};

connectWithRetry();

app.post('/case', (req, res) => {
  const { caseId, description } = req.body;
  client.query('INSERT INTO cases (caseId, description) VALUES ($1, $2)', [caseId, description], (err, result) => {
    if (err) {
      res.status(500).send(err);
    } else {
      res.status(200).send('Case added');
    }
  });
});

app.get('/case/:caseId', (req, res) => {
  const caseId = req.params.caseId;
  client.query('SELECT * FROM cases WHERE caseId = $1', [caseId], (err, result) => {
    if (err) {
      res.status(500).send(err);
    } else {
      res.status(200).json(result.rows);
    }
  });
});

app.post('/questionnaire', (req, res) => {
  const { questionnaireId, responses } = req.body;
  client.query('INSERT INTO questionnaires (questionnaireId, responses) VALUES ($1, $2)', [questionnaireId, responses], (err, result) => {
    if (err) {
      res.status(500).send(err);
    } else {
      res.status(200).send('Questionnaire added');
    }
  });
});

app.get('/questionnaire/:questionnaireId', (req, res) => {
  const questionnaireId = req.params.questionnaireId;
  client.query('SELECT * FROM questionnaires WHERE questionnaireId = $1', [questionnaireId], (err, result) => {
    if (err) {
      res.status(500).send(err);
    } else {
      res.status(200).json(result.rows);
    }
  });
});

app.post('/jury', (req, res) => {
  const { juryId, details } = req.body;
  client.query('INSERT INTO jury (juryId, details) VALUES ($1, $2)', [juryId, details], (err, result) => {
    if (err) {
      res.status(500).send(err);
    } else {
      res.status(200).send('Jury details added');
    }
  });
});

app.get('/jury/:juryId', (req, res) => {
  const juryId = req.params.juryId;
  client.query('SELECT * FROM jury WHERE juryId = $1', [juryId], (err, result) => {
    if (err) {
      res.status(500).send(err);
    } else {
      res.status(200).json(result.rows);
    }
  });
});

app.listen(port, () => {
  console.log(`App running on port ${port}`);
});