175 lines
3.7 KiB
Go
175 lines
3.7 KiB
Go
package products
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
const createProduct = `-- name: CreateProduct :one
|
|
INSERT INTO products (
|
|
created_at, created_by, updated_at, updated_by, nome, codigo, estoque_total, estoque_corte, estoque_disponivel, preco_de, preco_por
|
|
) VALUES (
|
|
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11
|
|
)
|
|
RETURNING created_by, created_at, updated_by, updated_at, nome, codigo, estoque_total, estoque_corte, estoque_disponivel, preco_de, preco_por
|
|
`
|
|
|
|
type CreateProductParams struct {
|
|
CreatedAt uint64
|
|
CreatedBy string
|
|
UpdatedAt uint64
|
|
UpdatedBy string
|
|
Nome string
|
|
Codigo string
|
|
EstoqueTotal int64
|
|
EstoqueCorte int64
|
|
EstoqueDisponivel int64
|
|
PrecoDe float32
|
|
PrecoPor float32
|
|
}
|
|
|
|
func (q *Queries) CreateProduct(ctx context.Context, userId string, arg CreateProductParams) (Product, error) {
|
|
t := time.Now().Unix()
|
|
row := q.db.QueryRow(ctx, createProduct,
|
|
t,
|
|
userId,
|
|
nil,
|
|
nil,
|
|
arg.Nome,
|
|
arg.Codigo,
|
|
arg.EstoqueTotal,
|
|
arg.EstoqueCorte,
|
|
arg.EstoqueDisponivel,
|
|
arg.PrecoDe,
|
|
arg.PrecoPor,
|
|
)
|
|
var i Product
|
|
err := row.Scan(
|
|
&i.CreatedBy,
|
|
&i.CreatedAt,
|
|
&i.UpdatedBy,
|
|
&i.UpdatedAt,
|
|
&i.Nome,
|
|
&i.Codigo,
|
|
&i.EstoqueTotal,
|
|
&i.EstoqueCorte,
|
|
&i.EstoqueDisponivel,
|
|
&i.PrecoDe,
|
|
&i.PrecoPor,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getProduct = `-- name: GetProduct :one
|
|
SELECT created_by, created_at, updated_by, updated_at, nome, codigo, estoque_total, estoque_corte, estoque_disponivel, preco_de, preco_por FROM products
|
|
WHERE codigo = $1 LIMIT 1
|
|
`
|
|
|
|
func (q *Queries) GetProduct(ctx context.Context, codigo string) (Product, error) {
|
|
row := q.db.QueryRow(ctx, getProduct, codigo)
|
|
var i Product
|
|
err := row.Scan(
|
|
&i.CreatedBy,
|
|
&i.CreatedAt,
|
|
&i.UpdatedBy,
|
|
&i.UpdatedAt,
|
|
&i.Nome,
|
|
&i.Codigo,
|
|
&i.EstoqueTotal,
|
|
&i.EstoqueCorte,
|
|
&i.EstoqueDisponivel,
|
|
&i.PrecoDe,
|
|
&i.PrecoPor,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const listProducts = `-- name: ListProducts :many
|
|
SELECT created_by, created_at, updated_by, updated_at, nome, codigo, estoque_total, estoque_corte, estoque_disponivel, preco_de, preco_por FROM products
|
|
ORDER BY created_at
|
|
`
|
|
|
|
func (q *Queries) ListProducts(ctx context.Context) ([]Product, error) {
|
|
rows, err := q.db.Query(ctx, listProducts)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Product
|
|
for rows.Next() {
|
|
var i Product
|
|
if err := rows.Scan(
|
|
&i.CreatedBy,
|
|
&i.CreatedAt,
|
|
&i.UpdatedBy,
|
|
&i.UpdatedAt,
|
|
&i.Nome,
|
|
&i.Codigo,
|
|
&i.EstoqueTotal,
|
|
&i.EstoqueCorte,
|
|
&i.EstoqueDisponivel,
|
|
&i.PrecoDe,
|
|
&i.PrecoPor,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const updateProduct = `-- name: UpdateProduct :exec
|
|
UPDATE products
|
|
SET
|
|
updated_at = $2,
|
|
updated_by = $3,
|
|
nome = $4,
|
|
estoque_total = $5,
|
|
estoque_corte = $6,
|
|
estoque_disponivel = $7,
|
|
preco_de = $8,
|
|
preco_por = $9
|
|
WHERE codigo = $1
|
|
`
|
|
|
|
type UpdateProductParams struct {
|
|
UpdatedAt uint64
|
|
UpdatedBy string
|
|
Nome string
|
|
Codigo string
|
|
EstoqueTotal int64
|
|
EstoqueCorte int64
|
|
EstoqueDisponivel int64
|
|
PrecoDe float32
|
|
PrecoPor float32
|
|
}
|
|
|
|
func (q *Queries) UpdateProduct(ctx context.Context, userId string, arg UpdateProductParams) error {
|
|
t := time.Now().Unix()
|
|
_, err := q.db.Exec(ctx, updateProduct,
|
|
arg.Codigo,
|
|
t,
|
|
userId,
|
|
arg.Nome,
|
|
arg.EstoqueTotal,
|
|
arg.EstoqueCorte,
|
|
arg.EstoqueDisponivel,
|
|
arg.PrecoDe,
|
|
arg.PrecoPor,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const deleteProduct = `-- name: DeleteProduct :exec
|
|
DELETE FROM products
|
|
WHERE codigo = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteProduct(ctx context.Context, codigo string) error {
|
|
_, err := q.db.Exec(ctx, deleteProduct, codigo)
|
|
return err
|
|
}
|