Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

label stubs

Haydenf956ec8eb2e4
diff --git a/backend/app/api/docs/docs.go b/backend/app/api/docs/docs.go
index 211e15f3..5ea7d646 100644
--- a/backend/app/api/docs/docs.go
+++ b/backend/app/api/docs/docs.go
@@ -261,6 +261,170 @@ const docTemplate = `{
}
}
},
+ "/v1/labels": {
+ "get": {
+ "security": [
+ {
+ "Bearer": []
+ }
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "Labels"
+ ],
+ "summary": "Get All Labels",
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "allOf": [
+ {
+ "$ref": "#/definitions/server.Results"
+ },
+ {
+ "type": "object",
+ "properties": {
+ "items": {
+ "type": "array",
+ "items": {
+ "$ref": "#/definitions/types.LabelOut"
+ }
+ }
+ }
+ }
+ ]
+ }
+ }
+ }
+ },
+ "post": {
+ "security": [
+ {
+ "Bearer": []
+ }
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "Labels"
+ ],
+ "summary": "Create a new label",
+ "parameters": [
+ {
+ "description": "Label Data",
+ "name": "payload",
+ "in": "body",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/types.LabelCreate"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "$ref": "#/definitions/types.LabelSummary"
+ }
+ }
+ }
+ }
+ },
+ "/v1/labels/{id}": {
+ "get": {
+ "security": [
+ {
+ "Bearer": []
+ }
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "Labels"
+ ],
+ "summary": "Gets a label and fields",
+ "parameters": [
+ {
+ "type": "string",
+ "description": "Label ID",
+ "name": "id",
+ "in": "path",
+ "required": true
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "$ref": "#/definitions/types.LabelOut"
+ }
+ }
+ }
+ },
+ "put": {
+ "security": [
+ {
+ "Bearer": []
+ }
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "Labels"
+ ],
+ "summary": "updates a label",
+ "parameters": [
+ {
+ "type": "string",
+ "description": "Label ID",
+ "name": "id",
+ "in": "path",
+ "required": true
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "$ref": "#/definitions/types.LabelOut"
+ }
+ }
+ }
+ },
+ "delete": {
+ "security": [
+ {
+ "Bearer": []
+ }
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "Labels"
+ ],
+ "summary": "deletes a label",
+ "parameters": [
+ {
+ "type": "string",
+ "description": "Label ID",
+ "name": "id",
+ "in": "path",
+ "required": true
+ }
+ ],
+ "responses": {
+ "204": {
+ "description": ""
+ }
+ }
+ }
+ },
"/v1/locations": {
"get": {
"security": [
@@ -1108,6 +1272,15 @@ const docTemplate = `{
}
}
},
+ "types.LabelCreate": {
+ "type": "object"
+ },
+ "types.LabelOut": {
+ "type": "object"
+ },
+ "types.LabelSummary": {
+ "type": "object"
+ },
"types.LocationCreate": {
"type": "object",
"properties": {
diff --git a/backend/app/api/docs/swagger.json b/backend/app/api/docs/swagger.json
index b688cb76..5f4ab5a9 100644
--- a/backend/app/api/docs/swagger.json
+++ b/backend/app/api/docs/swagger.json
@@ -253,6 +253,170 @@
}
}
},
+ "/v1/labels": {
+ "get": {
+ "security": [
+ {
+ "Bearer": []
+ }
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "Labels"
+ ],
+ "summary": "Get All Labels",
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "allOf": [
+ {
+ "$ref": "#/definitions/server.Results"
+ },
+ {
+ "type": "object",
+ "properties": {
+ "items": {
+ "type": "array",
+ "items": {
+ "$ref": "#/definitions/types.LabelOut"
+ }
+ }
+ }
+ }
+ ]
+ }
+ }
+ }
+ },
+ "post": {
+ "security": [
+ {
+ "Bearer": []
+ }
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "Labels"
+ ],
+ "summary": "Create a new label",
+ "parameters": [
+ {
+ "description": "Label Data",
+ "name": "payload",
+ "in": "body",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/types.LabelCreate"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "$ref": "#/definitions/types.LabelSummary"
+ }
+ }
+ }
+ }
+ },
+ "/v1/labels/{id}": {
+ "get": {
+ "security": [
+ {
+ "Bearer": []
+ }
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "Labels"
+ ],
+ "summary": "Gets a label and fields",
+ "parameters": [
+ {
+ "type": "string",
+ "description": "Label ID",
+ "name": "id",
+ "in": "path",
+ "required": true
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "$ref": "#/definitions/types.LabelOut"
+ }
+ }
+ }
+ },
+ "put": {
+ "security": [
+ {
+ "Bearer": []
+ }
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "Labels"
+ ],
+ "summary": "updates a label",
+ "parameters": [
+ {
+ "type": "string",
+ "description": "Label ID",
+ "name": "id",
+ "in": "path",
+ "required": true
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "$ref": "#/definitions/types.LabelOut"
+ }
+ }
+ }
+ },
+ "delete": {
+ "security": [
+ {
+ "Bearer": []
+ }
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "Labels"
+ ],
+ "summary": "deletes a label",
+ "parameters": [
+ {
+ "type": "string",
+ "description": "Label ID",
+ "name": "id",
+ "in": "path",
+ "required": true
+ }
+ ],
+ "responses": {
+ "204": {
+ "description": ""
+ }
+ }
+ }
+ },
"/v1/locations": {
"get": {
"security": [
@@ -1100,6 +1264,15 @@
}
}
},
+ "types.LabelCreate": {
+ "type": "object"
+ },
+ "types.LabelOut": {
+ "type": "object"
+ },
+ "types.LabelSummary": {
+ "type": "object"
+ },
"types.LocationCreate": {
"type": "object",
"properties": {
diff --git a/backend/app/api/docs/swagger.yaml b/backend/app/api/docs/swagger.yaml
index 62cf2968..c13ac90f 100644
--- a/backend/app/api/docs/swagger.yaml
+++ b/backend/app/api/docs/swagger.yaml
@@ -353,6 +353,12 @@ definitions:
updatedAt:
type: string
type: object
+ types.LabelCreate:
+ type: object
+ types.LabelOut:
+ type: object
+ types.LabelSummary:
+ type: object
types.LocationCreate:
properties:
description:
@@ -583,6 +589,103 @@ paths:
summary: Update a User
tags:
- 'Admin: Users'
+ /v1/labels:
+ get:
+ produces:
+ - application/json
+ responses:
+ "200":
+ description: OK
+ schema:
+ allOf:
+ - $ref: '#/definitions/server.Results'
+ - properties:
+ items:
+ items:
+ $ref: '#/definitions/types.LabelOut'
+ type: array
+ type: object
+ security:
+ - Bearer: []
+ summary: Get All Labels
+ tags:
+ - Labels
+ post:
+ parameters:
+ - description: Label Data
+ in: body
+ name: payload
+ required: true
+ schema:
+ $ref: '#/definitions/types.LabelCreate'
+ produces:
+ - application/json
+ responses:
+ "200":
+ description: OK
+ schema:
+ $ref: '#/definitions/types.LabelSummary'
+ security:
+ - Bearer: []
+ summary: Create a new label
+ tags:
+ - Labels
+ /v1/labels/{id}:
+ delete:
+ parameters:
+ - description: Label ID
+ in: path
+ name: id
+ required: true
+ type: string
+ produces:
+ - application/json
+ responses:
+ "204":
+ description: ""
+ security:
+ - Bearer: []
+ summary: deletes a label
+ tags:
+ - Labels
+ get:
+ parameters:
+ - description: Label ID
+ in: path
+ name: id
+ required: true
+ type: string
+ produces:
+ - application/json
+ responses:
+ "200":
+ description: OK
+ schema:
+ $ref: '#/definitions/types.LabelOut'
+ security:
+ - Bearer: []
+ summary: Gets a label and fields
+ tags:
+ - Labels
+ put:
+ parameters:
+ - description: Label ID
+ in: path
+ name: id
+ required: true
+ type: string
+ produces:
+ - application/json
+ responses:
+ "200":
+ description: OK
+ schema:
+ $ref: '#/definitions/types.LabelOut'
+ security:
+ - Bearer: []
+ summary: updates a label
+ tags:
+ - Labels
/v1/locations:
get:
produces:
diff --git a/backend/app/api/routes.go b/backend/app/api/routes.go
index 81efe4d6..c50b39a3 100644
--- a/backend/app/api/routes.go
+++ b/backend/app/api/routes.go
@@ -55,6 +55,12 @@ func (a *app) newRouter(repos *repo.AllRepos) *chi.Mux {
r.Get(v1Base("/locations/{id}"), v1Handlers.HandleLocationGet())
r.Put(v1Base("/locations/{id}"), v1Handlers.HandleLocationUpdate())
r.Delete(v1Base("/locations/{id}"), v1Handlers.HandleLocationDelete())
+
+ r.Get(v1Base("/labels"), v1Handlers.HandleLabelsGetAll())
+ r.Post(v1Base("/labels"), v1Handlers.HandleLabelsCreate())
+ r.Get(v1Base("/labels/{id}"), v1Handlers.HandleLabelGet())
+ r.Put(v1Base("/labels/{id}"), v1Handlers.HandleLabelUpdate())
+ r.Delete(v1Base("/labels/{id}"), v1Handlers.HandleLabelDelete())
})
r.Group(func(r chi.Router) {
diff --git a/backend/app/api/v1/partials.go b/backend/app/api/v1/partials.go
new file mode 100644
index 00000000..018c043b
--- /dev/null
+++ b/backend/app/api/v1/partials.go
@@ -0,0 +1,36 @@
+package v1
+
+import (
+ "net/http"
+
+ "github.com/go-chi/chi/v5"
+ "github.com/google/uuid"
+ "github.com/hay-kot/content/backend/internal/services"
+ "github.com/hay-kot/content/backend/internal/types"
+ "github.com/hay-kot/content/backend/pkgs/logger"
+ "github.com/hay-kot/content/backend/pkgs/server"
+)
+
+/*
+This is where we put partial snippets/functions for actions that are commonly
+used within the controller class. This _hopefully_ helps with code duplication
+and makes it a little more consistent when error handling and logging.
+*/
+
+// partialParseIdAndUser parses the ID from the requests URL and pulls the user
+// from the context. If either of these fail, it will return an error. When an error
+// occurs it will also write the error to the response. As such, if an error is returned
+// from this function you can return immediately without writing to the response.
+func (ctrl *V1Controller) partialParseIdAndUser(w http.ResponseWriter, r *http.Request) (uuid.UUID, *types.UserOut, error) {
+ uid, err := uuid.Parse(chi.URLParam(r, "id"))
+ if err != nil {
+ ctrl.log.Debug(err.Error(), logger.Props{
+ "details": "failed to convert id to valid UUID",
+ })
+ server.RespondError(w, http.StatusBadRequest, err)
+ return uuid.Nil, nil, err
+ }
+
+ user := services.UseUserCtx(r.Context())
+ return uid, user, nil
+}
diff --git a/backend/app/api/v1/v1_ctrl_labels.go b/backend/app/api/v1/v1_ctrl_labels.go
new file mode 100644
index 00000000..f31976b7
--- /dev/null
+++ b/backend/app/api/v1/v1_ctrl_labels.go
@@ -0,0 +1,69 @@
+package v1
+
+import (
+ "net/http"
+)
+
+// HandleLabelsGetAll godoc
+// @Summary Get All Labels
+// @Tags Labels
+// @Produce json
+// @Success 200 {object} server.Results{items=[]types.LabelOut}
+// @Router /v1/labels [GET]
+// @Security Bearer
+func (ctrl *V1Controller) HandleLabelsGetAll() http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ }
+}
+
+// HandleLabelsCreate godoc
+// @Summary Create a new label
+// @Tags Labels
+// @Produce json
+// @Param payload body types.LabelCreate true "Label Data"
+// @Success 200 {object} types.LabelSummary
+// @Router /v1/labels [POST]
+// @Security Bearer
+func (ctrl *V1Controller) HandleLabelsCreate() http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ }
+}
+
+// HandleLabelDelete godocs
+// @Summary deletes a label
+// @Tags Labels
+// @Produce json
+// @Param id path string true "Label ID"
+// @Success 204
+// @Router /v1/labels/{id} [DELETE]
+// @Security Bearer
+func (ctrl *V1Controller) HandleLabelDelete() http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ }
+}
+
+// HandleLabelGet godocs
+// @Summary Gets a label and fields
+// @Tags Labels
+// @Produce json
+// @Param id path string true "Label ID"
+// @Success 200 {object} types.LabelOut
+// @Router /v1/labels/{id} [GET]
+// @Security Bearer
+func (ctrl *V1Controller) HandleLabelGet() http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ }
+}
+
+// HandleLabelUpdate godocs
+// @Summary updates a label
+// @Tags Labels
+// @Produce json
+// @Param id path string true "Label ID"
+// @Success 200 {object} types.LabelOut
+// @Router /v1/labels/{id} [PUT]
+// @Security Bearer
+func (ctrl *V1Controller) HandleLabelUpdate() http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ }
+}
diff --git a/backend/app/api/v1/v1_ctrl_locations.go b/backend/app/api/v1/v1_ctrl_locations.go
index f0b8ef15..14b37754 100644
--- a/backend/app/api/v1/v1_ctrl_locations.go
+++ b/backend/app/api/v1/v1_ctrl_locations.go
@@ -3,11 +3,8 @@ package v1
import (
"net/http"
- "github.com/go-chi/chi/v5"
- "github.com/google/uuid"
"github.com/hay-kot/content/backend/internal/services"
"github.com/hay-kot/content/backend/internal/types"
- "github.com/hay-kot/content/backend/pkgs/logger"
"github.com/hay-kot/content/backend/pkgs/server"
)
@@ -61,20 +58,6 @@ func (ctrl *V1Controller) HandleLocationCreate() http.HandlerFunc {
}
}
-func (ctrl *V1Controller) partialParseIdAndUser(w http.ResponseWriter, r *http.Request) (uuid.UUID, *types.UserOut, error) {
- uid, err := uuid.Parse(chi.URLParam(r, "id"))
- if err != nil {
- ctrl.log.Debug(err.Error(), logger.Props{
- "details": "failed to convert id to valid UUID",
- })
- server.RespondError(w, http.StatusBadRequest, err)
- return uuid.Nil, nil, err
- }
-
- user := services.UseUserCtx(r.Context())
- return uid, user, nil
-}
-
// HandleLocationDelete godocs
// @Summary deletes a location
// @Tags Locations
diff --git a/backend/internal/types/label_types.go b/backend/internal/types/label_types.go
new file mode 100644
index 00000000..029a375c
--- /dev/null
+++ b/backend/internal/types/label_types.go
@@ -0,0 +1,7 @@
+package types
+
+type LabelOut struct{}
+
+type LabelCreate struct{}
+
+type LabelSummary struct{}