Added some error checking

Added flashes on campaign modal (coming soon to group modal)
pull/24/head
Jordan 2014-02-19 19:40:23 -06:00
parent 1e52267bfa
commit 77ea41c2f9
4 changed files with 28 additions and 11 deletions

View File

@ -73,6 +73,10 @@ 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 {
http.Error(w, "Error: No groups specified", http.StatusBadRequest)
return
}
// Fill in the details // Fill in the details
c.CreatedDate = time.Now() c.CreatedDate = time.Now()
c.CompletedDate = time.Time{} c.CompletedDate = time.Time{}

View File

@ -124,13 +124,7 @@ func GetCampaign(id int64, uid int64) (models.Campaign, error) {
// PostCampaign inserts a campaign and all associated records into the database. // PostCampaign inserts a campaign and all associated records into the database.
func PostCampaign(c *models.Campaign, uid int64) error { func PostCampaign(c *models.Campaign, uid int64) error {
// Insert into the DB // Check to make sure all the groups already exist
err = Conn.Insert(c)
if err != nil {
Logger.Println(err)
return err
}
// Insert all the results
for i, g := range c.Groups { for i, g := range c.Groups {
c.Groups[i], err = GetGroupByName(g.Name, uid) c.Groups[i], err = GetGroupByName(g.Name, uid)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
@ -140,8 +134,17 @@ func PostCampaign(c *models.Campaign, uid int64) error {
Logger.Println(err) Logger.Println(err)
return err return err
} }
}
// Insert into the DB
err = Conn.Insert(c)
if err != nil {
Logger.Println(err)
return err
}
// Insert all the results
for _, g := range c.Groups {
// Insert a result for each target in the group // Insert a result for each target in the group
for _, t := range c.Groups[i].Targets { for _, t := range g.Targets {
r := models.Result{Target: t, Status: "Unknown"} r := models.Result{Target: t, Status: "Unknown"}
c.Results = append(c.Results, r) c.Results = append(c.Results, r)
fmt.Printf("%v", c.Results) fmt.Printf("%v", c.Results)

View File

@ -21,6 +21,7 @@ app.factory('GroupService', function($resource) {
}); });
app.controller('CampaignCtrl', function($scope, CampaignService, ngTableParams) { app.controller('CampaignCtrl', function($scope, CampaignService, ngTableParams) {
$scope.flashes = []
$scope.mainTableParams = new ngTableParams({ $scope.mainTableParams = new ngTableParams({
page: 1, // show first page page: 1, // show first page
count: 10, // count per page count: 10, // count per page
@ -89,14 +90,16 @@ app.controller('CampaignCtrl', function($scope, CampaignService, ngTableParams)
}; };
$scope.saveCampaign = function(campaign) { $scope.saveCampaign = function(campaign) {
$scope.flashes = []
var newCampaign = new CampaignService(campaign); var newCampaign = new CampaignService(campaign);
newCampaign.$save({}, function() { newCampaign.$save({}, function() {
$scope.campaigns.push(newCampaign); $scope.campaigns.push(newCampaign);
$scope.mainTableParams.reload() $scope.mainTableParams.reload()
}, function(response){
$scope.errorFlash(response.data)
}); });
$scope.group = { $scope.campaign = {
name: '', groups: [],
targets: [],
}; };
$scope.editGroupTableParams.reload() $scope.editGroupTableParams.reload()
} }
@ -109,6 +112,10 @@ app.controller('CampaignCtrl', function($scope, CampaignService, ngTableParams)
$scope.mainTableParams.reload(); $scope.mainTableParams.reload();
}); });
} }
$scope.errorFlash = function(message) {
$scope.flashes.push({"type" : "danger", "message" : message, "icon" : "fa-exclamation-circle"})
}
}); });
app.controller('GroupCtrl', function($scope, GroupService, ngTableParams) { app.controller('GroupCtrl', function($scope, GroupService, ngTableParams) {

View File

@ -60,6 +60,9 @@
<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"> <input type="text" class="form-control" ng-model="campaign.name" id="name" placeholder="Campaign name">