generate API types

This commit is contained in:
Hayden 2022-10-31 21:39:42 -08:00
parent 7b25878c89
commit 8a4207a5be
No known key found for this signature in database
GPG key ID: 17CF79474E257545
6 changed files with 67 additions and 12 deletions

View file

@ -47,15 +47,24 @@ func (ctrl *V1Controller) HandleItemsGetAll() server.HandlerFunc {
return i return i
} }
getBool := func(s string) bool {
b, err := strconv.ParseBool(s)
if err != nil {
return false
}
return b
}
extractQuery := func(r *http.Request) repo.ItemQuery { extractQuery := func(r *http.Request) repo.ItemQuery {
params := r.URL.Query() params := r.URL.Query()
return repo.ItemQuery{ return repo.ItemQuery{
Page: intOrNegativeOne(params.Get("page")), Page: intOrNegativeOne(params.Get("page")),
PageSize: intOrNegativeOne(params.Get("perPage")), PageSize: intOrNegativeOne(params.Get("perPage")),
Search: params.Get("q"), Search: params.Get("q"),
LocationIDs: uuidList(params, "locations"), LocationIDs: uuidList(params, "locations"),
LabelIDs: uuidList(params, "labels"), LabelIDs: uuidList(params, "labels"),
IncludeArchived: getBool(params.Get("includeArchived")),
} }
} }

View file

@ -1274,6 +1274,9 @@ const docTemplate = `{
"repo.ItemOut": { "repo.ItemOut": {
"type": "object", "type": "object",
"properties": { "properties": {
"archived": {
"type": "boolean"
},
"attachments": { "attachments": {
"type": "array", "type": "array",
"items": { "items": {
@ -1383,6 +1386,9 @@ const docTemplate = `{
"repo.ItemSummary": { "repo.ItemSummary": {
"type": "object", "type": "object",
"properties": { "properties": {
"archived": {
"type": "boolean"
},
"createdAt": { "createdAt": {
"type": "string" "type": "string"
}, },
@ -1421,6 +1427,9 @@ const docTemplate = `{
"repo.ItemUpdate": { "repo.ItemUpdate": {
"type": "object", "type": "object",
"properties": { "properties": {
"archived": {
"type": "boolean"
},
"description": { "description": {
"type": "string" "type": "string"
}, },

View file

@ -1266,6 +1266,9 @@
"repo.ItemOut": { "repo.ItemOut": {
"type": "object", "type": "object",
"properties": { "properties": {
"archived": {
"type": "boolean"
},
"attachments": { "attachments": {
"type": "array", "type": "array",
"items": { "items": {
@ -1375,6 +1378,9 @@
"repo.ItemSummary": { "repo.ItemSummary": {
"type": "object", "type": "object",
"properties": { "properties": {
"archived": {
"type": "boolean"
},
"createdAt": { "createdAt": {
"type": "string" "type": "string"
}, },
@ -1413,6 +1419,9 @@
"repo.ItemUpdate": { "repo.ItemUpdate": {
"type": "object", "type": "object",
"properties": { "properties": {
"archived": {
"type": "boolean"
},
"description": { "description": {
"type": "string" "type": "string"
}, },

View file

@ -85,6 +85,8 @@ definitions:
type: object type: object
repo.ItemOut: repo.ItemOut:
properties: properties:
archived:
type: boolean
attachments: attachments:
items: items:
$ref: '#/definitions/repo.ItemAttachment' $ref: '#/definitions/repo.ItemAttachment'
@ -161,6 +163,8 @@ definitions:
type: object type: object
repo.ItemSummary: repo.ItemSummary:
properties: properties:
archived:
type: boolean
createdAt: createdAt:
type: string type: string
description: description:
@ -187,6 +191,8 @@ definitions:
type: object type: object
repo.ItemUpdate: repo.ItemUpdate:
properties: properties:
archived:
type: boolean
description: description:
type: string type: string
fields: fields:

View file

@ -20,12 +20,13 @@ type ItemsRepository struct {
type ( type (
ItemQuery struct { ItemQuery struct {
Page int Page int
PageSize int PageSize int
Search string `json:"search"` Search string `json:"search"`
LocationIDs []uuid.UUID `json:"locationIds"` LocationIDs []uuid.UUID `json:"locationIds"`
LabelIDs []uuid.UUID `json:"labelIds"` LabelIDs []uuid.UUID `json:"labelIds"`
SortBy string `json:"sortBy"` SortBy string `json:"sortBy"`
IncludeArchived bool `json:"includeArchived"`
} }
ItemField struct { ItemField struct {
@ -55,6 +56,7 @@ type (
Description string `json:"description"` Description string `json:"description"`
Quantity int `json:"quantity"` Quantity int `json:"quantity"`
Insured bool `json:"insured"` Insured bool `json:"insured"`
Archived bool `json:"archived"`
// Edges // Edges
LocationID uuid.UUID `json:"locationId"` LocationID uuid.UUID `json:"locationId"`
@ -93,6 +95,7 @@ type (
Description string `json:"description"` Description string `json:"description"`
Quantity int `json:"quantity"` Quantity int `json:"quantity"`
Insured bool `json:"insured"` Insured bool `json:"insured"`
Archived bool `json:"archived"`
CreatedAt time.Time `json:"createdAt"` CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"` UpdatedAt time.Time `json:"updatedAt"`
@ -157,6 +160,7 @@ func mapItemSummary(item *ent.Item) ItemSummary {
Quantity: item.Quantity, Quantity: item.Quantity,
CreatedAt: item.CreatedAt, CreatedAt: item.CreatedAt,
UpdatedAt: item.UpdatedAt, UpdatedAt: item.UpdatedAt,
Archived: item.Archived,
// Edges // Edges
Location: location, Location: location,
@ -276,7 +280,21 @@ func (e *ItemsRepository) GetOneByGroup(ctx context.Context, gid, id uuid.UUID)
// QueryByGroup returns a list of items that belong to a specific group based on the provided query. // QueryByGroup returns a list of items that belong to a specific group based on the provided query.
func (e *ItemsRepository) QueryByGroup(ctx context.Context, gid uuid.UUID, q ItemQuery) (PaginationResult[ItemSummary], error) { func (e *ItemsRepository) QueryByGroup(ctx context.Context, gid uuid.UUID, q ItemQuery) (PaginationResult[ItemSummary], error) {
qb := e.db.Item.Query().Where(item.HasGroupWith(group.ID(gid))) qb := e.db.Item.Query().Where(
item.HasGroupWith(group.ID(gid)),
)
if q.IncludeArchived {
println("include archived")
qb = qb.Where(
item.Or(
item.Archived(true),
item.Archived(false),
),
)
} else {
qb = qb.Where(item.Archived(false))
}
if len(q.LabelIDs) > 0 { if len(q.LabelIDs) > 0 {
labels := make([]predicate.Item, 0, len(q.LabelIDs)) labels := make([]predicate.Item, 0, len(q.LabelIDs))
@ -384,6 +402,7 @@ func (e *ItemsRepository) UpdateByGroup(ctx context.Context, gid uuid.UUID, data
SetSerialNumber(data.SerialNumber). SetSerialNumber(data.SerialNumber).
SetModelNumber(data.ModelNumber). SetModelNumber(data.ModelNumber).
SetManufacturer(data.Manufacturer). SetManufacturer(data.Manufacturer).
SetArchived(data.Archived).
SetPurchaseTime(data.PurchaseTime). SetPurchaseTime(data.PurchaseTime).
SetPurchaseFrom(data.PurchaseFrom). SetPurchaseFrom(data.PurchaseFrom).
SetPurchasePrice(data.PurchasePrice). SetPurchasePrice(data.PurchasePrice).

View file

@ -63,6 +63,7 @@ export interface ItemField {
} }
export interface ItemOut { export interface ItemOut {
archived: boolean;
attachments: ItemAttachment[]; attachments: ItemAttachment[];
children: ItemSummary[]; children: ItemSummary[];
createdAt: Date; createdAt: Date;
@ -107,6 +108,7 @@ export interface ItemOut {
} }
export interface ItemSummary { export interface ItemSummary {
archived: boolean;
createdAt: Date; createdAt: Date;
description: string; description: string;
id: string; id: string;
@ -121,6 +123,7 @@ export interface ItemSummary {
} }
export interface ItemUpdate { export interface ItemUpdate {
archived: boolean;
description: string; description: string;
fields: ItemField[]; fields: ItemField[];
id: string; id: string;