summaryrefslogtreecommitdiffstats
path: root/models/issue_stopwatch.go
diff options
context:
space:
mode:
author6543 <6543@obermui.de>2020-09-06 03:20:27 +0200
committerGitHub <noreply@github.com>2020-09-05 21:20:27 -0400
commit1c35353c53977bbb50e5089e20bcb0591305286f (patch)
treebf0519cbb2efb69263c42beceb16cbaaa66f0af4 /models/issue_stopwatch.go
parentf10d3ea803548ebefee3158c708a7b3b0129176a (diff)
downloadgitea-1c35353c53977bbb50e5089e20bcb0591305286f.tar.gz
gitea-1c35353c53977bbb50e5089e20bcb0591305286f.zip
implemet Cache for Stopwatches.APIFormat() (#12730)
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'models/issue_stopwatch.go')
-rw-r--r--models/issue_stopwatch.go35
1 files changed, 31 insertions, 4 deletions
diff --git a/models/issue_stopwatch.go b/models/issue_stopwatch.go
index f80ce4139c..789f8ebdfc 100644
--- a/models/issue_stopwatch.go
+++ b/models/issue_stopwatch.go
@@ -216,12 +216,39 @@ func (sw *Stopwatch) APIFormat() (api.StopWatch, error) {
// APIFormat convert Stopwatches type to api.StopWatches type
func (sws Stopwatches) APIFormat() (api.StopWatches, error) {
result := api.StopWatches(make([]api.StopWatch, 0, len(sws)))
+
+ issueCache := make(map[int64]*Issue)
+ repoCache := make(map[int64]*Repository)
+ var (
+ issue *Issue
+ repo *Repository
+ ok bool
+ err error
+ )
+
for _, sw := range sws {
- apiSW, err := sw.APIFormat()
- if err != nil {
- return nil, err
+ issue, ok = issueCache[sw.IssueID]
+ if !ok {
+ issue, err = GetIssueByID(sw.IssueID)
+ if err != nil {
+ return nil, err
+ }
}
- result = append(result, apiSW)
+ repo, ok = repoCache[issue.RepoID]
+ if !ok {
+ repo, err = GetRepositoryByID(issue.RepoID)
+ if err != nil {
+ return nil, err
+ }
+ }
+
+ result = append(result, api.StopWatch{
+ Created: sw.CreatedUnix.AsTime(),
+ IssueIndex: issue.Index,
+ IssueTitle: issue.Title,
+ RepoOwnerName: repo.OwnerName,
+ RepoName: repo.Name,
+ })
}
return result, nil
}