1
Fork 0

[API] extend StopWatch (#9196)

* squash api-stopwatch

* fix prepair logic! + add Tests

* fix lint

* more robust time compare

* delete responce 202 -> 204

* change http responce in test too
This commit is contained in:
6543 2019-12-12 05:23:05 +01:00 committed by techknowlogick
parent 382936a668
commit aceb1085c7
9 changed files with 482 additions and 141 deletions

View file

@ -2,10 +2,10 @@
id: 1
user_id: 1
issue_id: 1
created_unix: 1500988502
created_unix: 1500988001
-
id: 2
user_id: 2
issue_id: 2
created_unix: 1500988502
created_unix: 1500988002

View file

@ -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
}