Add solution for Challenge 13 by imankhodadi#1000
Add solution for Challenge 13 by imankhodadi#1000imankhodadi wants to merge 1 commit intoRezaSi:mainfrom
Conversation
WalkthroughIntroduces 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
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Pre-merge checks❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
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. Comment |
There was a problem hiding this comment.
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.dbalready 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
CreateProductand 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
📒 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
Productstruct has appropriately exported fields for database scanning, andProductStorecorrectly 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
RowsAffectedorsql.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.
Challenge 13 Solution
Submitted by: @imankhodadi
Challenge: Challenge 13
Description
This PR contains my solution for Challenge 13.
Changes
challenge-13/submissions/imankhodadi/solution-template.goTesting
Thank you for reviewing my submission! 🚀