feat: add components management functionality
This commit is contained in:
186
sqlc_gen/component.sql.go
Normal file
186
sqlc_gen/component.sql.go
Normal file
@@ -0,0 +1,186 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.30.0
|
||||
// source: component.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const createComponent = `-- name: CreateComponent :one
|
||||
INSERT INTO components (component_type_id,name, description,unit,min_quantity,metadata, created_at)
|
||||
VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5,
|
||||
$6,
|
||||
$7
|
||||
)
|
||||
RETURNING id, component_type_id, name, description, unit, total_quantity, min_quantity, metadata, created_at, updated_at
|
||||
`
|
||||
|
||||
type CreateComponentParams struct {
|
||||
ComponentTypeID int64 `db:"component_type_id" json:"componentTypeId"`
|
||||
Name string `db:"name" json:"name"`
|
||||
Description pgtype.Text `db:"description" json:"description"`
|
||||
Unit string `db:"unit" json:"unit"`
|
||||
MinQuantity int32 `db:"min_quantity" json:"minQuantity"`
|
||||
Metadata []byte `db:"metadata" json:"metadata"`
|
||||
CreatedAt time.Time `db:"created_at" json:"createdAt"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateComponent(ctx context.Context, arg CreateComponentParams) (Component, error) {
|
||||
row := q.db.QueryRow(ctx, createComponent,
|
||||
arg.ComponentTypeID,
|
||||
arg.Name,
|
||||
arg.Description,
|
||||
arg.Unit,
|
||||
arg.MinQuantity,
|
||||
arg.Metadata,
|
||||
arg.CreatedAt,
|
||||
)
|
||||
var i Component
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ComponentTypeID,
|
||||
&i.Name,
|
||||
&i.Description,
|
||||
&i.Unit,
|
||||
&i.TotalQuantity,
|
||||
&i.MinQuantity,
|
||||
&i.Metadata,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const deleteComponent = `-- name: DeleteComponent :execrows
|
||||
DELETE FROM components
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteComponent(ctx context.Context, id int64) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, deleteComponent, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const getComponentByID = `-- name: GetComponentByID :one
|
||||
SELECT id, component_type_id, name, description, unit, total_quantity, min_quantity, metadata, created_at, updated_at FROM components
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetComponentByID(ctx context.Context, id int64) (Component, error) {
|
||||
row := q.db.QueryRow(ctx, getComponentByID, id)
|
||||
var i Component
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ComponentTypeID,
|
||||
&i.Name,
|
||||
&i.Description,
|
||||
&i.Unit,
|
||||
&i.TotalQuantity,
|
||||
&i.MinQuantity,
|
||||
&i.Metadata,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listComponents = `-- name: ListComponents :many
|
||||
SELECT id, component_type_id, name, description, unit, total_quantity, min_quantity, metadata, created_at, updated_at FROM components
|
||||
ORDER BY created_at DESC
|
||||
`
|
||||
|
||||
func (q *Queries) ListComponents(ctx context.Context) ([]Component, error) {
|
||||
rows, err := q.db.Query(ctx, listComponents)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Component
|
||||
for rows.Next() {
|
||||
var i Component
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.ComponentTypeID,
|
||||
&i.Name,
|
||||
&i.Description,
|
||||
&i.Unit,
|
||||
&i.TotalQuantity,
|
||||
&i.MinQuantity,
|
||||
&i.Metadata,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const updateComponent = `-- name: UpdateComponent :one
|
||||
UPDATE components
|
||||
SET name = CASE WHEN $1 = '' THEN name ELSE $1 END,
|
||||
component_type_id = coalesce($2, component_type_id),
|
||||
description = coalesce($3, description),
|
||||
unit = coalesce($4, unit),
|
||||
min_quantity = coalesce($5, min_quantity),
|
||||
metadata = coalesce($6, metadata),
|
||||
updated_at = $7
|
||||
WHERE id = $8
|
||||
RETURNING id, component_type_id, name, description, unit, total_quantity, min_quantity, metadata, created_at, updated_at
|
||||
`
|
||||
|
||||
type UpdateComponentParams struct {
|
||||
Name interface{} `db:"name" json:"name"`
|
||||
ComponentTypeID int64 `db:"component_type_id" json:"componentTypeId"`
|
||||
Description pgtype.Text `db:"description" json:"description"`
|
||||
Unit string `db:"unit" json:"unit"`
|
||||
MinQuantity int32 `db:"min_quantity" json:"minQuantity"`
|
||||
Metadata []byte `db:"metadata" json:"metadata"`
|
||||
UpdatedAt time.Time `db:"updated_at" json:"updatedAt"`
|
||||
ID int64 `db:"id" json:"id"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateComponent(ctx context.Context, arg UpdateComponentParams) (Component, error) {
|
||||
row := q.db.QueryRow(ctx, updateComponent,
|
||||
arg.Name,
|
||||
arg.ComponentTypeID,
|
||||
arg.Description,
|
||||
arg.Unit,
|
||||
arg.MinQuantity,
|
||||
arg.Metadata,
|
||||
arg.UpdatedAt,
|
||||
arg.ID,
|
||||
)
|
||||
var i Component
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ComponentTypeID,
|
||||
&i.Name,
|
||||
&i.Description,
|
||||
&i.Unit,
|
||||
&i.TotalQuantity,
|
||||
&i.MinQuantity,
|
||||
&i.Metadata,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
Reference in New Issue
Block a user