-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository_update_users_curd.go
65 lines (53 loc) · 1.44 KB
/
repository_update_users_curd.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package curd
import (
"time"
"github.com/nitinda/microservice-change-log/api/models"
"github.com/nitinda/microservice-change-log/api/utils/channels"
"github.com/nitinda/microservice-change-log/logger"
"gorm.io/gorm"
)
func (r *respositoryUsersCRUD) UpdateUser(uid uint32, user models.User) (int64, error) {
// var err error
var result *gorm.DB
done := make(chan bool)
go func(ch chan<- bool) {
defer close(ch)
// rs = r.db.Debug().Model(&models.User{}).Where("id = ?", uid).Take(&models.User{}).UpdateColumns(
// map[string]interface{}{
// "username": user.Username,
// "email": user.Email,
// "updated_at": time.Now(),
// },
// )
// if err != nil {
// ch <- false
// return
// }
// Update with conditions and model value
result = r.db.Model(&models.User{}).Where("id = ?", uid).Updates(
map[string]interface{}{
"username": user.Username,
"email": user.Email,
"updated_at": time.Now(),
},
)
// UPDATE users SET username='hello', updated_at='2013-11-17 21:34:10', email="asdfasdf" WHERE id=1 AND active=true;
if result.Error != nil {
logger.Error.Println(result.Error, gorm.ErrRecordNotFound)
ch <- false
return
}
dbSQL, ok := result.DB()
if ok == nil {
defer dbSQL.Close()
}
ch <- true
}(done)
if channels.ValidateChannel(done) {
if result.Error != nil {
return 0, result.Error
}
return result.RowsAffected, nil
}
return 0, result.Error
}