Only use 200 follows what others do. Less switching. Remove defense

This commit is contained in:
James Jeffrey
2017-07-05 12:30:18 -07:00
parent c66a0d0cc4
commit 4845ddb1d4
3 changed files with 26 additions and 47 deletions

View File

@@ -2,7 +2,6 @@ package server
import (
"context"
"fmt"
"net/http"
"path"
"strings"
@@ -25,12 +24,7 @@ func (s *Server) handleRouteCreateOrUpdate(c *gin.Context) {
ctx := c.MustGet("ctx").(context.Context)
log := common.Logger(ctx)
method := strings.ToUpper(c.Request.Method)
switch method {
case http.MethodPost, http.MethodPut, http.MethodPatch:
default:
c.JSON(http.StatusMethodNotAllowed, simpleError(fmt.Errorf(http.StatusText(http.StatusMethodNotAllowed))))
return
}
var wroute models.RouteWrapper
err := c.BindJSON(&wroute)
@@ -110,22 +104,21 @@ func (s *Server) handleRouteCreateOrUpdate(c *gin.Context) {
var route *models.Route
var createdOrUpdated int
resp := routeResponse{"Route successfully created", route}
up := routeResponse{"Route successfully updated", route}
switch method {
case http.MethodPost:
route, err = s.Datastore.InsertRoute(ctx, wroute.Route)
createdOrUpdated = 1
case http.MethodPut:
route, err = s.Datastore.UpdateRoute(ctx, wroute.Route)
createdOrUpdated = 2
if err == models.ErrRoutesNotFound {
// try insert then
route, err = s.Datastore.InsertRoute(ctx, wroute.Route)
createdOrUpdated = 1
}
case http.MethodPatch:
route, err = s.Datastore.UpdateRoute(ctx, wroute.Route)
createdOrUpdated = 2
resp = up
}
if err != nil {
@@ -135,15 +128,5 @@ func (s *Server) handleRouteCreateOrUpdate(c *gin.Context) {
s.cacheRefresh(route)
var msg string
var code int
switch createdOrUpdated {
case 1:
msg = "Route successfully created"
code = http.StatusCreated
case 2:
msg = "Route successfully updated"
code = http.StatusOK
}
c.JSON(code, routeResponse{msg, route})
c.JSON(http.StatusOK, resp)
}

View File

@@ -7,9 +7,9 @@ import (
"testing"
"gitlab-odx.oracle.com/odx/functions/api/datastore"
"gitlab-odx.oracle.com/odx/functions/api/logs"
"gitlab-odx.oracle.com/odx/functions/api/models"
"gitlab-odx.oracle.com/odx/functions/api/mqs"
"gitlab-odx.oracle.com/odx/functions/api/logs"
)
func TestRouteCreate(t *testing.T) {

View File

@@ -177,7 +177,7 @@ paths:
schema:
$ref: '#/definitions/RouteWrapper'
responses:
201:
200:
description: Route created
schema:
$ref: '#/definitions/RouteWrapper'
@@ -244,11 +244,7 @@ paths:
$ref: '#/definitions/RouteWrapper'
responses:
200:
description: Route updated
schema:
$ref: '#/definitions/RouteWrapper'
201:
description: Route created
description: Route created or updated
schema:
$ref: '#/definitions/RouteWrapper'
400: