From 4d06d10dbafe7cfd404889b636d8e243058ee96f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 11 Jan 2020 10:59:41 +0800 Subject: [PATCH] Move tracked time api convert to convert package (#9665) --- models/issue_tracked_time.go | 29 ----------------- modules/convert/issue.go | 38 +++++++++++++++++++++++ routers/api/v1/repo/issue_tracked_time.go | 11 ++++--- 3 files changed, 44 insertions(+), 34 deletions(-) create mode 100644 modules/convert/issue.go diff --git a/models/issue_tracked_time.go b/models/issue_tracked_time.go index b84adbc59..55e8baacd 100644 --- a/models/issue_tracked_time.go +++ b/models/issue_tracked_time.go @@ -8,7 +8,6 @@ import ( "time" "code.gitea.io/gitea/modules/setting" - api "code.gitea.io/gitea/modules/structs" "xorm.io/builder" "xorm.io/xorm" @@ -60,25 +59,6 @@ func (t *TrackedTime) loadAttributes(e Engine) (err error) { return } -// APIFormat converts TrackedTime to API format -func (t *TrackedTime) APIFormat() (apiT *api.TrackedTime) { - apiT = &api.TrackedTime{ - ID: t.ID, - IssueID: t.IssueID, - UserID: t.UserID, - UserName: t.User.Name, - Time: t.Time, - Created: t.Created, - } - if t.Issue != nil { - apiT.Issue = t.Issue.APIFormat() - } - if t.User != nil { - apiT.UserName = t.User.Name - } - return -} - // LoadAttributes load Issue, User func (tl TrackedTimeList) LoadAttributes() (err error) { for _, t := range tl { @@ -89,15 +69,6 @@ func (tl TrackedTimeList) LoadAttributes() (err error) { return } -// APIFormat converts TrackedTimeList to API format -func (tl TrackedTimeList) APIFormat() api.TrackedTimeList { - result := make([]*api.TrackedTime, 0, len(tl)) - for _, t := range tl { - result = append(result, t.APIFormat()) - } - return result -} - // FindTrackedTimesOptions represent the filters for tracked times. If an ID is 0 it will be ignored. type FindTrackedTimesOptions struct { IssueID int64 diff --git a/modules/convert/issue.go b/modules/convert/issue.go new file mode 100644 index 000000000..d88a742db --- /dev/null +++ b/modules/convert/issue.go @@ -0,0 +1,38 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package convert + +import ( + "code.gitea.io/gitea/models" + api "code.gitea.io/gitea/modules/structs" +) + +// ToTrackedTime converts TrackedTime to API format +func ToTrackedTime(t *models.TrackedTime) (apiT *api.TrackedTime) { + apiT = &api.TrackedTime{ + ID: t.ID, + IssueID: t.IssueID, + UserID: t.UserID, + UserName: t.User.Name, + Time: t.Time, + Created: t.Created, + } + if t.Issue != nil { + apiT.Issue = t.Issue.APIFormat() + } + if t.User != nil { + apiT.UserName = t.User.Name + } + return +} + +// ToTrackedTimeList converts TrackedTimeList to API format +func ToTrackedTimeList(tl models.TrackedTimeList) api.TrackedTimeList { + result := make([]*api.TrackedTime, 0, len(tl)) + for _, t := range tl { + result = append(result, ToTrackedTime(t)) + } + return result +} diff --git a/routers/api/v1/repo/issue_tracked_time.go b/routers/api/v1/repo/issue_tracked_time.go index dd959192c..323bf67d8 100644 --- a/routers/api/v1/repo/issue_tracked_time.go +++ b/routers/api/v1/repo/issue_tracked_time.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/routers/api/v1/utils" ) @@ -93,7 +94,7 @@ func ListTrackedTimes(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "LoadAttributes", err) return } - ctx.JSON(http.StatusOK, trackedTimes.APIFormat()) + ctx.JSON(http.StatusOK, convert.ToTrackedTimeList(trackedTimes)) } // AddTime add time manual to the given issue @@ -178,7 +179,7 @@ func AddTime(ctx *context.APIContext, form api.AddTimeOption) { ctx.Error(http.StatusInternalServerError, "LoadAttributes", err) return } - ctx.JSON(http.StatusOK, trackedTime.APIFormat()) + ctx.JSON(http.StatusOK, convert.ToTrackedTime(trackedTime)) } // ResetIssueTime reset time manual to the given issue @@ -399,7 +400,7 @@ func ListTrackedTimesByUser(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "LoadAttributes", err) return } - ctx.JSON(http.StatusOK, trackedTimes.APIFormat()) + ctx.JSON(http.StatusOK, convert.ToTrackedTimeList(trackedTimes)) } // ListTrackedTimesByRepository lists all tracked times of the repository @@ -486,7 +487,7 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "LoadAttributes", err) return } - ctx.JSON(http.StatusOK, trackedTimes.APIFormat()) + ctx.JSON(http.StatusOK, convert.ToTrackedTimeList(trackedTimes)) } // ListMyTrackedTimes lists all tracked times of the current user @@ -530,5 +531,5 @@ func ListMyTrackedTimes(ctx *context.APIContext) { return } - ctx.JSON(http.StatusOK, trackedTimes.APIFormat()) + ctx.JSON(http.StatusOK, convert.ToTrackedTimeList(trackedTimes)) }