Skip to content

Add solution for Challenge 13 by imankhodadi#1032

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

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

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 Jan 1, 2026

Walkthrough

This PR introduces a complete SQLite-backed product management module in Go with a public API for CRUD operations, batch updates, and dynamic filtering. The implementation includes a Product struct, ProductStore type with core methods, database initialization, transaction-based batch operations, and a demonstration main function.

Changes

Cohort / File(s) Summary
SQLite Product Management Module
challenge-13/submissions/imankhodadi/solution-template.go
Adds Product struct and ProductStore type with CRUD methods (CreateProduct, GetProduct, UpdateProduct, DeleteProduct, ListProducts), batch operations (BatchUpdateInventory, BulkUpdatePrices2), dynamic query filtering (ListProducts2), database initialization (InitDB, NewProductStore), data display utility (ShowData), and main entry point demonstrating initialization and workflow

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 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 clearly summarizes the main change: adding a solution file for Challenge 13 submitted by imankhodadi.
Description check ✅ Passed The description is directly related to the changeset, explaining the solution submission for Challenge 13 and listing the file changes.

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

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

229-241: Redundant explicit tx.Rollback() calls.

The deferred rollback (lines 215-218) already handles rollback when committed is false. The explicit tx.Rollback() calls here are redundant and will cause double-rollback attempts. Compare with BatchUpdateInventory which relies solely on the deferred rollback.

🔎 Proposed fix - remove explicit rollbacks
 		result, err := stmt.Exec(price, id)
 		if err != nil {
-			tx.Rollback()
 			return fmt.Errorf("failed to update price for product %d: %w", id, err)
 		}
 		rowsAffected, err := result.RowsAffected()
 		if err != nil {
-			tx.Rollback()
 			return fmt.Errorf("failed to get rows affected for product %d: %w", id, err)
 		}
 		if rowsAffected == 0 {
-			tx.Rollback()
 			return fmt.Errorf("product with ID %d not found", id)
 		}

270-275: Prefer named fields in struct literals for clarity and safety.

Positional struct literals are fragile—if Product fields are reordered, this code will silently break or produce incorrect data. Using named fields improves readability and prevents such issues.

🔎 Proposed fix
-	err = ps.CreateProduct(&Product{0, "Cacao", 10.5, 3, "junk"})
+	err = ps.CreateProduct(&Product{Name: "Cacao", Price: 10.5, Quantity: 3, Category: "junk"})
 	if err != nil {
 		fmt.Println("Failed to create product:", err)
 		return
 	}
-	err = ps.CreateProduct(&Product{0, "milk", 20.5, 2, "dairy"})
+	err = ps.CreateProduct(&Product{Name: "milk", Price: 20.5, Quantity: 2, Category: "dairy"})

134-167: Consider API consistency: standalone functions vs. ProductStore methods.

ListProducts2 and BulkUpdatePrices2 are standalone functions taking *sql.DB, while similar functionality (ListProducts, BatchUpdateInventory) are methods on ProductStore. For a cohesive API, consider making these methods on ProductStore as well.

Also applies to: 209-248

📜 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 8bb2e48.

📒 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 (4)
challenge-13/submissions/imankhodadi/solution-template.go (4)

11-43: LGTM on struct definitions and initialization.

The Product struct, ProductStore, NewProductStore, and InitDB are well-implemented. Good practice closing the database on table creation failure (line 39).


45-103: CRUD operations are well-implemented.

Good practices observed:

  • Parameterized queries prevent SQL injection
  • sql.ErrNoRows is properly handled in GetProduct
  • RowsAffected validation ensures operations affect expected rows

105-132: LGTM!

Proper use of defer rows.Close() and rows.Err() check after iteration. The category filter uses parameterized queries correctly.


169-207: Well-structured transactional batch update.

Good implementation with:

  • committed flag pattern for safe deferred rollback
  • Prepared statement reuse for performance
  • Per-ID validation of affected rows

Comment on lines +139 to +141
if name, ok := filters["name"]; ok {
conditions = append(conditions, "name LIKE ?")
args = append(args, "%"+name.(string)+"%")
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Unsafe type assertion can cause runtime panic.

The type assertion name.(string) will panic if the value is not a string. Use a type assertion with the "comma ok" idiom to handle this safely.

🔎 Proposed fix
 	if name, ok := filters["name"]; ok {
+		nameStr, isString := name.(string)
+		if !isString {
+			return nil, fmt.Errorf("filter 'name' must be a string")
+		}
 		conditions = append(conditions, "name LIKE ?")
-		args = append(args, "%"+name.(string)+"%")
+		args = append(args, "%"+nameStr+"%")
 	}
🤖 Prompt for AI Agents
In challenge-13/submissions/imankhodadi/solution-template.go around lines 139 to
141, the direct type assertion name.(string) can panic if filters["name"] is not
a string; change to a safe assertion using the "comma ok" idiom (e.g., val, ok
:= name.(string)) and only append the condition and args when ok is true, or
otherwise handle non-string values (skip, convert, or return an error) to avoid
runtime panics.

Comment on lines +164 to +166
products = append(products, product)
}
return products, nil
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Missing rows.Err() check after iteration.

After iterating through rows with rows.Next(), you should check rows.Err() to catch any errors that occurred during iteration. Compare with ListProducts (line 128) which does this correctly.

🔎 Proposed fix
 		products = append(products, product)
 	}
+	if err := rows.Err(); err != nil {
+		return nil, fmt.Errorf("error iterating products: %w", err)
+	}
 	return products, 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
products = append(products, product)
}
return products, nil
products = append(products, product)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating products: %w", err)
}
return products, nil
🤖 Prompt for AI Agents
In challenge-13/submissions/imankhodadi/solution-template.go around lines 164 to
166, the rows iteration return lacks a rows.Err() check; after the for
rows.Next() loop add a check like if err := rows.Err(); err != nil { return nil,
err } and return products only if that check passes so any iteration errors are
propagated.

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