Skip to content

Commit

Permalink
Add a group and save to DB
Browse files Browse the repository at this point in the history
  • Loading branch information
jwoo12 committed Mar 21, 2016
1 parent e4220e3 commit b92b3d4
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/groups/displayGroup/displayGroup.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ <h1 style="text-align: center;">Groups</h1>
<table class="table" style="margin-left: 20px;">
<thead>
<tr>
<th> Group Name </th>
<th>{{group.name}} </th>
</tr>
</thread>
<tbody>
Expand Down
12 changes: 9 additions & 3 deletions app/groups/groups.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
<script src="bower_components/angular/angular.js"></script>

<style>
td:hover{
.tableHeader{
background-color: transparent !important;
}
tr:hover{
background-color: #99ccff;
}

Expand All @@ -35,14 +38,17 @@ <h1 style="text-align: center;">Groups</h1>
</div>
<table class="table" style="margin-left: 20px;">
<thead>
<tr>
<tr class="tableHeader">
<th> Group Name </th>
<th> Group ID </th>
</tr>
</thread>
<tbody>
<tr ng-repeat="group in groups" ng-click="getSelected()">
<tr ng-repeat="group in groups" ng-click="getSelected(group)">
<td>{{group.name}}
</td>
<td>{{group.id}}
</td>
</tr>
</tbody>

Expand Down
26 changes: 23 additions & 3 deletions app/groups/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,38 @@ angular.module("myApp.groups", ["ngRoute", "LocalStorageModule", "myApp.groups.g
if(!getItem(storageKey)){
//User has not logged in yet so redirect to login
$location.url("/login");
}else{
usernameFromStorage = getItem(storageKey);
}

$scope.groups = [{"name": "test"}];
$scope.groups = [{"name": "test", "id": 123}];

$scope.getSelected = function() {
$scope.getSelected = function(group) {
console.log(group);
$location.url("/displaygroups");
};

$scope.addGroup = function () {
if($scope.regGroupName){
//need to add to db
$scope.groups.push({"name": $scope.regGroupName});

groupsHandler.getID(function(result) {
if(result.data.success){
groupsHandler.addGroup(result.data.lastID + 1, $scope.regGroupName, usernameFromStorage, function(){
if(result.data.success){
console.log("added");
// $scope.groups.push({"name": });
}else{
alert("Error adding group. Please try again.");
}
});
}else{
alert("Error adding group. Please try again.");
}
});


// $scope.groups.push({"name": $scope.regGroupName});
}else{
alert("Enter a group name");
}
Expand Down
27 changes: 27 additions & 0 deletions app/groups/groupsHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,33 @@ var SERVICE_BASE_URL = 'http://localhost:8080/';

angular.module("myApp.groups.groupsHandler", [])
.service("groupsHandler", function($http, $q) {
this.getID = function (callback) {
$http({
url: SERVICE_BASE_URL + "groupid",
method: "POST"
})
.then(function(result) {
return callback(result);
},
function(result) { // optional
return callback(result);
});
};

this.addGroup = function (groupID, groupName, userName, callback) {
console.log("handler " + groupID, groupName, userName);
$http({
url: SERVICE_BASE_URL + "groups",
method: "POST",
data: {groupID: groupID, userName: userName, groupName: groupName, date: null}
})
.then(function(result) {
return callback(result);
},
function(result) { // optional
return callback(result);
});

};

});
83 changes: 80 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ app.post("/login", function (req, res) {
} else {
// console.log(util.inspect(result, {showHidden: false, depth: null}));
if(result.rows.length > 0) {
// // Set cookies
// res.cookie("user_name", req.body.userName);
// res.cookie("login", true);
res.send({success: true});
} else {
res.status(422);
Expand All @@ -108,6 +105,86 @@ app.post("/login", function (req, res) {
);
});

app.post("/groupid", function(req, res){
oracledb.getConnection(dbConfig, function (err, connection) {
if (err) {
connectionError(err, res);
return;
}
connection.execute(
"SELECT GROUP_ID " +
"FROM groups " +
"ORDER BY GROUP_ID desc",
function (err, result) {
if (err) {
executeError(err, res);
} else {
// console.log(util.inspect(result, {showHidden: false, depth: null}));
if(result.rows.length > 0) {
// console.log(result.rows[0][0]);
res.send({lastID: result.rows[0][0], success: true});
} else {
res.status(500);
res.send(
{
error: true,
errorCode: 2,
message: "Could not retrieve data",
success: false
}
);
}
}
doRelease(connection);
}
);
}
);


});

app.route("/groups")
.post(function(req, res){
var DBQueryString =
"BEGIN\n " +
"INSERT INTO GROUPS (GROUP_ID, USER_NAME, GROUP_NAME, DATE_CREATED) " +
"VALUES (:groupID, :userName, :groupName, :date_created); " +

"INSERT INTO GROUP_LISTS (GROUP_ID, FRIEND_ID, DATE_ADDED, NOTICE) " +
"VALUES (:groupID, :userName, :date_created, :notice); " +

"END;",
DBQueryParam = {groupID: req.body.groupID,
userName: req.body.userName,
groupName: req.body.groupName,
date_created: null,
notice: null
};

// console.log(util.inspect(req, {showHidden: false, depth: null}));

oracledb.getConnection(dbConfig, function (err, connection) {
if (err) {
connectionError(err, res);
return;
}
connection.execute(DBQueryString, DBQueryParam,
{autoCommit: true},
function (err, result) {
if (err) {
executeError(err, res);
} else {
// console.log(util.inspect(result, {showHidden: false, depth: null}));
res.send({success: true});
}
doRelease(connection);
}
);
});

});



app.route("/register")
Expand Down

0 comments on commit b92b3d4

Please sign in to comment.