rimashah25 commented on code in PR #7213:
URL: https://github.com/apache/trafficcontrol/pull/7213#discussion_r1050008729


##########
traffic_ops/traffic_ops_golang/servercapability/servercapability.go:
##########
@@ -151,6 +153,202 @@ func (v *TOServerCapability) Update(h http.Header) 
(error, error, int) {
        return nil, nil, http.StatusOK
 }
 
+func GetServerCapability(w http.ResponseWriter, r *http.Request) {
+       var sc tc.ServerCapabilityV4
+       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: "sc.name", Checker: nil},
+       }
+       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
+       }
+
+       selectQuery := "SELECT name, description, last_updated FROM 
server_capability sc"
+       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("server capability read: error getting server capability(ies): %w", 
err))
+               return
+       }
+       defer log.Close(rows, "unable to close DB connection")
+
+       scList := []tc.ServerCapabilityV4{}
+       for rows.Next() {
+               if err = rows.Scan(&sc.Name, &sc.Description, &sc.LastUpdated); 
err != nil {
+                       api.HandleErr(w, r, tx.Tx, 
http.StatusInternalServerError, nil, fmt.Errorf("error getting server 
capability(ies): %w", err))
+                       return
+               }
+               scList = append(scList, sc)
+       }
+
+       api.WriteResp(w, r, scList)
+       return
+}
+
+func UpdateServerCapability(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
+       sc, readValErr := readAndValidateJsonStruct(r)
+       if readValErr != nil {
+               api.HandleErr(w, r, tx, http.StatusBadRequest, readValErr, nil)
+               return
+       }
+
+       requestedName := inf.Params["name"]
+       // check if the entity was already updated
+       userErr, sysErr, errCode = api.CheckIfUnModifiedByName(r.Header, 
inf.Tx, requestedName, "server_capability")
+       if userErr != nil || sysErr != nil {
+               api.HandleErr(w, r, tx, errCode, userErr, sysErr)
+               return
+       }
+
+       //update name and description of a capability
+       query := `UPDATE server_capability sc SET
+               name = $1,
+               description = $2
+       WHERE sc.name = $3
+       RETURNING sc.name, sc.description, sc.last_updated`
+
+       err := tx.QueryRow(query, sc.Name, sc.Description, 
requestedName).Scan(&sc.Name, &sc.Description, &sc.LastUpdated)
+       if err != nil && err == sql.ErrNoRows {
+               api.HandleErr(w, r, tx, http.StatusBadRequest, err, nil)
+               return
+       }
+       alerts := tc.CreateAlerts(tc.SuccessLevel, "server capability was 
updated")
+       api.WriteAlertsObj(w, r, http.StatusOK, alerts, sc)
+       return
+}
+
+func CreateServerCapability(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
+
+       sc, readValErr := readAndValidateJsonStruct(r)
+       if readValErr != nil {
+               api.HandleErr(w, r, tx, http.StatusBadRequest, readValErr, nil)
+               return
+       }
+
+       // check if capability already exists
+       var count int
+       err := tx.QueryRow("SELECT count(*) from server_capability where name = 
$1", sc.Name).Scan(&count)
+       if err != nil {
+               api.HandleErr(w, r, tx, http.StatusInternalServerError, nil, 
fmt.Errorf("checking if server capability with name %s exists", sc.Name))

Review Comment:
   yes. :)



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to