items and location item count

This commit is contained in:
Hayden 2022-09-03 01:17:48 -08:00
parent 11dcff450c
commit f4f7123073
19 changed files with 1350 additions and 50 deletions

View file

@ -261,6 +261,170 @@ const docTemplate = `{
}
}
},
"/v1/items": {
"get": {
"security": [
{
"Bearer": []
}
],
"produces": [
"application/json"
],
"tags": [
"Items"
],
"summary": "Get All Items",
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/server.Results"
},
{
"type": "object",
"properties": {
"items": {
"type": "array",
"items": {
"$ref": "#/definitions/types.ItemOut"
}
}
}
}
]
}
}
}
},
"post": {
"security": [
{
"Bearer": []
}
],
"produces": [
"application/json"
],
"tags": [
"Items"
],
"summary": "Create a new item",
"parameters": [
{
"description": "Item Data",
"name": "payload",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/types.ItemCreate"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/types.ItemSummary"
}
}
}
}
},
"/v1/items/{id}": {
"get": {
"security": [
{
"Bearer": []
}
],
"produces": [
"application/json"
],
"tags": [
"Items"
],
"summary": "Gets a item and fields",
"parameters": [
{
"type": "string",
"description": "Item ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/types.ItemOut"
}
}
}
},
"put": {
"security": [
{
"Bearer": []
}
],
"produces": [
"application/json"
],
"tags": [
"Items"
],
"summary": "updates a item",
"parameters": [
{
"type": "string",
"description": "Item ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/types.ItemOut"
}
}
}
},
"delete": {
"security": [
{
"Bearer": []
}
],
"produces": [
"application/json"
],
"tags": [
"Items"
],
"summary": "deletes a item",
"parameters": [
{
"type": "string",
"description": "Item ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"204": {
"description": ""
}
}
}
},
"/v1/labels": {
"get": {
"security": [
@ -453,7 +617,7 @@ const docTemplate = `{
"items": {
"type": "array",
"items": {
"$ref": "#/definitions/types.LocationOut"
"$ref": "#/definitions/types.LocationCount"
}
}
}
@ -1249,6 +1413,94 @@ const docTemplate = `{
}
}
},
"types.ItemCreate": {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"labelIds": {
"type": "array",
"items": {
"type": "string"
}
},
"locationId": {
"description": "Edges",
"type": "string"
},
"name": {
"type": "string"
}
}
},
"types.ItemOut": {
"type": "object",
"properties": {
"createdAt": {
"type": "string"
},
"description": {
"type": "string"
},
"id": {
"type": "string"
},
"labels": {
"type": "array",
"items": {
"$ref": "#/definitions/types.LabelSummary"
}
},
"location": {
"description": "Edges",
"$ref": "#/definitions/types.LocationSummary"
},
"manufacturer": {
"type": "string"
},
"modelNumber": {
"type": "string"
},
"name": {
"type": "string"
},
"notes": {
"description": "Extras",
"type": "string"
},
"purchaseFrom": {
"type": "string"
},
"purchasePrice": {
"type": "number"
},
"purchaseTime": {
"description": "Purchase",
"type": "string"
},
"serialNumber": {
"description": "Identifications",
"type": "string"
},
"soldNotes": {
"type": "string"
},
"soldPrice": {
"type": "number"
},
"soldTime": {
"description": "Sold",
"type": "string"
},
"soldTo": {
"type": "string"
},
"updatedAt": {
"type": "string"
}
}
},
"types.ItemSummary": {
"type": "object",
"properties": {
@ -1261,12 +1513,56 @@ const docTemplate = `{
"id": {
"type": "string"
},
"locationId": {
"labels": {
"type": "array",
"items": {
"$ref": "#/definitions/types.LabelSummary"
}
},
"location": {
"description": "Edges",
"$ref": "#/definitions/types.LocationSummary"
},
"manufacturer": {
"type": "string"
},
"modelNumber": {
"type": "string"
},
"name": {
"type": "string"
},
"notes": {
"description": "Extras",
"type": "string"
},
"purchaseFrom": {
"type": "string"
},
"purchasePrice": {
"type": "number"
},
"purchaseTime": {
"description": "Purchase",
"type": "string"
},
"serialNumber": {
"description": "Identifications",
"type": "string"
},
"soldNotes": {
"type": "string"
},
"soldPrice": {
"type": "number"
},
"soldTime": {
"description": "Sold",
"type": "string"
},
"soldTo": {
"type": "string"
},
"updatedAt": {
"type": "string"
}
@ -1338,6 +1634,32 @@ const docTemplate = `{
}
}
},
"types.LocationCount": {
"type": "object",
"properties": {
"createdAt": {
"type": "string"
},
"description": {
"type": "string"
},
"groupId": {
"type": "string"
},
"id": {
"type": "string"
},
"itemCount": {
"type": "integer"
},
"name": {
"type": "string"
},
"updatedAt": {
"type": "string"
}
}
},
"types.LocationCreate": {
"type": "object",
"properties": {

View file

@ -253,6 +253,170 @@
}
}
},
"/v1/items": {
"get": {
"security": [
{
"Bearer": []
}
],
"produces": [
"application/json"
],
"tags": [
"Items"
],
"summary": "Get All Items",
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/server.Results"
},
{
"type": "object",
"properties": {
"items": {
"type": "array",
"items": {
"$ref": "#/definitions/types.ItemOut"
}
}
}
}
]
}
}
}
},
"post": {
"security": [
{
"Bearer": []
}
],
"produces": [
"application/json"
],
"tags": [
"Items"
],
"summary": "Create a new item",
"parameters": [
{
"description": "Item Data",
"name": "payload",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/types.ItemCreate"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/types.ItemSummary"
}
}
}
}
},
"/v1/items/{id}": {
"get": {
"security": [
{
"Bearer": []
}
],
"produces": [
"application/json"
],
"tags": [
"Items"
],
"summary": "Gets a item and fields",
"parameters": [
{
"type": "string",
"description": "Item ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/types.ItemOut"
}
}
}
},
"put": {
"security": [
{
"Bearer": []
}
],
"produces": [
"application/json"
],
"tags": [
"Items"
],
"summary": "updates a item",
"parameters": [
{
"type": "string",
"description": "Item ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/types.ItemOut"
}
}
}
},
"delete": {
"security": [
{
"Bearer": []
}
],
"produces": [
"application/json"
],
"tags": [
"Items"
],
"summary": "deletes a item",
"parameters": [
{
"type": "string",
"description": "Item ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"204": {
"description": ""
}
}
}
},
"/v1/labels": {
"get": {
"security": [
@ -445,7 +609,7 @@
"items": {
"type": "array",
"items": {
"$ref": "#/definitions/types.LocationOut"
"$ref": "#/definitions/types.LocationCount"
}
}
}
@ -1241,6 +1405,94 @@
}
}
},
"types.ItemCreate": {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"labelIds": {
"type": "array",
"items": {
"type": "string"
}
},
"locationId": {
"description": "Edges",
"type": "string"
},
"name": {
"type": "string"
}
}
},
"types.ItemOut": {
"type": "object",
"properties": {
"createdAt": {
"type": "string"
},
"description": {
"type": "string"
},
"id": {
"type": "string"
},
"labels": {
"type": "array",
"items": {
"$ref": "#/definitions/types.LabelSummary"
}
},
"location": {
"description": "Edges",
"$ref": "#/definitions/types.LocationSummary"
},
"manufacturer": {
"type": "string"
},
"modelNumber": {
"type": "string"
},
"name": {
"type": "string"
},
"notes": {
"description": "Extras",
"type": "string"
},
"purchaseFrom": {
"type": "string"
},
"purchasePrice": {
"type": "number"
},
"purchaseTime": {
"description": "Purchase",
"type": "string"
},
"serialNumber": {
"description": "Identifications",
"type": "string"
},
"soldNotes": {
"type": "string"
},
"soldPrice": {
"type": "number"
},
"soldTime": {
"description": "Sold",
"type": "string"
},
"soldTo": {
"type": "string"
},
"updatedAt": {
"type": "string"
}
}
},
"types.ItemSummary": {
"type": "object",
"properties": {
@ -1253,12 +1505,56 @@
"id": {
"type": "string"
},
"locationId": {
"labels": {
"type": "array",
"items": {
"$ref": "#/definitions/types.LabelSummary"
}
},
"location": {
"description": "Edges",
"$ref": "#/definitions/types.LocationSummary"
},
"manufacturer": {
"type": "string"
},
"modelNumber": {
"type": "string"
},
"name": {
"type": "string"
},
"notes": {
"description": "Extras",
"type": "string"
},
"purchaseFrom": {
"type": "string"
},
"purchasePrice": {
"type": "number"
},
"purchaseTime": {
"description": "Purchase",
"type": "string"
},
"serialNumber": {
"description": "Identifications",
"type": "string"
},
"soldNotes": {
"type": "string"
},
"soldPrice": {
"type": "number"
},
"soldTime": {
"description": "Sold",
"type": "string"
},
"soldTo": {
"type": "string"
},
"updatedAt": {
"type": "string"
}
@ -1330,6 +1626,32 @@
}
}
},
"types.LocationCount": {
"type": "object",
"properties": {
"createdAt": {
"type": "string"
},
"description": {
"type": "string"
},
"groupId": {
"type": "string"
},
"id": {
"type": "string"
},
"itemCount": {
"type": "integer"
},
"name": {
"type": "string"
},
"updatedAt": {
"type": "string"
}
}
},
"types.LocationCreate": {
"type": "object",
"properties": {

View file

@ -338,6 +338,66 @@ definitions:
type: string
type: array
type: object
types.ItemCreate:
properties:
description:
type: string
labelIds:
items:
type: string
type: array
locationId:
description: Edges
type: string
name:
type: string
type: object
types.ItemOut:
properties:
createdAt:
type: string
description:
type: string
id:
type: string
labels:
items:
$ref: '#/definitions/types.LabelSummary'
type: array
location:
$ref: '#/definitions/types.LocationSummary'
description: Edges
manufacturer:
type: string
modelNumber:
type: string
name:
type: string
notes:
description: Extras
type: string
purchaseFrom:
type: string
purchasePrice:
type: number
purchaseTime:
description: Purchase
type: string
serialNumber:
description: Identifications
type: string
soldNotes:
type: string
soldPrice:
type: number
soldTime:
description: Sold
type: string
soldTo:
type: string
updatedAt:
type: string
type: object
types.ItemSummary:
properties:
createdAt:
@ -346,10 +406,41 @@ definitions:
type: string
id:
type: string
locationId:
labels:
items:
$ref: '#/definitions/types.LabelSummary'
type: array
location:
$ref: '#/definitions/types.LocationSummary'
description: Edges
manufacturer:
type: string
modelNumber:
type: string
name:
type: string
notes:
description: Extras
type: string
purchaseFrom:
type: string
purchasePrice:
type: number
purchaseTime:
description: Purchase
type: string
serialNumber:
description: Identifications
type: string
soldNotes:
type: string
soldPrice:
type: number
soldTime:
description: Sold
type: string
soldTo:
type: string
updatedAt:
type: string
type: object
@ -396,6 +487,23 @@ definitions:
updatedAt:
type: string
type: object
types.LocationCount:
properties:
createdAt:
type: string
description:
type: string
groupId:
type: string
id:
type: string
itemCount:
type: integer
name:
type: string
updatedAt:
type: string
type: object
types.LocationCreate:
properties:
description:
@ -626,6 +734,103 @@ paths:
summary: Update a User
tags:
- 'Admin: Users'
/v1/items:
get:
produces:
- application/json
responses:
"200":
description: OK
schema:
allOf:
- $ref: '#/definitions/server.Results'
- properties:
items:
items:
$ref: '#/definitions/types.ItemOut'
type: array
type: object
security:
- Bearer: []
summary: Get All Items
tags:
- Items
post:
parameters:
- description: Item Data
in: body
name: payload
required: true
schema:
$ref: '#/definitions/types.ItemCreate'
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/types.ItemSummary'
security:
- Bearer: []
summary: Create a new item
tags:
- Items
/v1/items/{id}:
delete:
parameters:
- description: Item ID
in: path
name: id
required: true
type: string
produces:
- application/json
responses:
"204":
description: ""
security:
- Bearer: []
summary: deletes a item
tags:
- Items
get:
parameters:
- description: Item ID
in: path
name: id
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/types.ItemOut'
security:
- Bearer: []
summary: Gets a item and fields
tags:
- Items
put:
parameters:
- description: Item ID
in: path
name: id
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/types.ItemOut'
security:
- Bearer: []
summary: updates a item
tags:
- Items
/v1/labels:
get:
produces:
@ -736,7 +941,7 @@ paths:
- properties:
items:
items:
$ref: '#/definitions/types.LocationOut'
$ref: '#/definitions/types.LocationCount'
type: array
type: object
security:

View file

@ -61,6 +61,12 @@ func (a *app) newRouter(repos *repo.AllRepos) *chi.Mux {
r.Get(v1Base("/labels/{id}"), v1Handlers.HandleLabelGet())
r.Put(v1Base("/labels/{id}"), v1Handlers.HandleLabelUpdate())
r.Delete(v1Base("/labels/{id}"), v1Handlers.HandleLabelDelete())
r.Get(v1Base("/items"), v1Handlers.HandleItemsGetAll())
r.Post(v1Base("/items"), v1Handlers.HandleItemsCreate())
r.Get(v1Base("/items/{id}"), v1Handlers.HandleItemGet())
r.Put(v1Base("/items/{id}"), v1Handlers.HandleItemUpdate())
r.Delete(v1Base("/items/{id}"), v1Handlers.HandleItemDelete())
})
r.Group(func(r chi.Router) {

View file

@ -0,0 +1,141 @@
package v1
import (
"net/http"
"github.com/hay-kot/content/backend/internal/services"
"github.com/hay-kot/content/backend/internal/types"
"github.com/hay-kot/content/backend/pkgs/server"
)
// HandleItemsGetAll godoc
// @Summary Get All Items
// @Tags Items
// @Produce json
// @Success 200 {object} server.Results{items=[]types.ItemOut}
// @Router /v1/items [GET]
// @Security Bearer
func (ctrl *V1Controller) HandleItemsGetAll() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
user := services.UseUserCtx(r.Context())
items, err := ctrl.svc.Items.GetAll(r.Context(), user.GroupID)
if err != nil {
ctrl.log.Error(err, nil)
server.RespondServerError(w)
return
}
server.Respond(w, http.StatusOK, server.Results{Items: items})
}
}
// HandleItemsCreate godoc
// @Summary Create a new item
// @Tags Items
// @Produce json
// @Param payload body types.ItemCreate true "Item Data"
// @Success 200 {object} types.ItemSummary
// @Router /v1/items [POST]
// @Security Bearer
func (ctrl *V1Controller) HandleItemsCreate() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
createData := types.ItemCreate{}
if err := server.Decode(r, &createData); err != nil {
ctrl.log.Error(err, nil)
server.RespondError(w, http.StatusInternalServerError, err)
return
}
user := services.UseUserCtx(r.Context())
item, err := ctrl.svc.Items.Create(r.Context(), user.GroupID, createData)
if err != nil {
ctrl.log.Error(err, nil)
server.RespondServerError(w)
return
}
server.Respond(w, http.StatusCreated, item)
}
}
// HandleItemDelete godocs
// @Summary deletes a item
// @Tags Items
// @Produce json
// @Param id path string true "Item ID"
// @Success 204
// @Router /v1/items/{id} [DELETE]
// @Security Bearer
func (ctrl *V1Controller) HandleItemDelete() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
uid, user, err := ctrl.partialParseIdAndUser(w, r)
if err != nil {
return
}
err = ctrl.svc.Items.Delete(r.Context(), user.GroupID, uid)
if err != nil {
ctrl.log.Error(err, nil)
server.RespondServerError(w)
return
}
server.Respond(w, http.StatusNoContent, nil)
}
}
// HandleItemGet godocs
// @Summary Gets a item and fields
// @Tags Items
// @Produce json
// @Param id path string true "Item ID"
// @Success 200 {object} types.ItemOut
// @Router /v1/items/{id} [GET]
// @Security Bearer
func (ctrl *V1Controller) HandleItemGet() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
uid, user, err := ctrl.partialParseIdAndUser(w, r)
if err != nil {
return
}
items, err := ctrl.svc.Items.GetOne(r.Context(), user.GroupID, uid)
if err != nil {
ctrl.log.Error(err, nil)
server.RespondServerError(w)
return
}
server.Respond(w, http.StatusOK, items)
}
}
// HandleItemUpdate godocs
// @Summary updates a item
// @Tags Items
// @Produce json
// @Param id path string true "Item ID"
// @Success 200 {object} types.ItemOut
// @Router /v1/items/{id} [PUT]
// @Security Bearer
func (ctrl *V1Controller) HandleItemUpdate() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
body := types.ItemUpdate{}
if err := server.Decode(r, &body); err != nil {
ctrl.log.Error(err, nil)
server.RespondError(w, http.StatusInternalServerError, err)
return
}
uid, user, err := ctrl.partialParseIdAndUser(w, r)
if err != nil {
return
}
body.ID = uid
result, err := ctrl.svc.Items.Update(r.Context(), user.GroupID, body)
if err != nil {
ctrl.log.Error(err, nil)
server.RespondServerError(w)
return
}
server.Respond(w, http.StatusOK, result)
}
}

View file

@ -12,7 +12,7 @@ import (
// @Summary Get All Locations
// @Tags Locations
// @Produce json
// @Success 200 {object} server.Results{items=[]types.LocationOut}
// @Success 200 {object} server.Results{items=[]types.LocationCount}
// @Router /v1/locations [GET]
// @Security Bearer
func (ctrl *V1Controller) HandleLocationGetAll() http.HandlerFunc {