Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve: refactors product quantity to be atomic #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS productquantity;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS productquantity (
`id` INT UNSIGNED NOT NULL,
`quantity` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`id`) REFERENCES products(`id`)
);
4 changes: 4 additions & 0 deletions services/cart/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ func (m *mockProductStore) UpdateProduct(product types.Product) error {
return nil
}

func (m *mockProductStore) UpdateProductQuantity(product types.Product) error {
return nil
}

type mockOrderStore struct{}

func (m *mockOrderStore) CreateOrder(order types.Order) (int, error) {
Expand Down
2 changes: 1 addition & 1 deletion services/cart/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (h *Handler) createOrder(products []types.Product, cartItems []types.CartCh
for _, item := range cartItems {
product := productsMap[item.ProductID]
product.Quantity -= item.Quantity
h.store.UpdateProduct(product)
h.store.UpdateProductQuantity(product)
}

// create order record
Expand Down
4 changes: 4 additions & 0 deletions services/product/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func (m *mockProductStore) GetProductsByID(ids []int) ([]types.Product, error) {
return []types.Product{}, nil
}

func (m *mockProductStore) UpdateProductQuantity(product types.Product) error {
return nil
}

type mockUserStore struct{}

func (m *mockUserStore) GetUserByID(userID int) (*types.User, error) {
Expand Down
71 changes: 66 additions & 5 deletions services/product/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewStore(db *sql.DB) *Store {
}

func (s *Store) GetProductByID(productID int) (*types.Product, error) {
rows, err := s.db.Query("SELECT * FROM products WHERE id = ?", productID)
rows, err := s.db.Query("SELECT p.id, p.name, p.description, p.image, p.price, pq.quantity, p.createdat FROM products p join productquantity pq on p.id = pq.id WHERE p.id = ?", productID)
if err != nil {
return nil, err
}
Expand All @@ -35,7 +35,7 @@ func (s *Store) GetProductByID(productID int) (*types.Product, error) {

func (s *Store) GetProductsByID(productIDs []int) ([]types.Product, error) {
placeholders := strings.Repeat(",?", len(productIDs)-1)
query := fmt.Sprintf("SELECT * FROM products WHERE id IN (?%s)", placeholders)
query := fmt.Sprintf("SELECT p.id, p.name, p.description, p.image, p.price, pq.quantity, p.createdat FROM products p join productquantity pq on p.id = pq.id WHERE p.id IN (?%s)", placeholders)

// Convert productIDs to []interface{}
args := make([]interface{}, len(productIDs))
Expand Down Expand Up @@ -63,7 +63,7 @@ func (s *Store) GetProductsByID(productIDs []int) ([]types.Product, error) {
}

func (s *Store) GetProducts() ([]*types.Product, error) {
rows, err := s.db.Query("SELECT * FROM products")
rows, err := s.db.Query("SELECT p.id, p.name, p.description, p.image, p.price, pq.quantity, p.createdat FROM products p join productquantity pq on p.id = pq.id")
if err != nil {
return nil, err
}
Expand All @@ -82,20 +82,81 @@ func (s *Store) GetProducts() ([]*types.Product, error) {
}

func (s *Store) CreateProduct(product types.CreateProductPayload) error {
_, err := s.db.Exec("INSERT INTO products (name, price, image, description, quantity) VALUES (?, ?, ?, ?, ?)", product.Name, product.Price, product.Image, product.Description, product.Quantity)

// transaction to add product and quantity together
tx, err := s.db.Begin()
if err != nil {
return err
}

res, err := tx.Exec("INSERT INTO products (name, price, image, description) VALUES (?, ?, ?, ?)", product.Name, product.Price, product.Image, product.Description)
if err != nil {
tx.Rollback()
return err
}

productID, err := res.LastInsertId()
if err != nil {
tx.Rollback()
return err
}

_, err = tx.Exec("INSERT INTO productquantity (id, quantity) VALUES (?, ?)", productID, product.Quantity)

if err != nil {
tx.Rollback()
return err
}

if err = tx.Commit(); err != nil {
return err
}

return nil
}

func (s *Store) UpdateProduct(product types.Product) error {
_, err := s.db.Exec("UPDATE products SET name = ?, price = ?, image = ?, description = ?, quantity = ? WHERE id = ?", product.Name, product.Price, product.Image, product.Description, product.Quantity, product.ID)

tx, err := s.db.Begin()
if err != nil {
return err
}


_, err = tx.Exec("UPDATE products SET name = ?, price = ?, image = ?, description = ? WHERE id = ?", product.Name, product.Price, product.Image, product.Description, product.ID)
if err != nil {
tx.Rollback()
return err
}

_, err = tx.Exec("UPDATE productquantity SET quantity = ? WHERE id = ?", product.Quantity, product.ID)
if err != nil {
tx.Rollback()
return err
}

if err = tx.Commit(); err != nil {
return err
}

return nil
}

func (s *Store) UpdateProductQuantity(product types.Product) error {
tx, err := s.db.Begin()
if err != nil {
return err
}

_, err = tx.Exec("UPDATE productquantity SET quantity = ? WHERE id = ?", product.Quantity, product.ID)
if err != nil {
tx.Rollback()
return err
}

if err := tx.Commit(); err != nil {
return err
}
return nil
}

Expand Down
1 change: 1 addition & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type ProductStore interface {
GetProducts() ([]*Product, error)
CreateProduct(CreateProductPayload) error
UpdateProduct(Product) error
UpdateProductQuantity(Product) error
}

type OrderStore interface {
Expand Down