-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.js
More file actions
112 lines (99 loc) · 3.45 KB
/
controller.js
File metadata and controls
112 lines (99 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* Created by asistent on 14.05.2016.
*/
var TodoModel = require('./model').TodoModel;
var index = function (req, res) {
res.render('index');
};
// Getting all tasks
var getting = function (req, res) {
TodoModel.find({}, function (err, todos) {
if (err) {
return res.json({status: 'error', msg: 'Ошибка получения данных от сервера'});
}
res.json({status: 'success', todo: todos});
});
};
// Add task to collection
var add = function (req, res) {
if ((req.body.title === '') || (req.body.description === '') || (req.body.date === '')) {
return res.send({status: 'error', msg: 'Ошибка, нельзя добавить пустую задачу, заполните все поля'});
}
var todo = new TodoModel({
title: req.body.title,
date: req.body.date,
description: req.body.description,
completed: 0
});
todo.save(function (err) {
if (err) {
return res.send({status: 'error', msg: 'Ошибка добавления задачи в базу данных'});
} else {
return res.send({status: 'success', todo: todo});
}
});
};
// Change task description
var description = function (req, res) {
return TodoModel.findById(req.body.id, function (err, todo) {
if (err) {
return res.send({status: 'error', msg: 'Ошибка получения данных по этой задаче'});
}
if ((req.body.title === '') || (req.body.description === '') || (req.body.date === '')) {
return res.send({status: 'error', msg: 'Ошибка, все поля должны быть заполнены'});
}
todo.title = req.body.title;
todo.date = req.body.date;
todo.description = req.body.description;
todo.completed = 0;
return todo.save(function (err) {
if (err) {
return res.send({status: 'error', msg: 'Ошибка, не удалось внести изменения в базу данных'});
}
res.send({status: 'success', todo: todo});
});
});
};
// Mark task as resolved
var completed = function (req, res) {
return TodoModel.findById(req.body.id, function (err, todo) {
if (!todo) {
return res.send({status: 'error', msg: 'Ошибка получения данных по этой задаче'});
}
var completed;
if (todo.completed === true) {
completed = 0;
} else {
completed = 1;
}
todo.completed = completed;
return todo.save(function (err) {
if (err) {
return res.send({status: 'error'});
}
res.send({status: 'success', todo: todo});
});
});
};
// Delete task
var dell = function (req, res) {
return TodoModel.findById(req.body.id, function (err, todo) {
if (!todo) {
return res.send({status: 'error', msg: 'Ошибка получения данных по этой задаче'});
}
return TodoModel.remove({_id: req.body.id}, function (err) {
if (err) {
return res.send(err);
}
res.send({status: 'success'});
});
});
};
module.exports = {
index: index,
getting: getting,
add: add,
description: description,
completed: completed,
dell: dell
};