forked from mirrors/homebox
fix: conditionally filter parent locations (#133)
This commit is contained in:
parent
fbcbde836a
commit
8e1947d971
18 changed files with 135 additions and 67 deletions
|
@ -2,6 +2,7 @@ package repo
|
|||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
@ -90,8 +91,12 @@ func mapLocationOut(location *ent.Location) LocationOut {
|
|||
}
|
||||
}
|
||||
|
||||
type LocationQuery struct {
|
||||
FilterChildren bool `json:"filterChildren"`
|
||||
}
|
||||
|
||||
// GetALlWithCount returns all locations with item count field populated
|
||||
func (r *LocationRepository) GetAll(ctx context.Context, groupId uuid.UUID) ([]LocationOutCount, error) {
|
||||
func (r *LocationRepository) GetAll(ctx context.Context, GID uuid.UUID, filter LocationQuery) ([]LocationOutCount, error) {
|
||||
query := `--sql
|
||||
SELECT
|
||||
id,
|
||||
|
@ -111,13 +116,18 @@ func (r *LocationRepository) GetAll(ctx context.Context, groupId uuid.UUID) ([]L
|
|||
FROM
|
||||
locations
|
||||
WHERE
|
||||
locations.group_locations = ?
|
||||
AND locations.location_children IS NULL
|
||||
locations.group_locations = ? {{ FILTER_CHILDREN }}
|
||||
ORDER BY
|
||||
locations.name ASC
|
||||
`
|
||||
|
||||
rows, err := r.db.Sql().QueryContext(ctx, query, groupId)
|
||||
if filter.FilterChildren {
|
||||
query = strings.Replace(query, "{{ FILTER_CHILDREN }}", "AND locations.location_children IS NULL", 1)
|
||||
} else {
|
||||
query = strings.Replace(query, "{{ FILTER_CHILDREN }}", "", 1)
|
||||
}
|
||||
|
||||
rows, err := r.db.Sql().QueryContext(ctx, query, GID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ func TestLocationRepositoryGetAllWithCount(t *testing.T) {
|
|||
|
||||
assert.NoError(t, err)
|
||||
|
||||
results, err := tRepos.Locations.GetAll(context.Background(), tGroup.ID)
|
||||
results, err := tRepos.Locations.GetAll(context.Background(), tGroup.ID, LocationQuery{})
|
||||
assert.NoError(t, err)
|
||||
|
||||
for _, loc := range results {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue