MCPcopy Create free account
hub / github.com/cloudwan/gohan / GetSingleResourceInTransaction

Function GetSingleResourceInTransaction

server/resources/resource_management.go:426–479  ·  view source on GitHub ↗

GetSingleResourceInTransaction get resource in single transaction

(context middleware.Context, resourceSchema *schema.Schema, resourceID string, tenantIDs []string)

Source from the content-addressed store, hash-verified

424
425//GetSingleResourceInTransaction get resource in single transaction
426func GetSingleResourceInTransaction(context middleware.Context, resourceSchema *schema.Schema, resourceID string, tenantIDs []string) (err error) {
427 defer measureRequestTime(time.Now(), "get.single.in_tx", resourceSchema.ID)
428 var options *transaction.ViewOptions
429 r, ok := context["http_request"].(*http.Request)
430 if ok {
431 options = listOptionsFromQueryParameter(r.URL.Query())
432 }
433 mainTransaction := context["transaction"].(transaction.Transaction)
434 environmentManager := extension.GetManager()
435 environment, ok := environmentManager.GetEnvironment(resourceSchema.ID)
436 if !ok {
437 return fmt.Errorf("no environment for schema")
438 }
439
440 if err := extension.HandleEvent(context, environment, "pre_show_in_transaction", resourceSchema.ID); err != nil {
441 return err
442 }
443 if rawResponse, ok := context["response"]; ok {
444 if _, ok := rawResponse.(map[string]interface{}); ok {
445 return nil
446 }
447 return fmt.Errorf("extension returned invalid JSON: %v", rawResponse)
448 }
449 filter := transaction.IDFilter(resourceID)
450 if tenantIDs != nil {
451 filter["tenant_id"] = tenantIDs
452 }
453
454 auth := context["auth"].(schema.Authorization)
455 policy := context["policy"].(*schema.Policy)
456 currCond := policy.GetCurrentResourceCondition()
457 currCond.AddCustomFilters(filter, auth.TenantID())
458
459 object, err := mainTransaction.Fetch(resourceSchema, filter, options)
460 if object == nil {
461 switch err {
462 case transaction.ErrResourceNotFound:
463 log.Info("Fetch failed: %v", err)
464 return ResourceError{err, "Resource not found", NotFound}
465 default:
466 log.Error("Fetch failed: %v", err)
467 return ResourceError{err, "Error when fetching resource", InternalServerError}
468 }
469 }
470
471 response := map[string]interface{}{}
472 response[resourceSchema.Singular] = object.Data()
473 context["response"] = response
474
475 if err := extension.HandleEvent(context, environment, "post_show_in_transaction", resourceSchema.ID); err != nil {
476 return err
477 }
478 return
479}
480
481// CreateOrUpdateResource updates resource if it existed and otherwise creates it and returns true.
482func CreateOrUpdateResource(

Callers 1

GetSingleResourceFunction · 0.85

Calls 13

measureRequestTimeFunction · 0.85
GetEnvironmentMethod · 0.80
AddCustomFiltersMethod · 0.80
QueryMethod · 0.65
ErrorfMethod · 0.65
HandleEventMethod · 0.65
TenantIDMethod · 0.65
FetchMethod · 0.65
InfoMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected