Skip to content

Commit

Permalink
Merge pull request #24 from fac19/backend-files
Browse files Browse the repository at this point in the history
add backend files and folders
  • Loading branch information
Alexreid95 authored May 18, 2020
2 parents 30dcf46 + 9e1ffc2 commit 7925746
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 0 deletions.
12 changes: 12 additions & 0 deletions wip-rest-api/src/database/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const fs = require("fs");
const path = require("path");
const db = require("./connection");

const initPath = path.join(__dirname, "init.sql");
const initSQL = fs.readFileSync(initPath, "utf-8");

function build (){
return db.query(initSQL);
}

module.exports = build;
12 changes: 12 additions & 0 deletions wip-rest-api/src/database/connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const pg = require("pg");
const dotenv = require("dotenv");

dotenv.config();

const db = new pg.Pool({
connectionString: process.env.DATABASE_URL,
// if we have a database URL (e.g. from Heroku we'll use that)
// otherwise it'll default to your local .env variables
});

module.exports = db;
31 changes: 31 additions & 0 deletions wip-rest-api/src/database/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
BEGIN;

DROP TABLE IF EXISTS users, posts;

CREATE TABLE users(
-- id SERIAL PRIMARY KEY UNIQUE,
-- username VARCHAR(255) NOT NULL UNIQUE,
-- email VARCHAR(255) NOT NULL UNIQUE,
-- password VARCHAR(255)
);

CREATE TABLE posts(
-- id SERIAL PRIMARY KEY,
-- user_id INTEGER REFERENCES users(id),
-- category VARCHAR(255) NOT NULL,
-- tool_name VARCHAR(255),
-- tool_description VARCHAR(255),
-- tool_link VARCHAR(255)
);

INSERT INTO users (username, email, password) VALUES ()


INSERT INTO categories (category) VALUES ()


INSERT INTO posts (user_id, cat_id, tool_name, tool_description, tool_link, date_added) VALUES
(1,3, 'Netflix', 'Stream shows and movies', 'netflix.com', '2019-08-01'),


END;
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
8 changes: 8 additions & 0 deletions wip-rest-api/src/middleware/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function logger(req, res, next) {
const time = new Date().toLocaleTimeString();
console.log(`${time} ${req.method} ${req.url}`);
next();
}

module.exports = logger;

Empty file.
Empty file.
Empty file.
23 changes: 23 additions & 0 deletions wip-rest-api/src/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const express = require("express");
const auth = require("../middleware/auth");
const error = require("../middleware/error");
const logger = require('./middleware/logger');

const PORT = process.env.PORT || 3000;

const server = express();
server.listen(PORT, () => console.log(`Listening on http://localhost:${PORT}`));

server.use(express.json()); //so that express knows to use JSON
const logger = require('./middleware/logger')

//Routes for users


//Routes for projects


//Routes for comments


//Error handler
20 changes: 20 additions & 0 deletions wip-rest-api/src/test/dbtest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const test = require('tape')
const build = require('../database/build')
const model = require('../model')

test('Test to see if this runs', t => {
t.equal(1 + 1, 2, '1+1 should equal 2')
t.end()
})

test('Check model is exporting getAllUsers function', t => {
build()
.then(() => {
t.equal('getAllUsers' in model, true)
t.end()
})
.catch(error => {
t.error(error)
t.end()
})
})
Empty file.

0 comments on commit 7925746

Please sign in to comment.