-
Notifications
You must be signed in to change notification settings - Fork 0
/
gctt.go
175 lines (135 loc) · 4.23 KB
/
gctt.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2017 The StudyGolang Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// https://studygolang.com
// Author:polaris polaris@studygolang.com
package logic
import (
"context"
"github.com/ue4-community/learnue/model"
"time"
. "github.com/ue4-community/learnue/db"
)
type GCTTLogic struct{}
var DefaultGCTT = GCTTLogic{}
func (self GCTTLogic) FindTranslator(ctx context.Context, me *model.Me) *model.GCTTUser {
objLog := GetLogger(ctx)
gcttUser := &model.GCTTUser{}
_, err := MasterDB.Where("uid=?", me.Uid).Get(gcttUser)
if err != nil {
objLog.Errorln("GCTTLogic FindTranslator error:", err)
return nil
}
return gcttUser
}
func (self GCTTLogic) FindOne(ctx context.Context, username string) *model.GCTTUser {
objLog := GetLogger(ctx)
gcttUser := &model.GCTTUser{}
_, err := MasterDB.Where("username=?", username).Get(gcttUser)
if err != nil {
objLog.Errorln("GCTTLogic FindOne error:", err)
return nil
}
return gcttUser
}
func (self GCTTLogic) BindUser(ctx context.Context, gcttUser *model.GCTTUser, uid int, githubUser *model.BindUser) error {
objLog := GetLogger(ctx)
var err error
if gcttUser.Id > 0 {
gcttUser.Uid = uid
_, err = MasterDB.Id(gcttUser.Id).Update(gcttUser)
} else {
gcttUser = &model.GCTTUser{
Username: githubUser.Username,
Avatar: githubUser.Avatar,
Uid: uid,
JoinedAt: time.Now().Unix(),
}
_, err = MasterDB.Insert(gcttUser)
}
if err != nil {
objLog.Errorln("GCTTLogic BindUser error:", err)
}
return err
}
func (self GCTTLogic) FindCoreUsers(ctx context.Context) []*model.GCTTUser {
objLog := GetLogger(ctx)
gcttUsers := make([]*model.GCTTUser, 0)
err := MasterDB.Where("role!=?", model.GCTTRoleTranslator).OrderBy("role ASC").Find(&gcttUsers)
if err != nil {
objLog.Errorln("GCTTLogic FindUsers error:", err)
}
return gcttUsers
}
func (self GCTTLogic) FindUsers(ctx context.Context) []*model.GCTTUser {
objLog := GetLogger(ctx)
gcttUsers := make([]*model.GCTTUser, 0)
err := MasterDB.Where("num>0").OrderBy("num DESC,words DESC").Find(&gcttUsers)
if err != nil {
objLog.Errorln("GCTTLogic FindUsers error:", err)
}
return gcttUsers
}
func (self GCTTLogic) FindUnTranslateIssues(ctx context.Context, limit int) []*model.GCTTIssue {
objLog := GetLogger(ctx)
gcttIssues := make([]*model.GCTTIssue, 0)
err := MasterDB.Where("state=?", model.IssueOpened).
Limit(limit).OrderBy("id DESC").Find(&gcttIssues)
if err != nil {
objLog.Errorln("GCTTLogic FindUnTranslateIssues error:", err)
}
return gcttIssues
}
func (self GCTTLogic) FindIssues(ctx context.Context, paginator *Paginator, querysring string, args ...interface{}) []*model.GCTTIssue {
objLog := GetLogger(ctx)
gcttIssues := make([]*model.GCTTIssue, 0)
session := MasterDB.Limit(paginator.PerPage(), paginator.Offset())
if args[0] == model.LabelClaimed {
session.OrderBy("translating_at DESC")
} else {
session.OrderBy("id DESC")
}
if querysring != "" {
session.Where(querysring, args...)
}
err := session.Limit(paginator.PerPage(), paginator.Offset()).Find(&gcttIssues)
if err != nil {
objLog.Errorln("GCTTLogic FindIssues error:", err)
}
return gcttIssues
}
func (self GCTTLogic) IssueCount(ctx context.Context, querystring string, args ...interface{}) int64 {
objLog := GetLogger(ctx)
var (
total int64
err error
)
if querystring == "" {
total, err = MasterDB.Count(new(model.GCTTIssue))
} else {
total, err = MasterDB.Where(querystring, args...).Count(new(model.GCTTIssue))
}
if err != nil {
objLog.Errorln("GCTTLogic Count error:", err)
}
return total
}
func (self GCTTLogic) FindNewestGit(ctx context.Context) []*model.GCTTGit {
objLog := GetLogger(ctx)
gcttGits := make([]*model.GCTTGit, 0)
err := MasterDB.Where("translated_at!=0").OrderBy("translated_at DESC").
Limit(10).Find(&gcttGits)
if err != nil {
objLog.Errorln("GCTTLogic FindNewestGit error:", err)
}
return gcttGits
}
func (self GCTTLogic) FindTimeLines(ctx context.Context) []*model.GCTTTimeLine {
objLog := GetLogger(ctx)
gcttTimeLines := make([]*model.GCTTTimeLine, 0)
err := MasterDB.Find(&gcttTimeLines)
if err != nil {
objLog.Errorln("GCTTLogic FindTimeLines error:", err)
}
return gcttTimeLines
}