Exemple #1
0
// 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())
				}
			}
		}
	}
}
Exemple #2
0
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
}
Exemple #3
0
// 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
}
Exemple #4
0
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
}
Exemple #5
0
// ParamIDName return param name for primary key like :product_id
func (res Resource) ParamIDName() string {
	return fmt.Sprintf(":%v_id", inflection.Singular(res.ToParam()))
}