Skip to content

Commit 4d06d10

Browse files
lunnytechknowlogick
authored andcommitted
Move tracked time api convert to convert package (#9665)
1 parent 705b1e4 commit 4d06d10

File tree

3 files changed

+44
-34
lines changed

3 files changed

+44
-34
lines changed

models/issue_tracked_time.go

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

1010
"code.gitea.io/gitea/modules/setting"
11-
api "code.gitea.io/gitea/modules/structs"
1211

1312
"xorm.io/builder"
1413
"xorm.io/xorm"
@@ -60,25 +59,6 @@ func (t *TrackedTime) loadAttributes(e Engine) (err error) {
6059
return
6160
}
6261

63-
// APIFormat converts TrackedTime to API format
64-
func (t *TrackedTime) APIFormat() (apiT *api.TrackedTime) {
65-
apiT = &api.TrackedTime{
66-
ID: t.ID,
67-
IssueID: t.IssueID,
68-
UserID: t.UserID,
69-
UserName: t.User.Name,
70-
Time: t.Time,
71-
Created: t.Created,
72-
}
73-
if t.Issue != nil {
74-
apiT.Issue = t.Issue.APIFormat()
75-
}
76-
if t.User != nil {
77-
apiT.UserName = t.User.Name
78-
}
79-
return
80-
}
81-
8262
// LoadAttributes load Issue, User
8363
func (tl TrackedTimeList) LoadAttributes() (err error) {
8464
for _, t := range tl {
@@ -89,15 +69,6 @@ func (tl TrackedTimeList) LoadAttributes() (err error) {
8969
return
9070
}
9171

92-
// APIFormat converts TrackedTimeList to API format
93-
func (tl TrackedTimeList) APIFormat() api.TrackedTimeList {
94-
result := make([]*api.TrackedTime, 0, len(tl))
95-
for _, t := range tl {
96-
result = append(result, t.APIFormat())
97-
}
98-
return result
99-
}
100-
10172
// FindTrackedTimesOptions represent the filters for tracked times. If an ID is 0 it will be ignored.
10273
type FindTrackedTimesOptions struct {
10374
IssueID int64

modules/convert/issue.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2020 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 convert
6+
7+
import (
8+
"code.gitea.io/gitea/models"
9+
api "code.gitea.io/gitea/modules/structs"
10+
)
11+
12+
// ToTrackedTime converts TrackedTime to API format
13+
func ToTrackedTime(t *models.TrackedTime) (apiT *api.TrackedTime) {
14+
apiT = &api.TrackedTime{
15+
ID: t.ID,
16+
IssueID: t.IssueID,
17+
UserID: t.UserID,
18+
UserName: t.User.Name,
19+
Time: t.Time,
20+
Created: t.Created,
21+
}
22+
if t.Issue != nil {
23+
apiT.Issue = t.Issue.APIFormat()
24+
}
25+
if t.User != nil {
26+
apiT.UserName = t.User.Name
27+
}
28+
return
29+
}
30+
31+
// ToTrackedTimeList converts TrackedTimeList to API format
32+
func ToTrackedTimeList(tl models.TrackedTimeList) api.TrackedTimeList {
33+
result := make([]*api.TrackedTime, 0, len(tl))
34+
for _, t := range tl {
35+
result = append(result, ToTrackedTime(t))
36+
}
37+
return result
38+
}

routers/api/v1/repo/issue_tracked_time.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"code.gitea.io/gitea/models"
1414
"code.gitea.io/gitea/modules/context"
15+
"code.gitea.io/gitea/modules/convert"
1516
api "code.gitea.io/gitea/modules/structs"
1617
"code.gitea.io/gitea/routers/api/v1/utils"
1718
)
@@ -93,7 +94,7 @@ func ListTrackedTimes(ctx *context.APIContext) {
9394
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
9495
return
9596
}
96-
ctx.JSON(http.StatusOK, trackedTimes.APIFormat())
97+
ctx.JSON(http.StatusOK, convert.ToTrackedTimeList(trackedTimes))
9798
}
9899

99100
// AddTime add time manual to the given issue
@@ -178,7 +179,7 @@ func AddTime(ctx *context.APIContext, form api.AddTimeOption) {
178179
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
179180
return
180181
}
181-
ctx.JSON(http.StatusOK, trackedTime.APIFormat())
182+
ctx.JSON(http.StatusOK, convert.ToTrackedTime(trackedTime))
182183
}
183184

184185
// ResetIssueTime reset time manual to the given issue
@@ -399,7 +400,7 @@ func ListTrackedTimesByUser(ctx *context.APIContext) {
399400
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
400401
return
401402
}
402-
ctx.JSON(http.StatusOK, trackedTimes.APIFormat())
403+
ctx.JSON(http.StatusOK, convert.ToTrackedTimeList(trackedTimes))
403404
}
404405

405406
// ListTrackedTimesByRepository lists all tracked times of the repository
@@ -486,7 +487,7 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) {
486487
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
487488
return
488489
}
489-
ctx.JSON(http.StatusOK, trackedTimes.APIFormat())
490+
ctx.JSON(http.StatusOK, convert.ToTrackedTimeList(trackedTimes))
490491
}
491492

492493
// ListMyTrackedTimes lists all tracked times of the current user
@@ -530,5 +531,5 @@ func ListMyTrackedTimes(ctx *context.APIContext) {
530531
return
531532
}
532533

533-
ctx.JSON(http.StatusOK, trackedTimes.APIFormat())
534+
ctx.JSON(http.StatusOK, convert.ToTrackedTimeList(trackedTimes))
534535
}

0 commit comments

Comments
 (0)