Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azhar mvc-tweedr #110

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions controllers/pokemon.js

This file was deleted.

174 changes: 174 additions & 0 deletions controllers/tweedr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
module.exports = (db) => {
const sha256 = require('js-sha256');
/**
* ===========================================
* Controller logic
* ===========================================
*/

let loadIndex = (request, response) => {

// db.tweedr.getAll((error, allTweets) => {
// response.render('tweedr/index', { allTweets });
// });

var loggedIn = request.cookies['loggedIn'];
if (loggedIn == sha256('true')) {
response.redirect('/main');
} else if (loggedIn == "" || loggedIn == undefined) {

db.tweedr.getTweets((error, result) => {
var allTweets = result;
// console.log(allTweets);
var output = {};
output.tweets = allTweets;
response.cookie('username', "");
response.cookie('userid', "");
response.cookie('loggedIn', "");
response.render('tweedr/index', output);
});
}

};
let login = (request, response) => {

response.render('tweedr/login');
};
let signup = (request, response) => {

response.render('tweedr/signup');

};

let signupPost = (request, response) => {
var dataIn = request.body;
db.tweedr.signup(dataIn, (error, result) => {
var username = request.body.username;
var userid = result[0].userid;
response.cookie('username', username);
response.cookie('userid', userid);
response.cookie('loggedIn', sha256('true'));
// var output = {'loggedIn':sha256('true')}
response.redirect('/main');
});
};

let loginPost = (request, response) => {
var dataIn = request.body;
db.tweedr.login(dataIn, (error, result) => {
response.cookie('username', result.username);
response.cookie('userid', result.userid);
response.cookie('loggedIn', sha256('true'));
// var output = {'loggedIn':sha256('true'), 'username': result.username, 'userid': result.userid}
response.redirect('/main');
});
};

let loadMain = (request, response) => {
var loggedIn = request.cookies['loggedIn'];
if (loggedIn == "") {
response.redirect('/');
}
else {
// var dataIn = request.body;
var username = request.cookies['username'];
var userid = request.cookies['userid'];
var loggedIn = request.cookies['loggedIn'];
var output = { 'loggedIn': loggedIn, 'username': username, 'userid': userid }
db.tweedr.getTweets((error, result) => {
var allTweets = result;
// console.log(allTweets);
output.tweets = allTweets;
db.tweedr.getTags((error, result) => {
var allTags = result;
// console.log(allTags);
output.tags = allTags;
response.render('tweedr/main', output);
});
});
}
};

let logout = (request, response) => {
// var dataIn = request.body;
// db.tweedr.login(dataIn, (error, result) => {
// response.cookie('username', result.username);
// response.cookie('userid', result.userid);
// response.cookie('loggedIn', sha256('true'));
//});
response.cookie('username', "");
response.cookie('userid', "");
response.cookie('loggedIn', "");
response.redirect('/');

};

let makePost = (request, response) => {
console.log(request.body)
var userid = request.cookies['userid'];
// console.log(userid);

var dataIn = request.body;
dataIn.userid = userid;
console.log(dataIn);

db.tweedr.makePost(dataIn, (error, result) => {

response.redirect('/main');
});


};

let newTag = (request, response) => {
var loggedIn = request.cookies['loggedIn'];

var output = {'loggedIn': loggedIn}
response.render('tweedr/add-tag', output);
};

let addTag = (request, response) => {
var dataIn = request.body;
// console.log(dataIn)
db.tweedr.addTag(dataIn, (error, result) => {
response.redirect('/main');
});
};

let newFav = (request, response) => {
var loggedIn = request.cookies['loggedIn'];

var output = {'loggedIn': loggedIn}
response.render('tweedr/add-fav', output);
};

let addFav = (request, response) => {
var dataIn = request.body;
var userid = request.cookies['userid'];
dataIn.userid = userid;
// console.log(dataIn)
db.tweedr.addFav(dataIn, (error, result) => {
response.redirect('/main');
});
};
/**
* ===========================================
* Export controller functions as a module
* ===========================================
*/
return {
loadIndex: loadIndex,
login: login,
signup: signup,
signupPost: signupPost,
loginPost: loginPost,
loadMain: loadMain,
logout: logout,
makePost: makePost,
newTag: newTag,
addTag: addTag,
newFav: newFav,
addFav: addFav
};

}
10 changes: 5 additions & 5 deletions db.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ if( process.env.DATABASE_URL ){

}else{
configs = {
user: 'akira',
user: 'Azhar',
host: '127.0.0.1',
database: 'testdb',
database: 'tweedr_db',
port: 5432
};
}
Expand All @@ -62,9 +62,9 @@ pool.on('error', function (err) {
*/


const allPokemonModelsFunction = require('./models/pokemon');
const allTweedrModelsFunction = require('./models/tweedr');

const pokemonModelsObject = allPokemonModelsFunction( pool );
const tweedrModelsObject = allTweedrModelsFunction( pool );



Expand Down Expand Up @@ -95,5 +95,5 @@ module.exports = {
*/

// users: userModelsObject,
pokemon: pokemonModelsObject
tweedr: tweedrModelsObject
};
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const express = require('express');
const methodOverride = require('method-override');
const cookieParser = require('cookie-parser');

/**
* ===================================
* Configurations and set up
Expand All @@ -24,6 +23,8 @@ app.use(express.urlencoded({
extended: true
}));

app.use(express.static('public'))

// Set react-views to be the default view engine
const reactEngine = require('express-react-views').createEngine();

Expand Down
38 changes: 0 additions & 38 deletions models/pokemon.js

This file was deleted.

Loading