2023-03-05 23:35:41 +07:00
|
|
|
// Code generated by sqlc. DO NOT EDIT.
|
|
|
|
// versions:
|
|
|
|
// sqlc v1.17.2
|
2023-03-16 20:37:20 +07:00
|
|
|
// source: product.sql
|
2023-03-05 23:35:41 +07:00
|
|
|
|
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
const createProduct = `-- name: CreateProduct :one
|
|
|
|
INSERT INTO products (
|
|
|
|
merchant_id,
|
|
|
|
name,
|
|
|
|
selling_price,
|
2023-03-22 13:40:18 +07:00
|
|
|
product_type_id,
|
2023-03-05 23:35:41 +07:00
|
|
|
purchase_price,
|
2023-03-16 20:37:20 +07:00
|
|
|
product_category_id,
|
2023-04-05 14:04:49 +07:00
|
|
|
image,
|
2023-03-05 23:35:41 +07:00
|
|
|
stock
|
|
|
|
) VALUES (
|
2023-04-05 14:04:49 +07:00
|
|
|
$1, $2, $3, $4, $5, $6, $7, $8
|
2023-03-05 23:35:41 +07:00
|
|
|
)
|
2023-04-05 14:04:49 +07:00
|
|
|
RETURNING id, merchant_id, product_type_id, index_id, name, selling_price, purchase_price, stock, created_at, updated_at, product_category_id, image
|
2023-03-05 23:35:41 +07:00
|
|
|
`
|
|
|
|
|
|
|
|
type CreateProductParams struct {
|
2023-04-05 14:04:49 +07:00
|
|
|
MerchantID uuid.UUID `json:"merchant_id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
SellingPrice float64 `json:"selling_price"`
|
|
|
|
ProductTypeID int16 `json:"product_type_id"`
|
|
|
|
PurchasePrice float64 `json:"purchase_price"`
|
|
|
|
ProductCategoryID uuid.UUID `json:"product_category_id"`
|
|
|
|
Image sql.NullString `json:"image"`
|
|
|
|
Stock float64 `json:"stock"`
|
2023-03-05 23:35:41 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) CreateProduct(ctx context.Context, arg CreateProductParams) (Product, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, createProduct,
|
|
|
|
arg.MerchantID,
|
|
|
|
arg.Name,
|
|
|
|
arg.SellingPrice,
|
2023-03-22 13:40:18 +07:00
|
|
|
arg.ProductTypeID,
|
2023-03-05 23:35:41 +07:00
|
|
|
arg.PurchasePrice,
|
2023-03-16 20:37:20 +07:00
|
|
|
arg.ProductCategoryID,
|
2023-04-05 14:04:49 +07:00
|
|
|
arg.Image,
|
2023-03-05 23:35:41 +07:00
|
|
|
arg.Stock,
|
|
|
|
)
|
|
|
|
var i Product
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.MerchantID,
|
2023-03-22 13:40:18 +07:00
|
|
|
&i.ProductTypeID,
|
2023-03-05 23:35:41 +07:00
|
|
|
&i.IndexID,
|
|
|
|
&i.Name,
|
|
|
|
&i.SellingPrice,
|
|
|
|
&i.PurchasePrice,
|
|
|
|
&i.Stock,
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.UpdatedAt,
|
2023-03-16 16:55:45 +07:00
|
|
|
&i.ProductCategoryID,
|
2023-04-05 14:04:49 +07:00
|
|
|
&i.Image,
|
2023-03-05 23:35:41 +07:00
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
|
|
|
const deleteProduct = `-- name: DeleteProduct :exec
|
|
|
|
DELETE FROM products WHERE id = $1
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) DeleteProduct(ctx context.Context, id uuid.UUID) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, deleteProduct, id)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
const getProduct = `-- name: GetProduct :one
|
2023-04-05 14:04:49 +07:00
|
|
|
SELECT id, merchant_id, product_type_id, index_id, name, selling_price, purchase_price, stock, created_at, updated_at, product_category_id, image FROM products
|
2023-03-05 23:35:41 +07:00
|
|
|
WHERE id = $1
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) GetProduct(ctx context.Context, id uuid.UUID) (Product, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, getProduct, id)
|
|
|
|
var i Product
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.MerchantID,
|
2023-03-22 13:40:18 +07:00
|
|
|
&i.ProductTypeID,
|
2023-03-05 23:35:41 +07:00
|
|
|
&i.IndexID,
|
|
|
|
&i.Name,
|
|
|
|
&i.SellingPrice,
|
|
|
|
&i.PurchasePrice,
|
|
|
|
&i.Stock,
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.UpdatedAt,
|
2023-03-16 16:55:45 +07:00
|
|
|
&i.ProductCategoryID,
|
2023-04-05 14:04:49 +07:00
|
|
|
&i.Image,
|
2023-03-05 23:35:41 +07:00
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
2023-03-06 15:15:11 +07:00
|
|
|
const getStockForUpdateStock = `-- name: GetStockForUpdateStock :one
|
2023-04-05 14:04:49 +07:00
|
|
|
SELECT id, merchant_id, product_type_id, index_id, name, selling_price, purchase_price, stock, created_at, updated_at, product_category_id, image FROM products
|
2023-03-06 15:15:11 +07:00
|
|
|
WHERE id = $1
|
|
|
|
LIMIT 1
|
|
|
|
FOR NO KEY UPDATE
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) GetStockForUpdateStock(ctx context.Context, id uuid.UUID) (Product, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, getStockForUpdateStock, id)
|
|
|
|
var i Product
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.MerchantID,
|
2023-03-22 13:40:18 +07:00
|
|
|
&i.ProductTypeID,
|
2023-03-06 15:15:11 +07:00
|
|
|
&i.IndexID,
|
|
|
|
&i.Name,
|
|
|
|
&i.SellingPrice,
|
|
|
|
&i.PurchasePrice,
|
|
|
|
&i.Stock,
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.UpdatedAt,
|
2023-03-16 16:55:45 +07:00
|
|
|
&i.ProductCategoryID,
|
2023-04-05 14:04:49 +07:00
|
|
|
&i.Image,
|
2023-03-06 15:15:11 +07:00
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
2023-03-05 23:35:41 +07:00
|
|
|
const listProducts = `-- name: ListProducts :many
|
2023-04-05 14:04:49 +07:00
|
|
|
SELECT id, merchant_id, product_type_id, index_id, name, selling_price, purchase_price, stock, created_at, updated_at, product_category_id, image FROM products
|
2023-03-15 15:00:36 +07:00
|
|
|
WHERE merchant_id = $1
|
2023-03-05 23:35:41 +07:00
|
|
|
ORDER BY index_id
|
2023-03-15 15:00:36 +07:00
|
|
|
LIMIT $2
|
|
|
|
OFFSET $3
|
2023-03-05 23:35:41 +07:00
|
|
|
`
|
|
|
|
|
|
|
|
type ListProductsParams struct {
|
2023-03-15 15:00:36 +07:00
|
|
|
MerchantID uuid.UUID `json:"merchant_id"`
|
|
|
|
Limit int32 `json:"limit"`
|
|
|
|
Offset int32 `json:"offset"`
|
2023-03-05 23:35:41 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) ListProducts(ctx context.Context, arg ListProductsParams) ([]Product, error) {
|
2023-03-15 15:00:36 +07:00
|
|
|
rows, err := q.db.QueryContext(ctx, listProducts, arg.MerchantID, arg.Limit, arg.Offset)
|
2023-03-05 23:35:41 +07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
2023-03-12 11:01:43 +07:00
|
|
|
items := []Product{}
|
2023-03-05 23:35:41 +07:00
|
|
|
for rows.Next() {
|
|
|
|
var i Product
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.MerchantID,
|
2023-03-22 13:40:18 +07:00
|
|
|
&i.ProductTypeID,
|
2023-03-05 23:35:41 +07:00
|
|
|
&i.IndexID,
|
|
|
|
&i.Name,
|
|
|
|
&i.SellingPrice,
|
|
|
|
&i.PurchasePrice,
|
|
|
|
&i.Stock,
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.UpdatedAt,
|
2023-03-16 16:55:45 +07:00
|
|
|
&i.ProductCategoryID,
|
2023-04-05 14:04:49 +07:00
|
|
|
&i.Image,
|
2023-03-05 23:35:41 +07:00
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
items = append(items, i)
|
|
|
|
}
|
|
|
|
if err := rows.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateProduct = `-- name: UpdateProduct :one
|
|
|
|
UPDATE products
|
2023-04-05 14:04:49 +07:00
|
|
|
SET name = $2,
|
|
|
|
selling_price = $3,
|
|
|
|
purchase_price = $4,
|
|
|
|
product_category_id = $5,
|
|
|
|
image = $6,
|
|
|
|
updated_at = $7
|
2023-03-05 23:35:41 +07:00
|
|
|
WHERE id = $1
|
2023-04-05 14:04:49 +07:00
|
|
|
RETURNING id, merchant_id, product_type_id, index_id, name, selling_price, purchase_price, stock, created_at, updated_at, product_category_id, image
|
2023-03-05 23:35:41 +07:00
|
|
|
`
|
|
|
|
|
|
|
|
type UpdateProductParams struct {
|
2023-04-05 14:04:49 +07:00
|
|
|
ID uuid.UUID `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
SellingPrice float64 `json:"selling_price"`
|
|
|
|
PurchasePrice float64 `json:"purchase_price"`
|
|
|
|
ProductCategoryID uuid.UUID `json:"product_category_id"`
|
|
|
|
Image sql.NullString `json:"image"`
|
|
|
|
UpdatedAt sql.NullTime `json:"updated_at"`
|
2023-03-05 23:35:41 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) UpdateProduct(ctx context.Context, arg UpdateProductParams) (Product, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, updateProduct,
|
|
|
|
arg.ID,
|
|
|
|
arg.Name,
|
|
|
|
arg.SellingPrice,
|
|
|
|
arg.PurchasePrice,
|
2023-03-16 20:37:20 +07:00
|
|
|
arg.ProductCategoryID,
|
2023-04-05 14:04:49 +07:00
|
|
|
arg.Image,
|
2023-03-05 23:35:41 +07:00
|
|
|
arg.UpdatedAt,
|
|
|
|
)
|
|
|
|
var i Product
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.MerchantID,
|
2023-03-22 13:40:18 +07:00
|
|
|
&i.ProductTypeID,
|
2023-03-05 23:35:41 +07:00
|
|
|
&i.IndexID,
|
|
|
|
&i.Name,
|
|
|
|
&i.SellingPrice,
|
|
|
|
&i.PurchasePrice,
|
|
|
|
&i.Stock,
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.UpdatedAt,
|
2023-03-16 16:55:45 +07:00
|
|
|
&i.ProductCategoryID,
|
2023-04-05 14:04:49 +07:00
|
|
|
&i.Image,
|
2023-03-05 23:35:41 +07:00
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
2023-03-06 15:15:11 +07:00
|
|
|
const updateProductStock = `-- name: UpdateProductStock :exec
|
2023-03-05 23:35:41 +07:00
|
|
|
UPDATE products
|
|
|
|
SET stock = $1
|
|
|
|
WHERE id = $2
|
|
|
|
`
|
|
|
|
|
|
|
|
type UpdateProductStockParams struct {
|
|
|
|
Stock float64 `json:"stock"`
|
|
|
|
ID uuid.UUID `json:"id"`
|
|
|
|
}
|
|
|
|
|
2023-03-06 15:15:11 +07:00
|
|
|
func (q *Queries) UpdateProductStock(ctx context.Context, arg UpdateProductStockParams) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, updateProductStock, arg.Stock, arg.ID)
|
|
|
|
return err
|
2023-03-05 23:35:41 +07:00
|
|
|
}
|