This is an automated email from the ASF dual-hosted git repository. dangogh pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-trafficcontrol.git
commit ea6f53a2b833218de1e0a306caa6f7c573cad7f6 Author: Dewayne Richardson <dewr...@apache.org> AuthorDate: Wed Feb 14 10:34:20 2018 -0700 converted the read to use the Read() interface --- .../traffic_ops_golang/{ => division}/divisions.go | 63 ++++++++++++++-------- .../{ => division}/divisions_test.go | 2 +- traffic_ops/traffic_ops_golang/routes.go | 5 +- 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/traffic_ops/traffic_ops_golang/divisions.go b/traffic_ops/traffic_ops_golang/division/divisions.go similarity index 63% rename from traffic_ops/traffic_ops_golang/divisions.go rename to traffic_ops/traffic_ops_golang/division/divisions.go index 10eac0b..68acd03 100644 --- a/traffic_ops/traffic_ops_golang/divisions.go +++ b/traffic_ops/traffic_ops_golang/division/divisions.go @@ -1,4 +1,4 @@ -package main +package division /* * Licensed to the Apache Software Foundation (ASF) under one @@ -20,42 +20,59 @@ package main */ import ( - "encoding/json" "fmt" - "net/http" "github.com/apache/incubator-trafficcontrol/lib/go-log" "github.com/apache/incubator-trafficcontrol/lib/go-tc" "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/api" + "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/auth" "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers" "github.com/jmoiron/sqlx" ) -func divisionsHandler(db *sqlx.DB) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - handleErrs := tc.GetHandleErrorsFunc(w, r) +//we need a type alias to define functions on +type TODivision tc.Division - params, err := api.GetCombinedParams(r) - if err != nil { - log.Errorf("unable to get parameters from request: %s", err) - handleErrs(http.StatusInternalServerError, err) - } +//the refType is passed into the handlers where a copy of its type is used to decode the json. +var refType = TODivision(tc.Division{}) - resp, errs, errType := getDivisionsResponse(params, db) - if len(errs) > 0 { - tc.HandleErrorsWithType(errs, errType, handleErrs) - return - } +func GetRefType() *TODivision { + return &refType +} - respBts, err := json.Marshal(resp) - if err != nil { - handleErrs(http.StatusInternalServerError, err) - return - } +func (cdn *TODivision) Read(db *sqlx.DB, parameters map[string]string, user auth.CurrentUser) ([]interface{}, []error, tc.ApiErrorType) { + // Query Parameters to Database Query column mappings + // see the fields mapped in the SQL query + queryParamsToQueryCols := map[string]dbhelpers.WhereColumnInfo{ + "id": dbhelpers.WhereColumnInfo{"id", api.IsInt}, + "name": dbhelpers.WhereColumnInfo{"name", nil}, + } + where, orderBy, queryValues, errs := dbhelpers.BuildWhereAndOrderBy(parameters, queryParamsToQueryCols) + if len(errs) > 0 { + return nil, errs, tc.DataConflictError + } + + query := selectDivisionsQuery() + where + orderBy + log.Debugln("Query is ", query) - w.Header().Set("Content-Type", "application/json") - fmt.Fprintf(w, "%s", respBts) + rows, err := db.NamedQuery(query, queryValues) + if err != nil { + log.Errorf("Error querying Divisions: %v", err) + return nil, []error{tc.DBError}, tc.SystemError } + defer rows.Close() + + divisions := []interface{}{} + for rows.Next() { + var s tc.Division + if err = rows.StructScan(&s); err != nil { + log.Errorf("error parsing Division rows: %v", err) + return nil, []error{tc.DBError}, tc.SystemError + } + divisions = append(divisions, s) + } + + return divisions, []error{}, tc.NoError } func getDivisionsResponse(params map[string]string, db *sqlx.DB) (*tc.DivisionsResponse, []error, tc.ApiErrorType) { diff --git a/traffic_ops/traffic_ops_golang/divisions_test.go b/traffic_ops/traffic_ops_golang/division/divisions_test.go similarity index 99% rename from traffic_ops/traffic_ops_golang/divisions_test.go rename to traffic_ops/traffic_ops_golang/division/divisions_test.go index 636b3fb5..dd6a9a7 100644 --- a/traffic_ops/traffic_ops_golang/divisions_test.go +++ b/traffic_ops/traffic_ops_golang/division/divisions_test.go @@ -1,4 +1,4 @@ -package main +package division /* * Licensed to the Apache Software Foundation (ASF) under one diff --git a/traffic_ops/traffic_ops_golang/routes.go b/traffic_ops/traffic_ops_golang/routes.go index 5936fc8..4bdd5a7 100644 --- a/traffic_ops/traffic_ops_golang/routes.go +++ b/traffic_ops/traffic_ops_golang/routes.go @@ -33,6 +33,7 @@ import ( "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/auth" "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/cdn" dsrequest "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/deliveryservice/request" + "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/division" "github.com/basho/riak-go-client" ) @@ -85,8 +86,10 @@ func Routes(d ServerData) ([]Route, http.Handler, error) { {1.3, http.MethodPost, `deliveryservices/{xmlID}/urisignkeys$`, saveDeliveryServiceURIKeysHandler(d.DB, d.Config), auth.PrivLevelAdmin, Authenticated, nil}, {1.3, http.MethodPut, `deliveryservices/{xmlID}/urisignkeys$`, saveDeliveryServiceURIKeysHandler(d.DB, d.Config), auth.PrivLevelAdmin, Authenticated, nil}, {1.3, http.MethodDelete, `deliveryservices/{xmlID}/urisignkeys$`, removeDeliveryServiceURIKeysHandler(d.DB, d.Config), auth.PrivLevelAdmin, Authenticated, nil}, + //Divisions - {1.2, http.MethodGet, `divisions/?(\.json)?$`, divisionsHandler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, + {1.2, http.MethodGet, `divisions/?(\.json)?$`, api.ReadHandler(division.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, + //HwInfo {1.2, http.MethodGet, `hwinfo-wip/?(\.json)?$`, hwInfoHandler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, //Parameters -- To stop receiving notification emails like this one, please contact dang...@apache.org.