diff --git a/wip-rest-api/src/database/build.js b/wip-rest-api/src/database/build.js new file mode 100644 index 0000000..c48378d --- /dev/null +++ b/wip-rest-api/src/database/build.js @@ -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; \ No newline at end of file diff --git a/wip-rest-api/src/database/connection.js b/wip-rest-api/src/database/connection.js new file mode 100644 index 0000000..572cbed --- /dev/null +++ b/wip-rest-api/src/database/connection.js @@ -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; \ No newline at end of file diff --git a/wip-rest-api/src/database/init.sql b/wip-rest-api/src/database/init.sql new file mode 100644 index 0000000..6a1e247 --- /dev/null +++ b/wip-rest-api/src/database/init.sql @@ -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; \ No newline at end of file diff --git a/wip-rest-api/src/handlers/comments.js b/wip-rest-api/src/handlers/comments.js new file mode 100644 index 0000000..e69de29 diff --git a/wip-rest-api/src/handlers/projects.js b/wip-rest-api/src/handlers/projects.js new file mode 100644 index 0000000..e69de29 diff --git a/wip-rest-api/src/handlers/users.js b/wip-rest-api/src/handlers/users.js new file mode 100644 index 0000000..e69de29 diff --git a/wip-rest-api/src/middleware/authorise.js b/wip-rest-api/src/middleware/authorise.js new file mode 100644 index 0000000..e69de29 diff --git a/wip-rest-api/src/middleware/error.js b/wip-rest-api/src/middleware/error.js new file mode 100644 index 0000000..e69de29 diff --git a/wip-rest-api/src/middleware/getUser.js b/wip-rest-api/src/middleware/getUser.js new file mode 100644 index 0000000..e69de29 diff --git a/wip-rest-api/src/middleware/logger.js b/wip-rest-api/src/middleware/logger.js new file mode 100644 index 0000000..252e4bd --- /dev/null +++ b/wip-rest-api/src/middleware/logger.js @@ -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; + \ No newline at end of file diff --git a/wip-rest-api/src/model/comments-model.js b/wip-rest-api/src/model/comments-model.js new file mode 100644 index 0000000..e69de29 diff --git a/wip-rest-api/src/model/projects-model.js b/wip-rest-api/src/model/projects-model.js new file mode 100644 index 0000000..e69de29 diff --git a/wip-rest-api/src/model/users-model.js b/wip-rest-api/src/model/users-model.js new file mode 100644 index 0000000..e69de29 diff --git a/wip-rest-api/src/server.js b/wip-rest-api/src/server.js new file mode 100644 index 0000000..8895394 --- /dev/null +++ b/wip-rest-api/src/server.js @@ -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 diff --git a/wip-rest-api/src/test/dbtest.js b/wip-rest-api/src/test/dbtest.js new file mode 100644 index 0000000..17ba786 --- /dev/null +++ b/wip-rest-api/src/test/dbtest.js @@ -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() + }) + }) \ No newline at end of file diff --git a/wip-rest-api/src/test/servertest.js b/wip-rest-api/src/test/servertest.js new file mode 100644 index 0000000..e69de29