Skip to content

Commit

Permalink
Refactor qor
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Oct 19, 2017
1 parent ffbd782 commit c495921
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 45 deletions.
7 changes: 3 additions & 4 deletions context.go
Expand Up @@ -13,15 +13,14 @@ type CurrentUser interface {

// Context qor context, which is used for many qor components, used to share information between them
type Context struct {
CurrentUser CurrentUser
Request *http.Request
Writer http.ResponseWriter
CurrentUser CurrentUser
Roles []string
ResourceID string
DB *gorm.DB
Config *Config
Errors

DB *gorm.DB
Config *Config
}

// Clone clone current context
Expand Down
44 changes: 22 additions & 22 deletions resource/crud.go
Expand Up @@ -11,7 +11,27 @@ import (
"github.com/qor/roles"
)

// ToPrimaryQueryParams to primary query params
// CallFindOne call find one method
func (res *Resource) CallFindOne(result interface{}, metaValues *MetaValues, context *qor.Context) error {
return res.FindOneHandler(result, metaValues, context)
}

// CallFindMany call find many method
func (res *Resource) CallFindMany(result interface{}, context *qor.Context) error {
return res.FindManyHandler(result, context)
}

// CallSave call save method
func (res *Resource) CallSave(result interface{}, context *qor.Context) error {
return res.SaveHandler(result, context)
}

// CallDelete call delete method
func (res *Resource) CallDelete(result interface{}, context *qor.Context) error {
return res.DeleteHandler(result, context)
}

// ToPrimaryQueryParams generate query params based on primary key, multiple primary value are linked with a comma
func (res *Resource) ToPrimaryQueryParams(primaryValue string, context *qor.Context) (string, []interface{}) {
if primaryValue != "" {
scope := context.GetDB().NewScope(res.Value)
Expand Down Expand Up @@ -44,7 +64,7 @@ func (res *Resource) ToPrimaryQueryParams(primaryValue string, context *qor.Cont
return "", []interface{}{}
}

// ToPrimaryQueryParamsFromMetaValue to primary query params from meta values
// ToPrimaryQueryParamsFromMetaValue generate query params based on MetaValues
func (res *Resource) ToPrimaryQueryParamsFromMetaValue(metaValues *MetaValues, context *qor.Context) (string, []interface{}) {
var (
sqls []string
Expand Down Expand Up @@ -126,23 +146,3 @@ func (res *Resource) deleteHandler(result interface{}, context *qor.Context) err
}
return roles.ErrPermissionDenied
}

// CallFindOne call find one method
func (res *Resource) CallFindOne(result interface{}, metaValues *MetaValues, context *qor.Context) error {
return res.FindOneHandler(result, metaValues, context)
}

// CallFindMany call find many method
func (res *Resource) CallFindMany(result interface{}, context *qor.Context) error {
return res.FindManyHandler(result, context)
}

// CallSave call save method
func (res *Resource) CallSave(result interface{}, context *qor.Context) error {
return res.SaveHandler(result, context)
}

// CallDelete call delete method
func (res *Resource) CallDelete(result interface{}, context *qor.Context) error {
return res.DeleteHandler(result, context)
}
3 changes: 1 addition & 2 deletions resource/meta_value.go
Expand Up @@ -28,9 +28,8 @@ type MetaValue struct {
Name string
Value interface{}
Index int
MetaValues *MetaValues
Meta Metaor
error error
MetaValues *MetaValues
}

func decodeMetaValuesToField(res Resourcer, field reflect.Value, metaValue *MetaValue, context *qor.Context) {
Expand Down
28 changes: 16 additions & 12 deletions resource/processor.go
Expand Up @@ -43,12 +43,14 @@ func (processor *processor) checkSkipLeft(errs ...error) bool {
return processor.SkipLeft
}

// Initialize initialize a processor
func (processor *processor) Initialize() error {
err := processor.Resource.CallFindOne(processor.Result, processor.MetaValues, processor.Context)
processor.checkSkipLeft(err)
return err
}

// Validate run validators
func (processor *processor) Validate() error {
var errors qor.Errors
if processor.checkSkipLeft() {
Expand Down Expand Up @@ -105,6 +107,20 @@ func (processor *processor) decode() (errors []error) {
return
}

// Start start processor
func (processor *processor) Start() error {
var errors qor.Errors
processor.Initialize()
if errors.AddError(processor.Validate()); !errors.HasError() {
errors.AddError(processor.Commit())
}
if errors.HasError() {
return errors
}
return nil
}

// Commit commit data into result
func (processor *processor) Commit() error {
var errors qor.Errors
errors.AddError(processor.decode()...)
Expand All @@ -122,15 +138,3 @@ func (processor *processor) Commit() error {
}
return errors
}

func (processor *processor) Start() error {
var errors qor.Errors
processor.Initialize()
if errors.AddError(processor.Validate()); !errors.HasError() {
errors.AddError(processor.Commit())
}
if errors.HasError() {
return errors
}
return nil
}
10 changes: 5 additions & 5 deletions resource/resource.go
Expand Up @@ -62,6 +62,11 @@ func New(value interface{}) *Resource {
return res
}

// GetResource return itself to match interface `Resourcer`
func (res *Resource) GetResource() *Resource {
return res
}

// SetPrimaryFields set primary fields
func (res *Resource) SetPrimaryFields(fields ...string) error {
scope := gorm.Scope{Value: res.Value}
Expand All @@ -86,11 +91,6 @@ func (res *Resource) SetPrimaryFields(fields ...string) error {
return fmt.Errorf("no valid primary field for resource %v", res.Name)
}

// GetResource return itself to match interface `Resourcer`
func (res *Resource) GetResource() *Resource {
return res
}

// Validator validator struct
type Validator struct {
Name string
Expand Down

0 comments on commit c495921

Please sign in to comment.