Skip to content

Add solution for Challenge 13 by blxxdclxud#1558

Merged
github-actions[bot] merged 2 commits intoRezaSi:mainfrom
blxxdclxud:challenge-13-blxxdclxud
Apr 13, 2026
Merged

Add solution for Challenge 13 by blxxdclxud#1558
github-actions[bot] merged 2 commits intoRezaSi:mainfrom
blxxdclxud:challenge-13-blxxdclxud

Conversation

@blxxdclxud
Copy link
Copy Markdown
Contributor

Challenge 13 Solution

Submitted by: @blxxdclxud
Challenge: Challenge 13

Description

This PR contains my solution for Challenge 13.

Changes

  • Added solution file to challenge-13/submissions/blxxdclxud/solution-template.go

Testing

  • Solution passes all test cases
  • Code follows Go best practices

Thank you for reviewing my submission! 🚀

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 7, 2026

Walkthrough

A new Go file introduces a complete SQLite-backed product inventory system featuring a Product struct, ProductStore with database connection, InitDB for schema initialization, CRUD methods (CreateProduct, GetProduct, UpdateProduct, DeleteProduct), filtering via ListProducts, and batch inventory updates with transaction support.

Changes

Cohort / File(s) Summary
SQLite Product Inventory Implementation
challenge-13/submissions/blxxdclxud/solution-template.go
Adds Product and ProductStore types; implements InitDB() with SQLite connection and table creation; provides CRUD methods with parameterized queries; includes ListProducts() with optional category filtering; implements BatchUpdateInventory() with transaction-based multi-product updates and row-affected validation; contains placeholder main() entry point.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Poem

🐰 Hop along through SQL we go,
Products stored in SQLite's glow,
Create, read, update with care,
Batch transactions—quite the pair!
Inventory dreams, now they're real,
With CRUD that has such appeal!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: adding a solution for Challenge 13 submitted by a specific user. It clearly communicates the primary purpose of the PR.
Description check ✅ Passed The description is directly related to the changeset, explaining that it contains a Challenge 13 solution submission with a new file added to the submissions folder.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 6


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 852d9240-5dad-4e06-9c10-ff7e1def749c

📥 Commits

Reviewing files that changed from the base of the PR and between 6a002af and 9079958.

📒 Files selected for processing (1)
  • challenge-13/submissions/blxxdclxud/solution-template.go

Comment on lines +40 to +43
ctx, _ := context.WithTimeout(context.Background(), 5 * time.Second)
if err = db.PingContext(ctx); err != nil {
return nil, err
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Context cancel function leaked.

The cancel function from context.WithTimeout is discarded. This can leak resources associated with the context. Always call the cancel function when the context is no longer needed.

🛠️ Proposed fix
-	ctx, _ := context.WithTimeout(context.Background(), 5 * time.Second)
+	ctx, cancel := context.WithTimeout(context.Background(), 5 * time.Second)
+	defer cancel()
 	if err = db.PingContext(ctx); err != nil {
 	    return nil, err
 	}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
ctx, _ := context.WithTimeout(context.Background(), 5 * time.Second)
if err = db.PingContext(ctx); err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), 5 * time.Second)
defer cancel()
if err = db.PingContext(ctx); err != nil {
return nil, err
}

Comment on lines +85 to +87
query := "UPDATE products SET name=$2, price=$3, quantity=$4, category=$5 WHERE id = $1"
_, err := ps.db.Exec(query, product.Name, product.Price, product.Quantity, product.Category, product.ID)
return err
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical: Parameter order mismatch will cause incorrect updates.

The query uses $1 for id in the WHERE clause, but the parameters are passed with product.Name first. This means $1 receives the name instead of the ID, causing the update to either fail or modify incorrect rows.

🐛 Proposed fix
 	query := "UPDATE products SET name=$2, price=$3, quantity=$4, category=$5 WHERE id = $1"
-	_, err := ps.db.Exec(query, product.Name, product.Price, product.Quantity, product.Category, product.ID)
+	_, err := ps.db.Exec(query, product.ID, product.Name, product.Price, product.Quantity, product.Category)
 	return err
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
query := "UPDATE products SET name=$2, price=$3, quantity=$4, category=$5 WHERE id = $1"
_, err := ps.db.Exec(query, product.Name, product.Price, product.Quantity, product.Category, product.ID)
return err
query := "UPDATE products SET name=$2, price=$3, quantity=$4, category=$5 WHERE id = $1"
_, err := ps.db.Exec(query, product.ID, product.Name, product.Price, product.Quantity, product.Category)
return err

Comment on lines +90 to +97
// DeleteProduct removes a product by ID
func (ps *ProductStore) DeleteProduct(id int64) error {
// TODO: Delete the product from the database
// TODO: Return an error if the product doesn't exist
query := "DELETE FROM products WHERE id = $1"
_, err := ps.db.Exec(query, id)
return err
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Missing "product not found" error when deleting non-existent product.

Per the TODO comment, this method should return an error if the product doesn't exist. Currently, deleting a non-existent ID silently succeeds.

🐛 Proposed fix
 func (ps *ProductStore) DeleteProduct(id int64) error {
 	// TODO: Delete the product from the database
 	// TODO: Return an error if the product doesn't exist
 	query := "DELETE FROM products WHERE id = $1"
-	_, err := ps.db.Exec(query, id)
-	return err
+	result, err := ps.db.Exec(query, id)
+	if err != nil {
+		return err
+	}
+	rowsAffected, err := result.RowsAffected()
+	if err != nil {
+		return err
+	}
+	if rowsAffected == 0 {
+		return sql.ErrNoRows
+	}
+	return nil
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// DeleteProduct removes a product by ID
func (ps *ProductStore) DeleteProduct(id int64) error {
// TODO: Delete the product from the database
// TODO: Return an error if the product doesn't exist
query := "DELETE FROM products WHERE id = $1"
_, err := ps.db.Exec(query, id)
return err
}
// DeleteProduct removes a product by ID
func (ps *ProductStore) DeleteProduct(id int64) error {
// TODO: Delete the product from the database
// TODO: Return an error if the product doesn't exist
query := "DELETE FROM products WHERE id = $1"
result, err := ps.db.Exec(query, id)
if err != nil {
return err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return err
}
if rowsAffected == 0 {
return sql.ErrNoRows
}
return nil
}

Comment on lines +106 to +114
query := "SELECT * from products WHERE category = $1"
if category == "" {
query = "SELECT * from products"
}
var res []*Product
rows, err := ps.db.Query(query, category)
if err != nil {
return nil, err
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Bug: Parameter passed to query without placeholders when category is empty.

When category == "", the query is set to "SELECT * from products" (no placeholders), but ps.db.Query(query, category) still passes the category parameter. This can cause unexpected behavior or errors.

🐛 Proposed fix
-	query := "SELECT * from products WHERE category = $1"
-	if category == "" {
-	   query = "SELECT * from products" 
-	}
 	var res []*Product
-	rows, err := ps.db.Query(query, category)
+	var rows *sql.Rows
+	var err error
+	if category == "" {
+		rows, err = ps.db.Query("SELECT * FROM products")
+	} else {
+		rows, err = ps.db.Query("SELECT * FROM products WHERE category = $1", category)
+	}
 	if err != nil {
 	    return nil, err
 	}
+	defer rows.Close()
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
query := "SELECT * from products WHERE category = $1"
if category == "" {
query = "SELECT * from products"
}
var res []*Product
rows, err := ps.db.Query(query, category)
if err != nil {
return nil, err
}
var res []*Product
var rows *sql.Rows
var err error
if category == "" {
rows, err = ps.db.Query("SELECT * FROM products")
} else {
rows, err = ps.db.Query("SELECT * FROM products WHERE category = $1", category)
}
if err != nil {
return nil, err
}
defer rows.Close()

Comment on lines +138 to +139
tx, _ := ps.db.BeginTx(context.Background(), nil)
defer tx.Rollback()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Transaction start error is ignored, risking nil pointer panic.

If BeginTx fails, tx will be nil and the subsequent defer tx.Rollback() will panic.

🐛 Proposed fix
-	tx, _ := ps.db.BeginTx(context.Background(), nil)
+	tx, err := ps.db.BeginTx(context.Background(), nil)
+	if err != nil {
+		return err
+	}
 	defer tx.Rollback()
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
tx, _ := ps.db.BeginTx(context.Background(), nil)
defer tx.Rollback()
tx, err := ps.db.BeginTx(context.Background(), nil)
if err != nil {
return err
}
defer tx.Rollback()

Comment on lines +159 to +160
tx.Commit()
return nil
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Commit error is ignored.

If the transaction commit fails (e.g., constraint violation, I/O error), the error is silently discarded. The function returns nil even though the updates may not have persisted.

🐛 Proposed fix
-	tx.Commit()
-	return nil
+	return tx.Commit()
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
tx.Commit()
return nil
return tx.Commit()

@github-actions github-actions Bot merged commit 42c41b2 into RezaSi:main Apr 13, 2026
6 checks passed
@github-actions
Copy link
Copy Markdown

🎉 Auto-merged!

This PR was automatically merged after 2 days with all checks passing.

Thank you for your contribution, @blxxdclxud!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants