// DeleteRelatedLis deletes all related data func (li LineItem) DeleteRelatedLis(id float64, model *Model) { _, ok := model.Get(id) if !ok { return } for _, rotuer := range model.router.apiFaker.Routers { var isRelatedRouter bool var foreign_key = fmt.Sprintf("%s_id", inflection.Singular(model.Name)) for _, column := range rotuer.Model.Columns { if column.Name == foreign_key { isRelatedRouter = true break } } if isRelatedRouter { for _, li := range rotuer.Model.ToLineItems() { key, ok := li.Get(foreign_key) if ok && key == id { rotuer.Model.Delete(li.ID()) } } } } }
func (g *Generator) getModelOrCreate(modelName string) *modelInfo { singularName := inflection.Singular(modelName) mInfo, modelExists := g.modelsInfo[singularName] if !modelExists { mInfo = newModelInfo(singularName) g.modelsInfo[singularName] = mInfo } return mInfo }
// InsertRelatedData allocates and returns a new LineItem, // it will has all data of the caller LineItem, // it will insert all related data if the given Model's has any Column named xxx_id func (li LineItem) InsertRelatedData(model *Model) LineItem { // has one relationship newLi := NewLineItemWithMap(li.ToMap()) singularName := inflection.Singular(model.Name) // HasOne for _, resName := range model.HasOne { resStruct := map[string]interface{}{} if resRouter, ok := model.router.apiFaker.Routers[inflection.Plural(resName)]; ok { resLis := resRouter.Model.ToLineItems() sort.Sort(resLis) for _, resLi := range resLis { curId, ok := resLi.Get(fmt.Sprintf("%s_id", singularName)) if ok && curId == newLi.Id() { resStruct = resLi.ToMap() break } } } if len(resStruct) > 0 { newLi.Set(inflection.Singular(resName), resStruct) } } // HasMany for _, resName := range model.HasMany { resSlice := []interface{}{} if resRouter, ok := model.router.apiFaker.Routers[resName]; ok { resLis := resRouter.Model.ToLineItems() sort.Sort(resLis) for _, resLi := range resLis { curId, ok := resLi.Get(fmt.Sprintf("%s_id", singularName)) if ok && curId == newLi.Id() { resSlice = append(resSlice, resLi.ToMap()) } } } if len(resSlice) > 0 { newLi.Set(resName, resSlice) } } return newLi }
func (p *property) extractType(attributes propertyAttributes, val interface{}) { value := reflect.TypeOf(val) switch value.Kind() { case reflect.Map: // The value is an object, the type name is the property name p.Type = inflection.Singular(p.Name) case reflect.Array, reflect.Slice: p.IsArray = true arrayVal := reflect.ValueOf(val) if arrayVal.Len() > 0 { p.extractType(attributes, arrayVal.Index(0).Interface()) } default: p.Type = value.String() } if attributes.forcedType != "" { p.Type = attributes.forcedType } p.TypeLabel = p.Type p.IsMap = attributes.forceAsMap }
// ParamIDName return param name for primary key like :product_id func (res Resource) ParamIDName() string { return fmt.Sprintf(":%v_id", inflection.Singular(res.ToParam())) }