当前位置: 首页>>代码示例>>Golang>>正文


Golang Context.GetDB方法代码示例

本文整理汇总了Golang中github.com/qor/qor/admin.Context.GetDB方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.GetDB方法的具体用法?Golang Context.GetDB怎么用?Golang Context.GetDB使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/qor/qor/admin.Context的用法示例。


在下文中一共展示了Context.GetDB方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: PublishOrDiscard

func (db *publishController) PublishOrDiscard(context *admin.Context) {
	var request = context.Request
	var ids = request.Form["checked_ids[]"]
	var records = []interface{}{}
	var values = map[string][]string{}

	for _, id := range ids {
		if keys := strings.Split(id, "__"); len(keys) == 2 {
			name, id := keys[0], keys[1]
			values[name] = append(values[name], id)
		}
	}

	draftDB := context.GetDB().Set("publish:draft_mode", true).Unscoped()
	for name, value := range values {
		res := context.Admin.GetResource(name)
		results := res.NewSlice()
		if draftDB.Find(results, fmt.Sprintf("%v IN (?)", res.PrimaryDBName()), value).Error == nil {
			resultValues := reflect.Indirect(reflect.ValueOf(results))
			for i := 0; i < resultValues.Len(); i++ {
				records = append(records, resultValues.Index(i).Interface())
			}
		}
	}

	if request.Form.Get("publish_type") == "publish" {
		Publish{DB: draftDB}.Publish(records...)
	} else if request.Form.Get("publish_type") == "discard" {
		Publish{DB: draftDB}.Discard(records...)
	}
	http.Redirect(context.Writer, context.Request, context.Request.RequestURI, http.StatusFound)
}
开发者ID:jmptrader,项目名称:qor,代码行数:32,代码来源:controller.go

示例2: Preview

func (db *publishController) Preview(context *admin.Context) {
	type resource struct {
		*admin.Resource
		Value interface{}
	}

	var drafts = []resource{}

	draftDB := context.GetDB().Set(publishDraftMode, true).Unscoped()
	for _, res := range context.Admin.GetResources() {
		if visibleInterface, ok := res.Value.(visiblePublishResourceInterface); ok {
			if !visibleInterface.VisiblePublishResource() {
				continue
			}
		} else if res.Config.Invisible {
			continue
		}

		results := res.NewSlice()
		if IsPublishableModel(res.Value) || IsPublishEvent(res.Value) {
			if draftDB.Unscoped().Where("publish_status = ?", DIRTY).Find(results).RowsAffected > 0 {
				drafts = append(drafts, resource{
					Resource: res,
					Value:    results,
				})
			}
		}
	}
	context.Execute("publish_drafts", drafts)
}
开发者ID:kingland,项目名称:qor,代码行数:30,代码来源:controller.go

示例3: updatePosition

func updatePosition(context *admin.Context) {
	if result, err := context.FindOne(); err == nil {
		if position, ok := result.(sortingInterface); ok {
			if pos, err := strconv.Atoi(context.Request.Form.Get("to")); err == nil {
				var count int
				if _, ok := result.(sortingDescInterface); ok {
					context.GetDB().New().Model(modelValue(result)).Count(&count)
					pos = count - pos + 1
				}

				if MoveTo(context.GetDB(), position, pos) == nil {
					var pos = position.GetPosition()
					if _, ok := result.(sortingDescInterface); ok {
						pos = count - pos + 1
					}

					context.Writer.Write([]byte(fmt.Sprintf("%d", pos)))
					return
				}
			}
		}
	}
	context.Writer.Write([]byte("Error"))
	context.Writer.WriteHeader(422)
}
开发者ID:pusparajm,项目名称:qor,代码行数:25,代码来源:controller.go

示例4: updatePosition

func updatePosition(context *admin.Context) {
	if result, err := context.FindOne(); err == nil {
		if position, ok := result.(sortingInterface); ok {
			if pos, err := strconv.Atoi(context.Request.Form.Get("to")); err == nil {
				var count int
				if _, ok := result.(sortingDescInterface); ok {
					var result = context.Resource.NewStruct()
					context.GetDB().Set("l10n:mode", "locale").Order("position DESC", true).First(result)
					count = result.(sortingInterface).GetPosition()
					pos = count - pos + 1
				}

				if MoveTo(context.GetDB(), position, pos) == nil {
					var pos = position.GetPosition()
					if _, ok := result.(sortingDescInterface); ok {
						pos = count - pos + 1
					}

					context.Writer.Write([]byte(fmt.Sprintf("%d", pos)))
					return
				}
			}
		}
	}
	context.Writer.WriteHeader(admin.HTTPUnprocessableEntity)
	context.Writer.Write([]byte("Error"))
}
开发者ID:NeoChow,项目名称:qor,代码行数:27,代码来源:controller.go

示例5: Preview

func (db *publishController) Preview(context *admin.Context) {
	drafts := make(map[*admin.Resource]interface{})
	draftDB := context.GetDB().Set("publish:draft_mode", true).Unscoped()
	for _, res := range context.Admin.GetResources() {
		results := res.NewSlice()
		if isPublishableModel(res.Value) {
			if draftDB.Where("publish_status = ?", DIRTY).Find(results).RowsAffected > 0 {
				drafts[res] = results
			}
		}
	}
	context.Execute("publish/drafts", drafts)
}
开发者ID:eryx,项目名称:qor,代码行数:13,代码来源:controller.go

示例6: Diff

func (db *publishController) Diff(context *admin.Context) {
	resourceID := strings.Split(context.Request.URL.Path, "/")[4]
	params := strings.Split(resourceID, "__")
	name, id := params[0], params[1]
	res := context.Admin.GetResource(name)

	draft := res.NewStruct()
	context.GetDB().Set("publish:draft_mode", true).Unscoped().First(draft, id)

	production := res.NewStruct()
	context.GetDB().Set("publish:draft_mode", false).Unscoped().First(production, id)

	results := map[string]interface{}{"Production": production, "Draft": draft, "Resource": res}

	fmt.Fprintf(context.Writer, string(context.Render("publish/diff", results)))
}
开发者ID:jmptrader,项目名称:qor,代码行数:16,代码来源:controller.go


注:本文中的github.com/qor/qor/admin.Context.GetDB方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。