diff --git a/controllers/pokemon.js b/controllers/pokemon.js deleted file mode 100644 index fcd9fac..0000000 --- a/controllers/pokemon.js +++ /dev/null @@ -1,25 +0,0 @@ -module.exports = (db) => { - - /** - * =========================================== - * Controller logic - * =========================================== - */ - - let indexControllerCallback = (request, response) => { - db.pokemon.getAll((error, allPokemon) => { - response.render('pokemon/index', { allPokemon }); - }); - }; - - - /** - * =========================================== - * Export controller functions as a module - * =========================================== - */ - return { - index: indexControllerCallback, - }; - -} diff --git a/controllers/tweedrcontrol.js b/controllers/tweedrcontrol.js new file mode 100644 index 0000000..e7738a7 --- /dev/null +++ b/controllers/tweedrcontrol.js @@ -0,0 +1,143 @@ + +module.exports = (db) => { + + /** + * =========================================== + * Controller logic + * =========================================== + */ + +let loginCookie; + + let allTweets = (request,response)=>{ + db.tweedr.getAllTweets((err,result)=>{ + if(err){ + response.send('Error at getAllTweets') + } else{ + let data =result.rows; + let obj = {data:data}; + // console.log(obj) + + response.render('tweedr/index',obj) + } + }) + } + + let logIn = (request, response) => { + // send response with some data (a string) + response.render('tweedr/login') + +}; + +let postLogIn = (request, response) => { + let values = [request.body.username,request.body.password]; + db.tweedr.postLogin(values,(err,result) =>{ + if(err){ + console.log('Error at postLogin---', err.message) + } else { + let userInfo = result.rows[0]; + let sessionid = userInfo.id; + let userName = userInfo.username; + let loginCookie = response.cookie("session",sessionid, { maxAge: 900000}); + let userCookie = response.cookie("userInfo",userName, { maxAge: 900000}); + response.send("Welcome "+ userName+ ' Write your tweed') + } + }) +} + let writeTweed = (request,response)=>{ + if(loginCookie=== null) { + response.render('tweedr/login') + } else { + let userName = request.cookies['userInfo']; + let author_id = request.cookies['session']; + let obj = {username:userName, author_id:author_id} + + response.render('tweedr/write',obj) + + } + } + +let postTweed = (request,response) => { + let userName = request.cookies['userInfo']; + let author_id = request.cookies['session']; + console.log(author_id); + let tweed = request.body.tweed; + +let values = [tweed,author_id]; +console.log(values) + db.tweedr.postedTweed(values,(err,result)=>{ + if(err){ + console.log('Error at postTweed---', err.message) + } else{ + + response.send("Success "+ userName+ ' Back to Timeline?') + } + }) + +} + +let getProfile =(request,response)=> { + let values = [request.params.id]; + db.tweedr.getUserProfile(values,(err,result)=>{ + if(err){ + console.log('Error at getProfile---', err.message) + } else{ + let data = result.rows; + let obj = {data:data}; + response.render('tweedr/profile',obj) + } + }) + +} +let getRegister =(request,response) =>{ + response.render('tweedr/register') + +} + +let postSignup = (request,response) =>{ + let values = [request.body.username,request.body.password]; + db.tweedr.postedSignup(values,(err,result)=>{ + if(err){ + console.log('Error at getRegistered---', err.message) + } else{ + response.send('Success! Login Now?') + } + }) +} + +let postFollowers = (request,response) =>{ + let follower_id = request.cookies['session']; + + let values = [request.params.id,follower_id]; + db.tweedr.postedFollowers(values,(err,result)=>{ + if(err){ + console.log('Error at postFollowers---', err.message) + } else{ + response.send('Successfully followed! Back to home?') + } + }) +} + + + + + + /** + * =========================================== + * Export controller functions as a module + * =========================================== + */ + return { + allTweets, + writeTweed, + logIn, + postLogIn, + postTweed, + getProfile, + getRegister, + postSignup, + postFollowers + + }; + +} \ No newline at end of file diff --git a/db.js b/db.js index 7b48a9e..b144d61 100644 --- a/db.js +++ b/db.js @@ -33,9 +33,9 @@ if( process.env.DATABASE_URL ){ }else{ configs = { - user: 'akira', + user: 'aurelialim', host: '127.0.0.1', - database: 'testdb', + database: 'tweedr_db', port: 5432 }; } @@ -62,9 +62,9 @@ pool.on('error', function (err) { */ -const allPokemonModelsFunction = require('./models/pokemon'); +const allTweedrModelsFunction = require('./models/tweedrmodel'); -const pokemonModelsObject = allPokemonModelsFunction( pool ); +const tweedrModelsObject = allTweedrModelsFunction( pool ); @@ -95,5 +95,5 @@ module.exports = { */ // users: userModelsObject, - pokemon: pokemonModelsObject -}; + tweedr: tweedrModelsObject +}; \ No newline at end of file diff --git a/models/pokemon.js b/models/pokemon.js deleted file mode 100644 index 766cf1b..0000000 --- a/models/pokemon.js +++ /dev/null @@ -1,38 +0,0 @@ -/** - * =========================================== - * Export model functions as a module - * =========================================== - */ -module.exports = (dbPoolInstance) => { - - // `dbPoolInstance` is accessible within this function scope - - let getAll = (callback) => { - - let query = 'SELECT * FROM pokemons'; - - dbPoolInstance.query(query, (error, queryResult) => { - if( error ){ - - // invoke callback function with results after query has executed - callback(error, null); - - }else{ - - // invoke callback function with results after query has executed - - if( queryResult.rows.length > 0 ){ - callback(null, queryResult.rows); - - }else{ - callback(null, null); - - } - } - }); - }; - - return { - getAll, - }; -}; diff --git a/models/tweedrmodel.js b/models/tweedrmodel.js new file mode 100644 index 0000000..253c0fb --- /dev/null +++ b/models/tweedrmodel.js @@ -0,0 +1,65 @@ +/** + * =========================================== + * Export model functions as a module + * =========================================== + */ +module.exports = (dbPoolInstance) => { + + // `dbPoolInstance` is accessible within this function scope +let getAllTweets = (callback) => { + let query = 'SELECT * FROM tweets'; + dbPoolInstance.query(query,(err,result)=>{ + callback(err,result) + + }) + } + +let postLogin = (values,callback) => { + let query = 'SELECT * FROM users WHERE username=$1 and password=$2'; + dbPoolInstance.query(query,values,(err,result)=>{ + callback(err,result) + + }) + } + + let postedTweed = (values,callback) => { + let query = 'INSERT INTO tweets (post, author_id) VALUES($1,$2)'; + dbPoolInstance.query(query,values,(err,result)=>{ + callback(err,result) + + }) + } + + let getUserProfile = (values,callback) => { + let query = 'SELECT users.username, users.id, tweets.post FROM users INNER JOIN tweets on users.id =$1 and tweets.author_id =$1'; + dbPoolInstance.query(query,values,(err,result)=>{ + callback(err,result) + + }) + } + + let postedSignup = (values,callback) => { + let query = 'INSERT INTO users (username, password) VALUES($1,$2)'; + dbPoolInstance.query(query,values,(err,result)=>{ + callback(err,result) + + }) + } + +let postedFollowers =(values,callback) => { +let query = 'INSERT INTO followers (user_id, follower_user_id) VALUES ($1,$2)'; +dbPoolInstance.query(query,values,(err,result)=>{ + callback(err,result) + + }) + } + + return { + getAllTweets, + postLogin, + postedTweed, + getUserProfile, + postedSignup, + postedFollowers + }; +}; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index abf70d8..1026d5e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,16 +4,6 @@ "lockfileVersion": 1, "requires": true, "dependencies": { - "@types/node": { - "version": "10.11.7", - "resolved": "https://registry.npmjs.org/@types/node/-/node-10.11.7.tgz", - "integrity": "sha512-yOxFfkN9xUFLyvWaeYj90mlqTJ41CsQzWKS3gXdOMOyPVacUsymejKxJ4/pMW7exouubuEeZLJawGgcNGYlTeg==" - }, - "@types/semver": { - "version": "5.5.0", - "resolved": "https://registry.npmjs.org/@types/semver/-/semver-5.5.0.tgz", - "integrity": "sha512-41qEJgBH/TWgo5NFSvBCJ1qkoi3Q6ONSF2avrHq1LVEZfYpdHmj0y9SuTK+u9ZhG1sYQKBL1AWXKyLWP4RaUoQ==" - }, "abbrev": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", @@ -775,6 +765,11 @@ "supports-color": "^2.0.0" } }, + "commander": { + "version": "2.20.3", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", + "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==" + }, "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -858,23 +853,14 @@ } }, "editorconfig": { - "version": "0.15.2", - "resolved": "https://registry.npmjs.org/editorconfig/-/editorconfig-0.15.2.tgz", - "integrity": "sha512-GWjSI19PVJAM9IZRGOS+YKI8LN+/sjkSjNyvxL5ucqP9/IqtYNXBaQ/6c/hkPNYQHyOHra2KoXZI/JVpuqwmcQ==", + "version": "0.15.3", + "resolved": "https://registry.npmjs.org/editorconfig/-/editorconfig-0.15.3.tgz", + "integrity": "sha512-M9wIMFx96vq0R4F+gRpY3o2exzb8hEj/n9S8unZtHSvYjibBp/iMufSzvmOcV/laG0ZtuTVGtiJggPOSW2r93g==", "requires": { - "@types/node": "^10.11.7", - "@types/semver": "^5.5.0", "commander": "^2.19.0", - "lru-cache": "^4.1.3", + "lru-cache": "^4.1.5", "semver": "^5.6.0", "sigmund": "^1.0.1" - }, - "dependencies": { - "commander": { - "version": "2.19.0", - "resolved": "https://registry.npmjs.org/commander/-/commander-2.19.0.tgz", - "integrity": "sha512-6tvAOO+D6OENvRAh524Dh9jcfKTYDQAqvqezbCW82xj5X0pSrcpxtvRKHLG0yBY6SD7PSDrJaj+0AiOcKVd1Xg==" - } } }, "ee-first": { @@ -986,6 +972,24 @@ "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" }, + "fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" + }, + "glob": { + "version": "7.1.6", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz", + "integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==", + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + }, "globals": { "version": "9.18.0", "resolved": "https://registry.npmjs.org/globals/-/globals-9.18.0.tgz", @@ -1027,6 +1031,15 @@ "safer-buffer": ">= 2.1.2 < 3" } }, + "inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "requires": { + "once": "^1.3.0", + "wrappy": "1" + } + }, "inherits": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", @@ -1059,14 +1072,22 @@ } }, "js-beautify": { - "version": "1.8.7", - "resolved": "https://registry.npmjs.org/js-beautify/-/js-beautify-1.8.7.tgz", - "integrity": "sha512-yhAMCTv0L9GNg6Gql7i+g4C1z9rQhfHXy4J0TGYFoBzzHR4reWYS573gkRrPuE58dYOH451LmBeAb8L1pLEfdA==", - "requires": { - "config-chain": "~1.1.5", - "editorconfig": "^0.15.0", - "mkdirp": "~0.5.0", - "nopt": "~4.0.1" + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/js-beautify/-/js-beautify-1.13.0.tgz", + "integrity": "sha512-/Tbp1OVzZjbwzwJQFIlYLm9eWQ+3aYbBXLSaqb1mEJzhcQAfrqMMQYtjb6io+U6KpD0ID4F+Id3/xcjH3l/sqA==", + "requires": { + "config-chain": "^1.1.12", + "editorconfig": "^0.15.3", + "glob": "^7.1.3", + "mkdirp": "^1.0.4", + "nopt": "^5.0.0" + }, + "dependencies": { + "mkdirp": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz", + "integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==" + } } }, "js-tokens": { @@ -1085,9 +1106,9 @@ "integrity": "sha1-Hq3nrMASA0rYTiOWdn6tn6VJWCE=" }, "lodash": { - "version": "4.17.11", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", - "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==" + "version": "4.17.20", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz", + "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==" }, "lodash.escaperegexp": { "version": "4.1.2", @@ -1103,9 +1124,9 @@ } }, "lru-cache": { - "version": "4.1.3", - "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.3.tgz", - "integrity": "sha512-fFEhvcgzuIoJVUF8fYr5KR0YqxD238zgObTps31YdADwPPAp82a4M8TrckkWyx7ekNlf9aBcVn81cFwwXngrJA==", + "version": "4.1.5", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.5.tgz", + "integrity": "sha512-sWZlbEP2OsHNkXrMl5GYk/jKk70MBng6UU4YI/qGDYbgf6YbP4EvmqISbXCoJiRKs+1bSpFHVgQxvJ17F2li5g==", "requires": { "pseudomap": "^1.0.2", "yallist": "^2.1.2" @@ -1164,16 +1185,16 @@ } }, "minimist": { - "version": "0.0.8", - "resolved": "http://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", - "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=" + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", + "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" }, "mkdirp": { - "version": "0.5.1", - "resolved": "http://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", - "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "version": "0.5.5", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz", + "integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==", "requires": { - "minimist": "0.0.8" + "minimist": "^1.2.5" } }, "ms": { @@ -1187,12 +1208,11 @@ "integrity": "sha1-KzJxhOiZIQEXeyhWP7XnECrNDKk=" }, "nopt": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/nopt/-/nopt-4.0.1.tgz", - "integrity": "sha1-0NRoWv1UFRk8jHUFYC0NF81kR00=", + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/nopt/-/nopt-5.0.0.tgz", + "integrity": "sha512-Tbj67rffqceeLpcRXrT7vKAN8CwfPeIBgM7E6iBkmKLV7bEMwpGgYLGv0jACUsECaa/vuxP0IjEont6umdMgtQ==", "requires": { - "abbrev": "1", - "osenv": "^0.1.4" + "abbrev": "1" } }, "number-is-nan": { @@ -1213,6 +1233,14 @@ "ee-first": "1.1.1" } }, + "once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "requires": { + "wrappy": "1" + } + }, "os-homedir": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/os-homedir/-/os-homedir-1.0.2.tgz", @@ -1223,15 +1251,6 @@ "resolved": "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", "integrity": "sha1-u+Z0BseaqFxc/sdm/lc0VV36EnQ=" }, - "osenv": { - "version": "0.1.5", - "resolved": "https://registry.npmjs.org/osenv/-/osenv-0.1.5.tgz", - "integrity": "sha512-0CWcCECdMVc2Rw3U5w9ZjqX6ga6ubk1xDVKxtBQPK7wis/0F2r9T6k4ydGYhecl7YUBxBVxhL5oisPsNxAPe2g==", - "requires": { - "os-homedir": "^1.0.0", - "os-tmpdir": "^1.0.0" - } - }, "packet-reader": { "version": "0.3.1", "resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-0.3.1.tgz", @@ -1601,6 +1620,11 @@ "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=" }, + "wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" + }, "xtend": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz", diff --git a/package.json b/package.json index 3d96687..ae851a8 100644 --- a/package.json +++ b/package.json @@ -17,5 +17,15 @@ "pg": "^7.4.3", "react": "^16.3.2", "react-dom": "^16.3.2" - } + }, + "description": "Let's make a cool new app called TWEEDR!! Not Twitter, geez...", + "repository": { + "type": "git", + "url": "git+https://github.com/loribean/mvc-tweedr.git" + }, + "author": "", + "bugs": { + "url": "https://github.com/loribean/mvc-tweedr/issues" + }, + "homepage": "https://github.com/loribean/mvc-tweedr#readme" } diff --git a/routes.js b/routes.js index efdee08..408a20d 100644 --- a/routes.js +++ b/routes.js @@ -13,8 +13,15 @@ module.exports = (app, allModels) => { */ // require the controller - const pokemonControllerCallbacks = require('./controllers/pokemon')(allModels); + const tweedrControllerCallbacks = require('./controllers/tweedrcontrol')(allModels); - app.get('/pokemons', pokemonControllerCallbacks.index); - //app.get('/pokemons/:id', pokemons.getPokemon); -}; + app.get('/', tweedrControllerCallbacks.allTweets); + app.get('/login', tweedrControllerCallbacks.logIn); + app.post('/storepassword', tweedrControllerCallbacks.postLogIn); + app.get('/write', tweedrControllerCallbacks.writeTweed); + app.post('/posted', tweedrControllerCallbacks.postTweed); + app.get('/users/:id',tweedrControllerCallbacks.getProfile) + app.get('/register', tweedrControllerCallbacks.getRegister); + app.post('/signed', tweedrControllerCallbacks.postSignup); + app.post("/followed/:id",tweedrControllerCallbacks.postFollowers) +}; \ No newline at end of file diff --git a/seed.sql b/seed.sql index e69de29..e3f9624 100644 --- a/seed.sql +++ b/seed.sql @@ -0,0 +1,28 @@ + +-- INSERT INTO tweets (post, author_id) VALUES ('ohreallyah','12345'); + +-- INSERT INTO users (username, password) VALUES ('timotits','12345'); + +-- INSERT INTO users (username, password) VALUES ('jewelyap','12345'); +-- INSERT INTO users (username, password) VALUES ('polkalette','12345'); +-- INSERT INTO users (username, password) VALUES ('shurui','12345'); + +-- INSERT INTO tweets (post, author_id) VALUES ('Hello world!',1); + +-- INSERT INTO tweets (post, author_id) VALUES ('So stressed...',2); + +-- INSERT INTO tweets (post, author_id) VALUES ('Proud of Tofu for passing his obedience test!',2); + +-- INSERT INTO tweets (post, author_id) VALUES ('Stonks',4); + +-- INSERT INTO tweets (post, author_id) VALUES ('Cant wait for the weekend!',5); + +-- INSERT INTO tweets (post, author_id) VALUES ('Bib Gourmand',9); + +-- INSERT INTO tweets (post, author_id) VALUES ('I am a lawyer.',7); + +-- INSERT INTO tweets (post, author_id) VALUES ('Starwars is awesome',10); + +-- INSERT INTO tweets (post, author_id) VALUES ('I miss cashie',11); + +-- INSERT INTO followers (user_id, follower_user_id) VALUES ($1,$2); \ No newline at end of file diff --git a/tables.sql b/tables.sql index e69de29..5d8e436 100644 --- a/tables.sql +++ b/tables.sql @@ -0,0 +1,18 @@ +CREATE TABLE IF NOT EXISTS users ( + id SERIAL PRIMARY KEY, + username TEXT, + password TEXT +); + +CREATE TABLE IF NOT EXISTS tweets ( + id SERIAL PRIMARY KEY, + post TEXT, + author_id INTEGER, + date_posted TIMESTAMP DEFAULT now() +); + +CREATE TABLE IF NOT EXISTS followers ( + id SERIAL PRIMARY KEY, + user_id INTEGER, + follower_user_id INTEGER +); \ No newline at end of file diff --git a/views/pokemon/index.jsx b/views/pokemon/index.jsx deleted file mode 100644 index b0c8382..0000000 --- a/views/pokemon/index.jsx +++ /dev/null @@ -1,17 +0,0 @@ -var React = require("react"); - -class Home extends React.Component { - render() { - console.log(this.props.types); - return ( - -
-