diff --git a/class7-week3/actions/clear.js b/class7-week3/actions/clear.js new file mode 100644 index 000000000..15d5fa393 --- /dev/null +++ b/class7-week3/actions/clear.js @@ -0,0 +1,16 @@ +const Todo = require('../models/todo') + +module.exports = function create(request, response) { + + Todo.clear(error => { + if (error) { + console.error(error) + response.status(500) + response.json({error: 'Internal error'}) + } else { + response.status(204) + response.end() + } + }) + +} \ No newline at end of file diff --git a/class7-week3/actions/create.js b/class7-week3/actions/create.js index 4948a8513..019d505ae 100644 --- a/class7-week3/actions/create.js +++ b/class7-week3/actions/create.js @@ -4,8 +4,8 @@ const deserializeTodo = require('../util/deserializeTodo') module.exports = function create(request, response) { const todo = deserializeTodo(request, response) + console.log(todo); if (todo == null) { return } - Todo.create(todo.description, (error, todo) => { if (error) { console.error(error) diff --git a/class7-week3/actions/index.js b/class7-week3/actions/index.js index 58056b22a..6fa8355cc 100644 --- a/class7-week3/actions/index.js +++ b/class7-week3/actions/index.js @@ -2,5 +2,8 @@ module.exports = { list: require('./list'), create: require('./create'), update: require('./update'), - remove: require('./remove') + remove: require('./remove'), + clear: require('./clear'), + markAsDone: require('./markAsDone'), + markAsNotDone: require('./markAsNotDone') } \ No newline at end of file diff --git a/class7-week3/actions/markAsDone.js b/class7-week3/actions/markAsDone.js new file mode 100644 index 000000000..068da549b --- /dev/null +++ b/class7-week3/actions/markAsDone.js @@ -0,0 +1,23 @@ +const Todo = require('../models/todo') +const deserializeTodo = require('../util/deserializeTodo') + +module.exports = function update(request, response) { + + const id = request.params.id + const todo = deserializeTodo(request, response) + if (todo == null) { return } + + Todo.update(id, todo.done, (error, todo) => { + if (error == null) { + response.json({todo}) + } else if (error.name === 'NotFound') { + response.status(404) + response.json({error: error.message}) + } else { + console.error(error) + response.status(500) + response.json({error: 'Internal error'}) + } + }) + +} \ No newline at end of file diff --git a/class7-week3/actions/markAsNotDone.js b/class7-week3/actions/markAsNotDone.js new file mode 100644 index 000000000..068da549b --- /dev/null +++ b/class7-week3/actions/markAsNotDone.js @@ -0,0 +1,23 @@ +const Todo = require('../models/todo') +const deserializeTodo = require('../util/deserializeTodo') + +module.exports = function update(request, response) { + + const id = request.params.id + const todo = deserializeTodo(request, response) + if (todo == null) { return } + + Todo.update(id, todo.done, (error, todo) => { + if (error == null) { + response.json({todo}) + } else if (error.name === 'NotFound') { + response.status(404) + response.json({error: error.message}) + } else { + console.error(error) + response.status(500) + response.json({error: 'Internal error'}) + } + }) + +} \ No newline at end of file diff --git a/class7-week3/data/todos.json b/class7-week3/data/todos.json index 3c4a24a8c..7c7c19e51 100644 --- a/class7-week3/data/todos.json +++ b/class7-week3/data/todos.json @@ -1 +1 @@ -[{"id":"f3e2462d-0bfb-46f2-8ea8-4fc7020f0f30","description":"Go into bed","done":false},{"id":"a2e2a68e-1a49-4fb5-ba66-f618a523e631","description":"Go into bed","done":false},{"id":"c8f9c333-2372-41c9-bc7b-3448b61f3371","description":"Do something","done":false},{"id":"bde9faac-582c-4536-8d01-b98252618340","description":"Blablabla","done":false}] \ No newline at end of file +[{"id":"a2e2a68e-1a49-4fb5-ba66-f618a523e631","description":"Go into bed","done":false},{"id":"c8f9c333-2372-41c9-bc7b-3448b61f3371","description":"Do something","done":false},{"id":"bde9faac-582c-4536-8d01-b98252618340","description":"Blablabla","done":false}] \ No newline at end of file diff --git a/class7-week3/index.js b/class7-week3/index.js index a28d0d431..909b01c98 100644 --- a/class7-week3/index.js +++ b/class7-week3/index.js @@ -5,11 +5,16 @@ const app = Express() app.use(bodyParser.json()) -const {list, create, update, remove} = require('./actions') +const {list, create, update, remove, clear,markAsDone, markAsNotDone} = require('./actions') app.get('/todos', list) app.post('/todos', create) app.put('/todos/:id', update) app.delete('/todos/:id', remove) +app.delete('/todos', clear) +app.post('/todos/:id/done', markAsDone) +app.delete('/todos/:id/done', markAsNotDone) -app.listen(3000) \ No newline at end of file +let port = 3000; +app.listen(port) +console.log("listen to port" + port); \ No newline at end of file diff --git a/class7-week3/models/todo.js b/class7-week3/models/todo.js index 2db6c7455..a1edc227d 100644 --- a/class7-week3/models/todo.js +++ b/class7-week3/models/todo.js @@ -75,6 +75,60 @@ class Todo { }) } + clear(callback) { + this.load((error, todos) => { + if (error) { callback(error); return } + + this.save(todos, error => { + if (error) { callback(error); return } + callback(null) + }) + }) + } + markAsDone(id, callback) { + this.load((error, todos) => { + if (error) { callback(error); return } + + const todo = todos.find(t => t.id === id) + if (todo == null) { + const error = new Error(`Todo with ID ${id} does not exist`) + error.name = 'NotFound' + + callback(error) + return + } + + todo.done = true; + + this.save(todos, error => { + if (error) { callback(error); return } + + callback(null, todo) + }) + }) + } + markAsNotDone(id, callback) { + this.load((error, todos) => { + if (error) { callback(error); return } + + const todo = todos.find(t => t.id === id) + if (todo == null) { + const error = new Error(`Todo with ID ${id} does not exist`) + error.name = 'NotFound' + + callback(error) + return + } + + todo.done = false; + + this.save(todos, error => { + if (error) { callback(error); return } + + callback(null, todo) + }) + }) + } } module.exports = new Todo() \ No newline at end of file diff --git a/hameed_hw/data1.json b/hameed_hw/data1.json new file mode 100644 index 000000000..ee559f577 --- /dev/null +++ b/hameed_hw/data1.json @@ -0,0 +1,3 @@ +{ + "name": "John" +} \ No newline at end of file diff --git a/hameed_hw/index.js b/hameed_hw/index.js new file mode 100644 index 000000000..0737f2d19 --- /dev/null +++ b/hameed_hw/index.js @@ -0,0 +1,89 @@ +// Create an http server that can add and subtract from a number, which we will call the "state". +// To run this file: node . +// Then in your browser: http://localhost:3000 +let http = require('http'); +// declare the state variabel which start from 10 +let state = 10 +let server = http.createServer(); +// give the port a number +const port = 3000; +//To listen to the port and console the state of this port +server.listen(port, function(error) { + if (error) { + console.log(error); + } else { + console.log('api listening on port', port); + } +}); +// creat a response for the request url +server.on('request', function(request, response) { + //console.log(response); + console.log('New http request received', request.url); + //use switch to determine the request url case and prosses the resultes + switch (request.url) { + // /state + // response: the current state in a html format + // when the server starts, this should return "10" + //in this case we just show the curent state value + case '/state': + console.log('state is ' + state); + showState(state); + break; + // This should add 1 to the current state + case '/add': + state++; + console.log('state is ' + state); + response.setHeader('content-type', 'text/html'); + showState(state); + break; + // This should subtract 1 ƒrom the current state + case '/remove': + state--; + console.log('state is ' + state); + showState(state); + break; + // This should set the state back to 10 + // I didnt want to start with error so I did the first step as reset and show the state. + case '/reset': + case '/': + state = 10; + console.log('state is ' + state); + showState(state); + break; + // Any other URL + // Response: return error. + default: + console.log('error code 404: Not found'); + setError('error code 404: Not found',response); + } + // this function take the state value and make some html tages to show it. + // I did some extra button to make the request url faster. + function showState(state){ + response.setHeader('content-type', 'text/html'); + response.write(` + +
+or use a good url in our case like (add, remove, or reset)
+ + `); + response.end(); + } +}); \ No newline at end of file diff --git a/hameed_hw/package.json b/hameed_hw/package.json new file mode 100644 index 000000000..5549db1ce --- /dev/null +++ b/hameed_hw/package.json @@ -0,0 +1,11 @@ +{ + "name": "hameed_hw", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC" +} diff --git a/hameed_hw/read.js b/hameed_hw/read.js new file mode 100644 index 000000000..d425a832c --- /dev/null +++ b/hameed_hw/read.js @@ -0,0 +1,13 @@ +var fs = require('fs'); +var data = require('./data1.json'); +console.log(data.name); +// +fs.readdir('../hameed_hw', function(err, data) { + console.log(data); +}); + +fs.readFile('./data1.json', 'utf-8', function(err, data) { + + data = JSON.parse(data); + console.log(data); +}); \ No newline at end of file diff --git a/week0/test.js b/week0/test.js new file mode 100644 index 000000000..32f95c0d1 --- /dev/null +++ b/week0/test.js @@ -0,0 +1 @@ +hi \ No newline at end of file