Skip to content

Commit

Permalink
chore(parametrized SQL and CSS)
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroMartinSteenstrup committed Apr 21, 2024
1 parent 26eeb1b commit dae6d25
Show file tree
Hide file tree
Showing 16 changed files with 299 additions and 107 deletions.
2 changes: 1 addition & 1 deletion portfolio/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ services:
context: ./nginx
dockerfile: Dockerfile
ports:
- 8005:${NGINX_PORT}
- 80:${NGINX_PORT}
# volumes:
# - certbot-etc:/etc/letsencrypt
# - certbot-var:/var/lib/letsencrypt
Expand Down
43 changes: 0 additions & 43 deletions portfolio/public/index.html

This file was deleted.

54 changes: 53 additions & 1 deletion portfolio/scripts/build-db.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
-- autocommit is deactivated
BEGIN TRANSACTION;
SET datestyle to SQL,
DMY;
SET search_path TO public;
CREATE TABLE IF NOT EXISTS settings (
id SERIAL PRIMARY KEY,
Expand Down Expand Up @@ -58,4 +60,54 @@ VALUES (
'Wise',
'Data Analyst',
'Mise en place d''indicateurs de performance, de solutions de visualisation et de scripts d''extraction de données'
)
);
DROP TABLE IF EXISTS realisations;
CREATE TABLE IF NOT EXISTS realisations (
id SERIAL PRIMARY KEY,
r_début DATE NOT NULL,
r_fin DATE NOT NULL,
r_intitule TEXT NOT NULL,
r_description TEXT NOT NULL,
est_gestion_patrimoine BOOLEAN,
est_response_incidents BOOLEAN,
est_presence_en_ligne BOOLEAN,
est_travail_mode_projet BOOLEAN,
est_deploiement_service BOOLEAN,
est_developpement_pro BOOLEAN
);
INSERT INTO realisations (
r_début,
r_fin,
r_intitule,
r_description,
est_gestion_patrimoine,
est_response_incidents,
est_presence_en_ligne,
est_travail_mode_projet,
est_deploiement_service,
est_developpement_pro
)
VALUES (
'05-03-2024',
'30-04-2024',
'Création d''un site portfolio dans le cadre de l''épreuve E4',
'Express, nodejs, postgres, template, conteneurisé',
FALSE,
FALSE,
TRUE,
TRUE,
TRUE,
TRUE
),
(
'05-03-2024',
'30-04-2024',
'Automatisation de la gestion du code, des resources (utilisation du paradigme dit IaC – Infrastructure as Code) ainsi que du déploiement',
'Github Actions, Terraform, Ansible',
TRUE,
FALSE,
FALSE,
FALSE,
TRUE,
TRUE
);
36 changes: 36 additions & 0 deletions portfolio/src/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Items in the global namespace are accessible throught out the node application
// https://node-postgres.com/features/pooling
const Pool = require("pg").Pool;
// https://nodejs.org/en/learn/command-line/how-to-read-environment-variables-from-nodejs
const pool = new Pool({
user: process.env.POSTGRES_USER,
host: process.env.POSTGRES_HOST,
database: process.env.POSTGRES_DB,
password: process.env.POSTGRES_PASSWORD,
port: process.env.POSTGRES_PORT,
});

//get all from our database
const getData = async (sql_query, values) => {
try {
return await new Promise(function (resolve, reject) {
pool.query(sql_query, values, (error, results) => {
if (error) {
reject(error);
}
if (results && results.rows) {
resolve(results.rows);
} else {
reject(new Error("No results found"));
}
});
});
} catch (err) {
console.error(err);
}
};


module.exports = {
getData,
};
21 changes: 6 additions & 15 deletions portfolio/src/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
const express = require('express')
const path = require('node:path');
const app = express()
const port = process.env.NODE_PORT
var bodyParser = require("body-parser");

app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs'); // set the app to use ejs for rendering
app.use(express.static(__dirname + '/public')); // set location of static files
app.use(express.static(__dirname + '../public')); // set location of static files

app.locals.title = "Portfolio de Pedro";
app.locals.author = "Pedro Martin";

// Items in the global namespace are accessible throught out the node application
// https://node-postgres.com/features/pooling
const Pool = require("pg").Pool;
// https://nodejs.org/en/learn/command-line/how-to-read-environment-variables-from-nodejs
global.pool = new Pool({
user: process.env.POSTGRES_USER,
host: process.env.POSTGRES_HOST,
database: process.env.POSTGRES_DB,
password: process.env.POSTGRES_PASSWORD,
port: process.env.POSTGRES_PORT,
});
app.use('/favicon.ico', express.static('favicon.ico'));

app.use(express.json())
app.use(function (req, res, next) {
Expand All @@ -30,8 +20,6 @@ app.use(function (req, res, next) {
next();
});

const defRoutes = require('./routes/courses')

// Add all the route handlers to the app
const mainRoutes = require('./routes/main');
app.use(mainRoutes);
Expand All @@ -42,6 +30,9 @@ app.use(coursesRoutes);
const xpRoutes = require('./routes/experiences');
app.use(xpRoutes);

const reaRoutes = require('./routes/realisations');
app.use(reaRoutes);

// Make the web application listen for HTTP requests
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`)
Expand Down
40 changes: 39 additions & 1 deletion portfolio/src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions portfolio/src/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
{
"dependencies": {
"dotenv": "^16.4.5",
"ejs": "^3.1.10",
"express": "^4.19.2",
"express-validator": "^7.0.1",
"pg": "^8.11.5"
"pg": "^8.11.5",
"serve-favicon": "^2.5.0"
},
"scripts": {
"start-local": "node -r dotenv/config index.js dotenv_config_path=./../../.env.local",
"build-db": ". ../.env && PGPASSWORD=$POSTGRES_PASSWORD psql -e -h localhost -d $POSTGRES_DB -U $POSTGRES_USER -p 5433 -f ./scripts/build-db.sql",
"clean-db": ". ../.env && PGPASSWORD=$POSTGRES_PASSWORD psql -e -h localhost -d $POSTGRES_DB -U $POSTGRES_USER -p 5433 -f ./scripts/clean-db.sql",
"build-db-local": ". ../.env.local && PGPASSWORD=$POSTGRES_PASSWORD psql -e -h localhost -d $POSTGRES_DB -U $POSTGRES_USER -p 5433 -f ../scripts/build-db.sql",
"clean-db-local": ". ../.env.local && PGPASSWORD=$POSTGRES_PASSWORD psql -e -h localhost -d $POSTGRES_DB -U $POSTGRES_USER -p 5433 -f ../scripts/clean-db.sql",
"build-db-local": ". ../../.env.local && PGPASSWORD=$POSTGRES_PASSWORD psql -e -h localhost -d $POSTGRES_DB -U $POSTGRES_USER -p 5433 -f ../scripts/build-db.sql",
"clean-db-local": ". ../../.env.local && PGPASSWORD=$POSTGRES_PASSWORD psql -e -h localhost -d $POSTGRES_DB -U $POSTGRES_USER -p 5433 -f ../scripts/clean-db.sql",
"build-db-docker": ". ../.env && PGPASSWORD=$POSTGRES_PASSWORD psql -e -h localhost -d $POSTGRES_DB -U $POSTGRES_USER -p 5433 -f ../scripts/build-db.sql",
"clean-db-docker": ". ../.env && PGPASSWORD=$POSTGRES_PASSWORD psql -e -h localhost -d $POSTGRES_DB -U $POSTGRES_USER -p 5433 -f ../scripts/clean-db.sql"
},
Expand Down
23 changes: 2 additions & 21 deletions portfolio/src/routes/courses.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
const express = require("express");
const router = express.Router();
const getData = require("../db").getData;

//get all from our database
const getData = async (sql_query) => {
try {
return await new Promise(function (resolve, reject) {
pool.query(sql_query, (error, results) => {
if (error) {
reject(error);
}
if (results && results.rows) {
resolve(results.rows);
} else {
reject(new Error("No results found"));
}
});
});
} catch (error_1) {
console.error(error_1);
throw new Error("Internal server error");
}
};
/**
* @desc Display all the users
*/
Expand All @@ -38,4 +19,4 @@ router.get("/list-courses", (req, res, next) => {
});

// Export the router object so index.js can access it
module.exports = router;
module.exports = router;
24 changes: 2 additions & 22 deletions portfolio/src/routes/experiences.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
const express = require("express");
const router = express.Router();

//get all from our database
const getData = async (sql_query) => {
try {
return await new Promise(function (resolve, reject) {
pool.query(sql_query, (error, results) => {
if (error) {
reject(error);
}
if (results && results.rows) {
resolve(results.rows);
} else {
reject(new Error("No results found"));
}
});
});
} catch (error_1) {
console.error(error_1);
throw new Error("Internal server error");
}
};
const getData = require("../db").getData;

/**
* @desc Display all the users
Expand All @@ -40,4 +20,4 @@ router.get("/experiences", (req, res, next) => {


// Export the router object so index.js can access it
module.exports = router;
module.exports = router;
34 changes: 34 additions & 0 deletions portfolio/src/routes/realisations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const express = require("express");
const router = express.Router();
const getData = require("../db").getData;

/**
* @desc Lister toutes les réalisations recensées
*/
router.get("/realisations", (req, res, next) => {
// Define the query
SQLquery = "SELECT * FROM realisations ORDER BY r_début DESC;"
getData(SQLquery)
.then((data) => {
res.render('realisations.ejs',
{ realisations: data, title: req.app.locals.title });
});
});

/**
* @desc Voir le détail d'une réalisation
*/
router.get("/realisation/:id", (req, res, next) => {
var id = req.params.id;
// Define the query
SQLquery = `SELECT * FROM realisations WHERE id = $1::int;`
getData(SQLquery, [id])
.then((data) => {
console.log(data[0])
res.render('realisation_view.ejs',
{ realisation: data[0], title: req.app.locals.title });
});
});

// Export the router object so index.js can access it
module.exports = router;
Loading

0 comments on commit dae6d25

Please sign in to comment.