Implemented DELETE /api/groups/:id

pull/24/head
Jordan 2014-02-13 12:05:22 -06:00
parent 631cd1ad13
commit 2420e19e15
4 changed files with 31 additions and 8 deletions

View File

@ -195,8 +195,12 @@ func API_Groups_Id(w http.ResponseWriter, r *http.Request) {
} }
writeJSON(w, gj) writeJSON(w, gj)
case r.Method == "DELETE": case r.Method == "DELETE":
err := db.DeleteGroup(id, ctx.Get(r, "user_id").(int64)) _, err := db.GetGroup(id, ctx.Get(r, "user_id").(int64))
if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) { if checkError(err, w, "No group found", http.StatusNotFound) {
return
}
err = db.DeleteGroup(id)
if checkError(err, w, "Error deleting group", http.StatusInternalServerError) {
return return
} }
writeJSON(w, []byte("{\"success\" : \"true\"}")) writeJSON(w, []byte("{\"success\" : \"true\"}"))

View File

@ -37,7 +37,8 @@ func Setup() error {
`CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL, hash VARCHAR(60) NOT NULL, api_key VARCHAR(32), UNIQUE(username), UNIQUE(api_key));`, `CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL, hash VARCHAR(60) NOT NULL, api_key VARCHAR(32), UNIQUE(username), UNIQUE(api_key));`,
`CREATE TABLE campaigns (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, created_date TIMESTAMP NOT NULL, completed_date TIMESTAMP, template TEXT, status TEXT NOT NULL, uid INTEGER, FOREIGN KEY (uid) REFERENCES users(id));`, `CREATE TABLE campaigns (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, created_date TIMESTAMP NOT NULL, completed_date TIMESTAMP, template TEXT, status TEXT NOT NULL, uid INTEGER, FOREIGN KEY (uid) REFERENCES users(id));`,
`CREATE TABLE targets (id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT NOT NULL, UNIQUE(email));`, `CREATE TABLE targets (id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT NOT NULL, UNIQUE(email));`,
`CREATE TABLE groups (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, modified_date TIMESTAMP NOT NULL, UNIQUE(name));`, `CREATE TABLE groups (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, modified_date TIMESTAMP NOT NULL);`,
`CREATE TABLE campaign_results (cid INTEGER NOT NULL, tid INTEGER NOT NULL, result TEXT NOT NULL, FOREIGN KEY (cid) REFERENCES users(id), FOREIGN KEY (tid) REFERENCES targets(id), UNIQUE(cid, tid))`,
`CREATE TABLE user_groups (uid INTEGER NOT NULL, gid INTEGER NOT NULL, FOREIGN KEY (uid) REFERENCES users(id), FOREIGN KEY (gid) REFERENCES groups(id), UNIQUE(uid, gid))`, `CREATE TABLE user_groups (uid INTEGER NOT NULL, gid INTEGER NOT NULL, FOREIGN KEY (uid) REFERENCES users(id), FOREIGN KEY (gid) REFERENCES groups(id), UNIQUE(uid, gid))`,
`CREATE TABLE group_targets (gid INTEGER NOT NULL, tid INTEGER NOT NULL, FOREIGN KEY (gid) REFERENCES groups(id), FOREIGN KEY (tid) REFERENCES targets(id), UNIQUE(gid, tid));`, `CREATE TABLE group_targets (gid INTEGER NOT NULL, tid INTEGER NOT NULL, FOREIGN KEY (gid) REFERENCES groups(id), FOREIGN KEY (tid) REFERENCES targets(id), UNIQUE(gid, tid));`,
} }
@ -265,6 +266,19 @@ func insertTargetIntoGroup(t models.Target, gid int64) error {
return nil return nil
} }
func DeleteGroup(id int64, uid int64) error { // DeleteGroup deletes a given group by group ID and user ID
return nil func DeleteGroup(id int64) error {
// Delete all the group_targets entries for this group
_, err := Conn.Exec("DELETE FROM group_targets WHERE gid=?", id)
if err != nil {
return err
}
// Delete the reference to the group in the user_group table
_, err = Conn.Exec("DELETE FROM user_groups WHERE gid=?", id)
if err != nil {
return err
}
// Delete the group itself
_, err = Conn.Exec("DELETE FROM groups WHERE id=?", id)
return err
} }

View File

@ -96,14 +96,13 @@ app.controller('GroupCtrl', function($scope, GroupService, ngTableParams) {
$scope.editGroupTableParams.reload() $scope.editGroupTableParams.reload()
}; };
$scope.saveGroup = function(group) { $scope.saveGroup = function(group) {
var newGroup = new GroupService($scope.group); var newGroup = new GroupService(group);
if ($scope.newGroup) { if ($scope.newGroup) {
newGroup.$save({},function() { newGroup.$save({},function() {
$scope.groups.push(newGroup); $scope.groups.push(newGroup);
$scope.mainTableParams.reload() $scope.mainTableParams.reload()
}); });
} else { } else {
console.log(newGroup.id)
newGroup.$update({id : newGroup.id}) newGroup.$update({id : newGroup.id})
} }
$scope.group = { $scope.group = {
@ -112,4 +111,10 @@ app.controller('GroupCtrl', function($scope, GroupService, ngTableParams) {
}; };
$scope.editGroupTableParams.reload() $scope.editGroupTableParams.reload()
} }
$scope.deleteGroup = function(group) {
var deleteGroup = new GroupService(group);
deleteGroup.$delete({id : deleteGroup.id}, function() {
$scope.mainTableParams.reload();
});
}
}) })

View File

@ -44,7 +44,7 @@
<li><a ng-click="editGroup(group)" data-toggle="modal" ng-href="#" data-target="#newGroupModal">Edit</a> <li><a ng-click="editGroup(group)" data-toggle="modal" ng-href="#" data-target="#newGroupModal">Edit</a>
</li> </li>
<li class="divider"></li> <li class="divider"></li>
<li><a ng-href="/groups/{{group.id}}/delete">Delete</a> <li><a ng-click="deleteGroup(group)" ng-href="#">Delete</a>
</li> </li>
</ul> </ul>
</div> </div>