Skip to content

Commit

Permalink
Randomize return value of /code/
Browse files Browse the repository at this point in the history
Read database credentials from environmental variables, and update test
database
  • Loading branch information
spaceawlker committed May 19, 2021
1 parent 8f874fc commit b17187d
Show file tree
Hide file tree
Showing 24 changed files with 902 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
- Run `npm install` on the `server` directory, then run `npm run start`
- The website will now be available on localhost:3000.

This assumes you have the database running, and properly replaced the access credentials where needed.
This assumes you have the database running, and have a `.env` file in place of `.env.sample` with the
needed information. `sample_database_dump.sql` is a file SQL can read that initializes a test database.

2 changes: 2 additions & 0 deletions server/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DATABASE_USERNAME=username_of_db_user
DATABASE_PASSWORD=password_of_db_user
2 changes: 2 additions & 0 deletions server/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require('dotenv').config();

var createError = require('http-errors');
var express = require('express');
var path = require('path');
Expand Down
28 changes: 15 additions & 13 deletions server/controllers/codeSnippetController.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
var mysql = require('mysql2');

var con = mysql.createConnection({
host: "localhost",
user: "[PLACEHOLDER]",
password: "[PLACEHOLDER]",
database: "[PLACEHOLDER]"
});
const database = require('../utils/database');
const random = require('../utils/random');

exports.code_snippet = function (req, res) {
process_sql_result = function(err, result) {
const con = database.get_connection();

con.query("SELECT COUNT(*) FROM code_snippet;", process_count_query);

function process_count_query(err, result) {
if (err) throw err;
res.send(result);
const code_snippet_count = result[0]["COUNT(*)"];
const id_to_retrieve = random.randint(0, code_snippet_count - 1);
con.query("SELECT * FROM code_snippet LIMIT ?, 1", [id_to_retrieve],
process_query_result);
};

con.connect(function(err) {

function process_query_result(err, result) {
if (err) throw err;
con.query("SELECT * FROM code_snippet;", process_sql_result);
});
res.json(result);
};
};


8 changes: 8 additions & 0 deletions server/node_modules/.package-lock.json

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

1 change: 1 addition & 0 deletions server/node_modules/dotenv/.github/FUNDING.yml

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

210 changes: 210 additions & 0 deletions server/node_modules/dotenv/CHANGELOG.md

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

23 changes: 23 additions & 0 deletions server/node_modules/dotenv/LICENSE

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

Loading

0 comments on commit b17187d

Please sign in to comment.