-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
119 lines (102 loc) · 2.78 KB
/
javascript.js
File metadata and controls
119 lines (102 loc) · 2.78 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
113
114
115
116
117
118
$(document).ready(function(){
updateUserList();
loadEvents();
});
var url = "http://477-33.csse.rose-hulman.edu:8080/UserID/";
var addUrl = "http://477-32.csse.rose-hulman.edu:8080/Calculator/";
var loadEvents = function() {
$("#submitButton").click(function(){
var name = $("#nameInput").val();
var email = $("#emailInput").val();
var data = {
"name": name,
"email": email
};
var post = $.post(url, JSON.stringify(data))
.done(function(response) {
updateUserList();
alert("User created!");
})
.fail(function(err){
alert("Creating a new user failed: " + err.status + ": " + err.statusText);
});
});
$("#userSelect").change(function(){
var id = $("userSelect").val();
var get = $.get(url+id, function(response) {
response = JSON.parse(response);
$('editNameInput').val(response.name);
$('editEmailInput').val(response.email);
});
});
$("#editUserButton").click(function() {
var editName = $("#editNameInput").val();
var editEmail = $("#editEmailInput").val();
var id = $("#userSelect").val();
var data = {
"name": editName,
"email": editEmail
};
$.ajax({
type: "PUT",
url:url+id,
data: JSON.stringify(data)
})
.done(function(response) {
updateUserList();
alert("User updated!");
})
.fail(function(response) {
alert("Updating failed!");
});
});
$("#deleteUserButton").click(function(){
var id = $("#userSelect").val();
$.ajax({
type: "DELETE",
url: url+id
})
.done(function(response){
updateUserList();
alert("Delete successful!");
})
.fail(function(response){
alert("Delete failed!");
});
});
$("#addButton").click(function(){
var firstNum = $("#firstNumberInput").val();
var secondNum = $("#secondNumberInput").val();
var data = {
"first": firstNum,
"second": secondNum
};
var post = $.post(addUrl, JSON.stringify(data))
.done(function(response) {
response = JSON.parse(response);
$("#result").text(response.answer);
alert("Addition Succeeded!");
})
.fail(function(response){
alert("Addition failed!");
});
});
};
var updateUserList = function() {
var get = $.get(url, function(response) {
response = JSON.parse(response);
$("#usersList").empty();
$("#userSelect").empty();
if(response.length === 0){
alert("no users!");
return;
}
$.each(response, function(i, user) {
$("#usersList").append($("<li>"+user.id + ": " + user.name + " " + user.email + "</li>"));
$("#userSelect").append($("<option>").text(user.id).attr("value", user.id));
});
})
.fail(function(){
alert("Updating user list failed!");
});
};