diff --git a/controllers/api.go b/controllers/api.go index 5acfb83b..a4fdcc8f 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -176,7 +176,7 @@ func API_Groups_Id(w http.ResponseWriter, r *http.Request) { g.UserId = ctx.Get(r, "user_id").(int64) err = models.PutGroup(&g) if err != nil { - JSONResponse(w, models.Response{Success: false, Message: "Error updating group"}, http.StatusInternalServerError) + JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest) return } JSONResponse(w, g, http.StatusOK) diff --git a/models/group.go b/models/group.go index 2d7da33a..070325e5 100644 --- a/models/group.go +++ b/models/group.go @@ -117,6 +117,9 @@ func PostGroup(g *Group) error { // PutGroup updates the given group if found in the database. func PutGroup(g *Group) error { + if err := g.Validate(); err != nil { + return err + } ts := []Target{} ts, err = GetTargets(g.Id) if err != nil { diff --git a/static/js/app/users.js b/static/js/app/users.js index d74d81d0..0ad1a1e7 100644 --- a/static/js/app/users.js +++ b/static/js/app/users.js @@ -1,5 +1,7 @@ -// Save attempts to POST to /groups/ -function save(){ +var groups = [] + +// Save attempts to POST or PUT to /groups/ +function save(idx){ var targets = [] $.each($("#targetsTable").DataTable().rows().data(), function(i, target){ targets.push({ @@ -13,31 +15,62 @@ function save(){ name: $("#name").val(), targets: targets } - console.log(group) // Submit the group - api.groups.post(group) - .success(function(data){ - successFlash("Group added successfully!") - load() - dismiss() - }) - .error(function(data){ - modalError(data.responseJSON.message) - }) + if (idx != -1) { + // If we're just editing an existing group, + // we need to PUT /groups/:id + group.id = groups[idx].id + api.groupId.put(group) + .success(function(data){ + successFlash("Group updated successfully!") + load() + dismiss() + }) + .error(function(data){ + modalError(data.responseJSON.message) + }) + } else { + // Else, if this is a new group, POST it + // to /groups + api.groups.post(group) + .success(function(data){ + successFlash("Group added successfully!") + load() + dismiss() + }) + .error(function(data){ + modalError(data.responseJSON.message) + }) + } } function dismiss(){ $("#targetsTable").dataTable().DataTable().clear().draw() + $("#name").val("") $("#modal\\.flashes").empty() $("#modal").modal('hide') } -function edit(group){ - if (group == "new") { +function edit(idx){ + targets = $("#targetsTable").dataTable() + $("#modalSubmit").unbind('click').click(function(){save(idx)}) + if (idx == -1) { group = {} + } else { + group = groups[idx] + $("#name").val(group.name) + $.each(group.targets, function(i, record) { + targets.DataTable() + .row.add([ + record.first_name, + record.last_name, + record.email, + record.position, + '' + ]).draw() + }); } // Handle file uploads - targets = $("#targetsTable").dataTable() $("#csvupload").fileupload({ dataType:"json", add: function(e, data){ @@ -51,7 +84,6 @@ function edit(group){ data.submit(); }, done: function(e, data){ - console.log(data.result) $.each(data.result, function(i, record) { targets.DataTable() .row.add([ @@ -64,6 +96,59 @@ function edit(group){ }); } }) +} + +function deleteGroup(idx){ + if (confirm("Delete " + groups[idx].name + "?")){ + api.groupId.delete(groups[idx].id) + .success(function(data){ + successFlash(data.message) + load() + }) + } +} + +function load(){ + api.groups.get() + .success(function(gs){ + if (gs.length > 0){ + groups = gs + $("#loading").hide() + $("#groupTable").show() + groupTable = $("#groupTable").DataTable(); + groupTable.clear(); + $.each(groups, function(i, group){ + var targets = "" + $.each(group.targets, function(i, target){ + targets += target.email + ", " + if (targets.length > 50) { + targets = targets.slice(0,-3) + "..." + return false; + } + }) + groupTable.row.add([ + group.name, + targets, + moment(group.modified_date).format('MMMM Do YYYY, h:mm:ss a'), + "