dangogh commented on a change in pull request #1895: Updated /regions to use 
the CRUD interface 
URL: 
https://github.com/apache/incubator-trafficcontrol/pull/1895#discussion_r170089763
 
 

 ##########
 File path: traffic_ops/traffic_ops_golang/region/regions.go
 ##########
 @@ -0,0 +1,322 @@
+package region
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+       "errors"
+       "fmt"
+
+       "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"
+       "github.com/lib/pq"
+)
+
+//we need a type alias to define functions on
+type TORegion tc.Region
+
+//the refType is passed into the handlers where a copy of its type is used to 
decode the json.
+var refType = TORegion(tc.Region{})
+
+func GetRefType() *TORegion {
+       return &refType
+}
+
+//Implementation of the Identifier, Validator interface functions
+func (region *TORegion) GetID() (int, bool) {
+       return region.ID, true
+}
+
+func (region *TORegion) GetAuditName() string {
+       return region.Name
+}
+
+func (region *TORegion) GetType() string {
+       return "region"
+}
+
+func (region *TORegion) SetID(i int) {
+       region.ID = i
+}
+
+func (region *TORegion) Validate(db *sqlx.DB) []error {
+       errs := []error{}
+       if len(region.Name) < 1 {
+               errs = append(errs, errors.New(`Region 'name' is required.`))
+       }
+       return errs
+}
+
+func (region *TORegion) Read(db *sqlx.DB, parameters map[string]string, user 
auth.CurrentUser) ([]interface{}, []error, tc.ApiErrorType) {
+       var rows *sqlx.Rows
+
+       // Query Parameters to Database Query column mappings
+       // see the fields mapped in the SQL query
+       queryParamsToQueryCols := map[string]dbhelpers.WhereColumnInfo{
+               "name":     dbhelpers.WhereColumnInfo{"name", nil},
+               "division": dbhelpers.WhereColumnInfo{"division", nil},
+               "id":       dbhelpers.WhereColumnInfo{"id", api.IsInt},
+       }
+       where, orderBy, queryValues, errs := 
dbhelpers.BuildWhereAndOrderBy(parameters, queryParamsToQueryCols)
+       if len(errs) > 0 {
+               return nil, errs, tc.DataConflictError
+       }
+
+       query := selectQuery() + where + orderBy
+       log.Debugln("Query is ", query)
+
+       rows, err := db.NamedQuery(query, queryValues)
+       if err != nil {
+               log.Errorf("Error querying Regions: %v", err)
+               return nil, []error{tc.DBError}, tc.SystemError
+       }
+       defer rows.Close()
+
+       Regions := []interface{}{}
 
 Review comment:
   should be lowercase -- `regions`.  It's a local var, but upcase makes it 
look like it's exported

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to