diff options
Diffstat (limited to 'models/issue_stopwatch.go')
-rw-r--r-- | models/issue_stopwatch.go | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/models/issue_stopwatch.go b/models/issue_stopwatch.go index b3022cfc27..1e8cf4c6a6 100644 --- a/models/issue_stopwatch.go +++ b/models/issue_stopwatch.go @@ -8,6 +8,7 @@ import ( "fmt" "time" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/timeutil" "xorm.io/xorm" @@ -21,6 +22,10 @@ type Stopwatch struct { CreatedUnix timeutil.TimeStamp `xorm:"created"` } +func init() { + db.RegisterModel(new(Stopwatch)) +} + // Seconds returns the amount of time passed since creation, based on local server time func (s Stopwatch) Seconds() int64 { return int64(timeutil.TimeStampNow() - s.CreatedUnix) @@ -31,7 +36,7 @@ func (s Stopwatch) Duration() string { return SecToTime(s.Seconds()) } -func getStopwatch(e Engine, userID, issueID int64) (sw *Stopwatch, exists bool, err error) { +func getStopwatch(e db.Engine, userID, issueID int64) (sw *Stopwatch, exists bool, err error) { sw = new(Stopwatch) exists, err = e. Where("user_id = ?", userID). @@ -43,7 +48,7 @@ func getStopwatch(e Engine, userID, issueID int64) (sw *Stopwatch, exists bool, // GetUserStopwatches return list of all stopwatches of a user func GetUserStopwatches(userID int64, listOptions ListOptions) ([]*Stopwatch, error) { sws := make([]*Stopwatch, 0, 8) - sess := x.Where("stopwatch.user_id = ?", userID) + sess := db.DefaultContext().Engine().Where("stopwatch.user_id = ?", userID) if listOptions.Page != 0 { sess = setSessionPagination(sess, &listOptions) } @@ -57,21 +62,21 @@ func GetUserStopwatches(userID int64, listOptions ListOptions) ([]*Stopwatch, er // CountUserStopwatches return count of all stopwatches of a user func CountUserStopwatches(userID int64) (int64, error) { - return x.Where("user_id = ?", userID).Count(&Stopwatch{}) + return db.DefaultContext().Engine().Where("user_id = ?", userID).Count(&Stopwatch{}) } // StopwatchExists returns true if the stopwatch exists func StopwatchExists(userID, issueID int64) bool { - _, exists, _ := getStopwatch(x, userID, issueID) + _, exists, _ := getStopwatch(db.DefaultContext().Engine(), userID, issueID) return exists } // HasUserStopwatch returns true if the user has a stopwatch func HasUserStopwatch(userID int64) (exists bool, sw *Stopwatch, err error) { - return hasUserStopwatch(x, userID) + return hasUserStopwatch(db.DefaultContext().Engine(), userID) } -func hasUserStopwatch(e Engine, userID int64) (exists bool, sw *Stopwatch, err error) { +func hasUserStopwatch(e db.Engine, userID int64) (exists bool, sw *Stopwatch, err error) { sw = new(Stopwatch) exists, err = e. Where("user_id = ?", userID). @@ -81,7 +86,7 @@ func hasUserStopwatch(e Engine, userID int64) (exists bool, sw *Stopwatch, err e // CreateOrStopIssueStopwatch will create or remove a stopwatch and will log it into issue's timeline. func CreateOrStopIssueStopwatch(user *User, issue *Issue) error { - sess := x.NewSession() + sess := db.DefaultContext().NewSession() defer sess.Close() if err := sess.Begin(); err != nil { return err @@ -170,7 +175,7 @@ func createOrStopIssueStopwatch(e *xorm.Session, user *User, issue *Issue) error // CancelStopwatch removes the given stopwatch and logs it into issue's timeline. func CancelStopwatch(user *User, issue *Issue) error { - sess := x.NewSession() + sess := db.DefaultContext().NewSession() defer sess.Close() if err := sess.Begin(); err != nil { return err |