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

hw-w05d02-todo #27

Open
wants to merge 1 commit 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
51 changes: 51 additions & 0 deletions controllers/todo_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var express = require('express');
var router = express.Router();

var todo = require('../modules/todo');


router.get('/', todo.getAll, renderIndex);
router.get('/new', renderNew);
router.get('/:id/edit', todo.find, renderEdite)
router.post('/', todo.create, redirectShow);
router.get('/:id', todo.find, renderShow);
router.put('/:id', todo.update, redirectShow);

function renderIndex(req, res){
var mustacheVariables = {
todo: res.locals.todo
}
res.render('./todo/index', mustacheVariables);
}
function renderShow(req, res){
var todoVariables = {
todo: res.locals.todo
}
res.render('./todo/show', todoVariables);
};

function renderNew(req, res){
res.render('./todo/new');
};

function redirectShow(req, res){
res.redirect(`/todo/${res.locals.todo_id}`);
};

function redirectindex(req, res){
res.redirect('/todo');
};

function renderEdite(req, res){
var todoVariables = {
todo: res.locals.todo
};
res.render('./todo/edit', todoVariables);
};






module.exports = router;
15 changes: 15 additions & 0 deletions db/cofig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// configure postgres to connect our db to our express app
var pgPromise = require('pg-promise');
var pgInstance = pgPromise();

var config = {
host: 'localhost',
port: 5432,
database: 'todo_lists',
password:'123456',
user: 'Postgre' // your username here!!
}

var connection = pgInstance(config);

module.exports = connection;
18 changes: 18 additions & 0 deletions db/seed.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
DROP DATABASE IF EXISTS todo_lists;
CREATE DATABASE todo_lists;

\c todo_lists

CREATE TABLE todo(
id serial primary key,
name varchar,
definition text
);

INSERT INTO todo(name, definition) VALUES
('note1', 'text1'),
('note2', 'text2'),
('note3', 'text3 ');



47 changes: 47 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// top level config ------------------------------------------------------
// require our packages and dependancies

var express = require('express');
var mustache = require('mustache-express');
var port = 3001;

// morgan is a logger - similar to what we built in the middleware
// lesson it will log each request made to our server
var logger = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');


// app level config ------------------------------------------------------
// create our application and tell it what to do
var app = express();

// mustache set up
app.engine('html', mustache());
app.set('view engine', 'html');
app.set('views', __dirname + '/views');

app.use('/static', express.static(__dirname + '/public'));

app.use(logger('dev'));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));

app.use(methodOverride('_method'));

// import our controllers
app.get('/', function(req, res){
res.render('./index');
})
var todoController = require('./controllers/todo_controller');

//route controllers
app.use('/todo', todoController );

// start the server!
app.listen(port, function () {
console.log('---------------------------------------');
console.log('Express listening on localhost:' + port);
console.log('---------------------------------------');
});
67 changes: 67 additions & 0 deletions modules/todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var db = require('../db/cofig');

var todo = {};

// create a method that gets all the data from the "pokemon" table
todo.getAll = function (req, res, next) {
db.manyOrNone("SELECT * FROM todo;") // query the database
.then(function (result) {
console.log(result); // return the data as a javascript object "result"
res.locals.todo = result; // save that javascript object to the response object in res.locals.pokemon
next(); // move on to the next command
})
.catch(function(error){ // if there is an issue when getting all the pokemon
console.log(error); // log the error
next(); // move on to the next command
})
}

todo.find = function(req, res, next){
var id = req.params.id;
db.oneOrNone("SELECT * FROM todo WHERE id=$1;", [id])
.then(function(result){
res.locals.todo = result;
next();
}).catch(function(error){
console.log(error);
next();
})
}

todo.create = function(req, res, next){
db.one("INSERT INTO todo(name, definition) VALUES($1, $2) RETURNING id;", [req.body.name, req.body.definition])
.then(function(result){
res.locals.todo_id = result.id;
next();
}).catch(function(error){
console.log(error);
next();
})
};

todo.delete = function(req, res, next){
db.none("DELETE FROM todo WHERE id=$1;", [req.params.id])
.then(function(){
console.log("successful delete")
next();
}).catch(function(error){
console.log(error);
});
};

todo.update = function(req, res, next){
db.one("UPDATE todo SET subject=$1, content=$2 WHERE id=$3 RETURNING id;", [req.body.subject, req.body.content, req.params.id])
.then(function(result){
console.log('seccessful update')
res.locals.todo_id = result.id;
next();
}).catch(function(error){
console.log(error);
});
};





module.exports = todo;
Loading