Commit f32b8539e6880b0fcbd09c1788592375c2387a08

Authored by unknown
1 parent 46de8790
Exists in master

Improved Code

public/controllers/controller.js
... ... @@ -9,13 +9,40 @@
9 9 $scope.todolist = response;
10 10 });
11 11 $scope.task = {
12   - taskname: "E.g Task",
  12 + taskname: "",
13 13 Done: false,
14   - details: ''
15   - }
  14 + details: ""
  15 + }
16 16 $scope.addTask = function() {
17 17 //console.log($scope.todo);
  18 +
18 19 $http.post('/todolist', $scope.task).success(function(response){
  20 + $http.get('/todolist').success(function(response) {
  21 + $scope.todolist = response;
  22 + });
  23 + console.log(response);
  24 + $scope.task = {
  25 + taskname: "",
  26 + Done: false,
  27 + details: ""
  28 + }
  29 + });
  30 +
  31 +
  32 +
  33 + };
  34 +
  35 + $scope.updateTask = function(task) {
  36 + $http.put('/todolist/'+task._id,task).success(function(response) {
  37 +
  38 + });
  39 + };
  40 +
  41 + $scope.deleteTask = function(id) {
  42 + var x=confirm("Are you sure?")
  43 +
  44 + if(x==true){
  45 + $http.delete('/todolist/' + id).success(function(response) {
19 46 console.log(response);
20 47 });
21 48 $http.get('/todolist').success(function(response) {
... ... @@ -23,10 +50,7 @@
23 50 });
24 51 $scope.todo = "";
25 52 };
26   -
27   - $scope.markTaskDone = function() {
28   - console.log($scope.todo);
29   - };
  53 + }
30 54  
31 55 }]);
32 56  
... ...
public/index.html
... ... @@ -12,8 +12,11 @@
12 12 text-decoration: line-through;
13 13 color:#ccc;
14 14 }
15   - small {
16   - cursor: pointer;
  15 + button{
  16 + display:none;
  17 + }
  18 + .delete:hover button{
  19 +display:inline;
17 20 }
18 21 </style>
19 22 </head>
... ... @@ -24,24 +27,21 @@
24 27 <thead>
25 28 <tr>
26 29 <th>Tasks</th>
27   - <th>Details</th>
28 30 <th>Actions</th>
29 31 </tr>
30 32 </thead>
31 33 <tbody>
32 34 <tr>
33   - <td><input type="text" class="form-control" ng-model="task.taskname" required></td>
  35 + <td><input id="task" type="text" class="form-control" ng-model="task.taskname" required></td>
34 36 <td><textarea ng-model="task.details" required></textarea></td>
35 37 <td><button class="btn btn-primary" ng-click="addTask()">Add Task</button></td>
36 38 </tr>
37   - <tr ng-repeat="todo in todolist">
  39 + <tr ng-repeat="todo in todolist.slice().reverse()">
38 40 <!--<td><input type="checkbox" ng-model="todo.done">&nbsp;&nbsp;
39 41 <span ng-class="{'done':todo.done}">{{todo.taskname}}</span>-->
40 42 <td colspan="3">
41   - <span ng-click="DeleteTask">x</span>
42   - <input type="checkbox" ng-model="todo.Done" ng-checked="todo.Done" ng-click="markTaskDone()">&nbsp;&nbsp;
43   - <span ng-class="(todo.Done==true)? 'done':''">{{todo.taskname}}</span> <small ng-click="more=!more" ng-show="todo.details"> {{(more)? 'Hide Desc' : 'Show Desc'}}</small><br />
44   - <small ng-show="more==true">{{todo.details}}</small>
  43 + <div class=delete><button ng-click="deleteTask(todo._id)">Delete</button>&nbsp;&nbsp;<input type="checkbox" ng-model="todo.Done" ng-checked="todo.Done" ng-click="updateTask(todo)">&nbsp;&nbsp;<span ng-class="(todo.Done==true)? 'done':''">{{todo.taskname}}</span></div>
  44 +
45 45 </td>
46 46  
47 47  
... ...
server.js
... ... @@ -36,13 +36,29 @@ app.post(&#39;/todolist&#39;, function(req, res) {
36 36 });
37 37 });
38 38  
39   -app.put('/todolist', function(req, res){
40   - var id = req.params.id;
41   - console.log(req.body.Done);
42   - db.contactlist.findAndModify({query: {_id: mongojs.ObjectId(id)},
43   - update: {$set: {name: req.body.name, email: req.body.email, number: req.body.number}},
44   - new: true}, function(err, doc) {
45   - res.json(doc);
  39 +app.put('/todolist/:id', function(req, res){
  40 + console.log(req.body);
  41 + Todo.update({_id:req.params.id},req.body,{upsert:true},function(err) {
  42 + if(!err) {
  43 + res.send('updated');
  44 + }
  45 + else {
  46 + console.log(err);
  47 + res.send(404);
  48 + }
  49 + });
  50 +});
  51 +
  52 +app.delete('/todolist/:id', function(req, res){
  53 + Todo.remove({_id: req.params.id}, function(err) {
  54 + if(!err) {
  55 + res.send('deleted');
  56 + }
  57 + else {
  58 + console.log(err);
  59 + res.send(404);
  60 + }
  61 + });
46 62 });
47 63  
48 64  
... ...