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) { if checkError(err, w, "Invalid Request", http.StatusBadRequest) {
return return
} }
if len(c.Groups) == 0 { if m, ok := models.ValidateCampaign(&c); !ok {
http.Error(w, "Error: No groups specified", http.StatusBadRequest) http.Error(w, "Error: "+m, http.StatusBadRequest)
return return
} }
// Fill in the details // Fill in the details

View File

@ -78,7 +78,6 @@ func PostCampaign(c *Campaign, uid int64) error {
for _, t := range g.Targets { for _, t := range g.Targets {
r := Result{Email: t.Email, Status: "Unknown", CampaignId: c.Id} r := Result{Email: t.Email, Status: "Unknown", CampaignId: c.Id}
c.Results = append(c.Results, r) c.Results = append(c.Results, r)
fmt.Printf("%v", c.Results)
err := db.Save(&r).Error err := db.Save(&r).Error
if err != nil { if err != nil {
Logger.Printf("Error adding result record for target %s\n", t.Email) 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 { 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 //DeleteCampaign deletes the specified campaign
@ -109,3 +108,13 @@ func DeleteCampaign(id int64) error {
} }
return err 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.flashes = []
$scope.mainTableParams = new ngTableParams({ $scope.mainTableParams = new ngTableParams({
page: 1, // show first page page: 1, // show first page
@ -53,6 +53,10 @@ app.controller('CampaignCtrl', function($scope, CampaignService, GroupService, n
$scope.groups = groups; $scope.groups = groups;
}) })
TemplateService.query(function(templates) {
$scope.templates = templates;
})
$scope.addGroup = function() { $scope.addGroup = function() {
if ($scope.group.name != "") { if ($scope.group.name != "") {
$scope.campaign.groups.push({ $scope.campaign.groups.push({
@ -91,8 +95,13 @@ app.controller('CampaignCtrl', function($scope, CampaignService, GroupService, n
$scope.saveCampaign = function(campaign) { $scope.saveCampaign = function(campaign) {
$scope.flashes = [] $scope.flashes = []
$scope.validated = true
/*if (campaign.template.name == "") {
$scope.errorFlash("Must specify a template")
}*/
var newCampaign = new CampaignService(campaign); var newCampaign = new CampaignService(campaign);
newCampaign.$save({}, function() { newCampaign.$save({}, function() {
$scope.successFlash("Campaign added successfully")
$scope.campaigns.push(newCampaign); $scope.campaigns.push(newCampaign);
$scope.mainTableParams.reload() $scope.mainTableParams.reload()
}, function(response){ }, function(response){
@ -116,6 +125,10 @@ app.controller('CampaignCtrl', function($scope, CampaignService, GroupService, n
$scope.errorFlash = function(message) { $scope.errorFlash = function(message) {
$scope.flashes.push({"type" : "danger", "message" : message, "icon" : "fa-exclamation-circle"}) $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) { app.controller('CampaignResultsCtrl', function($scope, CampaignService, GroupService, ngTableParams, $http, $window) {

View File

@ -22,6 +22,11 @@
</ul> </ul>
</div> </div>
<div class="col-md-9" ng-controller="CampaignCtrl"> <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"> <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> <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> </div>
@ -62,13 +67,13 @@
<h4 class="modal-title" id="campaignModalLabel">New Campaign</h4> <h4 class="modal-title" id="campaignModalLabel">New Campaign</h4>
</div> </div>
<div class="modal-body"> <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"> <div class="form-group">
<label for="name">Name:</label> <label for="name">Name:</label>
<input type="text" class="form-control" ng-model="campaign.name" id="name" placeholder="Campaign name" autofocus> <input type="text" class="form-control" ng-model="campaign.name" id="name" placeholder="Campaign name" autofocus>
<br /> <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> <label class="control-label" for="users">Groups:</label>
<form ng:submit="addGroup()"> <form ng:submit="addGroup()">
<div class="input-group"> <div class="input-group">