Skip to content

Commit

Permalink
refactor(api) remove duplicated lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Cedrok committed Aug 9, 2024
1 parent d43033c commit edaaa82
Showing 1 changed file with 25 additions and 39 deletions.
64 changes: 25 additions & 39 deletions API/controllers/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,26 +472,7 @@ func HandleGenericObjects(w http.ResponseWriter, r *http.Request) {

// Respond
if r.Method == "DELETE" {
for _, obj := range matchingObjects {
entStr := obj["entity"].(string)

var objStr string

if u.IsEntityNonHierarchical(u.EntityStrToInt(entStr)) {
objStr = obj["slug"].(string)
} else {
objStr = obj["id"].(string)
}

modelErr := models.DeleteObject(entStr, objStr, user.Roles)
if modelErr != nil {
u.ErrLog("Error while deleting object: "+objStr, "DELETE GetGenericObjectById", modelErr.Message, r)
u.RespondWithError(w, modelErr)
return
}
eventNotifier <- u.FormatNotifyData("delete", entStr, objStr)
}
u.Respond(w, u.RespDataWrapper("successfully deleted objects", matchingObjects))
deleteGenericObjects(w, r, user, matchingObjects)
} else if r.Method == "OPTIONS" {
u.WriteOptionsHeader(w, "GET, DELETE")
} else {
Expand Down Expand Up @@ -556,6 +537,28 @@ func checkChildrenLimit(entData []map[string]interface{}, entStr string, filters
return true, nil
}

func deleteGenericObjects(w http.ResponseWriter, r *http.Request, user *models.Account, matchingObjects []map[string]interface{}) {
for _, obj := range matchingObjects {
entStr := obj["entity"].(string)

var objStr string
if u.IsEntityNonHierarchical(u.EntityStrToInt(entStr)) {
objStr = obj["slug"].(string)
} else {
objStr = obj["id"].(string)
}

modelErr := models.DeleteObject(entStr, objStr, user.Roles)
if modelErr != nil {
u.ErrLog("Error while deleting object: "+objStr, "DELETE GetGenericObjectById", modelErr.Message, r)
u.RespondWithError(w, modelErr)
return
}
eventNotifier <- u.FormatNotifyData("delete", entStr, objStr)
}
u.Respond(w, u.RespDataWrapper("successfully deleted objects", matchingObjects))
}

// swagger:operation POST /api/objects/search Objects HandleComplexFilters
// Get all objects from any entity that match the complex filter. Return as a list.
// Wildcards can be used on any of the parameters present in query with equality and inequality operations.
Expand Down Expand Up @@ -701,26 +704,9 @@ func HandleComplexFilters(w http.ResponseWriter, r *http.Request) {
return
}

// Respond
if r.Method == "DELETE" {
for _, obj := range matchingObjects {
entStr := obj["entity"].(string)

var objStr string

if u.IsEntityNonHierarchical(u.EntityStrToInt(entStr)) {
objStr = obj["slug"].(string)
} else {
objStr = obj["id"].(string)
}

modelErr := models.DeleteObject(entStr, objStr, user.Roles)
if modelErr != nil {
u.ErrLog("Error while deleting object: "+objStr, "DELETE GetGenericObjectById", modelErr.Message, r)
u.RespondWithError(w, modelErr)
return
}
}
u.Respond(w, u.RespDataWrapper("successfully deleted objects", matchingObjects))
deleteGenericObjects(w, r, user, matchingObjects)
} else if r.Method == "OPTIONS" {
u.WriteOptionsHeader(w, "POST, DELETE")
} else {
Expand Down

0 comments on commit edaaa82

Please sign in to comment.