func validate(scope *gorm.Scope) { if _, ok := scope.Get("gorm:update_column"); !ok { if result, ok := scope.DB().Get(skipValidations); !(ok && result.(bool)) { scope.CallMethodWithErrorCheck("Validate") } } }
func isDraftMode(scope *gorm.Scope) bool { if draftMode, ok := scope.Get("publish:draft_mode"); ok { if isDraft, ok := draftMode.(bool); ok && isDraft { return true } } return false }
func createPublishEvent(scope *gorm.Scope) { if _, ok := scope.InstanceGet("publish:creating_publish_event"); ok { if event, ok := scope.Get(publishEvent); ok { if event, ok := event.(*PublishEvent); ok { event.PublishStatus = DIRTY scope.Err(scope.NewDB().Save(&event).Error) } } } }
func getModeAndNewScope(scope *gorm.Scope) (isProduction bool, clone *gorm.Scope) { if draftMode, ok := scope.Get("publish:draft_mode"); !ok || !draftMode.(bool) { if _, ok := scope.InstanceGet("publish:supported_model"); ok { table := originalTableName(scope.TableName()) clone := scope.New(scope.Value) clone.Search.Table(table) return true, clone } } return false, nil }
func deleteScope(scope *gorm.Scope) { if !scope.HasError() { _, supportedModel := scope.InstanceGet("publish:supported_model") isDraftMode, ok := scope.Get("publish:draft_mode") if supportedModel && (ok && isDraftMode.(bool)) { scope.Raw( fmt.Sprintf("UPDATE %v SET deleted_at=%v, publish_status=%v %v", scope.QuotedTableName(), scope.AddToVars(gorm.NowFunc()), scope.AddToVars(DIRTY), scope.CombinedConditionSql(), )) scope.Exec() } else { gorm.Delete(scope) } } }
func validate(scope *gorm.Scope) { if _, ok := scope.Get("gorm:update_column"); !ok { if result, ok := scope.DB().Get(skipValidations); !(ok && result.(bool)) { if !scope.HasError() { scope.CallMethod("Validate") resource := scope.IndirectValue().Interface() _, validatorErrors := govalidator.ValidateStruct(resource) if validatorErrors != nil { if errors, ok := validatorErrors.(govalidator.Errors); ok { for _, err := range flatValidatorErrors(errors) { scope.DB().AddError(formattedError(err, resource)) } } else { scope.DB().AddError(validatorErrors) } } } } } }