forked from mirrors/homebox
feat: locations tree viewer (#248)
* location tree API * test fixes * initial tree location elements * locations tree page * update meta-data * code-gen * store item display preferences * introduce basic table/card view elements * codegen * set parent location during location creation * add item support for tree query * refactor tree view * wip: location selector improvements * type gen * rename items -> search * remove various log statements * fix markdown rendering for description * update location selectors * fix tests * fix currency tests * formatting
This commit is contained in:
parent
4d220cdd9c
commit
3d295b5132
33 changed files with 1119 additions and 79 deletions
|
@ -131,6 +131,7 @@ const (
|
|||
CurrencyDkk Currency = "dkk"
|
||||
CurrencyInr Currency = "inr"
|
||||
CurrencyRmb Currency = "rmb"
|
||||
CurrencyBgn Currency = "bgn"
|
||||
)
|
||||
|
||||
func (c Currency) String() string {
|
||||
|
@ -140,7 +141,7 @@ func (c Currency) String() string {
|
|||
// CurrencyValidator is a validator for the "currency" field enum values. It is called by the builders before save.
|
||||
func CurrencyValidator(c Currency) error {
|
||||
switch c {
|
||||
case CurrencyUsd, CurrencyEur, CurrencyGbp, CurrencyJpy, CurrencyZar, CurrencyAud, CurrencyNok, CurrencySek, CurrencyDkk, CurrencyInr, CurrencyRmb:
|
||||
case CurrencyUsd, CurrencyEur, CurrencyGbp, CurrencyJpy, CurrencyZar, CurrencyAud, CurrencyNok, CurrencySek, CurrencyDkk, CurrencyInr, CurrencyRmb, CurrencyBgn:
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("group: invalid enum value for currency field: %q", c)
|
||||
|
|
|
@ -116,7 +116,7 @@ var (
|
|||
{Name: "created_at", Type: field.TypeTime},
|
||||
{Name: "updated_at", Type: field.TypeTime},
|
||||
{Name: "name", Type: field.TypeString, Size: 255},
|
||||
{Name: "currency", Type: field.TypeEnum, Enums: []string{"usd", "eur", "gbp", "jpy", "zar", "aud", "nok", "sek", "dkk", "inr", "rmb"}, Default: "usd"},
|
||||
{Name: "currency", Type: field.TypeEnum, Enums: []string{"usd", "eur", "gbp", "jpy", "zar", "aud", "nok", "sek", "dkk", "inr", "rmb", "bgn"}, Default: "usd"},
|
||||
}
|
||||
// GroupsTable holds the schema information for the "groups" table.
|
||||
GroupsTable = &schema.Table{
|
||||
|
|
|
@ -27,7 +27,7 @@ func (Group) Fields() []ent.Field {
|
|||
NotEmpty(),
|
||||
field.Enum("currency").
|
||||
Default("usd").
|
||||
Values("usd", "eur", "gbp", "jpy", "zar", "aud", "nok", "sek", "dkk", "inr", "rmb"),
|
||||
Values("usd", "eur", "gbp", "jpy", "zar", "aud", "nok", "sek", "dkk", "inr", "rmb", "bgn"),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -356,7 +356,6 @@ func (e *ItemsRepository) QueryByGroup(ctx context.Context, gid uuid.UUID, q Ite
|
|||
|
||||
}
|
||||
|
||||
|
||||
// QueryByAssetID returns items by asset ID. If the item does not exist, an error is returned.
|
||||
func (e *ItemsRepository) QueryByAssetID(ctx context.Context, gid uuid.UUID, assetID AssetID, page int, pageSize int) (PaginationResult[ItemSummary], error) {
|
||||
qb := e.db.Item.Query().Where(
|
||||
|
@ -372,7 +371,7 @@ func (e *ItemsRepository) QueryByAssetID(ctx context.Context, gid uuid.UUID, ass
|
|||
pageSize = -1
|
||||
}
|
||||
|
||||
items, err := mapItemsSummaryErr(
|
||||
items, err := mapItemsSummaryErr(
|
||||
qb.Order(ent.Asc(item.FieldName)).
|
||||
WithLabel().
|
||||
WithLocation().
|
||||
|
|
|
@ -36,6 +36,8 @@ func useItems(t *testing.T, len int) []ItemOut {
|
|||
for _, item := range items {
|
||||
_ = tRepos.Items.Delete(context.Background(), item.ID)
|
||||
}
|
||||
|
||||
_ = tRepos.Locations.Delete(context.Background(), location.ID)
|
||||
})
|
||||
|
||||
return items
|
||||
|
|
|
@ -19,8 +19,9 @@ type LocationRepository struct {
|
|||
|
||||
type (
|
||||
LocationCreate struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Name string `json:"name"`
|
||||
ParentID uuid.UUID `json:"parentId" extensions:"x-nullable"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
LocationUpdate struct {
|
||||
|
@ -152,7 +153,9 @@ func (r *LocationRepository) getOne(ctx context.Context, where ...predicate.Loca
|
|||
Where(where...).
|
||||
WithGroup().
|
||||
WithItems(func(iq *ent.ItemQuery) {
|
||||
iq.Where(item.Archived(false)).WithLabel()
|
||||
iq.Where(item.Archived(false)).
|
||||
Order(ent.Asc(item.FieldName)).
|
||||
WithLabel()
|
||||
}).
|
||||
WithParent().
|
||||
WithChildren().
|
||||
|
@ -168,11 +171,16 @@ func (r *LocationRepository) GetOneByGroup(ctx context.Context, GID, ID uuid.UUI
|
|||
}
|
||||
|
||||
func (r *LocationRepository) Create(ctx context.Context, GID uuid.UUID, data LocationCreate) (LocationOut, error) {
|
||||
location, err := r.db.Location.Create().
|
||||
q := r.db.Location.Create().
|
||||
SetName(data.Name).
|
||||
SetDescription(data.Description).
|
||||
SetGroupID(GID).
|
||||
Save(ctx)
|
||||
SetGroupID(GID)
|
||||
|
||||
if data.ParentID != uuid.Nil {
|
||||
q.SetParentID(data.ParentID)
|
||||
}
|
||||
|
||||
location, err := q.Save(ctx)
|
||||
|
||||
if err != nil {
|
||||
return LocationOut{}, err
|
||||
|
@ -218,3 +226,129 @@ func (r *LocationRepository) DeleteByGroup(ctx context.Context, GID, ID uuid.UUI
|
|||
_, err := r.db.Location.Delete().Where(location.ID(ID), location.HasGroupWith(group.ID(GID))).Exec(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
type TreeItem struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Children []*TreeItem `json:"children"`
|
||||
}
|
||||
|
||||
type FlatTreeItem struct {
|
||||
ID uuid.UUID
|
||||
Name string
|
||||
Type string
|
||||
ParentID uuid.UUID
|
||||
Level int
|
||||
}
|
||||
|
||||
type TreeQuery struct {
|
||||
WithItems bool `json:"withItems"`
|
||||
}
|
||||
|
||||
func (lr *LocationRepository) Tree(ctx context.Context, GID uuid.UUID, tq TreeQuery) ([]TreeItem, error) {
|
||||
query := `
|
||||
WITH recursive location_tree(id, NAME, location_children, level, node_type) AS
|
||||
(
|
||||
SELECT id,
|
||||
NAME,
|
||||
location_children,
|
||||
0 AS level,
|
||||
'location' AS node_type
|
||||
FROM locations
|
||||
WHERE location_children IS NULL
|
||||
AND group_locations = ?
|
||||
|
||||
UNION ALL
|
||||
SELECT c.id,
|
||||
c.NAME,
|
||||
c.location_children,
|
||||
level + 1,
|
||||
'location' AS node_type
|
||||
FROM locations c
|
||||
JOIN location_tree p
|
||||
ON c.location_children = p.id
|
||||
WHERE level < 10 -- prevent infinite loop & excessive recursion
|
||||
|
||||
{{ WITH_ITEMS }}
|
||||
)
|
||||
SELECT id,
|
||||
NAME,
|
||||
level,
|
||||
location_children,
|
||||
node_type
|
||||
FROM location_tree
|
||||
ORDER BY level,
|
||||
node_type DESC, -- sort locations before items
|
||||
NAME;`
|
||||
|
||||
if tq.WithItems {
|
||||
itemQuery := `
|
||||
UNION ALL
|
||||
SELECT i.id,
|
||||
i.name,
|
||||
location_items as location_children,
|
||||
level + 1,
|
||||
'item' AS node_type
|
||||
FROM items i
|
||||
JOIN location_tree p
|
||||
ON i.location_items = p.id
|
||||
WHERE level < 10 -- prevent infinite loop & excessive recursion`
|
||||
query = strings.ReplaceAll(query, "{{ WITH_ITEMS }}", itemQuery)
|
||||
} else {
|
||||
query = strings.ReplaceAll(query, "{{ WITH_ITEMS }}", "")
|
||||
}
|
||||
|
||||
rows, err := lr.db.Sql().QueryContext(ctx, query, GID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var locations []FlatTreeItem
|
||||
for rows.Next() {
|
||||
var location FlatTreeItem
|
||||
if err := rows.Scan(&location.ID, &location.Name, &location.Level, &location.ParentID, &location.Type); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
locations = append(locations, location)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ConvertLocationsToTree(locations), nil
|
||||
}
|
||||
|
||||
func ConvertLocationsToTree(locations []FlatTreeItem) []TreeItem {
|
||||
var locationMap = make(map[uuid.UUID]*TreeItem, len(locations))
|
||||
|
||||
var rootIds []uuid.UUID
|
||||
|
||||
for _, location := range locations {
|
||||
loc := &TreeItem{
|
||||
ID: location.ID,
|
||||
Name: location.Name,
|
||||
Type: location.Type,
|
||||
Children: []*TreeItem{},
|
||||
}
|
||||
|
||||
locationMap[location.ID] = loc
|
||||
if location.ParentID != uuid.Nil {
|
||||
parent, ok := locationMap[location.ParentID]
|
||||
if ok {
|
||||
parent.Children = append(parent.Children, loc)
|
||||
}
|
||||
} else {
|
||||
rootIds = append(rootIds, location.ID)
|
||||
}
|
||||
}
|
||||
|
||||
roots := make([]TreeItem, 0, len(rootIds))
|
||||
for _, id := range rootIds {
|
||||
roots = append(roots, *locationMap[id])
|
||||
}
|
||||
|
||||
return roots
|
||||
}
|
||||
|
|
|
@ -2,8 +2,11 @@ package repo
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/hay-kot/homebox/backend/internal/data/ent"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
@ -14,6 +17,30 @@ func locationFactory() LocationCreate {
|
|||
}
|
||||
}
|
||||
|
||||
func useLocations(t *testing.T, len int) []LocationOut {
|
||||
t.Helper()
|
||||
|
||||
out := make([]LocationOut, len)
|
||||
|
||||
for i := 0; i < len; i++ {
|
||||
loc, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory())
|
||||
assert.NoError(t, err)
|
||||
out[i] = loc
|
||||
}
|
||||
|
||||
t.Cleanup(func() {
|
||||
for _, loc := range out {
|
||||
err := tRepos.Locations.Delete(context.Background(), loc.ID)
|
||||
|
||||
if err != nil {
|
||||
assert.True(t, ent.IsNotFound(err))
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func TestLocationRepository_Get(t *testing.T) {
|
||||
loc, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory())
|
||||
assert.NoError(t, err)
|
||||
|
@ -29,13 +56,9 @@ func TestLocationRepository_Get(t *testing.T) {
|
|||
|
||||
func TestLocationRepositoryGetAllWithCount(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
result, err := tRepos.Locations.Create(ctx, tGroup.ID, LocationCreate{
|
||||
Name: fk.Str(10),
|
||||
Description: fk.Str(100),
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
result := useLocations(t, 1)[0]
|
||||
|
||||
_, err = tRepos.Items.Create(ctx, tGroup.ID, ItemCreate{
|
||||
_, err := tRepos.Items.Create(ctx, tGroup.ID, ItemCreate{
|
||||
Name: fk.Str(10),
|
||||
Description: fk.Str(100),
|
||||
LocationID: result.ID,
|
||||
|
@ -55,8 +78,7 @@ func TestLocationRepositoryGetAllWithCount(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLocationRepository_Create(t *testing.T) {
|
||||
loc, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory())
|
||||
assert.NoError(t, err)
|
||||
loc := useLocations(t, 1)[0]
|
||||
|
||||
// Get by ID
|
||||
foundLoc, err := tRepos.Locations.Get(context.Background(), loc.ID)
|
||||
|
@ -68,8 +90,7 @@ func TestLocationRepository_Create(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLocationRepository_Update(t *testing.T) {
|
||||
loc, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory())
|
||||
assert.NoError(t, err)
|
||||
loc := useLocations(t, 1)[0]
|
||||
|
||||
updateData := LocationUpdate{
|
||||
ID: loc.ID,
|
||||
|
@ -92,12 +113,154 @@ func TestLocationRepository_Update(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLocationRepository_Delete(t *testing.T) {
|
||||
loc, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory())
|
||||
assert.NoError(t, err)
|
||||
loc := useLocations(t, 1)[0]
|
||||
|
||||
err = tRepos.Locations.Delete(context.Background(), loc.ID)
|
||||
err := tRepos.Locations.Delete(context.Background(), loc.ID)
|
||||
assert.NoError(t, err)
|
||||
|
||||
_, err = tRepos.Locations.Get(context.Background(), loc.ID)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestItemRepository_TreeQuery(t *testing.T) {
|
||||
locs := useLocations(t, 3)
|
||||
|
||||
// Set relations
|
||||
_, err := tRepos.Locations.UpdateOneByGroup(context.Background(), tGroup.ID, locs[0].ID, LocationUpdate{
|
||||
ID: locs[0].ID,
|
||||
ParentID: locs[1].ID,
|
||||
Name: locs[0].Name,
|
||||
Description: locs[0].Description,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
locations, err := tRepos.Locations.Tree(context.Background(), tGroup.ID, TreeQuery{WithItems: true})
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, 2, len(locations))
|
||||
|
||||
// Check roots
|
||||
for _, loc := range locations {
|
||||
if loc.ID == locs[1].ID {
|
||||
assert.Equal(t, 1, len(loc.Children))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertLocationsToTree(t *testing.T) {
|
||||
uuid1, uuid2, uuid3, uuid4 := uuid.New(), uuid.New(), uuid.New(), uuid.New()
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
locations []FlatTreeItem
|
||||
expected []TreeItem
|
||||
}{
|
||||
{
|
||||
name: "Convert locations to tree",
|
||||
locations: []FlatTreeItem{
|
||||
{
|
||||
ID: uuid1,
|
||||
Name: "Root1",
|
||||
ParentID: uuid.Nil,
|
||||
Level: 0,
|
||||
},
|
||||
{
|
||||
ID: uuid2,
|
||||
Name: "Child1",
|
||||
ParentID: uuid1,
|
||||
Level: 1,
|
||||
},
|
||||
{
|
||||
ID: uuid3,
|
||||
Name: "Child2",
|
||||
ParentID: uuid1,
|
||||
Level: 1,
|
||||
},
|
||||
},
|
||||
expected: []TreeItem{
|
||||
{
|
||||
ID: uuid1,
|
||||
Name: "Root1",
|
||||
Children: []*TreeItem{
|
||||
{
|
||||
ID: uuid2,
|
||||
Name: "Child1",
|
||||
Children: []*TreeItem{},
|
||||
},
|
||||
{
|
||||
ID: uuid3,
|
||||
Name: "Child2",
|
||||
Children: []*TreeItem{},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Convert locations to tree with deeply nested children",
|
||||
locations: []FlatTreeItem{
|
||||
{
|
||||
ID: uuid1,
|
||||
Name: "Root1",
|
||||
ParentID: uuid.Nil,
|
||||
Level: 0,
|
||||
},
|
||||
{
|
||||
ID: uuid2,
|
||||
Name: "Child1",
|
||||
ParentID: uuid1,
|
||||
Level: 1,
|
||||
},
|
||||
{
|
||||
ID: uuid3,
|
||||
Name: "Child2",
|
||||
ParentID: uuid2,
|
||||
Level: 2,
|
||||
},
|
||||
{
|
||||
ID: uuid4,
|
||||
Name: "Child3",
|
||||
ParentID: uuid3,
|
||||
Level: 3,
|
||||
},
|
||||
},
|
||||
expected: []TreeItem{
|
||||
{
|
||||
ID: uuid1,
|
||||
Name: "Root1",
|
||||
Children: []*TreeItem{
|
||||
{
|
||||
ID: uuid2,
|
||||
Name: "Child1",
|
||||
Children: []*TreeItem{
|
||||
{
|
||||
ID: uuid3,
|
||||
Name: "Child2",
|
||||
Children: []*TreeItem{
|
||||
{
|
||||
ID: uuid4,
|
||||
Name: "Child3",
|
||||
Children: []*TreeItem{},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := ConvertLocationsToTree(tc.locations)
|
||||
|
||||
// Compare JSON strings
|
||||
expected, _ := json.Marshal(tc.expected)
|
||||
got, _ := json.Marshal(result)
|
||||
assert.Equal(t, string(expected), string(got))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue