jagan-parthiban commented on code in PR #7631: URL: https://github.com/apache/trafficcontrol/pull/7631#discussion_r1263818561
########## traffic_ops/traffic_ops_golang/physlocation/phys_locations.go: ########## @@ -20,8 +20,12 @@ package physlocation */ import ( + "database/sql" + "encoding/json" "errors" "fmt" + "github.com/apache/trafficcontrol/lib/go-log" + "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/util/ims" Review Comment: Fixed it ########## traffic_ops/traffic_ops_golang/physlocation/phys_locations.go: ########## @@ -215,3 +219,307 @@ func deleteQuery() string { WHERE id=:id` return query } + +func GetPhysLocation(w http.ResponseWriter, r *http.Request) { + var runSecond bool + var maxTime time.Time + inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil) + tx := inf.Tx + if userErr != nil || sysErr != nil { + api.HandleErr(w, r, tx.Tx, errCode, userErr, sysErr) + return + } + defer inf.Close() + + // Query Parameters to Database Query column mappings + queryParamsToQueryCols := map[string]dbhelpers.WhereColumnInfo{ + "name": {Column: "pl.name", Checker: nil}, + "id": {Column: "pl.id", Checker: api.IsInt}, + "region": {Column: "pl.region", Checker: api.IsInt}, + } + if _, ok := inf.Params["orderby"]; !ok { + inf.Params["orderby"] = "name" + } + where, orderBy, pagination, queryValues, errs := dbhelpers.BuildWhereAndOrderByAndPagination(inf.Params, queryParamsToQueryCols) + if len(errs) > 0 { + api.HandleErr(w, r, tx.Tx, http.StatusBadRequest, util.JoinErrs(errs), nil) + return + } + + if inf.Config.UseIMS { + runSecond, maxTime = ims.TryIfModifiedSinceQuery(tx, r.Header, queryValues, selectMaxLastUpdatedQuery(where)) + if !runSecond { + log.Debugln("IMS HIT") + api.AddLastModifiedHdr(w, maxTime) + w.WriteHeader(http.StatusNotModified) + return + } + log.Debugln("IMS MISS") + } else { + log.Debugln("Non IMS request") + } + + query := selectQuery() + where + orderBy + pagination + rows, err := tx.NamedQuery(query, queryValues) + if err != nil { + api.HandleErr(w, r, tx.Tx, http.StatusInternalServerError, nil, fmt.Errorf("Phy_Location read: error getting Phy_Location(s): %w", err)) + return + } + defer log.Close(rows, "unable to close DB connection") + + physLocation := tc.PhysLocationV5{} + physLocationList := []tc.PhysLocationV5{} + for rows.Next() { + if err = rows.Scan(&physLocation.Address, &physLocation.City, &physLocation.Comments, &physLocation.Email, &physLocation.ID, &physLocation.LastUpdated, &physLocation.Name, &physLocation.Phone, &physLocation.POC, &physLocation.RegionID, &physLocation.RegionName, &physLocation.ShortName, &physLocation.State, &physLocation.Zip); err != nil { + api.HandleErr(w, r, tx.Tx, http.StatusInternalServerError, nil, fmt.Errorf("error getting physLocation(s): %w", err)) + return + } + physLocationList = append(physLocationList, physLocation) + } + + api.WriteResp(w, r, physLocationList) + return +} + +func CreatePhysLocation(w http.ResponseWriter, r *http.Request) { + inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil) + if userErr != nil || sysErr != nil { + api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr) + return + } + defer inf.Close() + tx := inf.Tx.Tx + + physLocation, readValErr := readAndValidateJsonStruct(r) + if readValErr != nil { + api.HandleErr(w, r, tx, http.StatusBadRequest, readValErr, nil) + return + } + + // check if phys_location already exists + var count int + err := tx.QueryRow("SELECT count(*) from phys_location where name = $1", physLocation.Name).Scan(&count) + if err != nil { + api.HandleErr(w, r, tx, http.StatusInternalServerError, nil, fmt.Errorf("error: %w, when checking if physLocation with name %s exists", err, physLocation.Name)) + return + } + if count == 1 { + api.HandleErr(w, r, tx, http.StatusBadRequest, fmt.Errorf("physLocation name '%s' already exists", physLocation.Name), nil) + return + } + + // create phys_location + query := ` + INSERT INTO phys_location ( + address, + city, + comments, + email, + name, + phone, + poc, + region, + short_name, + state, + zip + ) VALUES ( + $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11 + ) RETURNING id, last_updated +` + err = tx.QueryRow( + query, + physLocation.Address, + physLocation.City, + physLocation.Comments, + physLocation.Email, + physLocation.Name, + physLocation.Phone, + physLocation.POC, + physLocation.RegionID, + physLocation.ShortName, + physLocation.State, + physLocation.Zip, + ).Scan( + &physLocation.ID, + &physLocation.LastUpdated, + ) + + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + api.HandleErr(w, r, tx, http.StatusInternalServerError, fmt.Errorf("error: %w in physLocation with name: %s", err, physLocation.Name), nil) + return + } + usrErr, sysErr, code := api.ParseDBError(err) + api.HandleErr(w, r, tx, code, usrErr, sysErr) + return + } + alerts := tc.CreateAlerts(tc.SuccessLevel, "physLocation was created.") + w.Header().Set("Location", fmt.Sprintf("/api/%d.%d/phys_locations?name=%s", inf.Version.Major, inf.Version.Minor, physLocation.Name)) + api.WriteAlertsObj(w, r, http.StatusCreated, alerts, physLocation) + return +} + +func UpdatePhysLocation(w http.ResponseWriter, r *http.Request) { + inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil) + if userErr != nil || sysErr != nil { + api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr) + return + } + defer inf.Close() + + tx := inf.Tx.Tx + physLocation, readValErr := readAndValidateJsonStruct(r) + if readValErr != nil { + api.HandleErr(w, r, tx, http.StatusBadRequest, readValErr, nil) + return + } + + requestedID := inf.Params["id"] + + intRequestId, convErr := strconv.Atoi(requestedID) + if convErr != nil { + api.HandleErr(w, r, inf.Tx.Tx, http.StatusBadRequest, fmt.Errorf("physLocation update error: %w, while converting from string to int", convErr), nil) + } + // check if the entity was already updated + userErr, sysErr, errCode = api.CheckIfUnModified(r.Header, inf.Tx, intRequestId, "phys_location") + if userErr != nil || sysErr != nil { + api.HandleErr(w, r, tx, errCode, userErr, sysErr) + return + } + + //update name and description of a phys_location + query := ` + UPDATE phys_location pl + SET + address = $1, + city = $2, + comments = $3, + email = $4, + name = $5, + phone = $6, + poc = $7, + region = $8, + short_name = $9, + state = $10, + zip = $11 + WHERE + pl.id = $12 + RETURNING + pl.id, pl.last_updated +` + + err := tx.QueryRow( + query, + physLocation.Address, + physLocation.City, + physLocation.Comments, + physLocation.Email, + physLocation.Name, + physLocation.Phone, + physLocation.POC, + physLocation.RegionID, + physLocation.ShortName, + physLocation.State, + physLocation.Zip, + requestedID, + ).Scan( + &physLocation.ID, + &physLocation.LastUpdated, + ) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + api.HandleErr(w, r, tx, http.StatusBadRequest, fmt.Errorf("physLocation with ID: %v not found", physLocation.ID), nil) + return + } + usrErr, sysErr, code := api.ParseDBError(err) + api.HandleErr(w, r, tx, code, usrErr, sysErr) + return + } + alerts := tc.CreateAlerts(tc.SuccessLevel, "physLocation was updated") + api.WriteAlertsObj(w, r, http.StatusOK, alerts, physLocation) + return +} + +func DeletePhysLocation(w http.ResponseWriter, r *http.Request) { + inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil) + tx := inf.Tx.Tx + if userErr != nil || sysErr != nil { + api.HandleErr(w, r, tx, errCode, userErr, sysErr) + return + } + defer inf.Close() + + id := inf.Params["id"] + exists, err := dbhelpers.PhysLocationExists(tx, id) + if err != nil { + api.HandleErr(w, r, tx, http.StatusInternalServerError, nil, err) + return + } + if !exists { + if id != "" { Review Comment: Fixed it. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@trafficcontrol.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org