Fixed the way Angular POSTS data

Added error code to checkError to support responses such as BadRequest, NotFound, etc.
pull/24/head
Jordan 2014-02-12 10:43:54 -06:00
parent cdc776ec03
commit 631cd1ad13
4 changed files with 22 additions and 23 deletions

View File

@ -61,7 +61,7 @@ func API_Campaigns(w http.ResponseWriter, r *http.Request) {
fmt.Println(err) fmt.Println(err)
} }
cj, err := json.MarshalIndent(cs, "", " ") cj, err := json.MarshalIndent(cs, "", " ")
if checkError(err, w, "Error looking up campaigns") { if checkError(err, w, "Error looking up campaigns", http.StatusInternalServerError) {
return return
} }
writeJSON(w, cj) writeJSON(w, cj)
@ -70,7 +70,7 @@ func API_Campaigns(w http.ResponseWriter, r *http.Request) {
c := models.Campaign{} c := models.Campaign{}
// Put the request into a campaign // Put the request into a campaign
err := json.NewDecoder(r.Body).Decode(&c) err := json.NewDecoder(r.Body).Decode(&c)
if checkError(err, w, "Invalid Request") { if checkError(err, w, "Invalid Request", http.StatusBadRequest) {
return return
} }
// Fill in the details // Fill in the details
@ -80,11 +80,11 @@ func API_Campaigns(w http.ResponseWriter, r *http.Request) {
c.Uid = ctx.Get(r, "user_id").(int64) c.Uid = ctx.Get(r, "user_id").(int64)
// Insert into the DB // Insert into the DB
err = db.Conn.Insert(&c) err = db.Conn.Insert(&c)
if checkError(err, w, "Cannot insert campaign into database") { if checkError(err, w, "Cannot insert campaign into database", http.StatusInternalServerError) {
return return
} }
cj, err := json.MarshalIndent(c, "", " ") cj, err := json.MarshalIndent(c, "", " ")
if checkError(err, w, "Error creating JSON response") { if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) {
return return
} }
writeJSON(w, cj) writeJSON(w, cj)
@ -100,11 +100,11 @@ func API_Campaigns_Id(w http.ResponseWriter, r *http.Request) {
case r.Method == "GET": case r.Method == "GET":
c := models.Campaign{} c := models.Campaign{}
c, err := db.GetCampaign(id, ctx.Get(r, "user_id").(int64)) c, err := db.GetCampaign(id, ctx.Get(r, "user_id").(int64))
if checkError(err, w, "No campaign found") { if checkError(err, w, "No campaign found", http.StatusNotFound) {
return return
} }
cj, err := json.MarshalIndent(c, "", " ") cj, err := json.MarshalIndent(c, "", " ")
if checkError(err, w, "Error creating JSON response") { if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) {
return return
} }
writeJSON(w, cj) writeJSON(w, cj)
@ -144,11 +144,11 @@ func API_Groups(w http.ResponseWriter, r *http.Request) {
switch { switch {
case r.Method == "GET": case r.Method == "GET":
gs, err := db.GetGroups(ctx.Get(r, "user_id").(int64)) gs, err := db.GetGroups(ctx.Get(r, "user_id").(int64))
if checkError(err, w, "Cannot retrieve group information") { if checkError(err, w, "Groups not found", http.StatusNotFound) {
return return
} }
gj, err := json.MarshalIndent(gs, "", " ") gj, err := json.MarshalIndent(gs, "", " ")
if checkError(err, w, "Error marshaling group information") { if checkError(err, w, "Error marshaling group information", http.StatusInternalServerError) {
return return
} }
writeJSON(w, gj) writeJSON(w, gj)
@ -157,21 +157,21 @@ func API_Groups(w http.ResponseWriter, r *http.Request) {
g := models.Group{} g := models.Group{}
// Put the request into a group // Put the request into a group
err := json.NewDecoder(r.Body).Decode(&g) err := json.NewDecoder(r.Body).Decode(&g)
if checkError(err, w, "Invalid Request") { if checkError(err, w, "Invalid Request", http.StatusBadRequest) {
return return
} }
// Check to make sure targets were specified // Check to make sure targets were specified
if len(g.Targets) == 0 { if len(g.Targets) == 0 {
http.Error(w, "Error: No targets specified", http.StatusInternalServerError) http.Error(w, "Error: No targets specified", http.StatusBadRequest)
return return
} }
g.ModifiedDate = time.Now() g.ModifiedDate = time.Now()
err = db.PostGroup(&g, ctx.Get(r, "user_id").(int64)) err = db.PostGroup(&g, ctx.Get(r, "user_id").(int64))
if checkError(err, w, "Error inserting group") { if checkError(err, w, "Error inserting group", http.StatusInternalServerError) {
return return
} }
gj, err := json.MarshalIndent(g, "", " ") gj, err := json.MarshalIndent(g, "", " ")
if checkError(err, w, "Error creating JSON response") { if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) {
return return
} }
writeJSON(w, gj) writeJSON(w, gj)
@ -186,23 +186,23 @@ func API_Groups_Id(w http.ResponseWriter, r *http.Request) {
switch { switch {
case r.Method == "GET": case r.Method == "GET":
g, err := db.GetGroup(id, ctx.Get(r, "user_id").(int64)) g, err := db.GetGroup(id, ctx.Get(r, "user_id").(int64))
if checkError(err, w, "No group found") { if checkError(err, w, "No group found", http.StatusNotFound) {
return return
} }
gj, err := json.MarshalIndent(g, "", " ") gj, err := json.MarshalIndent(g, "", " ")
if checkError(err, w, "Error creating JSON response") { if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) {
return return
} }
writeJSON(w, gj) writeJSON(w, gj)
case r.Method == "DELETE": case r.Method == "DELETE":
err := db.DeleteGroup(id, ctx.Get(r, "user_id").(int64)) err := db.DeleteGroup(id, ctx.Get(r, "user_id").(int64))
if checkError(err, w, "Error creating JSON response") { if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) {
return return
} }
writeJSON(w, []byte("{\"success\" : \"true\"}")) writeJSON(w, []byte("{\"success\" : \"true\"}"))
case r.Method == "PUT": case r.Method == "PUT":
_, err := db.GetGroup(id, ctx.Get(r, "user_id").(int64)) _, err := db.GetGroup(id, ctx.Get(r, "user_id").(int64))
if checkError(err, w, "No group found") { if checkError(err, w, "No group found", http.StatusNotFound) {
return return
} }
g := models.Group{} g := models.Group{}
@ -212,11 +212,11 @@ func API_Groups_Id(w http.ResponseWriter, r *http.Request) {
return return
} }
err = db.PutGroup(&g, ctx.Get(r, "user_id").(int64)) err = db.PutGroup(&g, ctx.Get(r, "user_id").(int64))
if checkError(err, w, "Error updating group") { if checkError(err, w, "Error updating group", http.StatusInternalServerError) {
return return
} }
gj, err := json.MarshalIndent(g, "", " ") gj, err := json.MarshalIndent(g, "", " ")
if checkError(err, w, "Error creating JSON response") { if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) {
return return
} }
writeJSON(w, gj) writeJSON(w, gj)

View File

@ -210,10 +210,10 @@ func getTemplate(w http.ResponseWriter, tmpl string) *template.Template {
return template.Must(templates, err) return template.Must(templates, err)
} }
func checkError(e error, w http.ResponseWriter, m string) bool { func checkError(e error, w http.ResponseWriter, m string, c int) bool {
if e != nil { if e != nil {
fmt.Println(e) fmt.Println(e)
http.Error(w, "Error: "+m, http.StatusInternalServerError) http.Error(w, "Error: "+m, c)
return true return true
} }
return false return false

View File

@ -73,7 +73,6 @@ app.controller('GroupCtrl', function($scope, GroupService, ngTableParams) {
$scope.group = { $scope.group = {
name: '', name: '',
targets: [], targets: [],
id: 0
}; };
} else { } else {
@ -99,7 +98,7 @@ app.controller('GroupCtrl', function($scope, GroupService, ngTableParams) {
$scope.saveGroup = function(group) { $scope.saveGroup = function(group) {
var newGroup = new GroupService($scope.group); var newGroup = new GroupService($scope.group);
if ($scope.newGroup) { if ($scope.newGroup) {
newGroup.$save(function() { newGroup.$save({},function() {
$scope.groups.push(newGroup); $scope.groups.push(newGroup);
$scope.mainTableParams.reload() $scope.mainTableParams.reload()
}); });
@ -110,7 +109,6 @@ app.controller('GroupCtrl', function($scope, GroupService, ngTableParams) {
$scope.group = { $scope.group = {
name: '', name: '',
targets: [], targets: [],
id: 0
}; };
$scope.editGroupTableParams.reload() $scope.editGroupTableParams.reload()
} }

View File

@ -55,6 +55,7 @@
</div> </div>
</div> </div>
<input type="hidden" name="csrf_token" value={{%.Token%}}/> <input type="hidden" name="csrf_token" value={{%.Token%}}/>
<br />
<button class="btn btn-primary" type="submit">Save</button> <button class="btn btn-primary" type="submit">Save</button>
</form> </form>
<br/> <br/>