-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
96 lines (83 loc) · 1.77 KB
/
log.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
package model
import (
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/bson/primitive"
"github.com/mongodb/mongo-go-driver/mongo"
log "github.com/sirupsen/logrus"
)
// Log ...
type Log struct {
Model `bson:",inline"`
UserID primitive.ObjectID `bson:"user_id"`
Method string `bson:"method"`
URL string `bson:"url"`
Permission string `bson:"permission"`
Err string `bson:"err"`
Detail string `bson:"detail"`
VisitIP string `bson:"visit_ip"`
}
// NewLog ...
func NewLog() *Log {
return &Log{
Model: model(),
}
}
// CreateIfNotExist ...
func (l *Log) CreateIfNotExist() error {
return CreateIfNotExist(l)
}
// Create ...
func (l *Log) Create() error {
return InsertOne(l)
}
// Update ...
func (l *Log) Update() error {
return UpdateOne(l)
}
// Delete ...
func (l *Log) Delete() error {
return DeleteByID(l)
}
// Find ...
func (l *Log) Find() error {
return FindByID(l)
}
// All ...
func (l *Log) All() ([]*Log, error) {
var logs []*Log
m := bson.M{}
err := Find(l, m, func(cursor mongo.Cursor) error {
log.Println(cursor.DecodeBytes())
var l Log
err := cursor.Decode(&l)
if err != nil {
return err
}
logs = append(logs, &l)
return nil
})
return logs, err
}
// Pages ...
func (l *Log) Pages(order, limit, current int64) ([]*Log, int64) {
var logs []*Log
m := bson.M{}
i, _ := Count(l, m)
err := Pages(l, m, order, limit, current, func(cursor mongo.Cursor) error {
log.Println(cursor.DecodeBytes())
var l Log
err := cursor.Decode(&l)
if err != nil {
return err
}
logs = append(logs, &l)
return nil
})
if err != nil {
return nil, 0
}
return logs, i
}
func (l *Log) _Name() string {
return "log"
}