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

WK3HW4 #11

Open
wants to merge 11 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "express-todo-app",
"version": "1.0.0",
"description": "**Objective:** Use Express to make a RESTful API for a to do list. Build a client for your app that uses AJAX and Handlebars templating to `CREATE`, `READ`, `UPDATE`, and `DELETE` todos.",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Cyndy528/express-todo-app.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/Cyndy528/express-todo-app/issues"
},
"homepage": "https://github.com/Cyndy528/express-todo-app#readme",
"dependencies": {
"body-parser": "^1.14.1",
"chai": "^3.4.1",
"express": "^4.13.3",
"mocha": "^2.3.3",
"request": "^2.65.0"
}
}
7 changes: 7 additions & 0 deletions public/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// wait for DOM to load before running JS
$(function() {




});
Empty file added public/styles.css
Empty file.
125 changes: 125 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
var express = require ('express');

var bodyParser = require('body-Parser');

var app = express();


//set up body parser for post stuff
app.use(bodyParser.urlencoded({ extended : true}));

// serve static files from public folder
app.use(express.static(__dirname + '/public'));


// set view engine to hbs (handlebars)
app.set('view engine', 'hbs');

//seed data, includes elements of weekday_Mornings
var weekday_Mornings = [
{_id : 1,
task: "Wake up at 6:30 am",
description: "Shower, eat breakfast, and pack lunch."
},
{_id : 2,
task: "Leave at 7:30 am",
description: "Walk to train, study day lesson for 20 minutes and read book for 10 minutes."
},
{_id : 3,
task: "Arrive at 8:30 am",
description: "Answer emails and phone calls, and prepare for the day."
}];

//set up routes
app.get('/api', function(req, res){
res.json({test : 'this is working'});
});

//set all mornings
app.get('/api/Mornings', function(req, res){
res.json( weekday_Mornings );
});

//get a single mornings
app.get('/api/mornings/:id', function(req, res){

//get mornings id from URL params
var morningID = parseInt(req.params.id);

//find mornings by id
var foundMorning = weekday_Mornings.filter(function(mornings){
return weekday_Mornings._id == morningID;
});
//send foundMorning as JSON
res.json(foundMorning);

});



//post a single morning
app.post('/api/mornings', function(req, res){

//create a new morning with form data
var newMornings = req.body;

//set a sequential id, only checking
if (weekday_Mornings.length > 0){
newMornings._id = weekday_Mornings[weekday_Mornings.length - 1]._id + 1;
} else {
newMornings._id = 1;
}
//add new morning to 'weekday_Mornings' array
weekday_Mornings.push(newMornings);

//send newMorning as JSON object
res.json( newMornings );
});


//update morning
app.put ('api/mornings/:id', function (req, res) {
//get morning id from url params ('req.params')
var morningID = parseInt(req.params.id);

//find morning to update it's id
var morningToUpDate = mornings.filter(function(morning){
return morning._id === morningId;
}) [0];
//update the morning's task
morningToUpDate.task = req.body.task;

//update the morning's description
morningToUpDate.description = req.body.description;

//send back updated morning
res.json(morningToUpDate);
});

//delete morning
app.delete('api/mornings/:id', function(req, res){
//get morning id from url params ('req.params')
var morningID= parseInt(req.params.id);

//find morning to delete by its id
var morningToDelete = morning.filter(function(morning) {
return morning._id === morningId;
}) [0];

//find index of morning in 'morning' array
var morningIndex = morning.indexOf(morningToDelete);

//remove morning from 'morning' array
morning.splice(morningIndex, 1);

//send back deleted morning
res.json(morningToDelete);

});




var server = app.listen(process.env.PORT || 3000, function(){
console.log("I am listening");
});
Empty file added test/mocha.opts
Empty file.
17 changes: 17 additions & 0 deletions test/todosTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// todosTest.js (file)

var request= require('request'),
expect = require('chai').expect,
baseUrl ='http://localhost:3000';

describe('Todos', function() {
//we want 'done' to be called back
it('should list ALL todos GET / api/todos', function(done){

request(baseUrl + '/api/todos, function (error, response, body');
//assertion
expect (response.statusCode).to.equal(200);
done();
});
});
// (similar to appt. lab)
76 changes: 76 additions & 0 deletions views/index.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">

<!-- set viewport to device width to make site responsive -->
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- bootstrap css -->
<link type="text/css" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

<!-- custom styles -->
<link rel="stylesheet" type="text/css" href="styles/main.css">

<title>Weekday Mornings</title>
</head>

<body>

<div class="header">
<h1>Early Morning<h1>
</div>

<!-- page layout -->
<div class="container">
<div class="row">
<div class="col-md-12">

<!--new todo form -->

<!-- new todo form-->
<form class="form-inline" id="create-todo">
<div class="form-group">
<input type="text" name="task" class="form-control" placeholder="Task" autofocus>
</div>
<div class="form-group">
<input type="text" name="description" class="form-control" placeholder="Description">
</div>
<div class="form-group">
<input type="submit" class="btn btn-default" value="Add To Do">
</div>
</form>
<br>

<ul class="list-group" id="to do list">

<!-- handlebars template -->
<script id="mornings-template" type="text/x-handlebars-template">
\{{#each mornings}}
<li class="list-group-item todo" data-id="\{{_id}}">

<span class="label label-default">\{{task}}</span>

\{{description}}
</script>

</div>
</div>
{{/each}}
</div>
</div>
</body>



<!-- jquery -->
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>

<!-- handlebars -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.3/handlebars.min.js"></script>

<!-- custom script -->
<script type="text/javascript" src="main.js"></script>

</body>
</html>