fix: conditionally filter parent locations (#133)

This commit is contained in:
Hayden 2022-11-02 11:54:43 -08:00 committed by GitHub
parent fbcbde836a
commit 8e1947d971
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 135 additions and 67 deletions

View file

@ -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
}

View file

@ -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 {