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

View file

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

View file

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

View file

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

View file

@ -49,7 +49,7 @@ type (
LabelIDs []uuid.UUID `json:"labelIds"` LabelIDs []uuid.UUID `json:"labelIds"`
} }
ItemUpdate struct { 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"` ID uuid.UUID `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Description string `json:"description"` Description string `json:"description"`

View file

@ -22,6 +22,7 @@ type (
} }
LocationUpdate struct { LocationUpdate struct {
ParentID uuid.UUID `json:"parentId" extensions:"x-nullable"`
ID uuid.UUID `json:"id"` ID uuid.UUID `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Description string `json:"description"` Description string `json:"description"`
@ -41,8 +42,10 @@ type (
} }
LocationOut struct { LocationOut struct {
Parent *LocationSummary `json:"parent,omitempty"`
LocationSummary LocationSummary
Items []ItemSummary `json:"items"` Items []ItemSummary `json:"items"`
Children []LocationSummary `json:"children"`
} }
) )
@ -61,7 +64,20 @@ var (
) )
func mapLocationOut(location *ent.Location) LocationOut { 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{ return LocationOut{
Parent: parent,
Children: children,
LocationSummary: LocationSummary{ LocationSummary: LocationSummary{
ID: location.ID, ID: location.ID,
Name: location.Name, Name: location.Name,
@ -125,6 +141,8 @@ func (r *LocationRepository) getOne(ctx context.Context, where ...predicate.Loca
WithItems(func(iq *ent.ItemQuery) { WithItems(func(iq *ent.ItemQuery) {
iq.WithLabel() iq.WithLabel()
}). }).
WithParent().
WithChildren().
Only(ctx)) 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) { 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). SetName(data.Name).
SetDescription(data.Description). SetDescription(data.Description)
Save(ctx)
if data.ParentID != uuid.Nil {
q.SetParentID(data.ParentID)
} else {
q.ClearParent()
}
_, err := q.Save(ctx)
if err != nil { if err != nil {
return LocationOut{}, err return LocationOut{}, err