Browse Source

Use api.TrackedTime in API (#2807)

* Use api.TrackedTime in API

* APIFormat() method

* Rename function
Ethan Koenig 1 year ago
parent
commit
e6a97b6675
2 changed files with 47 additions and 16 deletions
  1. 13 0
      models/issue_tracked_time.go
  2. 34 16
      routers/api/v1/repo/issue_tracked_time.go

+ 13 - 0
models/issue_tracked_time.go

@@ -7,6 +7,8 @@ package models
7 7
 import (
8 8
 	"time"
9 9
 
10
+	api "code.gitea.io/sdk/gitea"
11
+
10 12
 	"github.com/go-xorm/builder"
11 13
 )
12 14
 
@@ -25,6 +27,17 @@ func (t *TrackedTime) AfterLoad() {
25 27
 	t.Created = time.Unix(t.CreatedUnix, 0).Local()
26 28
 }
27 29
 
30
+// APIFormat converts TrackedTime to API format
31
+func (t *TrackedTime) APIFormat() *api.TrackedTime {
32
+	return &api.TrackedTime{
33
+		ID:      t.ID,
34
+		IssueID: t.IssueID,
35
+		UserID:  t.UserID,
36
+		Time:    t.Time,
37
+		Created: t.Created,
38
+	}
39
+}
40
+
28 41
 // FindTrackedTimesOptions represent the filters for tracked times. If an ID is 0 it will be ignored.
29 42
 type FindTrackedTimesOptions struct {
30 43
 	IssueID      int64

+ 34 - 16
routers/api/v1/repo/issue_tracked_time.go

@@ -10,6 +10,14 @@ import (
10 10
 	api "code.gitea.io/sdk/gitea"
11 11
 )
12 12
 
13
+func trackedTimesToAPIFormat(trackedTimes []*models.TrackedTime) []*api.TrackedTime {
14
+	apiTrackedTimes := make([]*api.TrackedTime, len(trackedTimes))
15
+	for i, trackedTime := range trackedTimes {
16
+		apiTrackedTimes[i] = trackedTime.APIFormat()
17
+	}
18
+	return apiTrackedTimes
19
+}
20
+
13 21
 // ListTrackedTimes list all the tracked times of an issue
14 22
 func ListTrackedTimes(ctx *context.APIContext) {
15 23
 	// swagger:route GET /repos/{username}/{reponame}/issues/{issue}/times repository issueTrackedTimes
@@ -35,11 +43,13 @@ func ListTrackedTimes(ctx *context.APIContext) {
35 43
 		return
36 44
 	}
37 45
 
38
-	if trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{IssueID: issue.ID}); err != nil {
46
+	trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{IssueID: issue.ID})
47
+	if err != nil {
39 48
 		ctx.Error(500, "GetTrackedTimesByIssue", err)
40
-	} else {
41
-		ctx.JSON(200, &trackedTimes)
49
+		return
42 50
 	}
51
+	apiTrackedTimes := trackedTimesToAPIFormat(trackedTimes)
52
+	ctx.JSON(200, &apiTrackedTimes)
43 53
 }
44 54
 
45 55
 // AddTime adds time manual to the given issue
@@ -73,13 +83,12 @@ func AddTime(ctx *context.APIContext, form api.AddTimeOption) {
73 83
 		ctx.Status(403)
74 84
 		return
75 85
 	}
76
-	var tt *models.TrackedTime
77
-	if tt, err = models.AddTime(ctx.User, issue, form.Time); err != nil {
86
+	trackedTime, err := models.AddTime(ctx.User, issue, form.Time)
87
+	if err != nil {
78 88
 		ctx.Error(500, "AddTime", err)
79 89
 		return
80 90
 	}
81
-	ctx.JSON(200, tt)
82
-
91
+	ctx.JSON(200, trackedTime.APIFormat())
83 92
 }
84 93
 
85 94
 // ListTrackedTimesByUser  lists all tracked times of the user
@@ -111,11 +120,15 @@ func ListTrackedTimesByUser(ctx *context.APIContext) {
111 120
 		ctx.Status(404)
112 121
 		return
113 122
 	}
114
-	if trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{UserID: user.ID, RepositoryID: ctx.Repo.Repository.ID}); err != nil {
123
+	trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{
124
+		UserID:       user.ID,
125
+		RepositoryID: ctx.Repo.Repository.ID})
126
+	if err != nil {
115 127
 		ctx.Error(500, "GetTrackedTimesByUser", err)
116
-	} else {
117
-		ctx.JSON(200, &trackedTimes)
128
+		return
118 129
 	}
130
+	apiTrackedTimes := trackedTimesToAPIFormat(trackedTimes)
131
+	ctx.JSON(200, &apiTrackedTimes)
119 132
 }
120 133
 
121 134
 // ListTrackedTimesByRepository lists all tracked times of the user
@@ -133,11 +146,14 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) {
133 146
 		ctx.JSON(400, struct{ Message string }{Message: "time tracking disabled"})
134 147
 		return
135 148
 	}
136
-	if trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{RepositoryID: ctx.Repo.Repository.ID}); err != nil {
149
+	trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{
150
+		RepositoryID: ctx.Repo.Repository.ID})
151
+	if err != nil {
137 152
 		ctx.Error(500, "GetTrackedTimesByUser", err)
138
-	} else {
139
-		ctx.JSON(200, &trackedTimes)
153
+		return
140 154
 	}
155
+	apiTrackedTimes := trackedTimesToAPIFormat(trackedTimes)
156
+	ctx.JSON(200, &apiTrackedTimes)
141 157
 }
142 158
 
143 159
 // ListMyTrackedTimes lists all tracked times of the current user
@@ -150,9 +166,11 @@ func ListMyTrackedTimes(ctx *context.APIContext) {
150 166
 	//     Responses:
151 167
 	//       200: TrackedTimes
152 168
 	//       500: error
153
-	if trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{UserID: ctx.User.ID}); err != nil {
169
+	trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{UserID: ctx.User.ID})
170
+	if err != nil {
154 171
 		ctx.Error(500, "GetTrackedTimesByUser", err)
155
-	} else {
156
-		ctx.JSON(200, &trackedTimes)
172
+		return
157 173
 	}
174
+	apiTrackedTimes := trackedTimesToAPIFormat(trackedTimes)
175
+	ctx.JSON(200, &apiTrackedTimes)
158 176
 }