Exemplo n.º 1
0
func (r Repository) All(c decode.Criteria) (repository.Result, error) {
	var models []Model

	binds := map[string]string{
		"created_at": "to_timestamp(?)",
		"updated_at": "to_timestamp(?)",
	}

	builders := map[string]func(decode.Filter) (string, string, []interface{}){
		"After":  repository.NewAfterCursor(rockTable, c),
		"Before": repository.NewBeforeCursor(rockTable, c),
	}

	opts := sql.SelectOpts{
		Table:        rockTable,
		C:            c,
		Binds:        binds,
		Builders:     builders,
		Db:           r.Db,
		DefaultOrder: "ID DESC",
		DefaultLimit: 100,
	}

	sqlBuilder := sql.Builder{}
	q, args := sqlBuilder.Select(opts)

	err := r.Db.Select(&models, q, args...)

	switch err {
	case dbSql.ErrNoRows:
		err = repository.NoRows{Msg: err.Error()}
	}

	b := make([]interface{}, len(models))
	for i := range models {
		b[i] = models[i]
	}

	var nullCount null.Int
	if err == nil && c.Pagination == true {
		var totalCount int
		cq, args := sqlBuilder.SelectCount(opts)
		err = r.Db.Get(&totalCount, cq, args...)

		nullCount = null.IntFrom(int64(totalCount))
	}

	result := repository.Result{
		Data:       b,
		TotalCount: nullCount,
	}

	return result, err
}
Exemplo n.º 2
0
func (r Repository) All(c decode.Criteria) (repository.Result, error) {
	var models []Model

	binds := map[string]string{
		"created_at": "to_timestamp(?)",
		"updated_at": "to_timestamp(?)",
	}

	builders := map[string]func(decode.Filter) (string, string, []interface{}){
		"Bounds": func(filter decode.Filter) (string, string, []interface{}) {
			innerJoin := fmt.Sprintf(`rocks ON rocks.id=%s.rock_id`, climbTable)
			where := `rocks.latitude > ? AND rocks.longitude > ? AND rocks.latitude < ? AND rocks.longitude < ?`
			strs := strings.Split(filter.Value, ",")

			var strArgs []interface{}
			for _, str := range strs {
				strArgs = append(strArgs, str)
			}

			return innerJoin, where, strArgs
		},
		"After":  repository.NewAfterCursor(climbTable, c),
		"Before": repository.NewBeforeCursor(climbTable, c),
	}

	opts := sql.SelectOpts{
		Table:        climbTable,
		C:            c,
		Binds:        binds,
		Builders:     builders,
		Db:           r.Db,
		DefaultOrder: "ID DESC",
		DefaultLimit: 100,
	}

	sqlBuilder := sql.Builder{}
	q, args := sqlBuilder.Select(opts)

	err := r.Db.Select(&models, q, args...)

	switch err {
	case dbSql.ErrNoRows:
		err = repository.NoRows{Msg: err.Error()}
	}

	b := make([]interface{}, len(models))
	for i := range models {
		b[i] = models[i]
	}

	var nullCount null.Int
	if err == nil && c.Pagination == true {
		var totalCount int
		cq, args := sqlBuilder.SelectCount(opts)
		err = r.Db.Get(&totalCount, cq, args...)

		nullCount = null.IntFrom(int64(totalCount))
	}

	result := repository.Result{
		Data:       b,
		TotalCount: nullCount,
	}

	return result, err
}