Skip to content

Commit 47dc0b8

Browse files
Add solution for gorm challenge-1-crud-operations by shansing (#1458)
* Add solution for gorm challenge-1-crud-operations * fix: transaction for user update and deletion --------- Co-authored-by: go-interview-practice-bot[bot] <230190823+go-interview-practice-bot[bot]@users.noreply.github.com>
1 parent 061594e commit 47dc0b8

File tree

1 file changed

+105
-0
lines changed
  • packages/gorm/challenge-1-crud-operations/submissions/shansing

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"time"
7+
8+
"gorm.io/driver/sqlite"
9+
"gorm.io/gorm"
10+
)
11+
12+
// User represents a user in the system
13+
type User struct {
14+
ID uint `gorm:"primaryKey"`
15+
Name string `gorm:"not null"`
16+
Email string `gorm:"unique;not null"`
17+
Age int `gorm:"check:age > 0"`
18+
CreatedAt time.Time
19+
UpdatedAt time.Time
20+
}
21+
22+
// ConnectDB establishes a connection to the SQLite database
23+
func ConnectDB() (*gorm.DB, error) {
24+
// Implement database connection
25+
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
26+
if err != nil {
27+
return nil, err
28+
}
29+
err = db.AutoMigrate(&User{})
30+
if err != nil {
31+
return nil, err
32+
}
33+
return db, nil
34+
}
35+
36+
// CreateUser creates a new user in the database
37+
func CreateUser(db *gorm.DB, user *User) error {
38+
// Implement user creation
39+
result := db.Create(user)
40+
if result.Error != nil {
41+
return result.Error
42+
}
43+
return nil
44+
}
45+
46+
// GetUserByID retrieves a user by their ID
47+
func GetUserByID(db *gorm.DB, id uint) (*User, error) {
48+
// Implement user retrieval by ID
49+
var user User
50+
result := db.First(&user, id)
51+
if result.Error != nil {
52+
return nil, result.Error
53+
}
54+
return &user, nil
55+
}
56+
57+
// GetAllUsers retrieves all users from the database
58+
func GetAllUsers(db *gorm.DB) ([]User, error) {
59+
// Implement retrieval of all users
60+
var users []User
61+
result := db.Find(&users)
62+
if result.Error != nil {
63+
return nil, result.Error
64+
}
65+
return users, nil
66+
}
67+
68+
// UpdateUser updates an existing user's information
69+
func UpdateUser(db *gorm.DB, user *User) error {
70+
// Implement user update
71+
return db.Transaction(func(tx *gorm.DB) error {
72+
if u := tx.First(&User{}, user.ID); u.Error != nil {
73+
if errors.Is(u.Error, gorm.ErrRecordNotFound) {
74+
return fmt.Errorf("user with id %d not found", user.ID)
75+
}
76+
return fmt.Errorf("user with id %d error", user.ID)
77+
}
78+
result := tx.Save(user)
79+
if result.Error != nil {
80+
return result.Error
81+
}
82+
return nil
83+
})
84+
}
85+
86+
// DeleteUser removes a user from the database
87+
func DeleteUser(db *gorm.DB, id uint) error {
88+
// Implement user deletion
89+
return db.Transaction(func(tx *gorm.DB) error {
90+
if u := tx.First(&User{}, id); u.Error != nil {
91+
if errors.Is(u.Error, gorm.ErrRecordNotFound) {
92+
return fmt.Errorf("user with id %d not found", id)
93+
}
94+
return fmt.Errorf("user with id %d error", id)
95+
}
96+
result := tx.Delete(&User{}, id)
97+
if result.Error != nil {
98+
return result.Error
99+
}
100+
if result.RowsAffected == 0 {
101+
return fmt.Errorf("user with id %d not found", id)
102+
}
103+
return nil
104+
})
105+
}

0 commit comments

Comments
 (0)