-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
137 lines (124 loc) · 4.19 KB
/
server.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict';
require('dotenv').config();
const pg = require('pg');
const express = require('express');
const PORT = process.env.PORT;
const app = express();
const client = new pg.Client(process.env.DATABASE_URL);
const superagent = require('superagent');
const startOfString = 'https://api.github.com/repos/';
client.connect();
// Additional requirements for utilizing node-html-pdf
const fs = require('fs');
const pdf = require('html-pdf');
var options = {
format : 'Letter',
base : 'http://p2t2.herokuapp.com/' };
const ejs = require('ejs');
// End of additional requirements for node-html-pdf
app.set('view engine', 'ejs');
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static('./public'));
app.post('/form/complete', githubPostToBase);
app.get('/form', initializeFormPage);
app.get('/dashboard/:id', initializeDashboardPage);
app.post('/dashboard/:id', testBump);
app.get('/about', initializeAboutPage);
app.get('/', initializeHomePage);
function initializeHomePage(req,res){
let SQL = `
SELECT id, collaborators, name, description
FROM projects;`;
client.query(SQL)
.then(result => {
let cardbase = result.rows;
res.render('pages/main', {cardbase});
});
}
function initializeFormPage(req, res) {
res.render('pages/form');
}
function testBump(req,res){
let SQL = 'INSERT INTO events(project_id,title,start,"end",description) VALUES($1,$2,$3,$4,$5);';
let values = [req.body.project_id, req.body.name, req.body.start, req.body.end,req.body.description];
client.query(SQL, values)
.then(() => {
res.redirect(`/dashboard/${req.body.project_id}`);
});
}
function initializeDashboardPage(req, res) {
let SQL = `SELECT name, repo_url FROM projects
WHERE id=$1;`;
let id = req.params.id;
let values = [req.params.id];
let SQLcal= 'SELECT title, start, "end", description FROM events WHERE project_id=$1;';
Promise.all([
client.query(SQL, values),
client.query(SQLcal, values),
]) .then(([projectsData, eventsData]) => {
let project = projectsData.rows[0];
let eventList = eventsData.rows;
let splitHubUser = project.repo_url.split('/')[3];
let splitHubRepo = project.repo_url.split('/')[4];
let conString = `${startOfString}${splitHubUser}/${splitHubRepo}`;
Promise.all([
superagent.get(`${conString}/issues`),
superagent.get(`${conString}/commits`),
]).then(([issues, commits]) => {
let latestIssue = issues.body.slice(0,5);
let latestCommit = commits.body.slice(0,5);
res.render('pages/dashboard', { latestIssue, latestCommit, eventList, id});
});
});
}
function initializeAboutPage(req, res) {
res.render('pages/about');
}
function githubPostToBase(req, res) {
let SQL = `
INSERT INTO projects(collaborators ,name, startdate, enddate, repo_url, description)
VALUES ($1, $2, $3, $4, $5, $6);`;
let values = [
req.body.collaborators,
req.body.project_name,
req.body.start_date,
req.body.due_date,
req.body.github_repo,
req.body.description];
client.query(SQL, values);
res.render('pages/success');
}
app.use(express.static(__dirname + '/public'));
app.use('*', (req, res) => res.render('pages/error'));
app.listen(PORT, () => console.log(`server hath started on port ${PORT}`));
// Create a PDF file function
function makePDF(req, res) {
let SQL = `SELECT name, repo_url FROM projects
WHERE id=1;`;
client.query(SQL)
.then (result => {
let project = result.rows[0];
let splitHubUser = project.repo_url.split('/')[3];
let splitHubRepo = project.repo_url.split('/')[4];
let conString = `${startOfString}${splitHubUser}/${splitHubRepo}/issues`;
superagent.get(conString)
.then(data => {
let latestIssue = data.body.slice(0, 4);
let html;
ejs.renderFile('./views/pages/dashboard.ejs', {latestIssue}, function(err, res) {
if(res) {
html = res;
console.log(html);
}
else {
console.log(err);
}
});
pdf.create(html, options).toFile(function(err, res) {
if (err) return console.log(err);
console.log(res);
});
});
});
}