Skip to content
Merged
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
Binary file added Task_with_mysql/my-todolist/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions Task_with_mysql/my-todolist/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.idea/
144 changes: 144 additions & 0 deletions Task_with_mysql/my-todolist/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
var express = require('express');
var session = require('cookie-session');
var mysql = require('mysql');

// Loads the piece of middleware for sessions
var bodyParser = require('body-parser'); // Loads the piece of middleware for managing the settings
var urlencodedParser = bodyParser.urlencoded({ extended: false });
var connection = require('express-myconnection');
var app = express();

var id,id1=0;
/* Using the sessions */
app.use(session({secret: 'todotopsecret'}))


/* If there is no to do list in the session,
we create an empty one in the form of an array before continuing */
.use(function(req, res, next){
if (typeof(req.session.todolist) == 'undefined') {
req.session.todolist = [];}
if (typeof(req.session.priority) == 'undefined'){
req.session.priority=[];
}
next();
})
app.use(
connection(mysql,{
host:'localhost',
user:'root',
password:'root',
database:'mydb'
},'pool')
)

/* The to do list and the form are displayed */
.get('/todo', function(req, res) {
req.getConnection(function(err,connection){
var query = connection.query('SELECT * FROM Task ORDER BY Priority',function(err,rows)
{

if(err)
console.log("Error Selecting : %s ",err );

res.render('todo.ejs',{data:rows});


});
});
// res.render('todo.ejs');
})
.get('/todo1', function(req, res) {
res.render('todo1.ejs');
})
/* Adding an item to the to do list */
.post('/todo/add/', urlencodedParser, function(req, res) {
if (req.body.newtodo != '') {
req.session.todolist.push(req.body.newtodo);
req.session.priority.push(req.body.s1);

req.getConnection(function (err, connection) {

var data = {

Todo: req.body.newtodo,
priority: req.body.s1,
Memberassigned:req.body.assigned

};

var query = connection.query("INSERT INTO Task set ? ", data, function (err, rows) {

if (err)
console.log("Error inserting : %s ", err);

res.redirect('/todo');

});

// console.log(query.sql); get raw query

});

}
})
.post('/todo/update/', urlencodedParser, function(req, res) {
req.getConnection(function (err, connection) {

var data = {
Todo: req.body.updatedtodo,
Memberassigned: req.body.updatedmember,
Priority: req.body.updtprior
};
connection.query("UPDATE Task set ? WHERE id = ? ",[data,id], function(err, rows)
{

if (err)
console.log("Error Updating : %s ",err );

res.redirect('/todo');

});


// console.log(query.sql); get raw query

});
// req.session.todolist[id]=req.body.updatedtodo;
//res.redirect('/todo');
})
/* Deletes an item from the to do list */
.get('/todo/delete/:id', function(req, res) {
var id2 = req.params.id;

req.getConnection(function (err, connection) {

connection.query("DELETE FROM Task WHERE id = ? ",[id2], function(err, rows)
{

if(err)
console.log("Error deleting : %s ",err );

res.redirect('/todo');

});

});
//res.redirect('/todo');
})
.get('/todo/update/:id', function(req, res) {
id=req.params.id;
id1=1;
res.redirect('/todo1');
})
/* Redirects to the to do list if the page requested is not found */
.use(function(req, res, next){
if(id1==0) {
res.redirect('/todo');
}
else
{
res.redirect('/todo1');
}
})
.listen(8001);
14 changes: 14 additions & 0 deletions Task_with_mysql/my-todolist/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "my-todolist",
"version": "0.1.0",
"dependencies": {
"express": "~4.11.0",
"ejs": "~2.1.4",
"express-myconnection": "1.0.4",
"cookie-session": "~1.1.0",
"body-parser": "~1.10.1",
"mysql":"2.12.0"
},
"author": "Mateo21 <mateo21@email.com>",
"description": "A very basic to do list manager"
}
Binary file added Task_with_mysql/my-todolist/views/.DS_Store
Binary file not shown.
49 changes: 49 additions & 0 deletions Task_with_mysql/my-todolist/views/todo.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!DOCTYPE html>

<html>
<head>
<title>My todolist</title>
<style>
a {text-decoration: none; color: black;}
</style>
</head>

<body>
<h1>My todolist</h1>

<ul>
<% if(data.length){

for(var i = 0;i < data.length;i++) { %>


<li><a href="/todo/update/<%=data[i].id%>">Update</a>
<%=(i+1)%>
<a href="/todo/delete/<%=data[i].id%>">Delete</a>
<%=data[i].Todo%>
<%=data[i].Memberassigned%>
<%=data[i].Priority%></li>

<%}}%>


</ul>

<form action="/todo/add/" method="post">
<p>
<label for="newtodo">What shoud I do?</label>
<input type="text" name="newtodo" id="newtodo" autofocus /><br>
<label for="newtodo">Assigned to Whom?</label>
<input type="text" name="assigned" id="assigned" autofocus /><br>
Priority:
<select name="s1" id="s1">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
<input type="submit" />
</p>
</form>
</body>
</html>
11 changes: 11 additions & 0 deletions Task_with_mysql/my-todolist/views/todo1.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<form action="/todo/update/" method="post">
<p>
<label for="newtodo">What shoud I do?</label>
<input type="text" name="updatedtodo" id="updatedtodo" autofocus />
<label for="newtodo">Assigned to whom?</label>
<input type="text" name="updatedmember" id="updatedmember" />
<label for="newtodo">Priority</label>
<input type="text" name="updtprior" id="updtprior" />
<input type="submit" />
</p>
</form>
1 change: 1 addition & 0 deletions my-todolist/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
.idea/
Loading