aboutsummaryrefslogtreecommitdiffstats
path: root/models/issue_stopwatch.go
diff options
context:
space:
mode:
Diffstat (limited to 'models/issue_stopwatch.go')
-rw-r--r--models/issue_stopwatch.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/models/issue_stopwatch.go b/models/issue_stopwatch.go
index d7c3a9f73b..8047f122b5 100644
--- a/models/issue_stopwatch.go
+++ b/models/issue_stopwatch.go
@@ -8,6 +8,7 @@ import (
"fmt"
"time"
+ api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
)
@@ -19,6 +20,9 @@ type Stopwatch struct {
CreatedUnix timeutil.TimeStamp `xorm:"created"`
}
+// Stopwatches is a List ful of Stopwatch
+type Stopwatches []Stopwatch
+
func getStopwatch(e Engine, userID, issueID int64) (sw *Stopwatch, exists bool, err error) {
sw = new(Stopwatch)
exists, err = e.
@@ -28,6 +32,16 @@ func getStopwatch(e Engine, userID, issueID int64) (sw *Stopwatch, exists bool,
return
}
+// GetUserStopwatches return list of all stopwatches of a user
+func GetUserStopwatches(userID int64) (sws *Stopwatches, err error) {
+ sws = new(Stopwatches)
+ err = x.Where("stopwatch.user_id = ?", userID).Find(sws)
+ if err != nil {
+ return nil, err
+ }
+ return sws, nil
+}
+
// StopwatchExists returns true if the stopwatch exists
func StopwatchExists(userID int64, issueID int64) bool {
_, exists, _ := getStopwatch(x, userID, issueID)
@@ -160,3 +174,28 @@ func SecToTime(duration int64) string {
return hrs
}
+
+// APIFormat convert Stopwatch type to api.StopWatch type
+func (sw *Stopwatch) APIFormat() (api.StopWatch, error) {
+ issue, err := getIssueByID(x, sw.IssueID)
+ if err != nil {
+ return api.StopWatch{}, err
+ }
+ return api.StopWatch{
+ Created: sw.CreatedUnix.AsTime(),
+ IssueIndex: issue.Index,
+ }, nil
+}
+
+// APIFormat convert Stopwatches type to api.StopWatches type
+func (sws Stopwatches) APIFormat() (api.StopWatches, error) {
+ result := api.StopWatches(make([]api.StopWatch, 0, len(sws)))
+ for _, sw := range sws {
+ apiSW, err := sw.APIFormat()
+ if err != nil {
+ return nil, err
+ }
+ result = append(result, apiSW)
+ }
+ return result, nil
+}