This is an automated email from the ASF dual-hosted git repository.

ocket8888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new cf10d69  Fixed create regions accepts mismatch values for divisionName 
(#6164)
cf10d69 is described below

commit cf10d69c326ba83fe10034f6202091bb110518ad
Author: dmohan001c <[email protected]>
AuthorDate: Wed Sep 15 19:23:06 2021 +0530

    Fixed create regions accepts mismatch values for divisionName (#6164)
    
    * Fixed error in creating regions response
    
    * removed new line and changed formatting directive
---
 traffic_ops/traffic_ops_golang/region/regions.go | 38 ++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 3 deletions(-)

diff --git a/traffic_ops/traffic_ops_golang/region/regions.go 
b/traffic_ops/traffic_ops_golang/region/regions.go
index b4f82c1..27a1807 100644
--- a/traffic_ops/traffic_ops_golang/region/regions.go
+++ b/traffic_ops/traffic_ops_golang/region/regions.go
@@ -21,6 +21,7 @@ package region
 
 import (
        "errors"
+       "fmt"
        "net/http"
        "time"
 
@@ -116,8 +117,38 @@ JOIN division d ON r.division = d.id ` + where + orderBy + 
pagination +
 }
 
 func (rg *TORegion) Update(h http.Header) (error, error, int) { return 
api.GenericUpdate(h, rg) }
-func (rg *TORegion) Create() (error, error, int)              { return 
api.GenericCreate(rg) }
-func (rg *TORegion) Delete() (error, error, int)              { return 
api.GenericDelete(rg) }
+
+func (rg *TORegion) Create() (error, error, int) {
+       resultRows, err := rg.APIInfo().Tx.NamedQuery(rg.InsertQuery(), rg)
+       if err != nil {
+               return api.ParseDBError(err)
+       }
+       defer resultRows.Close()
+
+       var divisionName string
+       var id int
+       var lastUpdated tc.TimeNoMod
+
+       rowsAffected := 0
+       for resultRows.Next() {
+               rowsAffected++
+               if err = resultRows.Scan(&id, &lastUpdated, &divisionName); err 
!= nil {
+                       return nil, fmt.Errorf("could not scan after insert: 
%w)", err), http.StatusInternalServerError
+               }
+       }
+
+       if rowsAffected == 0 {
+               return nil, fmt.Errorf("no region was inserted, nothing was 
returned"), http.StatusInternalServerError
+       } else if rowsAffected > 1 {
+               return nil, fmt.Errorf("too many rows affected from region 
insert"), http.StatusInternalServerError
+       }
+
+       rg.DivisionName = divisionName
+       rg.ID = id
+       rg.LastUpdated = lastUpdated
+       return nil, nil, http.StatusOK
+}
+func (rg *TORegion) Delete() (error, error, int) { return 
api.GenericDelete(rg) }
 
 // OptionsDelete deletes a resource identified either as a route parameter or 
as a query string parameter.
 func (rg *TORegion) OptionsDelete() (error, error, int) { return 
api.GenericOptionsDelete(rg) }
@@ -147,7 +178,8 @@ func insertQuery() string {
 division,
 name) VALUES (
 :division,
-:name) RETURNING id,last_updated`
+:name) RETURNING id,last_updated,
+(SELECT d.name FROM division d WHERE id = region.division)`
        return query
 }
 

Reply via email to