예제 #1
0
파일: utils.go 프로젝트: optimuse/zest
func FilterIfOwnerRelations(r *http.Request, filter *usecases.Filter) *usecases.Filter {
	ownerRelationsCtx := context.Get(r, "ownerRelations")
	if ownerRelationsCtx != nil {
		currentSession := context.Get(r, "currentSession").(domain.Session)

		idKey := "accountId"

		if context.Get(r, "resource").(string) == "accounts" {
			idKey = "id"
		}

		if filter == nil {
			filter = &usecases.Filter{
				Where: map[string]interface{}{idKey: currentSession.AccountID},
			}
		} else {
			if filter.Where == nil {
				filter.Where = map[string]interface{}{idKey: currentSession.AccountID}
			} else {
				filter.Where[idKey] = currentSession.AccountID
			}
		}
	}

	return filter
}
예제 #2
0
파일: utils.go 프로젝트: optimuse/zest
func FilterIfLastRessource(r *http.Request, filter *usecases.Filter) *usecases.Filter {
	lastRessourceCtx := context.Get(r, "lastRessource")

	if lastRessourceCtx != nil {
		lastRessource := lastRessourceCtx.(*Ressource)

		if filter == nil {
			filter = &usecases.Filter{
				Where: map[string]interface{}{lastRessource.IDKey: lastRessource.ID},
			}
		} else {
			if filter.Where == nil {
				filter.Where = map[string]interface{}{lastRessource.IDKey: lastRessource.ID}
			} else {
				filter.Where[lastRessource.IDKey] = lastRessource.ID
			}
		}
	}

	return filter
}
예제 #3
0
파일: gorm_store.go 프로젝트: optimuse/zest
func processFilter(filter *usecases.Filter) (*interfaces.GormFilter, error) {
	fields := filter.Fields
	dbNamedFields := make([]string, len(fields))

	for i, field := range fields {
		dbNamedFields[i] = utils.ToDBName(field)
	}

	if filter.Order != "" {
		order := strings.ToLower(filter.Order)
		matched, err := regexp.MatchString("\\A\\w+ (asc|desc)\\z", order)
		if err != nil || !matched {
			return nil, errors.New("invalid order filter")
		}

		split := strings.Split(filter.Order, " ")
		filter.Order = utils.ToDBName(split[0]) + " " + split[1]
	}

	processedFilter := &interfaces.GormFilter{Fields: dbNamedFields, Limit: filter.Limit, Offset: filter.Offset, Order: filter.Order}

	buffer := &bytes.Buffer{}
	err := processCondition(buffer, "", andSql, "", filter.Where)
	if err != nil {
		return nil, err
	}
	processedFilter.Where = buffer.String()

	gormIncludes, err := processInclude(filter.Include)
	if err != nil {
		return nil, err
	}
	processedFilter.Include = gormIncludes

	return processedFilter, nil
}