starting state fixed

This commit is contained in:
Märt Kalmo 2017-05-10 09:57:37 +03:00
parent 281735374c
commit bb5dc8c803
3 changed files with 6 additions and 34 deletions

View File

@ -8,7 +8,7 @@ const app = express();
app.get('/api/tasks', getTasks); app.get('/api/tasks', getTasks);
app.listen(3000, () => console.log('Server is running...')); app.listen(3000, () => console.log('Server is running on port 3000'));
function getTasks(request, response) { function getTasks(request, response) {
dao.getTasks() dao.getTasks()

View File

@ -11,43 +11,15 @@ class MemTaskService {
]; ];
} }
clone(what) {
return JSON.parse(JSON.stringify(what));
}
getTasks() { getTasks() {
return Promise.resolve(this.clone(this.tasks)); return Promise.resolve(this.clone(this.tasks));
} }
getTask(id) { clone(what) {
let found = this.clone(this.tasks) return JSON.parse(JSON.stringify(what));
.filter(each => each._id === id)
.pop();
return found ? Promise.resolve(found) : Promise.reject('no task with id: ' + id);
} }
saveTask(task) { getNewId() {
if (!task._id) {
task._id = this.getId();
this.tasks.push(task);
return Promise.resolve();
}
this.tasks = this.tasks
.map(each =>each._id === task._id ? task : each);
return Promise.resolve();
}
deleteTask(id) {
this.tasks = this.tasks
.filter(each => each._id !== id);
return Promise.resolve();
}
getId() {
return (Math.random() + 'A').substr(2); return (Math.random() + 'A').substr(2);
} }

View File

@ -1,8 +1,8 @@
'use strict'; 'use strict';
class Task { class Task {
constructor(title) { constructor(content) {
this.title = title; this.content = content;
} }
static withId(id, title) { static withId(id, title) {