Skip to content

Commit

Permalink
feat: easily increment quantity (#473)
Browse files Browse the repository at this point in the history
* fix vue version issue

* new patch API endpoint

* doc-gen

* new API class method for patch operations

* add quantity patch UI support

* fix typegen errors

* fix ts errors
  • Loading branch information
hay-kot committed Jun 2, 2023
1 parent 4dd036a commit ef1531e
Show file tree
Hide file tree
Showing 16 changed files with 353 additions and 163 deletions.
27 changes: 27 additions & 0 deletions backend/app/api/handlers/v1/v1_ctrl_items.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,33 @@ func (ctrl *V1Controller) HandleItemUpdate() errchain.HandlerFunc {
return adapters.ActionID("id", fn, http.StatusOK)
}


// HandleItemPatch godocs
//
// @Summary Update Item
// @Tags Items
// @Produce json
// @Param id path string true "Item ID"
// @Param payload body repo.ItemPatch true "Item Data"
// @Success 200 {object} repo.ItemOut
// @Router /v1/items/{id} [Patch]
// @Security Bearer
func (ctrl *V1Controller) HandleItemPatch() errchain.HandlerFunc {
fn := func(r *http.Request, ID uuid.UUID, body repo.ItemPatch) (repo.ItemOut, error) {
auth := services.NewContext(r.Context())

body.ID = ID
err := ctrl.repo.Items.Patch(auth, auth.GID, ID, body)
if err != nil {
return repo.ItemOut{}, err
}

return ctrl.repo.Items.GetOneByGroup(auth, auth.GID, ID)
}

return adapters.ActionID("id", fn, http.StatusOK)
}

// HandleGetAllCustomFieldNames godocs
//
// @Summary Get All Custom Field Names
Expand Down
1 change: 1 addition & 0 deletions backend/app/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (a *app) mountRoutes(r *chi.Mux, chain *errchain.ErrChain, repos *repo.AllR

r.Get(v1Base("/items/{id}"), chain.ToHandlerFunc(v1Ctrl.HandleItemGet(), userMW...))
r.Put(v1Base("/items/{id}"), chain.ToHandlerFunc(v1Ctrl.HandleItemUpdate(), userMW...))
r.Patch(v1Base("/items/{id}"), chain.ToHandlerFunc(v1Ctrl.HandleItemPatch(), userMW...))
r.Delete(v1Base("/items/{id}"), chain.ToHandlerFunc(v1Ctrl.HandleItemDelete(), userMW...))

r.Post(v1Base("/items/{id}/attachments"), chain.ToHandlerFunc(v1Ctrl.HandleItemAttachmentCreate(), userMW...))
Expand Down
53 changes: 53 additions & 0 deletions backend/app/api/static/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,46 @@ const docTemplate = `{
"description": "No Content"
}
}
},
"patch": {
"security": [
{
"Bearer": []
}
],
"produces": [
"application/json"
],
"tags": [
"Items"
],
"summary": "Update Item",
"parameters": [
{
"type": "string",
"description": "Item ID",
"name": "id",
"in": "path",
"required": true
},
{
"description": "Item Data",
"name": "payload",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/repo.ItemPatch"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/repo.ItemOut"
}
}
}
}
},
"/v1/items/{id}/attachments": {
Expand Down Expand Up @@ -2042,6 +2082,19 @@ const docTemplate = `{
}
}
},
"repo.ItemPatch": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"quantity": {
"type": "integer",
"x-nullable": true,
"x-omitempty": true
}
}
},
"repo.ItemSummary": {
"type": "object",
"properties": {
Expand Down
53 changes: 53 additions & 0 deletions backend/app/api/static/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,46 @@
"description": "No Content"
}
}
},
"patch": {
"security": [
{
"Bearer": []
}
],
"produces": [
"application/json"
],
"tags": [
"Items"
],
"summary": "Update Item",
"parameters": [
{
"type": "string",
"description": "Item ID",
"name": "id",
"in": "path",
"required": true
},
{
"description": "Item Data",
"name": "payload",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/repo.ItemPatch"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/repo.ItemOut"
}
}
}
}
},
"/v1/items/{id}/attachments": {
Expand Down Expand Up @@ -2034,6 +2074,19 @@
}
}
},
"repo.ItemPatch": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"quantity": {
"type": "integer",
"x-nullable": true,
"x-omitempty": true
}
}
},
"repo.ItemSummary": {
"type": "object",
"properties": {
Expand Down
34 changes: 34 additions & 0 deletions backend/app/api/static/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@ definitions:
warrantyExpires:
type: string
type: object
repo.ItemPatch:
properties:
id:
type: string
quantity:
type: integer
x-nullable: true
x-omitempty: true
type: object
repo.ItemSummary:
properties:
archived:
Expand Down Expand Up @@ -992,6 +1001,31 @@ paths:
summary: Get Item
tags:
- Items
patch:
parameters:
- description: Item ID
in: path
name: id
required: true
type: string
- description: Item Data
in: body
name: payload
required: true
schema:
$ref: '#/definitions/repo.ItemPatch'
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/repo.ItemOut'
security:
- Bearer: []
summary: Update Item
tags:
- Items
put:
parameters:
- description: Item ID
Expand Down
1 change: 1 addition & 0 deletions backend/app/tools/typegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func main() {
NewReReplace(` Services`, " "),
NewReReplace(` V1`, " "),
NewReReplace(`\?:`, ":"),
NewReReplace(`(\w+):\s(.*null.*)`, "$1?: $2"), // make null union types optional
NewReDate("createdAt"),
NewReDate("updatedAt"),
NewReDate("soldTime"),
Expand Down
4 changes: 4 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
Expand All @@ -531,6 +532,7 @@ github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRW
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc=
Expand Down Expand Up @@ -602,8 +604,10 @@ github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrf
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.9.2/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y=
github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU=
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.14.0/go.mod h1:WT//axPky3FdvXHzGw33dNdXXXfFQqmEalje+egj8As=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
2 changes: 1 addition & 1 deletion backend/internal/data/repo/repo_items.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ type (
ItemPatch struct {
ID uuid.UUID `json:"id"`
Quantity *int `json:"quantity,omitempty" extensions:"x-nullable,x-omitempty"`
ImportRef *string `json:"importRef,omitempty" extensions:"x-nullable,x-omitempty"`
ImportRef *string `json:"-,omitempty" extensions:"x-nullable,x-omitempty"`
}

ItemSummary struct {
Expand Down
53 changes: 53 additions & 0 deletions docs/docs/api/openapi-2.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,46 @@
"description": "No Content"
}
}
},
"patch": {
"security": [
{
"Bearer": []
}
],
"produces": [
"application/json"
],
"tags": [
"Items"
],
"summary": "Update Item",
"parameters": [
{
"type": "string",
"description": "Item ID",
"name": "id",
"in": "path",
"required": true
},
{
"description": "Item Data",
"name": "payload",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/repo.ItemPatch"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/repo.ItemOut"
}
}
}
}
},
"/v1/items/{id}/attachments": {
Expand Down Expand Up @@ -2034,6 +2074,19 @@
}
}
},
"repo.ItemPatch": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"quantity": {
"type": "integer",
"x-nullable": true,
"x-omitempty": true
}
}
},
"repo.ItemSummary": {
"type": "object",
"properties": {
Expand Down
15 changes: 15 additions & 0 deletions frontend/lib/api/classes/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ItemAttachmentUpdate,
ItemCreate,
ItemOut,
ItemPatch,
ItemSummary,
ItemUpdate,
MaintenanceEntry,
Expand Down Expand Up @@ -138,6 +139,20 @@ export class ItemsApi extends BaseAPI {
return payload;
}

async patch(id: string, item: ItemPatch) {
const resp = await this.http.patch<ItemPatch, ItemOut>({
url: route(`/items/${id}`),
body: this.dropFields(item),
});

if (!resp.data) {
return resp;
}

resp.data = parseDate(resp.data, ["purchaseTime", "soldTime", "warrantyExpires"]);
return resp;
}

import(file: File | Blob) {
const formData = new FormData();
formData.append("csv", file);
Expand Down

0 comments on commit ef1531e

Please sign in to comment.