Add solution for Challenge 13 by manik23#1256
Conversation
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ 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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@challenge-13/submissions/manik23/solution-template.go`:
- Around line 148-173: The ListProducts function opens a rows result set but
never closes it, leaking DB connections; after successfully getting rows in
ProductStore.ListProducts, add a defer rows.Close() immediately after the nil
err check (right after ps.db.Query returns) so the rows are always closed even
on early returns or errors during scanning/iteration; reference the rows
variable and the ListProducts method to locate where to insert the defer.
- Around line 31-57: In InitDB, fix the schema typo and resource leak: change
the SQL in initTable so the price column uses REAL NOT NULL (not READ), ensure
that if sql.Open succeeds but db.Ping() or db.Exec(initTable) fails you call
db.Close() before returning, and wrap the Ping error with fmt.Errorf("%w", err)
(or similar) when returning the Ping failure; reference InitDB, sql.Open,
db.Ping, initTable, and db.Exec in your changes.
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@challenge-13/submissions/manik23/solution-template.go`:
- Around line 85-96: The scan into p.Category will fail if category is NULL;
update the ps.db.QueryRow(...) scan to use a sql.NullString (e.g., declare a
local variable named category sql.NullString) and scan into &category instead of
&p.Category, then set p.Category = category.String (which yields empty string
when NULL); alternatively modify the SQL query to use COALESCE(category, '') AS
category and continue scanning into &p.Category. Ensure the change is applied
where QueryRow is called (ps.db.QueryRow, Product struct population) and handle
sql.ErrNoRows as before.
- Around line 154-169: ListProducts is scanning the DB category into a plain
string which panics/returns NULL when category is NULL; change the SELECT to use
COALESCE(category, '') or scan into sql.NullString and assign Product.Category =
ns.String (or ""), e.g. update the query variable used in ListProducts and/or
replace the rows.Scan target for category so null DB values are safely converted
to an empty string before appending Product instances.
🧹 Nitpick comments (3)
challenge-13/submissions/manik23/solution-template.go (3)
32-34: Remove leftover TODO comments.The implementation is complete, but the placeholder TODO comments remain. Consider removing them to keep the code clean.
♻️ Proposed fix
// InitDB sets up a new SQLite database and creates the products table func InitDB(dbPath string) (*sql.DB, error) { - // TODO: Open a SQLite database connection - // TODO: Create the products table if it doesn't exist - // The table should have columns: id, name, price, quantity, category - db, err := sql.Open("sqlite3", dbPath)
82-83: Remove leftover TODO comments.Implementation is complete; these placeholders should be removed.
151-153: Remove leftover TODO comments.Implementation is complete; these placeholders should be removed.
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Challenge 13 Solution
Submitted by: @manik23
Challenge: Challenge 13
Description
This PR contains my solution for Challenge 13.
Changes
challenge-13/submissions/manik23/solution-template.goTesting
Thank you for reviewing my submission! 🚀