Use api.TrackedTime in API (#2807)

* Use api.TrackedTime in API

* APIFormat() method

* Rename function
release/v1.15
Ethan Koenig 2017-10-31 19:25:14 -07:00 committed by Lunny Xiao
parent e22549747c
commit e6a97b6675
2 changed files with 47 additions and 16 deletions

View File

@ -7,6 +7,8 @@ package models
import ( import (
"time" "time"
api "code.gitea.io/sdk/gitea"
"github.com/go-xorm/builder" "github.com/go-xorm/builder"
) )
@ -25,6 +27,17 @@ func (t *TrackedTime) AfterLoad() {
t.Created = time.Unix(t.CreatedUnix, 0).Local() t.Created = time.Unix(t.CreatedUnix, 0).Local()
} }
// APIFormat converts TrackedTime to API format
func (t *TrackedTime) APIFormat() *api.TrackedTime {
return &api.TrackedTime{
ID: t.ID,
IssueID: t.IssueID,
UserID: t.UserID,
Time: t.Time,
Created: t.Created,
}
}
// FindTrackedTimesOptions represent the filters for tracked times. If an ID is 0 it will be ignored. // FindTrackedTimesOptions represent the filters for tracked times. If an ID is 0 it will be ignored.
type FindTrackedTimesOptions struct { type FindTrackedTimesOptions struct {
IssueID int64 IssueID int64

View File

@ -10,6 +10,14 @@ import (
api "code.gitea.io/sdk/gitea" api "code.gitea.io/sdk/gitea"
) )
func trackedTimesToAPIFormat(trackedTimes []*models.TrackedTime) []*api.TrackedTime {
apiTrackedTimes := make([]*api.TrackedTime, len(trackedTimes))
for i, trackedTime := range trackedTimes {
apiTrackedTimes[i] = trackedTime.APIFormat()
}
return apiTrackedTimes
}
// ListTrackedTimes list all the tracked times of an issue // ListTrackedTimes list all the tracked times of an issue
func ListTrackedTimes(ctx *context.APIContext) { func ListTrackedTimes(ctx *context.APIContext) {
// swagger:route GET /repos/{username}/{reponame}/issues/{issue}/times repository issueTrackedTimes // swagger:route GET /repos/{username}/{reponame}/issues/{issue}/times repository issueTrackedTimes
@ -35,11 +43,13 @@ func ListTrackedTimes(ctx *context.APIContext) {
return return
} }
if trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{IssueID: issue.ID}); err != nil { trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{IssueID: issue.ID})
if err != nil {
ctx.Error(500, "GetTrackedTimesByIssue", err) ctx.Error(500, "GetTrackedTimesByIssue", err)
} else { return
ctx.JSON(200, &trackedTimes)
} }
apiTrackedTimes := trackedTimesToAPIFormat(trackedTimes)
ctx.JSON(200, &apiTrackedTimes)
} }
// AddTime adds time manual to the given issue // AddTime adds time manual to the given issue
@ -73,13 +83,12 @@ func AddTime(ctx *context.APIContext, form api.AddTimeOption) {
ctx.Status(403) ctx.Status(403)
return return
} }
var tt *models.TrackedTime trackedTime, err := models.AddTime(ctx.User, issue, form.Time)
if tt, err = models.AddTime(ctx.User, issue, form.Time); err != nil { if err != nil {
ctx.Error(500, "AddTime", err) ctx.Error(500, "AddTime", err)
return return
} }
ctx.JSON(200, tt) ctx.JSON(200, trackedTime.APIFormat())
} }
// ListTrackedTimesByUser lists all tracked times of the user // ListTrackedTimesByUser lists all tracked times of the user
@ -111,11 +120,15 @@ func ListTrackedTimesByUser(ctx *context.APIContext) {
ctx.Status(404) ctx.Status(404)
return return
} }
if trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{UserID: user.ID, RepositoryID: ctx.Repo.Repository.ID}); err != nil { trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{
UserID: user.ID,
RepositoryID: ctx.Repo.Repository.ID})
if err != nil {
ctx.Error(500, "GetTrackedTimesByUser", err) ctx.Error(500, "GetTrackedTimesByUser", err)
} else { return
ctx.JSON(200, &trackedTimes)
} }
apiTrackedTimes := trackedTimesToAPIFormat(trackedTimes)
ctx.JSON(200, &apiTrackedTimes)
} }
// ListTrackedTimesByRepository lists all tracked times of the user // ListTrackedTimesByRepository lists all tracked times of the user
@ -133,11 +146,14 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) {
ctx.JSON(400, struct{ Message string }{Message: "time tracking disabled"}) ctx.JSON(400, struct{ Message string }{Message: "time tracking disabled"})
return return
} }
if trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{RepositoryID: ctx.Repo.Repository.ID}); err != nil { trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{
RepositoryID: ctx.Repo.Repository.ID})
if err != nil {
ctx.Error(500, "GetTrackedTimesByUser", err) ctx.Error(500, "GetTrackedTimesByUser", err)
} else { return
ctx.JSON(200, &trackedTimes)
} }
apiTrackedTimes := trackedTimesToAPIFormat(trackedTimes)
ctx.JSON(200, &apiTrackedTimes)
} }
// ListMyTrackedTimes lists all tracked times of the current user // ListMyTrackedTimes lists all tracked times of the current user
@ -150,9 +166,11 @@ func ListMyTrackedTimes(ctx *context.APIContext) {
// Responses: // Responses:
// 200: TrackedTimes // 200: TrackedTimes
// 500: error // 500: error
if trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{UserID: ctx.User.ID}); err != nil { trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{UserID: ctx.User.ID})
if err != nil {
ctx.Error(500, "GetTrackedTimesByUser", err) ctx.Error(500, "GetTrackedTimesByUser", err)
} else { return
ctx.JSON(200, &trackedTimes)
} }
apiTrackedTimes := trackedTimesToAPIFormat(trackedTimes)
ctx.JSON(200, &apiTrackedTimes)
} }