Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Comment on lines +31 to +36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

**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.

Suggested change
// 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.


// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

UpdateUser has multiple correctness issues (wrong record checked, pointer level, unreachable code)

There are a few concrete bugs here:

  1. 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 to user.ID. This does not actually verify the existence of the user being updated.

  2. Pointer-of-pointer passed to GORM
    As in CreateUser, db.Save(&user) passes a **User instead of *User.

  3. Unreachable return
    The final return result.Error is unreachable after the if/else both 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.

Suggested change
// 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.


// 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
}
Loading