mirror of
https://github.com/hay-kot/homebox.git
synced 2025-08-04 16:50:27 +00:00
add item support for tree query
This commit is contained in:
parent
66ea327595
commit
a2c3f9defc
6 changed files with 80 additions and 8 deletions
|
@ -15,6 +15,7 @@ import (
|
||||||
// @Summary Get All Locations
|
// @Summary Get All Locations
|
||||||
// @Tags Locations
|
// @Tags Locations
|
||||||
// @Produce json
|
// @Produce json
|
||||||
|
// @Param withItems query bool false "include items in response tree"
|
||||||
// @Success 200 {object} server.Results{items=[]repo.TreeItem}
|
// @Success 200 {object} server.Results{items=[]repo.TreeItem}
|
||||||
// @Router /v1/locations/tree [GET]
|
// @Router /v1/locations/tree [GET]
|
||||||
// @Security Bearer
|
// @Security Bearer
|
||||||
|
@ -22,7 +23,18 @@ func (ctrl *V1Controller) HandleLocationTreeQuery() server.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) error {
|
return func(w http.ResponseWriter, r *http.Request) error {
|
||||||
user := services.UseUserCtx(r.Context())
|
user := services.UseUserCtx(r.Context())
|
||||||
|
|
||||||
locTree, err := ctrl.repo.Locations.Tree(r.Context(), user.GroupID)
|
q := r.URL.Query()
|
||||||
|
|
||||||
|
withItems := queryBool(q.Get("withItems"))
|
||||||
|
|
||||||
|
locTree, err := ctrl.repo.Locations.Tree(
|
||||||
|
r.Context(),
|
||||||
|
user.GroupID,
|
||||||
|
repo.TreeQuery{
|
||||||
|
WithItems: withItems,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msg("failed to get locations tree")
|
log.Err(err).Msg("failed to get locations tree")
|
||||||
return validate.NewRequestError(err, http.StatusInternalServerError)
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
||||||
|
|
|
@ -1059,6 +1059,14 @@ const docTemplate = `{
|
||||||
"Locations"
|
"Locations"
|
||||||
],
|
],
|
||||||
"summary": "Get All Locations",
|
"summary": "Get All Locations",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"type": "boolean",
|
||||||
|
"description": "include items in response tree",
|
||||||
|
"name": "withItems",
|
||||||
|
"in": "query"
|
||||||
|
}
|
||||||
|
],
|
||||||
"responses": {
|
"responses": {
|
||||||
"200": {
|
"200": {
|
||||||
"description": "OK",
|
"description": "OK",
|
||||||
|
@ -2173,6 +2181,9 @@ const docTemplate = `{
|
||||||
},
|
},
|
||||||
"name": {
|
"name": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
|
},
|
||||||
|
"type": {
|
||||||
|
"type": "string"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -1051,6 +1051,14 @@
|
||||||
"Locations"
|
"Locations"
|
||||||
],
|
],
|
||||||
"summary": "Get All Locations",
|
"summary": "Get All Locations",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"type": "boolean",
|
||||||
|
"description": "include items in response tree",
|
||||||
|
"name": "withItems",
|
||||||
|
"in": "query"
|
||||||
|
}
|
||||||
|
],
|
||||||
"responses": {
|
"responses": {
|
||||||
"200": {
|
"200": {
|
||||||
"description": "OK",
|
"description": "OK",
|
||||||
|
@ -2165,6 +2173,9 @@
|
||||||
},
|
},
|
||||||
"name": {
|
"name": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
|
},
|
||||||
|
"type": {
|
||||||
|
"type": "string"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -472,6 +472,8 @@ definitions:
|
||||||
type: string
|
type: string
|
||||||
name:
|
name:
|
||||||
type: string
|
type: string
|
||||||
|
type:
|
||||||
|
type: string
|
||||||
type: object
|
type: object
|
||||||
repo.UserOut:
|
repo.UserOut:
|
||||||
properties:
|
properties:
|
||||||
|
@ -1317,6 +1319,11 @@ paths:
|
||||||
- Locations
|
- Locations
|
||||||
/v1/locations/tree:
|
/v1/locations/tree:
|
||||||
get:
|
get:
|
||||||
|
parameters:
|
||||||
|
- description: include items in response tree
|
||||||
|
in: query
|
||||||
|
name: withItems
|
||||||
|
type: boolean
|
||||||
produces:
|
produces:
|
||||||
- application/json
|
- application/json
|
||||||
responses:
|
responses:
|
||||||
|
|
|
@ -232,45 +232,75 @@ func (r *LocationRepository) DeleteByGroup(ctx context.Context, GID, ID uuid.UUI
|
||||||
type TreeItem struct {
|
type TreeItem struct {
|
||||||
ID uuid.UUID `json:"id"`
|
ID uuid.UUID `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
Type string `json:"type"`
|
||||||
Children []*TreeItem `json:"children"`
|
Children []*TreeItem `json:"children"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type FlatTreeItem struct {
|
type FlatTreeItem struct {
|
||||||
ID uuid.UUID
|
ID uuid.UUID
|
||||||
Name string
|
Name string
|
||||||
|
Type string
|
||||||
ParentID uuid.UUID
|
ParentID uuid.UUID
|
||||||
Level int
|
Level int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lr *LocationRepository) Tree(ctx context.Context, GID uuid.UUID) ([]TreeItem, error) {
|
type TreeQuery struct {
|
||||||
|
WithItems bool `json:"withItems"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lr *LocationRepository) Tree(ctx context.Context, GID uuid.UUID, tq TreeQuery) ([]TreeItem, error) {
|
||||||
query := `
|
query := `
|
||||||
WITH recursive location_tree(id, NAME, location_children, level) AS
|
WITH recursive location_tree(id, NAME, location_children, level, node_type) AS
|
||||||
(
|
(
|
||||||
SELECT id,
|
SELECT id,
|
||||||
NAME,
|
NAME,
|
||||||
location_children,
|
location_children,
|
||||||
0 AS level
|
0 AS level,
|
||||||
|
'location' AS node_type
|
||||||
FROM locations
|
FROM locations
|
||||||
WHERE location_children IS NULL
|
WHERE location_children IS NULL
|
||||||
AND group_locations = ?
|
AND group_locations = ?
|
||||||
|
|
||||||
UNION ALL
|
UNION ALL
|
||||||
SELECT c.id,
|
SELECT c.id,
|
||||||
c.NAME,
|
c.NAME,
|
||||||
c.location_children,
|
c.location_children,
|
||||||
level + 1
|
level + 1,
|
||||||
|
'location' AS node_type
|
||||||
FROM locations c
|
FROM locations c
|
||||||
JOIN location_tree p
|
JOIN location_tree p
|
||||||
ON c.location_children = p.id
|
ON c.location_children = p.id
|
||||||
WHERE level < 10 -- prevent infinite loop & excessive recursion
|
WHERE level < 10 -- prevent infinite loop & excessive recursion
|
||||||
|
|
||||||
|
{{ WITH_ITEMS }}
|
||||||
)
|
)
|
||||||
SELECT id,
|
SELECT id,
|
||||||
NAME,
|
NAME,
|
||||||
level,
|
level,
|
||||||
location_children
|
location_children,
|
||||||
|
node_type
|
||||||
FROM location_tree
|
FROM location_tree
|
||||||
ORDER BY level,
|
ORDER BY level,
|
||||||
|
node_type DESC, -- sort locations before items
|
||||||
NAME;`
|
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)
|
rows, err := lr.db.Sql().QueryContext(ctx, query, GID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -280,7 +310,7 @@ func (lr *LocationRepository) Tree(ctx context.Context, GID uuid.UUID) ([]TreeIt
|
||||||
var locations []FlatTreeItem
|
var locations []FlatTreeItem
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var location FlatTreeItem
|
var location FlatTreeItem
|
||||||
if err := rows.Scan(&location.ID, &location.Name, &location.Level, &location.ParentID); err != nil {
|
if err := rows.Scan(&location.ID, &location.Name, &location.Level, &location.ParentID, &location.Type); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
locations = append(locations, location)
|
locations = append(locations, location)
|
||||||
|
@ -302,6 +332,7 @@ func ConvertLocationsToTree(locations []FlatTreeItem) []TreeItem {
|
||||||
loc := &TreeItem{
|
loc := &TreeItem{
|
||||||
ID: location.ID,
|
ID: location.ID,
|
||||||
Name: location.Name,
|
Name: location.Name,
|
||||||
|
Type: location.Type,
|
||||||
Children: []*TreeItem{},
|
Children: []*TreeItem{},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -134,7 +134,7 @@ func TestItemRepository_TreeQuery(t *testing.T) {
|
||||||
})
|
})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
locations, err := tRepos.Locations.Tree(context.Background(), tGroup.ID)
|
locations, err := tRepos.Locations.Tree(context.Background(), tGroup.ID, TreeQuery{WithItems: true})
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue