-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontributions_list.js
68 lines (60 loc) · 2.22 KB
/
contributions_list.js
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
const { Pool } = require("pg");
const i18n = require('i18n');
// Assuming pool is initialized somewhere globally or passed as a parameter
const pool = new Pool({
user: process.env.PG_USER,
host: process.env.PG_HOST,
database: process.env.PG_DATABASE,
password: process.env.PG_PASSWORD,
port: process.env.PG_PORT,
ssl: { rejectUnauthorized: process.env.PG_SSL_REJECT_UNAUTHORIZED === "true" },
});
async function getContributionsList(req, res) {
const { lang, problemName } = req.params;
i18n.setLocale(res, lang);
try {
const result = await pool.query(
`SELECT
c.*,
u.username,
u.full_name,
u.profile_picture
FROM (
SELECT
id, user_id, edited_at, problem_name, language, original_content, new_content, NULL::text AS ip_address, false AS content_changed
FROM github_contributions
UNION ALL
SELECT
id, user_id, edited_at, problem_name, language, original_content, new_content, ip_address, content_changed
FROM contributions
) c
LEFT JOIN users u ON c.user_id = u.id
WHERE c.problem_name = $1 AND c.language = $2
ORDER BY c.edited_at DESC`,
[problemName, lang]
);
console.log(result.rows);
const contributions = result.rows;
res.render("contributions_list", {
__: i18n.__,
lang,
problemName,
contributions,
formatDate: (date) => {
return new Date(date).toLocaleString(lang === 'ru' ? 'ru-RU' : 'en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit'
});
},
usernameCurrent: req.session.username || null,
userIdCurrent: req.session.userId || null
});
} catch (error) {
console.error("Error fetching contributions:", error);
res.status(500).send("Error fetching contributions");
}
}
module.exports = getContributionsList;