Skip to content

Add solution for Challenge 13 by berkkaradalan#793

Merged
RezaSi merged 3 commits intoRezaSi:mainfrom
berkkaradalan:challenge-13-berkkaradalan-1763901629
Nov 23, 2025
Merged

Add solution for Challenge 13 by berkkaradalan#793
RezaSi merged 3 commits intoRezaSi:mainfrom
berkkaradalan:challenge-13-berkkaradalan-1763901629

Conversation

@berkkaradalan
Copy link
Contributor

Challenge 13 Solution

Submitted by: @berkkaradalan
Challenge: Challenge 13

Description

This PR contains my solution for Challenge 13.

Changes

  • Added solution file to challenge-13/submissions/berkkaradalan/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 Nov 23, 2025

Walkthrough

A Go implementation of a SQLite-backed product inventory management system. Defines Product and ProductStore types with database initialization, CRUD operations (create, read, update, delete), product listing by category, and batch inventory updates.

Changes

Cohort / File(s) Summary
SQLite Product Inventory Implementation
challenge-13/submissions/berkkaradalan/solution-template.go
Adds Product struct (ID, Name, Price, Quantity, Category), ProductStore type wrapping *sql.DB, InitDB function for SQLite setup, NewProductStore constructor, and methods for CreateProduct, GetProduct, UpdateProduct, DeleteProduct, ListProducts (filtered by category), and BatchUpdateInventory. Includes main function demonstrating initialization and operations.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10–15 minutes

  • Verify SQL query correctness for CRUD and batch operations
  • Check error handling consistency across all database methods
  • Validate main function execution flow and edge case handling

Possibly related PRs

Pre-merge checks

❌ 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%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding a solution submission for Challenge 13 by a specific contributor.
Description check ✅ Passed The description is directly related to the changeset, explaining the Challenge 13 solution submission, the file added, and test status.

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d7d4dc3 and d7054c8.

📒 Files selected for processing (1)
  • challenge-13/submissions/berkkaradalan/solution-template.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • challenge-13/submissions/berkkaradalan/solution-template.go

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 (2)
challenge-13/submissions/berkkaradalan/solution-template.go (2)

206-211: Add database connection cleanup.

The database connection is never closed. While the OS will clean it up when the process exits, it's good practice to explicitly close resources.

Add this after the database initialization:

 	sqliteDB, err := InitDB("./test.sqlite")

 	if err != nil { 
 		fmt.Printf("Error :%v", err)
+		return
 	}
+	defer sqliteDB.Close()

215-221: Remove unnecessary ID field initialization.

The ID field is auto-generated by the database and will be overwritten in CreateProduct. Setting it here is unnecessary.

Apply this diff:

 	testProduct := Product{
-		ID: 1,
 		Name: "Test",
 		Price: 2500,
 		Quantity: 2500,
 		Category: "TestItem",
 	}
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ca8ea29 and e57c12f.

📒 Files selected for processing (1)
  • challenge-13/submissions/berkkaradalan/solution-template.go (1 hunks)
🔇 Additional comments (7)
challenge-13/submissions/berkkaradalan/solution-template.go (7)

1-9: LGTM!

The import structure is correct. Using a blank import for the SQLite driver is the standard Go database/sql pattern.


11-28: LGTM!

The struct definitions and constructor follow Go conventions properly.


55-73: LGTM!

The insertion logic is correct and properly sets the generated ID on the product struct.


75-92: LGTM!

The query logic correctly handles the case when a product is not found by returning sql.ErrNoRows.


94-115: LGTM!

The update logic correctly checks for product existence using RowsAffected.


117-140: LGTM!

The delete logic is consistent with the update pattern and correctly validates existence.


176-204: LGTM!

The transaction handling is correct with proper rollback on errors and validation of each update.

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

♻️ Duplicate comments (1)
challenge-13/submissions/berkkaradalan/solution-template.go (1)

35-37: Fix indentation inconsistency.

Lines 35-36 use spaces for indentation instead of tabs, and line 36 has excessive indentation. This is inconsistent with Go conventions and the rest of the file.

Apply this diff to fix the indentation:

 	db, err := sql.Open("sqlite3", dbPath)
-    if err != nil {
-		return nil, err
-    }
+	if err != nil {
+		return nil, err
+	}
🧹 Nitpick comments (5)
challenge-13/submissions/berkkaradalan/solution-template.go (5)

34-38: Consider verifying database connectivity.

sql.Open doesn't verify the connection to the database. Consider adding db.Ping() to ensure the database is accessible before returning.

Apply this diff to add connectivity verification:

 	db, err := sql.Open("sqlite3", dbPath)
 	if err != nil {
 		return nil, err
 	}
+	
+	if err = db.Ping(); err != nil {
+		return nil, err
+	}

75-92: Consider simplifying variable names.

The variable names selectedProduct and selectedProductStruct are somewhat verbose. Consider shorter, clearer names like row and product.

Apply this diff to improve readability:

-	selectedProduct := ps.db.QueryRow(query, id)
+	row := ps.db.QueryRow(query, id)
 
-	selectedProductStruct := Product{}
+	product := Product{}
 
-	err := selectedProduct.Scan(&selectedProductStruct.ID, &selectedProductStruct.Name, &selectedProductStruct.Price, &selectedProductStruct.Quantity, &selectedProductStruct.Category)
+	err := row.Scan(&product.ID, &product.Name, &product.Price, &product.Quantity, &product.Category)
 	
 	if err != nil {
 		return nil, err
 	}
 
-	return &selectedProductStruct, nil
+	return &product, nil

142-176: Good fix on the resource leak! Consider adding rows.Err() check.

The defer rows.Close() on line 160 properly addresses the previous review comment about the resource leak. However, it's recommended to check rows.Err() after the iteration loop to catch any errors that occurred during iteration.

Apply this diff to add the error check:

 	for rows.Next() {
 		i := Product{}
 		err = rows.Scan(&i.ID, &i.Name, &i.Price, &i.Quantity, &i.Category)
 
 		if err != nil {
 			return nil, err
 		}
 
 		data = append(data, &i)
 	}
+	
+	if err = rows.Err(); err != nil {
+		return nil, err
+	}
 	
 	return data, nil

179-206: Consider adding input validation.

The method doesn't validate that the updates map is non-empty or check for negative quantities. While negative quantities might be intentional for certain use cases, consider adding validation based on your business requirements.

Example validation:

func (ps *ProductStore) BatchUpdateInventory(updates map[int64]int) error {
	if len(updates) == 0 {
		return errors.New("updates map is empty")
	}
	
	// Optional: validate quantities based on business rules
	for id, quantity := range updates {
		if quantity < 0 {
			return fmt.Errorf("negative quantity not allowed: id=%d, quantity=%d", id, quantity)
		}
	}
	
	tx, err := ps.db.Begin()
	// ... rest of the method
}

208-256: Consider improving error handling in main.

The main function prints errors but continues execution, which can lead to cascading failures. Consider using log.Fatal or returning early after errors for cleaner demo code.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e57c12f and d7d4dc3.

📒 Files selected for processing (1)
  • challenge-13/submissions/berkkaradalan/solution-template.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
challenge-13/submissions/berkkaradalan/solution-template.go (3)
challenge-13/submissions/Gandook/solution-template.go (1)
  • ID (144-144)
challenge-13/submissions/Ali-Fartoot/solution-template.go (1)
  • DB (29-29)
packages/cobra/challenge-3-subcommands-persistence/submissions/ashwinipatankar/solution.go (1)
  • UpdateProduct (605-645)
🔇 Additional comments (7)
challenge-13/submissions/berkkaradalan/solution-template.go (7)

1-9: LGTM!

The package declaration and imports follow Go conventions. The blank import of the SQLite driver is standard practice.


11-18: LGTM!

The Product struct is well-defined with appropriate field types that align with the database schema.


20-23: LGTM!

The ProductStore struct properly encapsulates the database connection with an unexported field.


25-28: LGTM!

The constructor follows standard Go patterns.


55-73: LGTM!

The CreateProduct method correctly uses parameterized queries and properly sets the generated ID on the product.


94-115: LGTM!

The UpdateProduct method correctly uses parameterized queries and validates that the product exists by checking rows affected.


117-140: LGTM!

The DeleteProduct method follows the same reliable pattern as UpdateProduct with proper validation.

@RezaSi RezaSi merged commit 89ccc09 into RezaSi:main Nov 23, 2025
5 checks passed
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