當前位置: 首頁>>代碼示例>>Golang>>正文


Golang qor.Context類代碼示例

本文整理匯總了Golang中github.com/qor/qor.Context的典型用法代碼示例。如果您正苦於以下問題:Golang Context類的具體用法?Golang Context怎麽用?Golang Context使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Context類的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: findManyHandler

func (res *Resource) findManyHandler(result interface{}, context *qor.Context) error {
	if res.HasPermission(roles.Read, context) {
		return context.GetDB().Set("gorm:order_by_primary_key", "DESC").Find(result).Error
	} else {
		return roles.ErrPermissionDenied
	}
}
開發者ID:musicking,項目名稱:qor,代碼行數:7,代碼來源:crud.go

示例2: saveHandler

func (res *Resource) saveHandler(result interface{}, context *qor.Context) error {
	if (context.GetDB().NewScope(result).PrimaryKeyZero() &&
		res.HasPermission(roles.Create, context)) || // has create permission
		res.HasPermission(roles.Update, context) { // has update permission
		return context.GetDB().Save(result).Error
	} else {
		return roles.ErrPermissionDenied
	}
}
開發者ID:musicking,項目名稱:qor,代碼行數:9,代碼來源:crud.go

示例3: deleteHandler

func (res *Resource) deleteHandler(result interface{}, context *qor.Context) error {
	if res.HasPermission(roles.Delete, context) {
		if !context.GetDB().First(result, context.ResourceID).RecordNotFound() {
			return context.GetDB().Delete(result).Error
		} else {
			return gorm.RecordNotFound
		}
	} else {
		return roles.ErrPermissionDenied
	}
}
開發者ID:NeoChow,項目名稱:qor,代碼行數:11,代碼來源:curd.go

示例4: finder

func (res *Resource) finder(result interface{}, metaValues *resource.MetaValues, context *qor.Context) error {
	var primaryKey string
	if metaValues == nil {
		primaryKey = context.ResourceID
	} else if id := metaValues.Get(res.PrimaryFieldName()); id != nil {
		primaryKey = utils.ToString(id.Value)
	}

	if primaryKey != "" {
		if metaValues != nil {
			if destroy := metaValues.Get("_destroy"); destroy != nil {
				if fmt.Sprintf("%v", destroy.Value) != "0" {
					context.GetDB().Delete(result, primaryKey)
					return resource.ErrProcessorSkipLeft
				}
			}
		}
		return context.GetDB().First(result, primaryKey).Error
	}
	return nil
}
開發者ID:trigrass2,項目名稱:qor,代碼行數:21,代碼來源:admin.go

示例5: findOneHandler

func (res *Resource) findOneHandler(result interface{}, metaValues *MetaValues, context *qor.Context) error {
	if res.HasPermission(roles.Read, context) {
		var (
			scope        = context.GetDB().NewScope(res.Value)
			primaryField = res.PrimaryField()
			primaryKey   string
		)

		if metaValues == nil {
			primaryKey = context.ResourceID
		} else if primaryField == nil {
			return nil
		} else if id := metaValues.Get(primaryField.Name); id != nil {
			primaryKey = utils.ToString(id.Value)
		}

		if primaryKey != "" {
			if metaValues != nil {
				if destroy := metaValues.Get("_destroy"); destroy != nil {
					if fmt.Sprint(destroy.Value) != "0" && res.HasPermission(roles.Delete, context) {
						context.GetDB().Delete(result, fmt.Sprintf("%v = ?", scope.Quote(primaryField.DBName)), primaryKey)
						return ErrProcessorSkipLeft
					}
				}
			}
			return context.GetDB().First(result, fmt.Sprintf("%v = ?", scope.Quote(primaryField.DBName)), primaryKey).Error
		}
		return errors.New("failed to find")
	} else {
		return roles.ErrPermissionDenied
	}
}
開發者ID:musicking,項目名稱:qor,代碼行數:32,代碼來源:crud.go

示例6: Export

func (res *Resource) Export(container Container, context *qor.Context) error {
	results := res.NewSlice()

	if err := context.GetDB().Find(results).Error; err == nil {
		reflectValue := reflect.Indirect(reflect.ValueOf(results))
		if writer, err := container.NewWriter(res, context); err == nil {
			writer.WriteHeader()
			for i := 0; i < reflectValue.Len(); i++ {
				var result = reflectValue.Index(i).Interface()
				if err := writer.WriteRow(result); err != nil {
					return err
				}
			}
			writer.Flush()
		} else {
			return err
		}
	} else {
		return err
	}
	return nil
}
開發者ID:NeoChow,項目名稱:qor,代碼行數:22,代碼來源:exchange.go

示例7: getNestedModel

func getNestedModel(value interface{}, fieldName string, context *qor.Context) interface{} {
	model := reflect.Indirect(reflect.ValueOf(value))
	fields := strings.Split(fieldName, ".")
	for _, field := range fields[:len(fields)-1] {
		if model.CanAddr() {
			submodel := model.FieldByName(field)
			if key := submodel.FieldByName("Id"); !key.IsValid() || key.Uint() == 0 {
				if submodel.CanAddr() {
					context.GetDB().Model(model.Addr().Interface()).Related(submodel.Addr().Interface())
					model = submodel
				} else {
					break
				}
			} else {
				model = submodel
			}
		}
	}

	if model.CanAddr() {
		return model.Addr().Interface()
	}
	return nil
}
開發者ID:joelchen,項目名稱:qor,代碼行數:24,代碼來源:meta.go

示例8: deleteHandler

func (res *Resource) deleteHandler(result interface{}, context *qor.Context) error {
	if res.HasPermission(roles.Delete, context) {
		scope := context.GetDB().NewScope(res.Value)
		if !context.GetDB().First(result, fmt.Sprintf("%v = ?", scope.Quote(res.PrimaryDBName())), context.ResourceID).RecordNotFound() {
			return context.GetDB().Delete(result).Error
		}
		return gorm.ErrRecordNotFound
	}
	return roles.ErrPermissionDenied
}
開發者ID:caizengjun,項目名稱:qor,代碼行數:10,代碼來源:crud.go

示例9: saveHandler

func (res *Resource) saveHandler(result interface{}, context *qor.Context) error {
	if (context.GetDB().NewScope(result).PrimaryKeyZero() &&
		res.HasPermission(roles.Create, context)) || // has create permission
		res.HasPermission(roles.Update, context) { // has update permission
		results := context.GetDB().Save(result)

		if results.RowsAffected == 0 {
			primaryField := context.GetDB().NewScope(result).PrimaryField()
			// if primary field has value and it is not a auto increment field, then create it if nothing updated
			if _, ok := primaryField.TagSettings["AUTO_INCREMENT"]; !primaryField.IsBlank && !ok {
				return context.GetDB().Create(result).Error
			}
		}

		return results.Error
	}
	return roles.ErrPermissionDenied
}
開發者ID:caizengjun,項目名稱:qor,代碼行數:18,代碼來源:crud.go

示例10: findManyHandler

func (res *Resource) findManyHandler(result interface{}, context *qor.Context) error {
	if res.HasPermission(roles.Read, context) {
		db := context.GetDB()
		if _, ok := db.Get("qor:getting_total_count"); ok {
			return context.GetDB().Count(result).Error
		} else {
			return context.GetDB().Set("gorm:order_by_primary_key", "DESC").Find(result).Error
		}
	}
	return roles.ErrPermissionDenied
}
開發者ID:caizengjun,項目名稱:qor,代碼行數:11,代碼來源:crud.go

示例11: callScopes

func (s *Searcher) callScopes(context *qor.Context) *qor.Context {
	db := context.GetDB()

	// call default scopes
	for _, scope := range s.Resource.scopes {
		if scope.Default {
			db = scope.Handle(db, context)
		}
	}

	// call scopes
	for _, scope := range s.scopes {
		db = scope.Handle(db, context)
	}

	// call filters
	if s.filters != nil {
		for key, value := range s.filters {
			filter := s.Resource.filters[key]
			if filter != nil && filter.Handler != nil {
				db = filter.Handler(key, value, db, context)
			} else {
				db = DefaultHandler(key, value, db, context)
			}
		}
	}

	// add order by
	if order_by := context.Request.Form.Get("order_by"); order_by != "" {
		if regexp.MustCompile("^[a-zA-Z_]+$").MatchString(order_by) {
			if field, ok := db.NewScope(s.Context.Resource.Value).FieldByName(strings.TrimSuffix(order_by, "_desc")); ok {
				if strings.HasSuffix(order_by, "_desc") {
					db = db.Order(field.DBName+" DESC", true)
				} else {
					db = db.Order(field.DBName, true)
				}
			}
		}
	}

	// call search
	var keyword string
	if keyword = context.Request.Form.Get("keyword"); keyword == "" {
		keyword = context.Request.URL.Query().Get("keyword")
	}
	if keyword != "" && s.Resource.SearchHandler != nil {
		context.SetDB(s.Resource.SearchHandler(keyword, context).Debug())
		return context
	}

	context.SetDB(db)
	return context
}
開發者ID:8legd,項目名稱:qor-qor,代碼行數:53,代碼來源:searcher.go

示例12: callScopes

func (s *Searcher) callScopes(context *qor.Context) *qor.Context {
	db := context.GetDB()

	// call default scopes
	for _, scope := range s.Resource.scopes {
		if scope.Default {
			db = scope.Handle(db, context)
		}
	}

	// call scopes
	for _, scope := range s.scopes {
		db = scope.Handle(db, context)
	}

	// call filters
	if s.filters != nil {
		for key, value := range s.filters {
			filter := s.Resource.filters[key]
			if filter != nil && filter.Handler != nil {
				db = filter.Handler(key, value, db, context)
			} else {
				db = DefaultHandler(key, value, db, context)
			}
		}
	}

	// add order by
	if order_by := context.Request.Form.Get("order_by"); order_by != "" {
		if regexp.MustCompile("^[a-zA-Z_]+$").MatchString(order_by) {
			if strings.HasSuffix(order_by, "_desc") {
				db = db.Order(strings.TrimSuffix(order_by, "_desc")+" DESC", true)
			} else {
				db = db.Order(order_by, true)
			}
		}
	}

	// call search
	if keyword := context.Request.Form.Get("keyword"); keyword != "" {
		if s.Resource.SearchHandler != nil {
			context.SetDB(s.Resource.SearchHandler(keyword, context))
			return context
		}
	}

	context.SetDB(db)
	return context
}
開發者ID:pusparajm,項目名稱:qor,代碼行數:49,代碼來源:searcher.go

示例13: callScopes

func (s *Searcher) callScopes(context *qor.Context) *qor.Context {
	db := context.GetDB()

	// call default scopes
	for _, scope := range s.Resource.scopes {
		if scope.Default {
			db = scope.Handle(db, context)
		}
	}

	// call scopes
	for _, scope := range s.scopes {
		db = scope.Handle(db, context)
	}

	// call filters
	if s.filters != nil {
		for key, value := range s.filters {
			filter := s.Resource.filters[key]
			if filter != nil && filter.Handler != nil {
				db = filter.Handler(key, value, db, context)
			} else {
				db = DefaultHandler(key, value, db, context)
			}
		}
	}

	context.SetDB(db)

	// call search
	if keyword := context.Request.Form.Get("keyword"); keyword != "" {
		if s.Resource.SearchHandler != nil {
			context.SetDB(s.Resource.SearchHandler(keyword, context))
		}
	}
	return context
}
開發者ID:nilslice,項目名稱:qor,代碼行數:37,代碼來源:searcher.go


注:本文中的github.com/qor/qor.Context類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。