コード例 #1
0
ファイル: controller.go プロジェクト: kennylixi/qor
func (ac *controller) Update(context *Context) {
	if context.checkResourcePermission(roles.Update) {
		res := context.Resource
		if result, err := context.FindOne(); err == nil {
			if errs := res.Decode(context.Context, result); len(errs) == 0 {
				err := res.CallSaver(result, context.Context)
				responder.With("html", func() {
					if err == nil {
						context.FlashNow(context.dt("resource_successfully_updated", "{{.Name}} was successfully updated", res), "success")
					} else {
						context.Writer.WriteHeader(HTTPUnprocessableEntity)
					}
					context.Execute("show", result)
				}).With("json", func() {
					if err == nil {
						js, _ := json.Marshal(context.Resource.convertObjectToMap(context, result, "show"))
						context.Writer.Write(js)
					} else {
						context.Writer.WriteHeader(HTTPUnprocessableEntity)
						data, _ := json.Marshal(map[string]interface{}{"errors": validations.GetErrors(context.GetDB())})
						context.Writer.Write(data)
					}
				}).Respond(context.Writer, context.Request)
			}
		}
	}
}
コード例 #2
0
ファイル: controller.go プロジェクト: kennylixi/qor
func (ac *controller) Create(context *Context) {
	if context.checkResourcePermission(roles.Create) {
		res := context.Resource

		result := res.NewStruct()
		if errs := res.Decode(context.Context, result); len(errs) == 0 {
			err := res.CallSaver(result, context.Context)
			responder.With("html", func() {
				if err == nil {
					context.Flash(context.dt("resource_successfully_created", "{{.Name}} was successfully created", res), "success")
					primaryKey := fmt.Sprintf("%v", context.GetDB().NewScope(result).PrimaryKeyValue())
					http.Redirect(context.Writer, context.Request, path.Join(context.Request.URL.Path, primaryKey), http.StatusFound)
				} else {
					context.Writer.WriteHeader(HTTPUnprocessableEntity)
					context.Execute("new", result)
				}
			}).With("json", func() {
				if err == nil {
					js, _ := json.Marshal(context.Resource.convertObjectToMap(context, result, "show"))
					context.Writer.Write(js)
				} else {
					context.Writer.WriteHeader(HTTPUnprocessableEntity)
					data, _ := json.Marshal(map[string]interface{}{"errors": validations.GetErrors(context.GetDB())})
					context.Writer.Write(data)
				}
			}).Respond(context.Writer, context.Request)
		}
	}
}
コード例 #3
0
ファイル: func_map.go プロジェクト: kennylixi/qor
func (context *Context) FuncMap() template.FuncMap {
	funcMap := template.FuncMap{
		"current_user":         func() qor.CurrentUser { return context.CurrentUser },
		"get_resource":         context.GetResource,
		"new_resource_context": context.NewResourceContext,
		"is_new_record":        context.isNewRecord,
		"is_included":          context.isIncluded,
		"primary_key_of":       context.primaryKeyOf,
		"value_of":             context.ValueOf,

		"get_menus":             context.getMenus,
		"get_scopes":            context.GetScopes,
		"get_validation_errors": func() map[string][]string { return validations.GetErrors(context.GetDB()) },

		"escape":    html.EscapeString,
		"raw":       func(str string) template.HTML { return template.HTML(str) },
		"equal":     equal,
		"stringify": utils.Stringify,
		"plural":    inflection.Plural,
		"singular":  inflection.Singular,

		"render":                 context.Render,
		"render_form":            context.RenderForm,
		"render_index_meta":      context.renderIndexMeta,
		"url_for":                context.UrlFor,
		"link_to":                context.LinkTo,
		"patch_current_url":      context.patchCurrentURL,
		"patch_url":              context.patchURL,
		"new_resource_path":      context.newResourcePath,
		"qor_theme_class":        context.themesClass,
		"javascript_tag":         context.javaScriptTag,
		"stylesheet_tag":         context.styleSheetTag,
		"load_theme_stylesheets": context.loadThemeStyleSheets,
		"load_theme_javascripts": context.loadThemeJavaScripts,
		"load_index_actions":     context.loadIndexActions,
		"load_show_actions":      context.loadShowActions,
		"load_new_actions":       context.loadNewActions,
		"pagination":             context.Pagination,

		"all_metas":        context.allMetas,
		"index_metas":      context.indexMetas,
		"edit_metas":       context.editMetas,
		"show_metas":       context.showMetas,
		"new_metas":        context.newMetas,
		"is_sortable_meta": context.isSortableMeta,

		"has_create_permission": context.hasCreatePermission,
		"has_read_permission":   context.hasReadPermission,
		"has_update_permission": context.hasUpdatePermission,
		"has_delete_permission": context.hasDeletePermission,

		"logout_url": context.logoutURL,

		"marshal": func(v interface{}) template.JS {
			a, _ := json.Marshal(v)
			return template.JS(a)
		},

		"t":       context.T,
		"dt":      context.dt,
		"rt":      context.rt,
		"flashes": context.GetFlashes,
	}

	for key, value := range context.Admin.funcMaps {
		funcMap[key] = value
	}
	return funcMap
}