/
threat.go
38 lines (33 loc) · 1.18 KB
/
threat.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
package repository
import (
"context"
"github.com/google/wire"
"github.com/hackerchai/threatbook-ip-web/ent"
"github.com/hackerchai/threatbook-ip-web/ent/threat"
"github.com/hackerchai/threatbook-ip-web/pkg/logger"
)
import "github.com/hackerchai/threatbook-ip-web/internal/app/schema"
var ThreatRepoStruct = wire.Struct(new(ThreatRepo), "*")
type ThreatRepo struct {
DB *ent.Client
}
func (t *ThreatRepo) QueryThreatsWithPagnition(ctx context.Context, pagnition schema.PaginationParam) ([]*ent.Threat, schema.PaginationResponse, error) {
current := pagnition.GetCurrent()
pageSize := pagnition.GetPageSize()
offset := (current - 1) * pageSize
threats, err := t.DB.Threat.Query().Order(ent.Desc(threat.FieldCtime)).Offset(offset).Limit(pageSize).All(ctx)
if err != nil {
logger.Error("QueryThreatsWithPagnition error: " + err.Error())
return nil, schema.PaginationResponse{}, err
}
if total, err := t.DB.Threat.Query().Count(ctx); err != nil {
logger.Error("QueryThreatsWithPagnition error: " + err.Error())
return nil, schema.PaginationResponse{}, err
} else {
return threats, schema.PaginationResponse{
Current: current,
Total: total,
PageSize: pageSize,
}, nil
}
}