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
}
getBool := func(s string) bool {
b, err := strconv.ParseBool(s)
if err != nil {
return false
}
return b
}
extractQuery := func(r *http.Request) repo.ItemQuery {
params := r.URL.Query()
return repo.ItemQuery{
Page: intOrNegativeOne(params.Get("page")),
PageSize: intOrNegativeOne(params.Get("perPage")),
Search: params.Get("q"),
LocationIDs: uuidList(params, "locations"),
LabelIDs: uuidList(params, "labels"),
Page: intOrNegativeOne(params.Get("page")),
PageSize: intOrNegativeOne(params.Get("perPage")),
Search: params.Get("q"),
LocationIDs: uuidList(params, "locations"),
LabelIDs: uuidList(params, "labels"),
IncludeArchived: getBool(params.Get("includeArchived")),
}
}

View file

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

View file

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

View file

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

View file

@ -20,12 +20,13 @@ type ItemsRepository struct {
type (
ItemQuery struct {
Page int
PageSize int
Search string `json:"search"`
LocationIDs []uuid.UUID `json:"locationIds"`
LabelIDs []uuid.UUID `json:"labelIds"`
SortBy string `json:"sortBy"`
Page int
PageSize int
Search string `json:"search"`
LocationIDs []uuid.UUID `json:"locationIds"`
LabelIDs []uuid.UUID `json:"labelIds"`
SortBy string `json:"sortBy"`
IncludeArchived bool `json:"includeArchived"`
}
ItemField struct {
@ -55,6 +56,7 @@ type (
Description string `json:"description"`
Quantity int `json:"quantity"`
Insured bool `json:"insured"`
Archived bool `json:"archived"`
// Edges
LocationID uuid.UUID `json:"locationId"`
@ -93,6 +95,7 @@ type (
Description string `json:"description"`
Quantity int `json:"quantity"`
Insured bool `json:"insured"`
Archived bool `json:"archived"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
@ -157,6 +160,7 @@ func mapItemSummary(item *ent.Item) ItemSummary {
Quantity: item.Quantity,
CreatedAt: item.CreatedAt,
UpdatedAt: item.UpdatedAt,
Archived: item.Archived,
// Edges
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.
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 {
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).
SetModelNumber(data.ModelNumber).
SetManufacturer(data.Manufacturer).
SetArchived(data.Archived).
SetPurchaseTime(data.PurchaseTime).
SetPurchaseFrom(data.PurchaseFrom).
SetPurchasePrice(data.PurchasePrice).

View file

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