-
-
Notifications
You must be signed in to change notification settings - Fork 713
Add solution for gorm challenge-1-crud-operations by Relief-chat #763
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
Open
Relief-chat
wants to merge
1
commit into
RezaSi:main
Choose a base branch
from
Relief-chat:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
packages/gorm/challenge-1-crud-operations/submissions/Relief-chat/solution.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| 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{}) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return db, err | ||
| } | ||
|
|
||
| // CreateUser creates a new user in the database | ||
| func CreateUser(db *gorm.DB, user *User) error { | ||
| // TODO: Implement user creation | ||
| return db.Create(user).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) | ||
| if result.Error != nil { | ||
| return nil, result.Error | ||
| } | ||
| return users, result.Error | ||
| } | ||
|
|
||
| // UpdateUser updates an existing user's information | ||
| func UpdateUser(db *gorm.DB, user *User) error { | ||
| // TODO: Implement user update | ||
| if err := db.First(&User{}, user.ID).Error; err != nil { | ||
| return err | ||
| } | ||
| return db.Model(&user).Updates(*user).Error | ||
| } | ||
|
|
||
| // DeleteUser removes a user from the database | ||
| func DeleteUser(db *gorm.DB, id uint) error { | ||
| // TODO: Implement user deletion | ||
| if err := db.First(&User{}, id).Error; err != nil { | ||
| return err | ||
| } | ||
| return db.Delete(&User{}, id).Error | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 the Model method call.
Line 65 has a bug:
db.Model(&user)passes a pointer-to-pointer sinceuseris already*User. This should bedb.Model(user)instead.Apply this diff:
if err := db.First(&User{}, user.ID).Error; err != nil { return err } - return db.Model(&user).Updates(*user).Error + return db.Model(user).Updates(user).ErrorNote: You can also pass
userdirectly toUpdatesinstead of dereferencing it.📝 Committable suggestion
🤖 Prompt for AI Agents