Skip to content

Commit 973799b

Browse files
committed
squash api-stopwatch
1 parent baae90e commit 973799b

File tree

7 files changed

+393
-139
lines changed

7 files changed

+393
-139
lines changed

models/issue_stopwatch.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"time"
1010

11+
api "code.gitea.io/gitea/modules/structs"
1112
"code.gitea.io/gitea/modules/timeutil"
1213
)
1314

@@ -28,6 +29,16 @@ func getStopwatch(e Engine, userID, issueID int64) (sw *Stopwatch, exists bool,
2829
return
2930
}
3031

32+
// GetUserStopwatches return list of all stopwatches of a user
33+
func GetUserStopwatches(userID int64) (sws *Stopwatches, err error) {
34+
sws = new(Stopwatches)
35+
err = x.Where("stopwatch.user_id = ?", userID).Find(sws)
36+
if err != nil {
37+
return nil, err
38+
}
39+
return sws, nil
40+
}
41+
3142
// StopwatchExists returns true if the stopwatch exists
3243
func StopwatchExists(userID int64, issueID int64) bool {
3344
_, exists, _ := getStopwatch(x, userID, issueID)
@@ -160,3 +171,31 @@ func SecToTime(duration int64) string {
160171

161172
return hrs
162173
}
174+
175+
// APIFormat convert Stopwatch type to api.StopWatch type
176+
func (sw *Stopwatch) APIFormat() (api.StopWatch, error) {
177+
issue, err := getIssueByID(x, sw.IssueID)
178+
if err != nil {
179+
return api.StopWatch{}, err
180+
}
181+
return api.StopWatch{
182+
Created: sw.CreatedUnix.AsTime(),
183+
IssueIndex: issue.Index,
184+
}, nil
185+
}
186+
187+
// Stopwatches is a List ful of Stopwatch
188+
type Stopwatches []Stopwatch
189+
190+
// APIFormat convert Stopwatches type to api.StopWatches type
191+
func (sws Stopwatches) APIFormat() (api.StopWatches, error) {
192+
result := api.StopWatches(make([]api.StopWatch, 0, len(sws)))
193+
for _, sw := range sws {
194+
apiSW, err := sw.APIFormat()
195+
if err != nil {
196+
return nil, err
197+
}
198+
result = append(result, apiSW)
199+
}
200+
return result, nil
201+
}

modules/structs/issue_stopwatch.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package structs
6+
7+
import (
8+
"time"
9+
)
10+
11+
// StopWatch represent a running stopwatch
12+
type StopWatch struct {
13+
// swagger:strfmt date-time
14+
Created time.Time `json:"created"`
15+
IssueIndex int64 `json:"issue_index"`
16+
}
17+
18+
// StopWatches represent a list of stopwatches
19+
type StopWatches []StopWatch

routers/api/v1/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,8 @@ func RegisterRoutes(m *macaron.Macaron) {
584584
})
585585
m.Get("/times", repo.ListMyTrackedTimes)
586586

587+
m.Get("/stopwatches", repo.GetStopwatches)
588+
587589
m.Get("/subscriptions", user.GetMyWatchedRepos)
588590

589591
m.Get("/teams", org.ListUserTeams)
@@ -691,6 +693,7 @@ func RegisterRoutes(m *macaron.Macaron) {
691693
m.Group("/stopwatch", func() {
692694
m.Post("/start", reqToken(), repo.StartIssueStopwatch)
693695
m.Post("/stop", reqToken(), repo.StopIssueStopwatch)
696+
m.Delete("/delete", reqToken(), repo.DeleteIssueStopwatch)
694697
})
695698
m.Group("/subscriptions", func() {
696699
m.Get("", repo.GetIssueSubscribers)

routers/api/v1/repo/issue.go

Lines changed: 0 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -598,141 +598,3 @@ func UpdateIssueDeadline(ctx *context.APIContext, form api.EditDeadlineOption) {
598598

599599
ctx.JSON(201, api.IssueDeadline{Deadline: &deadline})
600600
}
601-
602-
// StartIssueStopwatch creates a stopwatch for the given issue.
603-
func StartIssueStopwatch(ctx *context.APIContext) {
604-
// swagger:operation POST /repos/{owner}/{repo}/issues/{index}/stopwatch/start issue issueStartStopWatch
605-
// ---
606-
// summary: Start stopwatch on an issue.
607-
// consumes:
608-
// - application/json
609-
// produces:
610-
// - application/json
611-
// parameters:
612-
// - name: owner
613-
// in: path
614-
// description: owner of the repo
615-
// type: string
616-
// required: true
617-
// - name: repo
618-
// in: path
619-
// description: name of the repo
620-
// type: string
621-
// required: true
622-
// - name: index
623-
// in: path
624-
// description: index of the issue to create the stopwatch on
625-
// type: integer
626-
// format: int64
627-
// required: true
628-
// responses:
629-
// "201":
630-
// "$ref": "#/responses/empty"
631-
// "403":
632-
// description: Not repo writer, user does not have rights to toggle stopwatch
633-
// "404":
634-
// description: Issue not found
635-
// "409":
636-
// description: Cannot start a stopwatch again if it already exists
637-
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
638-
if err != nil {
639-
if models.IsErrIssueNotExist(err) {
640-
ctx.NotFound()
641-
} else {
642-
ctx.Error(500, "GetIssueByIndex", err)
643-
}
644-
645-
return
646-
}
647-
648-
if !ctx.Repo.CanWrite(models.UnitTypeIssues) {
649-
ctx.Status(403)
650-
return
651-
}
652-
653-
if !ctx.Repo.CanUseTimetracker(issue, ctx.User) {
654-
ctx.Status(403)
655-
return
656-
}
657-
658-
if models.StopwatchExists(ctx.User.ID, issue.ID) {
659-
ctx.Error(409, "StopwatchExists", "a stopwatch has already been started for this issue")
660-
return
661-
}
662-
663-
if err := models.CreateOrStopIssueStopwatch(ctx.User, issue); err != nil {
664-
ctx.Error(500, "CreateOrStopIssueStopwatch", err)
665-
return
666-
}
667-
668-
ctx.Status(201)
669-
}
670-
671-
// StopIssueStopwatch stops a stopwatch for the given issue.
672-
func StopIssueStopwatch(ctx *context.APIContext) {
673-
// swagger:operation POST /repos/{owner}/{repo}/issues/{index}/stopwatch/stop issue issueStopWatch
674-
// ---
675-
// summary: Stop an issue's existing stopwatch.
676-
// consumes:
677-
// - application/json
678-
// produces:
679-
// - application/json
680-
// parameters:
681-
// - name: owner
682-
// in: path
683-
// description: owner of the repo
684-
// type: string
685-
// required: true
686-
// - name: repo
687-
// in: path
688-
// description: name of the repo
689-
// type: string
690-
// required: true
691-
// - name: index
692-
// in: path
693-
// description: index of the issue to stop the stopwatch on
694-
// type: integer
695-
// format: int64
696-
// required: true
697-
// responses:
698-
// "201":
699-
// "$ref": "#/responses/empty"
700-
// "403":
701-
// description: Not repo writer, user does not have rights to toggle stopwatch
702-
// "404":
703-
// description: Issue not found
704-
// "409":
705-
// description: Cannot stop a non existent stopwatch
706-
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
707-
if err != nil {
708-
if models.IsErrIssueNotExist(err) {
709-
ctx.NotFound()
710-
} else {
711-
ctx.Error(500, "GetIssueByIndex", err)
712-
}
713-
714-
return
715-
}
716-
717-
if !ctx.Repo.CanWrite(models.UnitTypeIssues) {
718-
ctx.Status(403)
719-
return
720-
}
721-
722-
if !ctx.Repo.CanUseTimetracker(issue, ctx.User) {
723-
ctx.Status(403)
724-
return
725-
}
726-
727-
if !models.StopwatchExists(ctx.User.ID, issue.ID) {
728-
ctx.Error(409, "StopwatchExists", "cannot stop a non existent stopwatch")
729-
return
730-
}
731-
732-
if err := models.CreateOrStopIssueStopwatch(ctx.User, issue); err != nil {
733-
ctx.Error(500, "CreateOrStopIssueStopwatch", err)
734-
return
735-
}
736-
737-
ctx.Status(201)
738-
}

0 commit comments

Comments
 (0)