ocket8888 commented on a change in pull request #5071:
URL: https://github.com/apache/trafficcontrol/pull/5071#discussion_r501999846



##########
File path: traffic_ops/traffic_ops_golang/deliveryservice/request/requests.go
##########
@@ -20,451 +22,843 @@ package request
  */
 
 import (
+       "database/sql"
+       "encoding/json"
        "errors"
        "fmt"
-       
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/util/ims"
        "net/http"
-       "strconv"
+       "strings"
        "time"
 
+       
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/util/ims"
+
        "github.com/apache/trafficcontrol/lib/go-log"
+       "github.com/apache/trafficcontrol/lib/go-rfc"
        "github.com/apache/trafficcontrol/lib/go-tc"
        "github.com/apache/trafficcontrol/lib/go-util"
        "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
-       "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/auth"
        
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
+       
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/deliveryservice"
+       
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/routing/middleware"
        "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/tenant"
 
        "github.com/jmoiron/sqlx"
+       "github.com/lib/pq"
 )
 
-// TODeliveryServiceRequest is the type alias to define functions on
-type TODeliveryServiceRequest struct {
-       api.APIInfoImpl `json:"-"`
-       tc.DeliveryServiceRequestNullable
-}
+const selectQuery = `
+SELECT
+       a.username AS author,
+       e.username AS lastEditedBy,
+       s.username AS assignee,
+       r.assignee_id,
+       r.author_id,
+       r.change_type,
+       r.created_at,
+       r.id,
+       r.last_edited_by_id,
+       r.last_updated,
+       r.deliveryservice,
+       r.original,
+       r.status
+FROM deliveryservice_request r
+JOIN tm_user a ON r.author_id = a.id
+LEFT OUTER JOIN tm_user s ON r.assignee_id = s.id
+LEFT OUTER JOIN tm_user e ON r.last_edited_by_id = e.id
+`
 
-func (v *TODeliveryServiceRequest) SetLastUpdated(t tc.TimeNoMod) { 
v.LastUpdated = &t }
-func (v *TODeliveryServiceRequest) InsertQuery() string           { return 
insertRequestQuery() }
-func (v *TODeliveryServiceRequest) UpdateQuery() string           { return 
updateRequestQuery() }
-func (v *TODeliveryServiceRequest) DeleteQuery() string {
-       return `DELETE FROM deliveryservice_request WHERE id = :id`
-}
+const originalsQuery = deliveryservice.SelectDeliveryServicesQuery + `
+WHERE ds.id = ANY(:ids)
+`
 
-func (req TODeliveryServiceRequest) GetKeyFieldsInfo() []api.KeyFieldInfo {
-       return []api.KeyFieldInfo{{"id", api.GetIntKey}}
-}
+const insertQuery = `
+INSERT INTO deliveryservice_request (
+       assignee_id,
+       author_id,
+       change_type,
+       last_edited_by_id,
+       deliveryservice,
+       original,
+       status
+) VALUES (
+       $1,
+       $2,
+       $3,
+       $2,
+       $4,
+       $5,
+       $6
+)
+RETURNING
+       id,
+       last_updated,
+       created_at
+`
 
-func (req TODeliveryServiceRequest) GetKeys() (map[string]interface{}, bool) {
-       if req.ID == nil {
-               return map[string]interface{}{"id": 0}, false
-       }
-       return map[string]interface{}{"id": *req.ID}, true
-}
+const updateQuery = `
+UPDATE deliveryservice_request
+SET
+       assignee_id = $1,
+       change_type = $2,
+       last_edited_by_id = $3,
+       deliveryservice = $4,
+       original = $5,
+       status = $6
+WHERE id = $7
+RETURNING
+       last_updated,
+       created_at
+`
 
-func (req *TODeliveryServiceRequest) SetKeys(keys map[string]interface{}) {
-       i, _ := keys["id"].(int) //this utilizes the non panicking type 
assertion, if the thrown away ok variable is false i will be the zero of the 
type, 0 here.
-       req.ID = &i
-}
+const deleteQuery = `
+DELETE
+FROM deliveryservice_request
+WHERE id=$1
+`
 
-// GetAuditName is part of the tc.Identifier interface
-func (req TODeliveryServiceRequest) GetAuditName() string {
-       return req.getXMLID()
+//TODO: figure out how to modify 'AddTenancyCheck' so this isn't necessary
+const customTenancyCheck = `(
+       CASE r.change_type
+       WHEN 'delete' THEN CAST(r.original->>'tenantId' AS bigint) = 
ANY(CAST(:accessibleTenants AS bigint[]))
+       ELSE CAST(r.deliveryservice->>'tenantId' AS bigint) = 
ANY(CAST(:accessibleTenants AS bigint[]))
+       END
+)`
+
+func selectMaxLastUpdatedQuery(where string) string {
+       return `SELECT max(t) from (
+               SELECT max(r.last_updated) as t FROM deliveryservice_request r
+       JOIN tm_user a ON r.author_id = a.id
+       LEFT OUTER JOIN tm_user s ON r.assignee_id = s.id
+       LEFT OUTER JOIN tm_user e ON r.last_edited_by_id = e.id ` + where +
+               ` UNION ALL
+       select max(last_updated) as t from last_deleted l where 
l.table_name='deliveryservice_request') as res`
 }
 
-// GetType is part of the tc.Identifier interface
-func (req TODeliveryServiceRequest) GetType() string {
-       return "deliveryservice_request"
+// getOriginals fetches the Delivery Services identified in 'ids' and sets
+// them as originals on the Delivery Services to which each ID maps in
+// needOriginals. It returns a response code to use if an error occurred, in
+// which case it also returns a user error and a system error.
+func getOriginals(ids []int, tx *sqlx.Tx, needOriginals 
map[int][]*tc.DeliveryServiceRequestV30) (int, error, error) {

Review comment:
       Discussed below




----------------------------------------------------------------
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.

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


Reply via email to