Skip to content

Commit df79f72

Browse files
hay-kotOrell Bühler
authored andcommitted
feat: locations tree viewer (hay-kot#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
1 parent b2d9e67 commit df79f72

File tree

32 files changed

+1115
-74
lines changed

32 files changed

+1115
-74
lines changed

backend/app/api/handlers/v1/v1_ctrl_locations.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,39 @@ import (
1111
"github.com/thechosenlan/homebox/backend/pkgs/server"
1212
)
1313

14+
// HandleLocationTreeQuery godoc
15+
// @Summary Get All Locations
16+
// @Tags Locations
17+
// @Produce json
18+
// @Param withItems query bool false "include items in response tree"
19+
// @Success 200 {object} server.Results{items=[]repo.TreeItem}
20+
// @Router /v1/locations/tree [GET]
21+
// @Security Bearer
22+
func (ctrl *V1Controller) HandleLocationTreeQuery() server.HandlerFunc {
23+
return func(w http.ResponseWriter, r *http.Request) error {
24+
user := services.UseUserCtx(r.Context())
25+
26+
q := r.URL.Query()
27+
28+
withItems := queryBool(q.Get("withItems"))
29+
30+
locTree, err := ctrl.repo.Locations.Tree(
31+
r.Context(),
32+
user.GroupID,
33+
repo.TreeQuery{
34+
WithItems: withItems,
35+
},
36+
)
37+
38+
if err != nil {
39+
log.Err(err).Msg("failed to get locations tree")
40+
return validate.NewRequestError(err, http.StatusInternalServerError)
41+
}
42+
43+
return server.Respond(w, http.StatusOK, server.Results{Items: locTree})
44+
}
45+
}
46+
1447
// HandleLocationGetAll godoc
1548
// @Summary Get All Locations
1649
// @Tags Locations

backend/app/api/routes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func (a *app) mountRoutes(repos *repo.AllRepos) {
9191

9292
a.server.Get(v1Base("/locations"), v1Ctrl.HandleLocationGetAll(), userMW...)
9393
a.server.Post(v1Base("/locations"), v1Ctrl.HandleLocationCreate(), userMW...)
94+
a.server.Get(v1Base("/locations/tree"), v1Ctrl.HandleLocationTreeQuery(), userMW...)
9495
a.server.Get(v1Base("/locations/{id}"), v1Ctrl.HandleLocationGet(), userMW...)
9596
a.server.Put(v1Base("/locations/{id}"), v1Ctrl.HandleLocationUpdate(), userMW...)
9697
a.server.Delete(v1Base("/locations/{id}"), v1Ctrl.HandleLocationDelete(), userMW...)

backend/app/api/static/docs/docs.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,53 @@ const docTemplate = `{
10391039
}
10401040
}
10411041
},
1042+
"/v1/locations/tree": {
1043+
"get": {
1044+
"security": [
1045+
{
1046+
"Bearer": []
1047+
}
1048+
],
1049+
"produces": [
1050+
"application/json"
1051+
],
1052+
"tags": [
1053+
"Locations"
1054+
],
1055+
"summary": "Get All Locations",
1056+
"parameters": [
1057+
{
1058+
"type": "boolean",
1059+
"description": "include items in response tree",
1060+
"name": "withItems",
1061+
"in": "query"
1062+
}
1063+
],
1064+
"responses": {
1065+
"200": {
1066+
"description": "OK",
1067+
"schema": {
1068+
"allOf": [
1069+
{
1070+
"$ref": "#/definitions/server.Results"
1071+
},
1072+
{
1073+
"type": "object",
1074+
"properties": {
1075+
"items": {
1076+
"type": "array",
1077+
"items": {
1078+
"$ref": "#/definitions/repo.TreeItem"
1079+
}
1080+
}
1081+
}
1082+
}
1083+
]
1084+
}
1085+
}
1086+
}
1087+
}
1088+
},
10421089
"/v1/locations/{id}": {
10431090
"get": {
10441091
"security": [
@@ -1912,6 +1959,10 @@ const docTemplate = `{
19121959
},
19131960
"name": {
19141961
"type": "string"
1962+
},
1963+
"parentId": {
1964+
"type": "string",
1965+
"x-nullable": true
19151966
}
19161967
}
19171968
},
@@ -2102,6 +2153,26 @@ const docTemplate = `{
21022153
}
21032154
}
21042155
},
2156+
"repo.TreeItem": {
2157+
"type": "object",
2158+
"properties": {
2159+
"children": {
2160+
"type": "array",
2161+
"items": {
2162+
"$ref": "#/definitions/repo.TreeItem"
2163+
}
2164+
},
2165+
"id": {
2166+
"type": "string"
2167+
},
2168+
"name": {
2169+
"type": "string"
2170+
},
2171+
"type": {
2172+
"type": "string"
2173+
}
2174+
}
2175+
},
21052176
"repo.UserOut": {
21062177
"type": "object",
21072178
"properties": {

backend/app/api/static/docs/swagger.json

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,53 @@
10311031
}
10321032
}
10331033
},
1034+
"/v1/locations/tree": {
1035+
"get": {
1036+
"security": [
1037+
{
1038+
"Bearer": []
1039+
}
1040+
],
1041+
"produces": [
1042+
"application/json"
1043+
],
1044+
"tags": [
1045+
"Locations"
1046+
],
1047+
"summary": "Get All Locations",
1048+
"parameters": [
1049+
{
1050+
"type": "boolean",
1051+
"description": "include items in response tree",
1052+
"name": "withItems",
1053+
"in": "query"
1054+
}
1055+
],
1056+
"responses": {
1057+
"200": {
1058+
"description": "OK",
1059+
"schema": {
1060+
"allOf": [
1061+
{
1062+
"$ref": "#/definitions/server.Results"
1063+
},
1064+
{
1065+
"type": "object",
1066+
"properties": {
1067+
"items": {
1068+
"type": "array",
1069+
"items": {
1070+
"$ref": "#/definitions/repo.TreeItem"
1071+
}
1072+
}
1073+
}
1074+
}
1075+
]
1076+
}
1077+
}
1078+
}
1079+
}
1080+
},
10341081
"/v1/locations/{id}": {
10351082
"get": {
10361083
"security": [
@@ -1904,6 +1951,10 @@
19041951
},
19051952
"name": {
19061953
"type": "string"
1954+
},
1955+
"parentId": {
1956+
"type": "string",
1957+
"x-nullable": true
19071958
}
19081959
}
19091960
},
@@ -2094,6 +2145,26 @@
20942145
}
20952146
}
20962147
},
2148+
"repo.TreeItem": {
2149+
"type": "object",
2150+
"properties": {
2151+
"children": {
2152+
"type": "array",
2153+
"items": {
2154+
"$ref": "#/definitions/repo.TreeItem"
2155+
}
2156+
},
2157+
"id": {
2158+
"type": "string"
2159+
},
2160+
"name": {
2161+
"type": "string"
2162+
},
2163+
"type": {
2164+
"type": "string"
2165+
}
2166+
}
2167+
},
20972168
"repo.UserOut": {
20982169
"type": "object",
20992170
"properties": {

backend/app/api/static/docs/swagger.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ definitions:
325325
type: string
326326
name:
327327
type: string
328+
parentId:
329+
type: string
330+
x-nullable: true
328331
type: object
329332
repo.LocationOut:
330333
properties:
@@ -449,6 +452,19 @@ definitions:
449452
total:
450453
type: number
451454
type: object
455+
repo.TreeItem:
456+
properties:
457+
children:
458+
items:
459+
$ref: '#/definitions/repo.TreeItem'
460+
type: array
461+
id:
462+
type: string
463+
name:
464+
type: string
465+
type:
466+
type: string
467+
type: object
452468
repo.UserOut:
453469
properties:
454470
email:
@@ -1287,6 +1303,32 @@ paths:
12871303
summary: updates a location
12881304
tags:
12891305
- Locations
1306+
/v1/locations/tree:
1307+
get:
1308+
parameters:
1309+
- description: include items in response tree
1310+
in: query
1311+
name: withItems
1312+
type: boolean
1313+
produces:
1314+
- application/json
1315+
responses:
1316+
"200":
1317+
description: OK
1318+
schema:
1319+
allOf:
1320+
- $ref: '#/definitions/server.Results'
1321+
- properties:
1322+
items:
1323+
items:
1324+
$ref: '#/definitions/repo.TreeItem'
1325+
type: array
1326+
type: object
1327+
security:
1328+
- Bearer: []
1329+
summary: Get All Locations
1330+
tags:
1331+
- Locations
12901332
/v1/qrcode:
12911333
get:
12921334
parameters:

backend/internal/data/ent/group/group.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/internal/data/ent/migrate/schema.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/internal/data/ent/schema/group.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (Group) Fields() []ent.Field {
2727
NotEmpty(),
2828
field.Enum("currency").
2929
Default("chf").
30-
Values("chf", "usd", "eur", "gbp", "jpy", "zar", "aud", "nok", "sek", "dkk", "rmb"),
30+
Values("chf", "usd", "eur", "gbp", "jpy", "zar", "aud", "nok", "sek", "dkk", "inr", "rmb", "bgn"),
3131
}
3232
}
3333

backend/internal/data/repo/repo_items_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ func useItems(t *testing.T, len int) []ItemOut {
3636
for _, item := range items {
3737
_ = tRepos.Items.Delete(context.Background(), item.ID)
3838
}
39+
40+
_ = tRepos.Locations.Delete(context.Background(), location.ID)
3941
})
4042

4143
return items

0 commit comments

Comments
 (0)