Skip to content

Add solution for gorm challenge-1-crud-operations by Onkar-25#1269

Open
Onkar-25 wants to merge 4 commits intoRezaSi:mainfrom
Onkar-25:package-gorm-challenge-1-crud-operations-Onkar-25
Open

Add solution for gorm challenge-1-crud-operations by Onkar-25#1269
Onkar-25 wants to merge 4 commits intoRezaSi:mainfrom
Onkar-25:package-gorm-challenge-1-crud-operations-Onkar-25

Conversation

@Onkar-25
Copy link
Contributor

@Onkar-25 Onkar-25 commented Feb 4, 2026

gorm challenge-1-crud-operations Solution

Submitted by: @Onkar-25
Package: gorm
Challenge: challenge-1-crud-operations

Description

This PR contains my solution for gorm challenge-1-crud-operations.

Changes

  • Added solution file to packages/gorm/challenge-1-crud-operations/submissions/Onkar-25/solution.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 Feb 4, 2026

Walkthrough

Adds a new GORM-backed User model and implements full CRUD functions (ConnectDB, CreateUser, GetUserByID, GetAllUsers, UpdateUser, DeleteUser) using SQLite in a new submission file.

Changes

Cohort / File(s) Summary
GORM CRUD Implementation
packages/gorm/challenge-1-crud-operations/submissions/Onkar-25/solution.go
New file introducing User struct with GORM tags and functions: ConnectDB (SQLite + AutoMigrate), CreateUser, GetUserByID, GetAllUsers, UpdateUser, DeleteUser. All CRUD operations implemented with error propagation; GetUserByID uses the provided ID parameter.

Sequence Diagram(s)

sequenceDiagram
    participant Client as Client
    participant Service as CRUD Service (solution.go)
    participant DB as SQLite (GORM)

    Client->>Service: CreateUser(user)
    Service->>DB: INSERT user (GORM)
    DB-->>Service: result / error
    Service-->>Client: created user / error

    Client->>Service: GetUserByID(id)
    Service->>DB: SELECT WHERE id (GORM)
    DB-->>Service: user / not found
    Service-->>Client: user / error

    Client->>Service: UpdateUser(user)
    Service->>DB: UPDATE WHERE id (GORM)
    DB-->>Service: rows affected / error
    Service-->>Client: success / error

    Client->>Service: DeleteUser(id)
    Service->>DB: DELETE WHERE id (GORM)
    DB-->>Service: rows affected / error
    Service-->>Client: success / error
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title directly describes the main change: adding a CRUD solution for a GORM challenge submitted by a specific user.
Description check ✅ Passed The description relates to the changeset by explaining that a solution file for the gorm challenge-1-crud-operations was added, matching the actual file changes.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ 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.

❤️ 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

🤖 Fix all issues with AI agents
In `@packages/gorm/challenge-1-crud-operations/submissions/Onkar-25/solution.go`:
- Around line 52-61: GetUserByID currently ignores the id parameter and calls
db.First(&user, 1); change the lookup to use the incoming id (i.e., call
db.First(&user, id)) so the function retrieves the requested user; update the
db.First invocation in the GetUserByID function and keep the same result.Error
handling and return values.
- Around line 74-90: UpdateUser currently dereferences user.ID without checking
user for nil, which can panic; add an early nil check in the UpdateUser function
(when given the user *User parameter) and return a clear error (e.g.,
gorm.ErrInvalidData or a new error) if user == nil before using user.ID, then
proceed with the existing db.Model(&User{}).Where("id = ?",
user.ID).Updates(user) logic and preserve the existing result.Error and
result.RowsAffected handling.
🧹 Nitpick comments (2)
packages/gorm/challenge-1-crud-operations/submissions/Onkar-25/solution.go (2)

35-49: Remove commented-out demo code and TODOs to keep the solution clean.

This submission is already implemented; leaving commented samples adds noise.

♻️ Suggested cleanup
 func CreateUser(db *gorm.DB, user *User) error {
-	// TODO: Implement user creation
     result := db.Create(user)
     if result.Error != nil {
         return result.Error
     }
-
-    // // Create multiple users
-    // users := []User{
-    //     {Name: "User 1", Email: "user1@example.com", Age: 25},
-    //     {Name: "User 2", Email: "user2@example.com", Age: 30},
-    // }
-    // result = db.Create(&users)
 	return nil
 }

63-71: Rename slice variable for clarity.

Using plural naming helps readability when returning a slice.

♻️ Suggested rename
 func GetAllUsers(db *gorm.DB) ([]User, error) {
-	var user []User
-    result := db.Find(&user) // Find by primary key
+	var users []User
+    result := db.Find(&users)
     if result.Error != nil {
-        return user,result.Error
+        return users,result.Error
     }
-	return user, nil
+	return users, nil
 }

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