diff --git a/api/product.go b/api/product.go index 4184022..154ce72 100644 --- a/api/product.go +++ b/api/product.go @@ -15,8 +15,9 @@ import ( type createProductRequest struct { Name string `json:"name" binding:"required"` SellingPrice float64 `json:"selling_price" binding:"required"` + ProductTypeID int16 `json:"product_type_id" 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"` } @@ -32,6 +33,7 @@ func (server *Server) createProduct(ctx *gin.Context) { arg := db.CreateProductParams{ MerchantID: authPayload.MerchantID, Name: req.Name, + ProductTypeID: req.ProductTypeID, SellingPrice: req.SellingPrice, PurchasePrice: req.PurchasePrice, ProductCategoryID: uuid.MustParse(req.ProductCategoryID), diff --git a/api/product_category_test.go b/api/product_category_test.go index 8c54be4..9a99def 100644 --- a/api/product_category_test.go +++ b/api/product_category_test.go @@ -35,11 +35,11 @@ func TestCreateProductCategory(t *testing.T) { "merchant_id": productCategory.MerchantID, }, 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) { arg := db.CreateProductCategoryParams{ - MerchantID: uuid.MustParse("54b8a2d9-16be-4239-8828-5daa317028dc"), + MerchantID: uuid.MustParse("7e525a4b-4208-4f05-99a1-a75df475dd9b"), Name: productCategory.Name, } store.EXPECT(). @@ -83,7 +83,7 @@ func TestCreateProductCategory(t *testing.T) { func createRandomProductCategory() db.ProductCategory { return db.ProductCategory{ ID: uuid.New(), - MerchantID: uuid.MustParse("54b8a2d9-16be-4239-8828-5daa317028dc"), + MerchantID: uuid.MustParse("7e525a4b-4208-4f05-99a1-a75df475dd9b"), Name: util.RandomString(5), } } diff --git a/api/product_test.go b/api/product_test.go index d3ee9f0..ad452db 100644 --- a/api/product_test.go +++ b/api/product_test.go @@ -21,7 +21,7 @@ import ( "github.com/stretchr/testify/require" ) -var MERCHANTID = "54b8a2d9-16be-4239-8828-5daa317028dc" +var MERCHANTID = "7e525a4b-4208-4f05-99a1-a75df475dd9b" func TestGetProductApi(t *testing.T) { product := randomProduct(MERCHANTID) diff --git a/db/migrations/000001_init_schema.up.sql b/db/migrations/000001_init_schema.up.sql index 588ad10..cf86d37 100644 --- a/db/migrations/000001_init_schema.up.sql +++ b/db/migrations/000001_init_schema.up.sql @@ -8,6 +8,16 @@ CREATE TABLE users( "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 ( "id" uuid default gen_random_uuid() primary key not null, "index_id" bigserial not null, @@ -27,6 +37,7 @@ create table suppliers ( "created_at" timestamp default(now()), "updated_at" timestamp default(now()) ); + CREATE TABLE customers ( "id" uuid default gen_random_uuid() primary key not null, "index_id" bigserial not null, @@ -37,9 +48,11 @@ CREATE TABLE customers ( "created_at" timestamp default(now()), "updated_at" timestamp default(now()) ); + CREATE TABLE products ( "id" uuid default gen_random_uuid() primary key 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, "name" varchar not null, "selling_price" double precision default(0::double precision) NOT NULL, diff --git a/db/query/product.sql b/db/query/product.sql index 2299582..fcb7200 100644 --- a/db/query/product.sql +++ b/db/query/product.sql @@ -3,11 +3,12 @@ INSERT INTO products ( merchant_id, name, selling_price, + product_type_id, purchase_price, product_category_id, stock ) VALUES ( - $1, $2, $3, $4, $5, $6 + $1, $2, $3, $4, $5, $6, $7 ) RETURNING *; diff --git a/db/sqlc/customer_test.go b/db/sqlc/customer_test.go index 0b19a11..edc6642 100644 --- a/db/sqlc/customer_test.go +++ b/db/sqlc/customer_test.go @@ -11,7 +11,7 @@ import ( func createRandomCustomer(t *testing.T) (Customer, CreateCustomersParams) { arg := CreateCustomersParams{ - MerchantID: uuid.MustParse("54b8a2d9-16be-4239-8828-5daa317028dc"), + MerchantID: uuid.MustParse("7e525a4b-4208-4f05-99a1-a75df475dd9b"), Name: util.RandomString(10), } diff --git a/db/sqlc/models.go b/db/sqlc/models.go index 58c4a5b..fa80854 100644 --- a/db/sqlc/models.go +++ b/db/sqlc/models.go @@ -79,6 +79,7 @@ type Merchant struct { type Product struct { ID uuid.UUID `json:"id"` MerchantID uuid.UUID `json:"merchant_id"` + ProductTypeID int16 `json:"product_type_id"` IndexID int64 `json:"index_id"` Name string `json:"name"` SellingPrice float64 `json:"selling_price"` @@ -96,6 +97,11 @@ type ProductCategory struct { Name string `json:"name"` } +type ProductType struct { + ID int16 `json:"id"` + Name string `json:"name"` +} + type PurchaseOrder struct { ID uuid.UUID `json:"id"` SupplierID uuid.UUID `json:"supplier_id"` diff --git a/db/sqlc/product.sql.go b/db/sqlc/product.sql.go index 95ec5f2..103398d 100644 --- a/db/sqlc/product.sql.go +++ b/db/sqlc/product.sql.go @@ -17,19 +17,21 @@ INSERT INTO products ( merchant_id, name, selling_price, + product_type_id, purchase_price, product_category_id, stock ) 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 { 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"` Stock float64 `json:"stock"` @@ -40,6 +42,7 @@ func (q *Queries) CreateProduct(ctx context.Context, arg CreateProductParams) (P arg.MerchantID, arg.Name, arg.SellingPrice, + arg.ProductTypeID, arg.PurchasePrice, arg.ProductCategoryID, arg.Stock, @@ -48,6 +51,7 @@ func (q *Queries) CreateProduct(ctx context.Context, arg CreateProductParams) (P err := row.Scan( &i.ID, &i.MerchantID, + &i.ProductTypeID, &i.IndexID, &i.Name, &i.SellingPrice, @@ -70,7 +74,7 @@ func (q *Queries) DeleteProduct(ctx context.Context, id uuid.UUID) error { } 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 ` @@ -80,6 +84,7 @@ func (q *Queries) GetProduct(ctx context.Context, id uuid.UUID) (Product, error) err := row.Scan( &i.ID, &i.MerchantID, + &i.ProductTypeID, &i.IndexID, &i.Name, &i.SellingPrice, @@ -93,7 +98,7 @@ func (q *Queries) GetProduct(ctx context.Context, id uuid.UUID) (Product, error) } 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 LIMIT 1 FOR NO KEY UPDATE @@ -105,6 +110,7 @@ func (q *Queries) GetStockForUpdateStock(ctx context.Context, id uuid.UUID) (Pro err := row.Scan( &i.ID, &i.MerchantID, + &i.ProductTypeID, &i.IndexID, &i.Name, &i.SellingPrice, @@ -118,7 +124,7 @@ func (q *Queries) GetStockForUpdateStock(ctx context.Context, id uuid.UUID) (Pro } 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 ORDER BY index_id LIMIT $2 @@ -143,6 +149,7 @@ func (q *Queries) ListProducts(ctx context.Context, arg ListProductsParams) ([]P if err := rows.Scan( &i.ID, &i.MerchantID, + &i.ProductTypeID, &i.IndexID, &i.Name, &i.SellingPrice, @@ -169,7 +176,7 @@ const updateProduct = `-- name: UpdateProduct :one UPDATE products SET name = $2, selling_price = $3, purchase_price = $4, product_category_id = $5, updated_at = $6 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 { @@ -194,6 +201,7 @@ func (q *Queries) UpdateProduct(ctx context.Context, arg UpdateProductParams) (P err := row.Scan( &i.ID, &i.MerchantID, + &i.ProductTypeID, &i.IndexID, &i.Name, &i.SellingPrice, diff --git a/db/sqlc/product_category_test.go b/db/sqlc/product_category_test.go index efba65e..1d7b0dd 100644 --- a/db/sqlc/product_category_test.go +++ b/db/sqlc/product_category_test.go @@ -9,7 +9,7 @@ import ( "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 { arg := CreateProductCategoryParams{ diff --git a/db/sqlc/product_test.go b/db/sqlc/product_test.go index 1568e83..a3bb500 100644 --- a/db/sqlc/product_test.go +++ b/db/sqlc/product_test.go @@ -19,7 +19,8 @@ func createRandomProduct(t *testing.T) (Product, CreateProductParams) { productCategory := createRandomProductCategory(t) arg := CreateProductParams{ - MerchantID: uuid.MustParse("54b8a2d9-16be-4239-8828-5daa317028dc"), + MerchantID: uuid.MustParse("7e525a4b-4208-4f05-99a1-a75df475dd9b"), + ProductTypeID: 1, Name: util.RandomString(10), SellingPrice: sellingPrice, PurchasePrice: purchasePrice, diff --git a/db/sqlc/suppliers_test.go b/db/sqlc/suppliers_test.go index 20d8696..e48f004 100644 --- a/db/sqlc/suppliers_test.go +++ b/db/sqlc/suppliers_test.go @@ -11,7 +11,7 @@ import ( func createRandomSupplier(t *testing.T) (Supplier, CreateSuppliersParams) { arg := CreateSuppliersParams{ - MerchantID: uuid.MustParse("54b8a2d9-16be-4239-8828-5daa317028dc"), + MerchantID: uuid.MustParse("7e525a4b-4208-4f05-99a1-a75df475dd9b"), Name: util.RandomString(10), }