-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
112 lines (90 loc) · 2.11 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package models
import (
"log"
"time"
"github.com/rosberry/go-pagination"
"github.com/DATA-DOG/go-sqlmock"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
type (
BaseModel struct {
ID uint
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt
}
User struct {
BaseModel
Name string
Role uint `cursor:"roleID"`
Clappers []Clapper
}
Clapper struct {
BaseModel
UserID uint
Name string
Token string
}
Material struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
Link string
Comment string
ItemID string //Unical +
ItemOwnerID int //Unical
ItemType string
UserID uint
User User `gorm:"foreignKey:UserID"`
}
)
func GetUsersList(role uint, paginator *pagination.Paginator) []User {
//db := mockDB().Session(&gorm.Session{DryRun: true})
db := liveDB().Session(&gorm.Session{DryRun: false})
paginator.Options.Limit = 2
paginator.Options.DB = db
paginator.Options.Model = &User{}
var users []User
q := db.Model(&User{}).Preload("Clappers").Where("id < ?", 190)
err := paginator.Find(q, &users)
if err != nil {
log.Println(err)
return nil
}
log.Printf("User: %+v", users)
return users
}
//GetMaterialsList return all materials
func GetMaterialsList(paginator *pagination.Paginator) (materials []Material) {
db := liveDB().Session(&gorm.Session{DryRun: false})
paginator.Options.Limit = 2
paginator.Options.DB = db
paginator.Options.Model = &Material{}
q := db.Model(&Material{}).Joins("User")
paginator.Find(q, &materials)
return
}
//DB connection
var gormConf = &gorm.Config{
PrepareStmt: true,
}
func mockDB() *gorm.DB {
sqlDB, _, _ := sqlmock.New()
db, err := gorm.Open(postgres.New(postgres.Config{Conn: sqlDB}), gormConf)
if err != nil {
log.Println(err)
return nil
}
return db.Debug()
}
func liveDB() *gorm.DB {
connString := "host=localhost port=5432 user=postgres dbname=clapper password=123 sslmode=disable"
db, err := gorm.Open(postgres.Open(connString), gormConf)
if err != nil {
log.Println(err)
return nil
}
return db.Debug()
}