forked from mirrors/homebox
fix: export child relationships (#385)
* tidy * ensure export contains locations path * update pnpm lock file * fix swagger stuff * code gen * fix linter issue * fix reverse order bug in test
This commit is contained in:
parent
6a853c07a0
commit
ced5aef6d1
17 changed files with 1965 additions and 1686 deletions
|
@ -249,6 +249,56 @@ type TreeQuery struct {
|
|||
WithItems bool `json:"withItems" schema:"withItems"`
|
||||
}
|
||||
|
||||
type LocationPath struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func (lr *LocationRepository) PathForLoc(ctx context.Context, GID, locID uuid.UUID) ([]LocationPath, error) {
|
||||
query := `WITH RECURSIVE location_path AS (
|
||||
SELECT id, name, location_children
|
||||
FROM locations
|
||||
WHERE id = ? -- Replace ? with the ID of the item's location
|
||||
AND group_locations = ? -- Replace ? with the ID of the group
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT loc.id, loc.name, loc.location_children
|
||||
FROM locations loc
|
||||
JOIN location_path lp ON loc.id = lp.location_children
|
||||
)
|
||||
|
||||
SELECT id, name
|
||||
FROM location_path`
|
||||
|
||||
rows, err := lr.db.Sql().QueryContext(ctx, query, locID, GID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var locations []LocationPath
|
||||
|
||||
for rows.Next() {
|
||||
var location LocationPath
|
||||
if err := rows.Scan(&location.ID, &location.Name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
locations = append(locations, location)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Reverse the order of the locations so that the root is last
|
||||
for i := len(locations)/2 - 1; i >= 0; i-- {
|
||||
opp := len(locations) - 1 - i
|
||||
locations[i], locations[opp] = locations[opp], locations[i]
|
||||
}
|
||||
|
||||
return locations, nil
|
||||
}
|
||||
|
||||
func (lr *LocationRepository) Tree(ctx context.Context, GID uuid.UUID, tq TreeQuery) ([]TreeItem, error) {
|
||||
query := `
|
||||
WITH recursive location_tree(id, NAME, parent_id, level, node_type) AS
|
||||
|
|
|
@ -146,6 +146,34 @@ func TestItemRepository_TreeQuery(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestLocationRepository_PathForLoc(t *testing.T) {
|
||||
locs := useLocations(t, 3)
|
||||
|
||||
// Set relations 3 -> 2 -> 1
|
||||
for i := 0; i < 2; i++ {
|
||||
_, err := tRepos.Locations.UpdateByGroup(context.Background(), tGroup.ID, locs[i].ID, LocationUpdate{
|
||||
ID: locs[i].ID,
|
||||
ParentID: locs[i+1].ID,
|
||||
Name: locs[i].Name,
|
||||
Description: locs[i].Description,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
last := locs[0]
|
||||
|
||||
path, err := tRepos.Locations.PathForLoc(context.Background(), tGroup.ID, last.ID)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 3, len(path))
|
||||
|
||||
// Check path and order
|
||||
for i, loc := range path {
|
||||
assert.Equal(t, locs[2-i].ID, loc.ID)
|
||||
assert.Equal(t, locs[2-i].Name, loc.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertLocationsToTree(t *testing.T) {
|
||||
uuid1, uuid2, uuid3, uuid4 := uuid.New(), uuid.New(), uuid.New(), uuid.New()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue