mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 00:20:25 +08:00 
			
		
		
		
	Refactor: move Commit To APIFormat Code & Lot of StopWatch related things (#12729)
* move GitCommit to APIFormat convertion into convert package * rename Commit convert functions * move stopwatch to api convertion into convert package & rm unused code & extend test * fix compare time * Gitea not Gogs ;)
This commit is contained in:
		@@ -8,7 +8,6 @@ import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	api "code.gitea.io/gitea/modules/structs"
 | 
			
		||||
	"code.gitea.io/gitea/modules/timeutil"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -20,9 +19,6 @@ type Stopwatch struct {
 | 
			
		||||
	CreatedUnix timeutil.TimeStamp `xorm:"created"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Stopwatches is a List ful of Stopwatch
 | 
			
		||||
type Stopwatches []Stopwatch
 | 
			
		||||
 | 
			
		||||
func getStopwatch(e Engine, userID, issueID int64) (sw *Stopwatch, exists bool, err error) {
 | 
			
		||||
	sw = new(Stopwatch)
 | 
			
		||||
	exists, err = e.
 | 
			
		||||
@@ -33,14 +29,14 @@ func getStopwatch(e Engine, userID, issueID int64) (sw *Stopwatch, exists bool,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetUserStopwatches return list of all stopwatches of a user
 | 
			
		||||
func GetUserStopwatches(userID int64, listOptions ListOptions) (*Stopwatches, error) {
 | 
			
		||||
	sws := new(Stopwatches)
 | 
			
		||||
func GetUserStopwatches(userID int64, listOptions ListOptions) ([]*Stopwatch, error) {
 | 
			
		||||
	sws := make([]*Stopwatch, 0, 8)
 | 
			
		||||
	sess := x.Where("stopwatch.user_id = ?", userID)
 | 
			
		||||
	if listOptions.Page != 0 {
 | 
			
		||||
		sess = listOptions.setSessionPagination(sess)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err := sess.Find(sws)
 | 
			
		||||
	err := sess.Find(&sws)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -194,61 +190,3 @@ func SecToTime(duration int64) string {
 | 
			
		||||
 | 
			
		||||
	return hrs
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// APIFormat convert Stopwatch type to api.StopWatch type
 | 
			
		||||
func (sw *Stopwatch) APIFormat() (api.StopWatch, error) {
 | 
			
		||||
	issue, err := getIssueByID(x, sw.IssueID)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return api.StopWatch{}, err
 | 
			
		||||
	}
 | 
			
		||||
	if err := issue.LoadRepo(); err != nil {
 | 
			
		||||
		return api.StopWatch{}, err
 | 
			
		||||
	}
 | 
			
		||||
	return api.StopWatch{
 | 
			
		||||
		Created:       sw.CreatedUnix.AsTime(),
 | 
			
		||||
		IssueIndex:    issue.Index,
 | 
			
		||||
		IssueTitle:    issue.Title,
 | 
			
		||||
		RepoOwnerName: issue.Repo.OwnerName,
 | 
			
		||||
		RepoName:      issue.Repo.Name,
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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 {
 | 
			
		||||
		issue, ok = issueCache[sw.IssueID]
 | 
			
		||||
		if !ok {
 | 
			
		||||
			issue, err = GetIssueByID(sw.IssueID)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return nil, err
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user