Added validation for campaigns... I'm considering making everything branch off of a Model Interface with Get(), Put(), Post(), and Delete(), etc. Might make things cleaner.

Added better error messages on the UI side
pull/24/head
Jordan 2014-03-28 00:21:42 -05:00
parent 7c50f4dbb4
commit 8c1bb58fe2
4 changed files with 35 additions and 8 deletions

View File

@ -73,8 +73,8 @@ func API_Campaigns(w http.ResponseWriter, r *http.Request) {
if checkError(err, w, "Invalid Request", http.StatusBadRequest) {
return
}
if len(c.Groups) == 0 {
http.Error(w, "Error: No groups specified", http.StatusBadRequest)
if m, ok := models.ValidateCampaign(&c); !ok {
http.Error(w, "Error: "+m, http.StatusBadRequest)
return
}
// Fill in the details

View File

@ -78,7 +78,6 @@ func PostCampaign(c *Campaign, uid int64) error {
for _, t := range g.Targets {
r := Result{Email: t.Email, Status: "Unknown", CampaignId: c.Id}
c.Results = append(c.Results, r)
fmt.Printf("%v", c.Results)
err := db.Save(&r).Error
if err != nil {
Logger.Printf("Error adding result record for target %s\n", t.Email)
@ -90,7 +89,7 @@ func PostCampaign(c *Campaign, uid int64) error {
}
func UpdateCampaignStatus(c *Campaign, s string) error {
return db.Debug().Model(c).UpdateColumn("status", s).Error
return db.Model(c).UpdateColumn("status", s).Error
}
//DeleteCampaign deletes the specified campaign
@ -109,3 +108,13 @@ func DeleteCampaign(id int64) error {
}
return err
}
func ValidateCampaign(c *Campaign) (string, bool) {
if c.Name == "" {
return "Must specify campaign name", false
}
if len(c.Groups) == 0 {
return "No groups specified", false
}
return "", true
}

View File

@ -30,7 +30,7 @@ app.factory('TemplateService', function($resource) {
});
});
app.controller('CampaignCtrl', function($scope, CampaignService, GroupService, ngTableParams, $http) {
app.controller('CampaignCtrl', function($scope, CampaignService, GroupService, TemplateService, ngTableParams, $http) {
$scope.flashes = []
$scope.mainTableParams = new ngTableParams({
page: 1, // show first page
@ -53,6 +53,10 @@ app.controller('CampaignCtrl', function($scope, CampaignService, GroupService, n
$scope.groups = groups;
})
TemplateService.query(function(templates) {
$scope.templates = templates;
})
$scope.addGroup = function() {
if ($scope.group.name != "") {
$scope.campaign.groups.push({
@ -91,8 +95,13 @@ app.controller('CampaignCtrl', function($scope, CampaignService, GroupService, n
$scope.saveCampaign = function(campaign) {
$scope.flashes = []
$scope.validated = true
/*if (campaign.template.name == "") {
$scope.errorFlash("Must specify a template")
}*/
var newCampaign = new CampaignService(campaign);
newCampaign.$save({}, function() {
$scope.successFlash("Campaign added successfully")
$scope.campaigns.push(newCampaign);
$scope.mainTableParams.reload()
}, function(response){
@ -116,6 +125,10 @@ app.controller('CampaignCtrl', function($scope, CampaignService, GroupService, n
$scope.errorFlash = function(message) {
$scope.flashes.push({"type" : "danger", "message" : message, "icon" : "fa-exclamation-circle"})
}
$scope.successFlash = function(message) {
$scope.flashes.push({"type" : "success", "message" : message, "icon" : "fa-check-circle"})
}
});
app.controller('CampaignResultsCtrl', function($scope, CampaignService, GroupService, ngTableParams, $http, $window) {

View File

@ -22,6 +22,11 @@
</ul>
</div>
<div class="col-md-9" ng-controller="CampaignCtrl">
<div class="row">
<div ng-repeat="flash in flashes" style="text-align:center" class="alert alert-{{flash.type}}">
<i class="fa {{flash.icon}}"></i> {{flash.message}}
</div>
</div>
<div class="row">
<button type="button" class="btn btn-primary" ng-click="newCampaign()" data-toggle="modal" data-target="#newCampaignModal"><i class="fa fa-plus"></i> New Campaign</button>
</div>
@ -62,13 +67,13 @@
<h4 class="modal-title" id="campaignModalLabel">New Campaign</h4>
</div>
<div class="modal-body">
<div ng-repeat="flash in flashes" style="text-align:center" class="alert alert-{{flash.type}}">
<i class="fa {{flash.icon}}"></i> {{flash.message}}
</div>
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" ng-model="campaign.name" id="name" placeholder="Campaign name" autofocus>
<br />
<label class="control-label" for="users">Template:</label>
<input type="text" class="form-control" placeholder="Template Name" id="template" typeahead="template.name for template in templates | filter:{name:$viewValue}" typeahead-editable="false" ng-model="template.name" />
<br />
<label class="control-label" for="users">Groups:</label>
<form ng:submit="addGroup()">
<div class="input-group">