location parent API

This commit is contained in:
Hayden 2022-10-23 20:36:35 -08:00
parent b8374d8cc8
commit 55ac472c9e
6 changed files with 146 additions and 9 deletions

View file

@ -845,6 +845,15 @@ const docTemplate = `{
"name": "id",
"in": "path",
"required": true
},
{
"description": "Location Data",
"name": "payload",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/repo.LocationUpdate"
}
}
],
"responses": {
@ -1310,6 +1319,8 @@ const docTemplate = `{
},
"location": {
"description": "Edges",
"x-nullable": true,
"x-omitempty": true,
"$ref": "#/definitions/repo.LocationSummary"
},
"manufacturer": {
@ -1395,6 +1406,8 @@ const docTemplate = `{
},
"location": {
"description": "Edges",
"x-nullable": true,
"x-omitempty": true,
"$ref": "#/definitions/repo.LocationSummary"
},
"name": {
@ -1455,7 +1468,8 @@ const docTemplate = `{
},
"parentId": {
"type": "string",
"x-nullable": true
"x-nullable": true,
"x-omitempty": true
},
"purchaseFrom": {
"type": "string"
@ -1571,6 +1585,12 @@ const docTemplate = `{
"repo.LocationOut": {
"type": "object",
"properties": {
"children": {
"type": "array",
"items": {
"$ref": "#/definitions/repo.LocationSummary"
}
},
"createdAt": {
"type": "string"
},
@ -1589,6 +1609,9 @@ const docTemplate = `{
"name": {
"type": "string"
},
"parent": {
"$ref": "#/definitions/repo.LocationSummary"
},
"updatedAt": {
"type": "string"
}
@ -1637,6 +1660,24 @@ const docTemplate = `{
}
}
},
"repo.LocationUpdate": {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"parentId": {
"type": "string",
"x-nullable": true
}
}
},
"repo.PaginationResult-repo_ItemSummary": {
"type": "object",
"properties": {

View file

@ -837,6 +837,15 @@
"name": "id",
"in": "path",
"required": true
},
{
"description": "Location Data",
"name": "payload",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/repo.LocationUpdate"
}
}
],
"responses": {
@ -1302,6 +1311,8 @@
},
"location": {
"description": "Edges",
"x-nullable": true,
"x-omitempty": true,
"$ref": "#/definitions/repo.LocationSummary"
},
"manufacturer": {
@ -1387,6 +1398,8 @@
},
"location": {
"description": "Edges",
"x-nullable": true,
"x-omitempty": true,
"$ref": "#/definitions/repo.LocationSummary"
},
"name": {
@ -1447,7 +1460,8 @@
},
"parentId": {
"type": "string",
"x-nullable": true
"x-nullable": true,
"x-omitempty": true
},
"purchaseFrom": {
"type": "string"
@ -1563,6 +1577,12 @@
"repo.LocationOut": {
"type": "object",
"properties": {
"children": {
"type": "array",
"items": {
"$ref": "#/definitions/repo.LocationSummary"
}
},
"createdAt": {
"type": "string"
},
@ -1581,6 +1601,9 @@
"name": {
"type": "string"
},
"parent": {
"$ref": "#/definitions/repo.LocationSummary"
},
"updatedAt": {
"type": "string"
}
@ -1629,6 +1652,24 @@
}
}
},
"repo.LocationUpdate": {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"parentId": {
"type": "string",
"x-nullable": true
}
}
},
"repo.PaginationResult-repo_ItemSummary": {
"type": "object",
"properties": {

View file

@ -115,6 +115,8 @@ definitions:
location:
$ref: '#/definitions/repo.LocationSummary'
description: Edges
x-nullable: true
x-omitempty: true
manufacturer:
type: string
modelNumber:
@ -174,6 +176,8 @@ definitions:
location:
$ref: '#/definitions/repo.LocationSummary'
description: Edges
x-nullable: true
x-omitempty: true
name:
type: string
quantity:
@ -215,6 +219,7 @@ definitions:
parentId:
type: string
x-nullable: true
x-omitempty: true
purchaseFrom:
type: string
purchasePrice:
@ -291,6 +296,10 @@ definitions:
type: object
repo.LocationOut:
properties:
children:
items:
$ref: '#/definitions/repo.LocationSummary'
type: array
createdAt:
type: string
description:
@ -303,6 +312,8 @@ definitions:
type: array
name:
type: string
parent:
$ref: '#/definitions/repo.LocationSummary'
updatedAt:
type: string
type: object
@ -334,6 +345,18 @@ definitions:
updatedAt:
type: string
type: object
repo.LocationUpdate:
properties:
description:
type: string
id:
type: string
name:
type: string
parentId:
type: string
x-nullable: true
type: object
repo.PaginationResult-repo_ItemSummary:
properties:
items:
@ -989,6 +1012,12 @@ paths:
name: id
required: true
type: string
- description: Location Data
in: body
name: payload
required: true
schema:
$ref: '#/definitions/repo.LocationUpdate'
produces:
- application/json
responses:

View file

@ -88,8 +88,9 @@ func (ctrl *V1Controller) HandleLocationGet() http.HandlerFunc {
// @Summary updates a location
// @Tags Locations
// @Produce json
// @Param id path string true "Location ID"
// @Success 200 {object} repo.LocationOut
// @Param id path string true "Location ID"
// @Param payload body repo.LocationUpdate true "Location Data"
// @Success 200 {object} repo.LocationOut
// @Router /v1/locations/{id} [PUT]
// @Security Bearer
func (ctrl *V1Controller) HandleLocationUpdate() http.HandlerFunc {

View file

@ -49,7 +49,7 @@ type (
LabelIDs []uuid.UUID `json:"labelIds"`
}
ItemUpdate struct {
ParentID uuid.UUID `json:"parentId" extensions:"x-nullable"`
ParentID uuid.UUID `json:"parentId" extensions:"x-nullable,x-omitempty"`
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Description string `json:"description"`

View file

@ -22,6 +22,7 @@ type (
}
LocationUpdate struct {
ParentID uuid.UUID `json:"parentId" extensions:"x-nullable"`
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
@ -41,8 +42,10 @@ type (
}
LocationOut struct {
Parent *LocationSummary `json:"parent,omitempty"`
LocationSummary
Items []ItemSummary `json:"items"`
Items []ItemSummary `json:"items"`
Children []LocationSummary `json:"children"`
}
)
@ -61,7 +64,20 @@ var (
)
func mapLocationOut(location *ent.Location) LocationOut {
var parent *LocationSummary
if location.Edges.Parent != nil {
p := mapLocationSummary(location.Edges.Parent)
parent = &p
}
children := make([]LocationSummary, 0, len(location.Edges.Children))
for _, c := range location.Edges.Children {
children = append(children, mapLocationSummary(c))
}
return LocationOut{
Parent: parent,
Children: children,
LocationSummary: LocationSummary{
ID: location.ID,
Name: location.Name,
@ -125,6 +141,8 @@ func (r *LocationRepository) getOne(ctx context.Context, where ...predicate.Loca
WithItems(func(iq *ent.ItemQuery) {
iq.WithLabel()
}).
WithParent().
WithChildren().
Only(ctx))
}
@ -152,10 +170,17 @@ func (r *LocationRepository) Create(ctx context.Context, gid uuid.UUID, data Loc
}
func (r *LocationRepository) Update(ctx context.Context, data LocationUpdate) (LocationOut, error) {
_, err := r.db.Location.UpdateOneID(data.ID).
q := r.db.Location.UpdateOneID(data.ID).
SetName(data.Name).
SetDescription(data.Description).
Save(ctx)
SetDescription(data.Description)
if data.ParentID != uuid.Nil {
q.SetParentID(data.ParentID)
} else {
q.ClearParent()
}
_, err := q.Save(ctx)
if err != nil {
return LocationOut{}, err