-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
50 lines (41 loc) · 994 Bytes
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package repository
import (
"github.com/icoder-new/reporter/models"
"gorm.io/gorm"
)
type UserRepository struct {
db *gorm.DB
}
func NewUserRepository(db *gorm.DB) *UserRepository {
return &UserRepository{
db: db,
}
}
func (u *UserRepository) GetUserById(id int) (models.User, error) {
var user models.User
err := u.db.Where("id = ? AND is_active = ?", id, true).First(&user).Error
if err != nil {
return user, err
}
return user, nil
}
func (u *UserRepository) UpdateUser(user models.User) (models.User, error) {
if err := u.db.Save(&user).Error; err != nil {
return models.User{}, err
}
return user, nil
}
func (u *UserRepository) DeleteUserById(id int) error {
return u.db.
Model(&models.User{}).
Where("id = ? AND is_active = TRUE", id).
Update("is_active", false).
Error
}
func (u *UserRepository) RestoreUserById(id int) error {
return u.db.
Model(&models.User{}).
Where("id = ? AND is_active = FALSE", id).
Update("is_active", true).
Error
}