add product type in products

This commit is contained in:
nochill 2023-03-22 13:40:18 +07:00
parent 5604f34e97
commit 46236aa71b
11 changed files with 47 additions and 16 deletions

View File

@ -15,8 +15,9 @@ import (
type createProductRequest struct { type createProductRequest struct {
Name string `json:"name" binding:"required"` Name string `json:"name" binding:"required"`
SellingPrice float64 `json:"selling_price" binding:"required"` SellingPrice float64 `json:"selling_price" binding:"required"`
ProductTypeID int16 `json:"product_type_id" binding:"required"`
PurchasePrice float64 `json:"purchase_price" binding:"required"` PurchasePrice float64 `json:"purchase_price" binding:"required"`
ProductCategoryID string `json:"product_category_id" binding:"required,uuid"` ProductCategoryID string `json:"product_category_id" binding:"required"`
Stock float64 `json:"stock" binding:"number"` Stock float64 `json:"stock" binding:"number"`
} }
@ -32,6 +33,7 @@ func (server *Server) createProduct(ctx *gin.Context) {
arg := db.CreateProductParams{ arg := db.CreateProductParams{
MerchantID: authPayload.MerchantID, MerchantID: authPayload.MerchantID,
Name: req.Name, Name: req.Name,
ProductTypeID: req.ProductTypeID,
SellingPrice: req.SellingPrice, SellingPrice: req.SellingPrice,
PurchasePrice: req.PurchasePrice, PurchasePrice: req.PurchasePrice,
ProductCategoryID: uuid.MustParse(req.ProductCategoryID), ProductCategoryID: uuid.MustParse(req.ProductCategoryID),

View File

@ -35,11 +35,11 @@ func TestCreateProductCategory(t *testing.T) {
"merchant_id": productCategory.MerchantID, "merchant_id": productCategory.MerchantID,
}, },
setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) { setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {
addAuthorization(t, request, tokenMaker, authorizationTypeBearer, util.RandomEmail(), "54b8a2d9-16be-4239-8828-5daa317028dc", time.Minute) addAuthorization(t, request, tokenMaker, authorizationTypeBearer, util.RandomEmail(), "7e525a4b-4208-4f05-99a1-a75df475dd9b", time.Minute)
}, },
buildStubs: func(store *mockdb.MockStore) { buildStubs: func(store *mockdb.MockStore) {
arg := db.CreateProductCategoryParams{ arg := db.CreateProductCategoryParams{
MerchantID: uuid.MustParse("54b8a2d9-16be-4239-8828-5daa317028dc"), MerchantID: uuid.MustParse("7e525a4b-4208-4f05-99a1-a75df475dd9b"),
Name: productCategory.Name, Name: productCategory.Name,
} }
store.EXPECT(). store.EXPECT().
@ -83,7 +83,7 @@ func TestCreateProductCategory(t *testing.T) {
func createRandomProductCategory() db.ProductCategory { func createRandomProductCategory() db.ProductCategory {
return db.ProductCategory{ return db.ProductCategory{
ID: uuid.New(), ID: uuid.New(),
MerchantID: uuid.MustParse("54b8a2d9-16be-4239-8828-5daa317028dc"), MerchantID: uuid.MustParse("7e525a4b-4208-4f05-99a1-a75df475dd9b"),
Name: util.RandomString(5), Name: util.RandomString(5),
} }
} }

View File

@ -21,7 +21,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
var MERCHANTID = "54b8a2d9-16be-4239-8828-5daa317028dc" var MERCHANTID = "7e525a4b-4208-4f05-99a1-a75df475dd9b"
func TestGetProductApi(t *testing.T) { func TestGetProductApi(t *testing.T) {
product := randomProduct(MERCHANTID) product := randomProduct(MERCHANTID)

View File

@ -8,6 +8,16 @@ CREATE TABLE users(
"updated_at" timestamp default(now()) "updated_at" timestamp default(now())
); );
CREATE TABLE product_types(
"id" smallserial primary key not null,
"name" varchar(35) not null
);
INSERT INTO product_types
VALUES
( 1, 'Barang Jadi'),
( 2, 'Bahan Baku');
CREATE TABLE merchants ( CREATE TABLE merchants (
"id" uuid default gen_random_uuid() primary key not null, "id" uuid default gen_random_uuid() primary key not null,
"index_id" bigserial not null, "index_id" bigserial not null,
@ -27,6 +37,7 @@ create table suppliers (
"created_at" timestamp default(now()), "created_at" timestamp default(now()),
"updated_at" timestamp default(now()) "updated_at" timestamp default(now())
); );
CREATE TABLE customers ( CREATE TABLE customers (
"id" uuid default gen_random_uuid() primary key not null, "id" uuid default gen_random_uuid() primary key not null,
"index_id" bigserial not null, "index_id" bigserial not null,
@ -37,9 +48,11 @@ CREATE TABLE customers (
"created_at" timestamp default(now()), "created_at" timestamp default(now()),
"updated_at" timestamp default(now()) "updated_at" timestamp default(now())
); );
CREATE TABLE products ( CREATE TABLE products (
"id" uuid default gen_random_uuid() primary key not null, "id" uuid default gen_random_uuid() primary key not null,
"merchant_id" uuid references "merchants"("id") not null, "merchant_id" uuid references "merchants"("id") not null,
"product_type_id" smallint references "product_types"("id") default(1) not null,
"index_id" bigserial not null, "index_id" bigserial not null,
"name" varchar not null, "name" varchar not null,
"selling_price" double precision default(0::double precision) NOT NULL, "selling_price" double precision default(0::double precision) NOT NULL,

View File

@ -3,11 +3,12 @@ INSERT INTO products (
merchant_id, merchant_id,
name, name,
selling_price, selling_price,
product_type_id,
purchase_price, purchase_price,
product_category_id, product_category_id,
stock stock
) VALUES ( ) VALUES (
$1, $2, $3, $4, $5, $6 $1, $2, $3, $4, $5, $6, $7
) )
RETURNING *; RETURNING *;

View File

@ -11,7 +11,7 @@ import (
func createRandomCustomer(t *testing.T) (Customer, CreateCustomersParams) { func createRandomCustomer(t *testing.T) (Customer, CreateCustomersParams) {
arg := CreateCustomersParams{ arg := CreateCustomersParams{
MerchantID: uuid.MustParse("54b8a2d9-16be-4239-8828-5daa317028dc"), MerchantID: uuid.MustParse("7e525a4b-4208-4f05-99a1-a75df475dd9b"),
Name: util.RandomString(10), Name: util.RandomString(10),
} }

View File

@ -79,6 +79,7 @@ type Merchant struct {
type Product struct { type Product struct {
ID uuid.UUID `json:"id"` ID uuid.UUID `json:"id"`
MerchantID uuid.UUID `json:"merchant_id"` MerchantID uuid.UUID `json:"merchant_id"`
ProductTypeID int16 `json:"product_type_id"`
IndexID int64 `json:"index_id"` IndexID int64 `json:"index_id"`
Name string `json:"name"` Name string `json:"name"`
SellingPrice float64 `json:"selling_price"` SellingPrice float64 `json:"selling_price"`
@ -96,6 +97,11 @@ type ProductCategory struct {
Name string `json:"name"` Name string `json:"name"`
} }
type ProductType struct {
ID int16 `json:"id"`
Name string `json:"name"`
}
type PurchaseOrder struct { type PurchaseOrder struct {
ID uuid.UUID `json:"id"` ID uuid.UUID `json:"id"`
SupplierID uuid.UUID `json:"supplier_id"` SupplierID uuid.UUID `json:"supplier_id"`

View File

@ -17,19 +17,21 @@ INSERT INTO products (
merchant_id, merchant_id,
name, name,
selling_price, selling_price,
product_type_id,
purchase_price, purchase_price,
product_category_id, product_category_id,
stock stock
) VALUES ( ) VALUES (
$1, $2, $3, $4, $5, $6 $1, $2, $3, $4, $5, $6, $7
) )
RETURNING id, merchant_id, index_id, name, selling_price, purchase_price, stock, created_at, updated_at, product_category_id RETURNING id, merchant_id, product_type_id, index_id, name, selling_price, purchase_price, stock, created_at, updated_at, product_category_id
` `
type CreateProductParams struct { type CreateProductParams struct {
MerchantID uuid.UUID `json:"merchant_id"` MerchantID uuid.UUID `json:"merchant_id"`
Name string `json:"name"` Name string `json:"name"`
SellingPrice float64 `json:"selling_price"` SellingPrice float64 `json:"selling_price"`
ProductTypeID int16 `json:"product_type_id"`
PurchasePrice float64 `json:"purchase_price"` PurchasePrice float64 `json:"purchase_price"`
ProductCategoryID uuid.UUID `json:"product_category_id"` ProductCategoryID uuid.UUID `json:"product_category_id"`
Stock float64 `json:"stock"` Stock float64 `json:"stock"`
@ -40,6 +42,7 @@ func (q *Queries) CreateProduct(ctx context.Context, arg CreateProductParams) (P
arg.MerchantID, arg.MerchantID,
arg.Name, arg.Name,
arg.SellingPrice, arg.SellingPrice,
arg.ProductTypeID,
arg.PurchasePrice, arg.PurchasePrice,
arg.ProductCategoryID, arg.ProductCategoryID,
arg.Stock, arg.Stock,
@ -48,6 +51,7 @@ func (q *Queries) CreateProduct(ctx context.Context, arg CreateProductParams) (P
err := row.Scan( err := row.Scan(
&i.ID, &i.ID,
&i.MerchantID, &i.MerchantID,
&i.ProductTypeID,
&i.IndexID, &i.IndexID,
&i.Name, &i.Name,
&i.SellingPrice, &i.SellingPrice,
@ -70,7 +74,7 @@ func (q *Queries) DeleteProduct(ctx context.Context, id uuid.UUID) error {
} }
const getProduct = `-- name: GetProduct :one const getProduct = `-- name: GetProduct :one
SELECT id, merchant_id, index_id, name, selling_price, purchase_price, stock, created_at, updated_at, product_category_id FROM products SELECT id, merchant_id, product_type_id, index_id, name, selling_price, purchase_price, stock, created_at, updated_at, product_category_id FROM products
WHERE id = $1 WHERE id = $1
` `
@ -80,6 +84,7 @@ func (q *Queries) GetProduct(ctx context.Context, id uuid.UUID) (Product, error)
err := row.Scan( err := row.Scan(
&i.ID, &i.ID,
&i.MerchantID, &i.MerchantID,
&i.ProductTypeID,
&i.IndexID, &i.IndexID,
&i.Name, &i.Name,
&i.SellingPrice, &i.SellingPrice,
@ -93,7 +98,7 @@ func (q *Queries) GetProduct(ctx context.Context, id uuid.UUID) (Product, error)
} }
const getStockForUpdateStock = `-- name: GetStockForUpdateStock :one const getStockForUpdateStock = `-- name: GetStockForUpdateStock :one
SELECT id, merchant_id, index_id, name, selling_price, purchase_price, stock, created_at, updated_at, product_category_id FROM products SELECT id, merchant_id, product_type_id, index_id, name, selling_price, purchase_price, stock, created_at, updated_at, product_category_id FROM products
WHERE id = $1 WHERE id = $1
LIMIT 1 LIMIT 1
FOR NO KEY UPDATE FOR NO KEY UPDATE
@ -105,6 +110,7 @@ func (q *Queries) GetStockForUpdateStock(ctx context.Context, id uuid.UUID) (Pro
err := row.Scan( err := row.Scan(
&i.ID, &i.ID,
&i.MerchantID, &i.MerchantID,
&i.ProductTypeID,
&i.IndexID, &i.IndexID,
&i.Name, &i.Name,
&i.SellingPrice, &i.SellingPrice,
@ -118,7 +124,7 @@ func (q *Queries) GetStockForUpdateStock(ctx context.Context, id uuid.UUID) (Pro
} }
const listProducts = `-- name: ListProducts :many const listProducts = `-- name: ListProducts :many
SELECT id, merchant_id, index_id, name, selling_price, purchase_price, stock, created_at, updated_at, product_category_id FROM products SELECT id, merchant_id, product_type_id, index_id, name, selling_price, purchase_price, stock, created_at, updated_at, product_category_id FROM products
WHERE merchant_id = $1 WHERE merchant_id = $1
ORDER BY index_id ORDER BY index_id
LIMIT $2 LIMIT $2
@ -143,6 +149,7 @@ func (q *Queries) ListProducts(ctx context.Context, arg ListProductsParams) ([]P
if err := rows.Scan( if err := rows.Scan(
&i.ID, &i.ID,
&i.MerchantID, &i.MerchantID,
&i.ProductTypeID,
&i.IndexID, &i.IndexID,
&i.Name, &i.Name,
&i.SellingPrice, &i.SellingPrice,
@ -169,7 +176,7 @@ const updateProduct = `-- name: UpdateProduct :one
UPDATE products UPDATE products
SET name = $2, selling_price = $3, purchase_price = $4, product_category_id = $5, updated_at = $6 SET name = $2, selling_price = $3, purchase_price = $4, product_category_id = $5, updated_at = $6
WHERE id = $1 WHERE id = $1
RETURNING id, merchant_id, index_id, name, selling_price, purchase_price, stock, created_at, updated_at, product_category_id RETURNING id, merchant_id, product_type_id, index_id, name, selling_price, purchase_price, stock, created_at, updated_at, product_category_id
` `
type UpdateProductParams struct { type UpdateProductParams struct {
@ -194,6 +201,7 @@ func (q *Queries) UpdateProduct(ctx context.Context, arg UpdateProductParams) (P
err := row.Scan( err := row.Scan(
&i.ID, &i.ID,
&i.MerchantID, &i.MerchantID,
&i.ProductTypeID,
&i.IndexID, &i.IndexID,
&i.Name, &i.Name,
&i.SellingPrice, &i.SellingPrice,

View File

@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
var merchantID = uuid.MustParse("54b8a2d9-16be-4239-8828-5daa317028dc") var merchantID = uuid.MustParse("7e525a4b-4208-4f05-99a1-a75df475dd9b")
func createRandomProductCategory(t *testing.T) ProductCategory { func createRandomProductCategory(t *testing.T) ProductCategory {
arg := CreateProductCategoryParams{ arg := CreateProductCategoryParams{

View File

@ -19,7 +19,8 @@ func createRandomProduct(t *testing.T) (Product, CreateProductParams) {
productCategory := createRandomProductCategory(t) productCategory := createRandomProductCategory(t)
arg := CreateProductParams{ arg := CreateProductParams{
MerchantID: uuid.MustParse("54b8a2d9-16be-4239-8828-5daa317028dc"), MerchantID: uuid.MustParse("7e525a4b-4208-4f05-99a1-a75df475dd9b"),
ProductTypeID: 1,
Name: util.RandomString(10), Name: util.RandomString(10),
SellingPrice: sellingPrice, SellingPrice: sellingPrice,
PurchasePrice: purchasePrice, PurchasePrice: purchasePrice,

View File

@ -11,7 +11,7 @@ import (
func createRandomSupplier(t *testing.T) (Supplier, CreateSuppliersParams) { func createRandomSupplier(t *testing.T) (Supplier, CreateSuppliersParams) {
arg := CreateSuppliersParams{ arg := CreateSuppliersParams{
MerchantID: uuid.MustParse("54b8a2d9-16be-4239-8828-5daa317028dc"), MerchantID: uuid.MustParse("7e525a4b-4208-4f05-99a1-a75df475dd9b"),
Name: util.RandomString(10), Name: util.RandomString(10),
} }