Skip to content

Commit 379e6f9

Browse files
committed
added update and delete feature
1 parent a82e466 commit 379e6f9

File tree

4 files changed

+142
-31
lines changed

4 files changed

+142
-31
lines changed

app/routes/notes.js

+42-3
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ module.exports = function(app) {
1010
},
1111
function(err, model) {
1212
if(err) {
13-
res.send("Please try again!");
13+
res.send("error");
1414
}
15-
res.send("You are Registered!");
15+
res.send(model);
1616
}
1717
);
1818
});
1919

2020
app.post('/getNote', function (req, res) {
21-
console.log(req.body.email);
2221
Model.find({
2322
email: req.body.email
2423
},
@@ -30,4 +29,44 @@ module.exports = function(app) {
3029
}
3130
);
3231
});
32+
33+
app.post('/deleteNote', function (req, res) {
34+
Model.remove({ _id: req.body.id }, function(err, result) {
35+
if (result.result.ok) {
36+
res.send("Success");
37+
}
38+
else {
39+
res.send("Error");
40+
}
41+
});
42+
});
43+
44+
app.post('/getNoteById', function (req, res) {
45+
Model.find({
46+
_id: req.body.noteId
47+
},
48+
function(err, notes) {
49+
if(err) {
50+
res.send("Error");
51+
} else {
52+
res.send(notes);
53+
}
54+
55+
}
56+
);
57+
});
58+
59+
app.post('/updateNote', function (req, res) {
60+
var conditions = { noteTitle: req.body.noteTitle, noteBody: req.body.noteBody }
61+
, options = { multi: true };
62+
var query = { _id: req.body.noteId };
63+
Model.update(query, conditions, options, function(err, numAffected) {
64+
if(err) {
65+
res.send("Error");
66+
} else {
67+
res.send(numAffected);
68+
}
69+
70+
})
71+
});
3372
}

public/assets/css/styles.css

+8
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@
22
box-sizing: border-box;
33
}
44

5+
html {
6+
overflow: scroll;
7+
}
8+
59
body {
610
width: 100%;
711
height: 100%;
812
}
913

14+
a, a label {
15+
cursor: pointer;
16+
}
17+
1018
.container {
1119
padding-right: 0;
1220
padding-left: 0;

public/controllers/dashboard-controller.js

+56-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
notes.controller('dashboardCtrl', function dashboardCtrl($scope, $window, $http) {
2-
console.log(sessionStorage.user_email);
2+
3+
$scope.updateNote = false;
4+
$scope.createNote = true;
35
$http.post('/getNote', {"email": sessionStorage.user_email})
46
.success(function(data) {
5-
$scope.records = data;
7+
$scope.notes = data;
68
})
79
.error(function(data) {
810
$window.location.href = '/dashboard';
@@ -29,12 +31,62 @@ notes.controller('dashboardCtrl', function dashboardCtrl($scope, $window, $http)
2931
$scope.createNote = function() {
3032
$http.post('/setNote', {"email": sessionStorage.user_email,"noteTitle": $scope.noteHeading,"noteBody": $scope.note})
3133
.success(function(data) {
32-
var html = '<div class="col-md-3 currentNote">'+$scope.note+'</div>';
34+
var html = '';
35+
html += '<div class="col-md-3 currentNote" id= "'+data._id+'" ng-repeat="note in notes">';
36+
html += '<div class="row">';
37+
html += '<a class="col-md-2" id="editNotedeleteNote" ng-click="getNoteById('+data._id+')">Edit</a>';
38+
html += '<a class="col-md-2" id="deleteNote" ng-click="deleteNote('+data._id+')">Delete</a>';
39+
html += '</div>';
40+
html += '<h1>'+$scope.noteHeading+'</h1>';
41+
html += '<div class="col-md-12">'+$scope.note+'</div>';
3342
angular.element('#notes').append(html);
3443
$scope.name = data.firstname + data.lastname;
44+
$('#myModal').modal('hide');
3545
})
3646
.error(function(data) {
3747
$window.location.href = '/dashboard';
3848
});
3949
}
40-
});
50+
51+
$scope.deleteNote = function(noteId) {
52+
$http.post('/deleteNote', {"id": noteId})
53+
.success(function(data) {
54+
if (data == "Success") {
55+
angular.element('#'+noteId).remove();
56+
}
57+
})
58+
.error(function(data) {
59+
$window.location.href = '/dashboard';
60+
});
61+
}
62+
63+
$scope.getNoteById = function(noteId) {
64+
$scope.updateNote = true;
65+
$scope.createNote = false;
66+
$http.post('/getNoteById', {"noteId": noteId})
67+
.success(function(data) {
68+
$scope.noteId = data[0]._id;
69+
$scope.noteHeading = data[0].noteTitle;
70+
$scope.note = data[0].noteBody;
71+
$('#myModal').modal();
72+
})
73+
.error(function(data) {
74+
alert("Cannot be edited");
75+
});
76+
}
77+
78+
$scope.updateNote = function() {
79+
$http.post('/updateNote', {"noteId": $scope.noteId, "noteTitle": $scope.noteHeading,"noteBody": $scope.note})
80+
.success(function(data) {
81+
$('#myModal').modal('hide');
82+
$scope.updateNote = true;
83+
$scope.createNote = false;
84+
})
85+
.error(function(data) {
86+
alert("Cannot be updated");
87+
$scope.updateNote = true;
88+
$scope.createNote = false;
89+
});
90+
}
91+
92+
});

public/views/dashboard.html

+36-24
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,45 @@ <h2 style="color: #fff">Welcome <span ng-bind="name.firstname"></span>!</h2>
2929
<button type="button" class="btn" id="logout" ng-click="logout()">Logout</button>
3030
</div>
3131
<div class="panel-body" id="notes">
32-
<div class="col-md-3 currentNote" ng-repeat="x in records">
33-
<h1>{{x.noteTitle}}</h1>
34-
<div class="col-md-12">{{x.noteBody}}</div>
32+
<div class="col-md-3 currentNote" id={{note._id}} ng-repeat="note in notes">
33+
<div class="row">
34+
<a class="col-md-2" id="editNotedeleteNote" ng-click="getNoteById(note._id)" data-target="#pModal">Edit</a>
35+
<a class="col-md-2" id="deleteNote" ng-click="deleteNote(note._id)">Delete</a>
36+
</div>
37+
<h1>{{note.noteTitle}}</h1>
38+
<div class="col-md-12">{{note.noteBody}}</div>
3539
</div>
3640
</div>
3741
</div>
3842
</div>
3943
<!-- Modal -->
40-
<div class="modal fade" id="myModal" role="dialog">
41-
<div class="modal-dialog modal-lg">
42-
<div class="modal-content">
43-
<div class="modal-header">
44-
<button type="button" class="close" data-dismiss="modal">&times;</button>
45-
<h4 class="modal-title">Create a new note</h4>
46-
</div>
47-
<div class="modal-body">
48-
<form>
49-
<input type="text" class="form-control" id="noteContent" placeholder="Here goes your note heading.." ng-model="noteHeading"></input><br>
50-
<textarea class="form-control" style="min-width: 100%" id="noteContent" placeholder="Here goes your note.." ng-model="note"></textarea>
51-
</form>
52-
</div>
53-
<div class="modal-footer">
54-
<button type="button" class="btn btn-primary" ng-click="createNote()">Create</button>
55-
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
56-
</div>
57-
</div>
58-
</div>
59-
</div>
44+
<div class="modal fade" id="myModal" role="dialog">
45+
<div class="modal-dialog modal-lg">
46+
<div class="modal-content">
47+
<div class="modal-header">
48+
<button type="button" class="close" data-dismiss="modal">&times;</button>
49+
<h4 class="modal-title">Create a new note</h4>
50+
</div>
51+
<div class="modal-body">
52+
<form>
53+
<input type="hidden" ng-model="noteId" value="">
54+
<input type="text" class="form-control" id="noteContent" placeholder="Here goes your note heading.." ng-model="noteHeading"></input><br>
55+
<textarea class="form-control" style="min-width: 100%" id="noteContent" placeholder="Here goes your note.." ng-model="note"></textarea>
56+
</form>
57+
</div>
58+
<div class="modal-footer">
59+
<button type="button" class="btn btn-primary" ng-show="updateNote" ng-click="updateNote()">Update</button>
60+
<button type="button" class="btn btn-primary" ng-show="createNote" ng-click="createNote()">Create</button>
61+
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
62+
</div>
63+
</div>
64+
</div>
65+
</div>
66+
<h2>Panel Footer</h2>
67+
<div class="panel panel-default">
68+
<div class="panel-heading">Panel Heading</div>
69+
<div class="panel-body">Panel Content</div>
70+
<div class="panel-footer">Panel Footer</div>
71+
</div>
6072
</body>
61-
</html>
73+
</html>

0 commit comments

Comments
 (0)