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

SEI 24 Aurelia's Tweedr MVC #131

Open
wants to merge 3 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.

143 changes: 143 additions & 0 deletions controllers/tweedrcontrol.js
Original file line number Diff line number Diff line change
@@ -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+ '<a href ="/write"> Write your tweed</a>')
}
})
}
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+ '<a href ="/"> Back to Timeline?</a>')
}
})

}

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! <a href ="/login"> Login Now?</a>')
}
})
}

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! <a href ="/"> Back to home?</a>')
}
})
}





/**
* ===========================================
* Export controller functions as a module
* ===========================================
*/
return {
allTweets,
writeTweed,
logIn,
postLogIn,
postTweed,
getProfile,
getRegister,
postSignup,
postFollowers

};

}
12 changes: 6 additions & 6 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: 'aurelialim',
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/tweedrmodel');

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



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

// users: userModelsObject,
pokemon: pokemonModelsObject
};
tweedr: tweedrModelsObject
};
38 changes: 0 additions & 38 deletions models/pokemon.js

This file was deleted.

65 changes: 65 additions & 0 deletions models/tweedrmodel.js
Original file line number Diff line number Diff line change
@@ -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
};
};
Loading