Skip to content

Add solution for Challenge 13 by imankhodadi#1000

Open
imankhodadi wants to merge 1 commit intoRezaSi:mainfrom
imankhodadi:challenge-13-imankhodadi-1766832753
Open

Add solution for Challenge 13 by imankhodadi#1000
imankhodadi wants to merge 1 commit intoRezaSi:mainfrom
imankhodadi:challenge-13-imankhodadi-1766832753

Conversation

@imankhodadi
Copy link
Contributor

Challenge 13 Solution

Submitted by: @imankhodadi
Challenge: Challenge 13

Description

This PR contains my solution for Challenge 13.

Changes

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

Testing

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

Thank you for reviewing my submission! 🚀

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 27, 2025

Walkthrough

Introduces a new SQLite-backed product store implementation in Go with a Product type and ProductStore wrapper. Implements CRUD operations (Create, Read, Update, Delete), category filtering, and batch inventory updates within transactions. Includes database initialization and a sample main function demonstrating the API.

Changes

Cohort / File(s) Summary
SQLite Product Store Implementation
challenge-13/submissions/imankhodadi/solution-template.go
Adds complete product store with Product struct (ID, Name, Price, Quantity, Category), ProductStore wrapper around sql.DB, constructors (NewProductStore, InitDB), CRUD methods, ListProducts for category filtering, BatchUpdateInventory with transactional support, ShowData helper, and sample main function. Includes error handling for DB operations and explicit row-affected checks.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Pre-merge checks

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding a solution for Challenge 13 submitted by imankhodadi, which directly matches the changeset.
Description check ✅ Passed The description is clearly related to the changeset, explaining that it contains the Challenge 13 solution and specifying the file added, with relevant testing information.

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
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: 0

🧹 Nitpick comments (2)
challenge-13/submissions/imankhodadi/solution-template.go (2)

35-36: Consider formatting the SQL for better readability.

The CREATE TABLE statement works correctly but is quite long on a single line. Consider breaking it into multiple lines for improved maintainability.

🔎 Suggested formatting
-	_, err = db.Exec(
-		"CREATE TABLE IF NOT EXISTS products (id INTEGER PRIMARY KEY, name TEXT, price REAL, quantity INTEGER, category TEXT)")
+	_, err = db.Exec(`
+		CREATE TABLE IF NOT EXISTS products (
+			id INTEGER PRIMARY KEY,
+			name TEXT,
+			price REAL,
+			quantity INTEGER,
+			category TEXT
+		)
+	`)

183-228: Consider using dynamically assigned IDs instead of hardcoded values.

The demonstration code hardcodes product IDs (line 204), which assumes the products just created have IDs 1 and 2. If database.db already contains data from a previous run, these IDs may not correspond to the newly created products, causing the batch update to fail.

Consider capturing the assigned IDs from CreateProduct and using those values instead.

🔎 Suggested approach
-	err = ps.CreateProduct(&Product{0, "Cacao", 10.5, 3, "junk"})
+	product1 := &Product{Name: "Cacao", Price: 10.5, Quantity: 3, Category: "junk"}
+	err = ps.CreateProduct(product1)
 	if err != nil {
 		fmt.Println("Failed to create product:", err)
 		return
 	}
-	err = ps.CreateProduct(&Product{0, "milk", 20.5, 2, "dairy"})
+	product2 := &Product{Name: "milk", Price: 20.5, Quantity: 2, Category: "dairy"}
+	err = ps.CreateProduct(product2)
 	if err != nil {
 		fmt.Println(err)
 		return
 	}
 	ps.ShowData()
 
-	if err := ps.BatchUpdateInventory(map[int64]int{1: 100, 2: 100}); err != nil {
+	if err := ps.BatchUpdateInventory(map[int64]int{product1.ID: 100, product2.ID: 100}); err != nil {
 		fmt.Println("Failed to batch update:", err)
 		return
 	}

Also update line 209 to use product1.ID:

-	prd, err := ps.GetProduct(1)
+	prd, err := ps.GetProduct(product1.ID)

And line 216 to use product2.ID:

-	if err := ps.UpdateProduct(&Product{2, "new milk", 30.0, 150, "new dairy"}); err != nil {
+	if err := ps.UpdateProduct(&Product{product2.ID, "new milk", 30.0, 150, "new dairy"}); err != nil {

And line 222 to use product1.ID:

-	if err := ps.DeleteProduct(1); err != nil {
+	if err := ps.DeleteProduct(product1.ID); err != nil {
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 669bf54 and 71ecd3a.

📒 Files selected for processing (1)
  • challenge-13/submissions/imankhodadi/solution-template.go
🧰 Additional context used
🧬 Code graph analysis (1)
challenge-13/submissions/imankhodadi/solution-template.go (2)
challenge-13/submissions/Gandook/solution-template.go (1)
  • ID (144-144)
challenge-13/submissions/Ali-Fartoot/solution-template.go (1)
  • DB (29-29)
🔇 Additional comments (6)
challenge-13/submissions/imankhodadi/solution-template.go (6)

1-8: LGTM! Clean imports and proper driver registration.

The package declaration and imports are well-structured. The blank import for the SQLite driver is the correct Go pattern for side-effect registration.


10-24: LGTM! Well-designed types with proper encapsulation.

The Product struct has appropriately exported fields for database scanning, and ProductStore correctly encapsulates the database connection as an unexported field.


44-102: LGTM! Excellent CRUD implementation with strong error handling.

All CRUD operations use parameterized queries (preventing SQL injection), handle errors appropriately, and detect not-found conditions by checking RowsAffected or sql.ErrNoRows. Well done!


104-131: LGTM! Solid implementation with proper resource management.

The method correctly handles both filtered and unfiltered queries, properly closes the rows cursor, and checks rows.Err() after iteration—an important best practice for detecting iteration errors.


133-170: LGTM! Excellent transactional implementation.

The batch update correctly uses transactions with a deferred rollback pattern, prepared statements for efficiency, and validates each update by checking RowsAffected. The all-or-nothing approach ensures data consistency.


172-182: LGTM! Useful helper for demonstration.

Simple and effective helper method for displaying products during testing and demonstration.

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.

1 participant