Fixed /users endpoint to now have the following functionality:

- Edit
- Delete
Also added group validation on PUT /api/groups/:id
pull/24/head
unknown 2015-08-05 00:23:05 -05:00
parent 5cd7268023
commit eacb4ddfd4
5 changed files with 117 additions and 80 deletions

View File

@ -176,7 +176,7 @@ func API_Groups_Id(w http.ResponseWriter, r *http.Request) {
g.UserId = ctx.Get(r, "user_id").(int64) g.UserId = ctx.Get(r, "user_id").(int64)
err = models.PutGroup(&g) err = models.PutGroup(&g)
if err != nil { 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 return
} }
JSONResponse(w, g, http.StatusOK) JSONResponse(w, g, http.StatusOK)

View File

@ -117,6 +117,9 @@ func PostGroup(g *Group) error {
// PutGroup updates the given group if found in the database. // PutGroup updates the given group if found in the database.
func PutGroup(g *Group) error { func PutGroup(g *Group) error {
if err := g.Validate(); err != nil {
return err
}
ts := []Target{} ts := []Target{}
ts, err = GetTargets(g.Id) ts, err = GetTargets(g.Id)
if err != nil { if err != nil {

View File

@ -1,5 +1,7 @@
// Save attempts to POST to /groups/ var groups = []
function save(){
// Save attempts to POST or PUT to /groups/
function save(idx){
var targets = [] var targets = []
$.each($("#targetsTable").DataTable().rows().data(), function(i, target){ $.each($("#targetsTable").DataTable().rows().data(), function(i, target){
targets.push({ targets.push({
@ -13,8 +15,23 @@ function save(){
name: $("#name").val(), name: $("#name").val(),
targets: targets targets: targets
} }
console.log(group)
// Submit the group // Submit the group
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) api.groups.post(group)
.success(function(data){ .success(function(data){
successFlash("Group added successfully!") successFlash("Group added successfully!")
@ -24,20 +41,36 @@ function save(){
.error(function(data){ .error(function(data){
modalError(data.responseJSON.message) modalError(data.responseJSON.message)
}) })
}
} }
function dismiss(){ function dismiss(){
$("#targetsTable").dataTable().DataTable().clear().draw() $("#targetsTable").dataTable().DataTable().clear().draw()
$("#name").val("")
$("#modal\\.flashes").empty() $("#modal\\.flashes").empty()
$("#modal").modal('hide') $("#modal").modal('hide')
} }
function edit(group){ function edit(idx){
if (group == "new") { targets = $("#targetsTable").dataTable()
$("#modalSubmit").unbind('click').click(function(){save(idx)})
if (idx == -1) {
group = {} 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,
'<span style="cursor:pointer;"><i class="fa fa-trash-o"></i></span>'
]).draw()
});
} }
// Handle file uploads // Handle file uploads
targets = $("#targetsTable").dataTable()
$("#csvupload").fileupload({ $("#csvupload").fileupload({
dataType:"json", dataType:"json",
add: function(e, data){ add: function(e, data){
@ -51,7 +84,6 @@ function edit(group){
data.submit(); data.submit();
}, },
done: function(e, data){ done: function(e, data){
console.log(data.result)
$.each(data.result, function(i, record) { $.each(data.result, function(i, record) {
targets.DataTable() targets.DataTable()
.row.add([ .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'),
"<div class='pull-right'><button class='btn btn-primary' data-toggle='modal' data-target='#modal' onclick='edit(" + i + ")'>\
<i class='fa fa-pencil'></i>\
</button>\
<button class='btn btn-danger' onclick='deleteGroup(" + i + ")'>\
<i class='fa fa-trash-o'></i>\
</button></div>"
]).draw()
})
}
})
.error(function(){
errorFlash("Error fetching groups")
})
}
$(document).ready(function(){
load()
$("#fileUpload").hover(function(){$("#fileUpload").tooltip('toggle')})
// Setup the event listeners
// Handle manual additions // Handle manual additions
$("#targetForm").submit(function(){ $("#targetForm").submit(function(){
targets.DataTable() targets.DataTable()
@ -86,44 +171,4 @@ function edit(group){
.remove() .remove()
.draw(); .draw();
}) })
}
function load(){
api.groups.get()
.success(function(groups){
if (groups.length > 0){
$("#emptyMessage").hide()
$("#groupTable").show()
groupTable = $("#groupTable").DataTable();
$.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'),
"<div class='pull-right'><button class='btn btn-primary' onclick='alert(\"test\")'>\
<i class='fa fa-pencil'></i>\
</button>\
<button class='btn btn-danger' onclick='alert(\"test\")'>\
<i class='fa fa-trash-o'></i>\
</button></div>"
]).draw()
})
}
})
.error(function(){
errorFlash("Error fetching groups")
})
}
$(document).ready(function(){
load()
$("#fileUpload").hover(function(){$("#fileUpload").tooltip('toggle')})
}) })

View File

@ -1,10 +1,12 @@
function errorFlash(message) { function errorFlash(message) {
$("#flashes").empty()
$("#flashes").append("<div style=\"text-align:center\" class=\"alert alert-danger\">\ $("#flashes").append("<div style=\"text-align:center\" class=\"alert alert-danger\">\
<i class=\"fa fa-exclamation-circle\"></i>" + message + "</div>" <i class=\"fa fa-exclamation-circle\"></i>" + message + "</div>"
) )
} }
function successFlash(message) { function successFlash(message) {
$("#flashes").empty()
$("#flashes").append("<div style=\"text-align:center\" class=\"alert alert-success\">\ $("#flashes").append("<div style=\"text-align:center\" class=\"alert alert-success\">\
<i class=\"fa fa-check-circle\"></i> " + message + "</div>" <i class=\"fa fa-check-circle\"></i> " + message + "</div>"
) )
@ -47,14 +49,6 @@ var api = {
get: function(id){ get: function(id){
return query("/campaigns/" + id, "GET", {}) return query("/campaigns/" + id, "GET", {})
}, },
// post() - Posts a campaign to POST /campaigns/:id
post: function(campaign){
return query("/campaigns/" + campaign.id, "POST", data)
},
// put() - Puts a campaign to PUT /campaigns/:id
put: function (campaign){
return query("/campaigns/" + campaign.id, "PUT", data)
},
// delete() - Deletes a campaign at DELETE /campaigns/:id // delete() - Deletes a campaign at DELETE /campaigns/:id
delete: function(id){ delete: function(id){
return query("/campaigns/" + id, "DELETE", data) return query("/campaigns/" + id, "DELETE", data)
@ -77,17 +71,13 @@ var api = {
get: function(id){ get: function(id){
return query("/groups/" + id, "GET", {}) return query("/groups/" + id, "GET", {})
}, },
// post() - Posts a campaign to POST /groups/:id
post: function(group){
return query("/groups/" + group.id, "POST", data)
},
// put() - Puts a campaign to PUT /groups/:id // put() - Puts a campaign to PUT /groups/:id
put: function (group){ put: function (group){
return query("/groups/" + group.id, "PUT", data) return query("/groups/" + group.id, "PUT", group)
}, },
// delete() - Deletes a campaign at DELETE /groups/:id // delete() - Deletes a campaign at DELETE /groups/:id
delete: function(id){ delete: function(id){
return query("/groups/" + id, "DELETE", data) return query("/groups/" + id, "DELETE", {})
} }
}, },
// templates contains the endpoints for /templates // templates contains the endpoints for /templates
@ -107,17 +97,13 @@ var api = {
get: function(id){ get: function(id){
return query("/templates/" + id, "GET", {}) return query("/templates/" + id, "GET", {})
}, },
// post() - Posts a campaign to POST /templates/:id
post: function(template){
return query("/templates/" + template.id, "POST", data)
},
// put() - Puts a campaign to PUT /templates/:id // put() - Puts a campaign to PUT /templates/:id
put: function (template){ put: function (template){
return query("/templates/" + template.id, "PUT", data) return query("/templates/" + template.id, "PUT", template)
}, },
// delete() - Deletes a campaign at DELETE /templates/:id // delete() - Deletes a campaign at DELETE /templates/:id
delete: function(id){ delete: function(id){
return query("/templates/" + id, "DELETE", data) return query("/templates/" + id, "DELETE", {})
} }
} }

View File

@ -29,10 +29,13 @@
</div> </div>
<div id="flashes" class="row"></div> <div id="flashes" class="row"></div>
<div class="row"> <div class="row">
<button type="button" class="btn btn-primary" onclick="edit('new')" data-toggle="modal" data-target="#modal"><i class="fa fa-plus"></i> New Group</button> <button type="button" class="btn btn-primary" onclick="edit(-1)" data-toggle="modal" data-target="#modal"><i class="fa fa-plus"></i> New Group</button>
</div> </div>
&nbsp; &nbsp;
<div id="emptyMessage" class="row"> <div id="loading">
<i class="fa fa-spinner fa-spin fa-4x"></i>
</div>
<div id="emptyMessage" class="row" style="display:none;">
<div class="alert alert-info"> <div class="alert alert-info">
No groups created yet. Let's create one! No groups created yet. Let's create one!
</div> </div>
@ -133,7 +136,7 @@
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button type="button" class="btn btn-default" onclick="dismiss()">Close</button> <button type="button" class="btn btn-default" onclick="dismiss()">Close</button>
<button type="button" class="btn btn-primary" onclick="save()">Save changes</button> <button type="button" class="btn btn-primary" id="modalSubmit">Save changes</button>
</div> </div>
</div> </div>
</div> </div>