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)
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)

View File

@ -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 {

View File

@ -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,
'<span style="cursor:pointer;"><i class="fa fa-trash-o"></i></span>'
]).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'),
"<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
$("#targetForm").submit(function(){
targets.DataTable()
@ -86,44 +171,4 @@ function edit(group){
.remove()
.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) {
$("#flashes").empty()
$("#flashes").append("<div style=\"text-align:center\" class=\"alert alert-danger\">\
<i class=\"fa fa-exclamation-circle\"></i>" + message + "</div>"
)
}
function successFlash(message) {
$("#flashes").empty()
$("#flashes").append("<div style=\"text-align:center\" class=\"alert alert-success\">\
<i class=\"fa fa-check-circle\"></i> " + message + "</div>"
)
@ -47,14 +49,6 @@ var api = {
get: function(id){
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: function(id){
return query("/campaigns/" + id, "DELETE", data)
@ -77,17 +71,13 @@ var api = {
get: function(id){
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: 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: function(id){
return query("/groups/" + id, "DELETE", data)
return query("/groups/" + id, "DELETE", {})
}
},
// templates contains the endpoints for /templates
@ -107,17 +97,13 @@ var api = {
get: function(id){
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: 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: function(id){
return query("/templates/" + id, "DELETE", data)
return query("/templates/" + id, "DELETE", {})
}
}

View File

@ -29,10 +29,13 @@
</div>
<div id="flashes" class="row"></div>
<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>
&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">
No groups created yet. Let's create one!
</div>
@ -133,7 +136,7 @@
</div>
<div class="modal-footer">
<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>