-
-
Notifications
You must be signed in to change notification settings - Fork 745
Add solution for gorm challenge-1-crud-operations by aswinsreeraj #842
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add solution for gorm challenge-1-crud-operations by aswinsreeraj #842
Conversation
WalkthroughAdds a new submission file implementing a User model with GORM/SQLite backend, exposing database connection, Create, Read (by ID and all), Update, and Delete operations. All functions return standard GORM errors. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Possibly related PRs
Pre-merge checks✅ Passed checks (3 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.
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 (5)
packages/gorm/challenge-1-crud-operations/submissions/aswinsreeraj/solution.go (5)
10-18: User model looks idiomatic; consider tightening the age constraint if neededThe struct definition and GORM tags are generally solid and idiomatic. If you want to strictly forbid “unknown” age values, you might also add
not nulltoAgeso that both the default zero value and negative ages are rejected at the DB level:Age int `gorm:"not null;check:age > 0"`This is optional and depends on the challenge requirements.
20-29: ConnectDB implementation is fine; stale TODO and hard‑coded DSNThe connection and
AutoMigrateusage are correct for a simple SQLite‑backed challenge solution. Two small improvements:
- Remove the
// TODOcomment now that the function is implemented.- Consider making
"test.db"configurable (e.g., parameter or env var) so tests or callers can control the DB path if needed.Example:
func ConnectDB(dsn string) (*gorm.DB, error) { db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{}) if err != nil { return nil, err } return db, db.AutoMigrate(&User{}) }
38-44: GetUserByID behavior is correct and straightforwardThis is a clean, idiomatic wrapper around
First, returning both the user and the underlying GORM error (includingErrRecordNotFoundwhen appropriate). Just a minor nit: you can safely remove the// TODOcomment.func GetUserByID(db *gorm.DB, id uint) (*User, error) { var user User err := db.First(&user, id).Error return &user, err }
46-52: GetAllUsers implementation looks goodFetching all users via
db.Find(&users)and returning the slice plus error is fine for this challenge. As above, you can drop the stale// TODOcomment:func GetAllUsers(db *gorm.DB) ([]User, error) { var users []User err := db.Find(&users).Error return users, err }
67-77: DeleteUser is functionally fine; minor cleanup possibleThe pattern of
Firstto verify existence and thenDeleteby ID is reasonable for this challenge and correctly surfacesErrRecordNotFoundfrom the initialFirst.A couple of minor cleanups you might consider:
- Use an unnamed
Uservalue since you don’t use the loaded record:var u User result := db.First(&u, id)
- Or compress slightly:
func DeleteUser(db *gorm.DB, id uint) error { var u User if err := db.First(&u, id).Error; err != nil { return err } return db.Delete(&User{}, id).Error }Also, the
// TODOcomment can be removed.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/gorm/challenge-1-crud-operations/submissions/aswinsreeraj/solution.go(1 hunks)
| // CreateUser creates a new user in the database | ||
| func CreateUser(db *gorm.DB, user *User) error { | ||
| // TODO: Implement user creation | ||
| result := db.Create(&user) | ||
| return result.Error | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
**Fix pointer level in CreateUser (currently passing User to GORM)
CreateUser receives user *User but passes &user to GORM, which is a **User. That’s non‑idiomatic and may lead to incorrect behavior when GORM reflects on the value.
You should pass the pointer you already have:
func CreateUser(db *gorm.DB, user *User) error {
- // TODO: Implement user creation
- result := db.Create(&user)
- return result.Error
+ if user == nil {
+ return fmt.Errorf("user is nil")
+ }
+ return db.Create(user).Error
}If you don’t want the nil guard, you can drop that part, but the key change is db.Create(user).
📝 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.
| // CreateUser creates a new user in the database | |
| func CreateUser(db *gorm.DB, user *User) error { | |
| // TODO: Implement user creation | |
| result := db.Create(&user) | |
| return result.Error | |
| } | |
| // CreateUser creates a new user in the database | |
| func CreateUser(db *gorm.DB, user *User) error { | |
| if user == nil { | |
| return fmt.Errorf("user is nil") | |
| } | |
| return db.Create(user).Error | |
| } |
🤖 Prompt for AI Agents
In
packages/gorm/challenge-1-crud-operations/submissions/aswinsreeraj/solution.go
around lines 31 to 36, CreateUser currently passes &user (a **User) into GORM;
change the call to pass the existing pointer user (db.Create(user)) so GORM
receives *User, and optionally add a nil check (return error if user == nil)
before calling db.Create to avoid passing a nil pointer.
| // UpdateUser updates an existing user's information | ||
| func UpdateUser(db *gorm.DB, user *User) error { | ||
| // TODO: Implement user update | ||
| var user2 User | ||
| result := db.First(&user2) | ||
| if result.RowsAffected == 0 { | ||
| return result.Error | ||
| } else { | ||
| return db.Save(&user).Error | ||
| } | ||
| return result.Error | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UpdateUser has multiple correctness issues (wrong record checked, pointer level, unreachable code)
There are a few concrete bugs here:
-
Existence check ignores the target user
db.First(&user2)without an ID or condition just loads the first row in the table, not the record corresponding touser.ID. This does not actually verify the existence of the user being updated. -
Pointer-of-pointer passed to GORM
As inCreateUser,db.Save(&user)passes a**Userinstead of*User. -
Unreachable return
The finalreturn result.Erroris unreachable after theif/elseboth return.
A more idiomatic implementation that verifies existence by ID and then updates:
func UpdateUser(db *gorm.DB, user *User) error {
- // TODO: Implement user update
- var user2 User
- result := db.First(&user2)
- if result.RowsAffected == 0 {
- return result.Error
- } else {
- return db.Save(&user).Error
- }
- return result.Error
+ if user == nil {
+ return fmt.Errorf("user is nil")
+ }
+
+ // Ensure the user exists (by primary key) before updating.
+ if err := db.First(&User{}, user.ID).Error; err != nil {
+ return err
+ }
+
+ return db.Save(user).Error
}Even if you skip the nil check, you should at minimum query with user.ID and call db.Save(user).
📝 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.
| // UpdateUser updates an existing user's information | |
| func UpdateUser(db *gorm.DB, user *User) error { | |
| // TODO: Implement user update | |
| var user2 User | |
| result := db.First(&user2) | |
| if result.RowsAffected == 0 { | |
| return result.Error | |
| } else { | |
| return db.Save(&user).Error | |
| } | |
| return result.Error | |
| } | |
| // UpdateUser updates an existing user's information | |
| func UpdateUser(db *gorm.DB, user *User) error { | |
| if user == nil { | |
| return fmt.Errorf("user is nil") | |
| } | |
| // Ensure the user exists (by primary key) before updating. | |
| if err := db.First(&User{}, user.ID).Error; err != nil { | |
| return err | |
| } | |
| return db.Save(user).Error | |
| } |
🤖 Prompt for AI Agents
In
packages/gorm/challenge-1-crud-operations/submissions/aswinsreeraj/solution.go
around lines 54 to 65, the UpdateUser function incorrectly checks existence by
loading the first row, passes a pointer-to-pointer to GORM, and contains an
unreachable return; fix by querying for the target record using the provided
user.ID (e.g., db.First(&existing, user.ID) or db.First(&existing, "id = ?",
user.ID)), handle result.Error and RowsAffected appropriately, call
db.Save(user) (not &user) to update the passed *User, and remove the final
unreachable return so the function returns the correct error from the existence
check or the save operation.
gorm challenge-1-crud-operations Solution
Submitted by: @aswinsreeraj
Package: gorm
Challenge: challenge-1-crud-operations
Description
This PR contains my solution for gorm challenge-1-crud-operations.
Changes
packages/gorm/challenge-1-crud-operations/submissions/aswinsreeraj/solution.goTesting
Thank you for reviewing my submission! 🚀