rimashah25 commented on code in PR #7408:
URL: https://github.com/apache/trafficcontrol/pull/7408#discussion_r1149788080
##########
traffic_ops/traffic_ops_golang/servicecategory/servicecategories.go:
##########
@@ -187,3 +189,209 @@ WHERE name=$2 RETURNING last_updated`
func deleteQuery() string {
return `DELETE FROM service_category WHERE name=:name`
}
+
+func GetServiceCategory(w http.ResponseWriter, r *http.Request) {
+ var sc tc.ServiceCategoryV5
+ 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, last_updated FROM service_category as 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.ServiceCategoryV5{}
+ for rows.Next() {
+ if err = rows.Scan(&sc.Name, &sc.LastUpdated); err != nil {
+ api.HandleErr(w, r, tx.Tx,
http.StatusInternalServerError, nil, fmt.Errorf("error getting service
categories(ies): %w", err))
+ return
+ }
+ scList = append(scList, sc)
+ }
+
+ api.WriteResp(w, r, scList)
+ return
+}
+
+func CreateServiceCategory(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 service category already exists
+ var count int
+ err := tx.QueryRow("SELECT count(*) from service_category where name =
$1 ", sc.Name).Scan(&count)
+ if err != nil {
+ api.HandleErr(w, r, tx, http.StatusInternalServerError, nil,
fmt.Errorf("error: %w, when checking if service category with name %s exists",
err, sc.Name))
+ return
+ }
+ if count == 1 {
+ api.HandleErr(w, r, tx, http.StatusBadRequest,
fmt.Errorf("service category name '%s' already exists.", sc.Name), nil)
+ return
+ }
+
+ // create server capability
Review Comment:
copy - paste comment mistake
--
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]