-
-
Notifications
You must be signed in to change notification settings - Fork 753
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,77 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| package main | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "time" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| "gorm.io/gorm" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "gorm.io/driver/sqlite" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // User represents a user in the system | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| type User struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ID uint `gorm:"primaryKey"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Name string `gorm:"not null"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Email string `gorm:"unique;not null"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Age int `gorm:"check:age > 0"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| CreatedAt time.Time | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| UpdatedAt time.Time | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // ConnectDB establishes a connection to the SQLite database | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| func ConnectDB() (*gorm.DB, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Implement database connection | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| err = db.AutoMigrate(&User{}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return db, err | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // GetUserByID retrieves a user by their ID | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| func GetUserByID(db *gorm.DB, id uint) (*User, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Implement user retrieval by ID | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| var user User | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| result := db.First(&user, id) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return &user, result.Error | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // GetAllUsers retrieves all users from the database | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| func GetAllUsers(db *gorm.DB) ([]User, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Implement retrieval of all users | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| var users []User | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| result := db.Find(&users) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return users, result.Error | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+54
to
+65
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
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 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // DeleteUser removes a user from the database | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| func DeleteUser(db *gorm.DB, id uint) error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Implement user deletion | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| var user2 User | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| result := db.First(&user2, id) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if result.RowsAffected == 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return result.Error | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = db.Delete(&User{}, id) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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)
CreateUserreceivesuser *Userbut passes&userto 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
🤖 Prompt for AI Agents