From d4ade0ef0f967bfc55278d3276a652e25ab6c25b Mon Sep 17 00:00:00 2001 From: Aleksander Cynarski Date: Wed, 4 Dec 2019 21:46:05 +0100 Subject: [PATCH] create DTO for Issue --- controller/issue.go | 16 +++++++--- giteaClient/createIssueProxy.go | 16 ---------- model/createIssueProxy.go | 52 +++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 20 deletions(-) delete mode 100644 giteaClient/createIssueProxy.go create mode 100644 model/createIssueProxy.go diff --git a/controller/issue.go b/controller/issue.go index 122709d..0713ec8 100644 --- a/controller/issue.go +++ b/controller/issue.go @@ -3,6 +3,7 @@ package controller import ( "code.gitea.io/sdk/gitea" "gitea-issue/giteaClient" + "gitea-issue/model" "github.com/gin-gonic/gin" "github.com/savaki/swag/endpoint" "github.com/savaki/swag/swagger" @@ -12,11 +13,18 @@ import ( ) func GetIssues(c *gin.Context) { - issues, err := giteaClient.GetIssues(); + giteaIssues, err := giteaClient.GetIssues(); + + proxyIssues := []model.GetCreateIssueProxy{} + + for _, issue := range giteaIssues { + proxyIssues = append(proxyIssues, model.TransformFromGitea(issue)) + } + if(err != nil){ c.AbortWithStatus(http.StatusNotFound) } - c.AsciiJSON(http.StatusOK, issues) + c.AsciiJSON(http.StatusOK, proxyIssues) } func GetIssuesSwagger() (*swagger.Endpoint){ @@ -24,7 +32,7 @@ func GetIssuesSwagger() (*swagger.Endpoint){ endpoint.Handler(GetIssues), endpoint.Description("Get all issues"), endpoint.Tags("issues"), - endpoint.Response(http.StatusOK, []gitea.Issue{}, "Gitea Issue list"), + endpoint.Response(http.StatusOK, []model.GetCreateIssueProxy{}, "Gitea Issue list"), ) } @@ -46,7 +54,7 @@ func GetIssueSwagger() (*swagger.Endpoint){ endpoint.Handler(GetIssue), endpoint.Description("Get one issue"), endpoint.Tags("issues"), - endpoint.Response(http.StatusOK, gitea.Issue{}, "Gitea Issue"), + endpoint.Response(http.StatusOK, model.GetCreateIssueProxy{}, "Gitea Issue"), ) } diff --git a/giteaClient/createIssueProxy.go b/giteaClient/createIssueProxy.go deleted file mode 100644 index bea0cec..0000000 --- a/giteaClient/createIssueProxy.go +++ /dev/null @@ -1,16 +0,0 @@ -package giteaClient - -type GetCreateIssueProxy struct { - Title string `json:"title"` - Body string `json:"body"` - Assignee string `json:"assignee"` - Labels []int64 `json:"labels"` - Closed bool `json:"closed"` -} - -type PostCreateIssueProxy struct { - Title string `json:"title"` - Body string `json:"body"` - Labels []int64 `json:"labels"` -} - diff --git a/model/createIssueProxy.go b/model/createIssueProxy.go new file mode 100644 index 0000000..0a961fd --- /dev/null +++ b/model/createIssueProxy.go @@ -0,0 +1,52 @@ +package model + +import ( + "code.gitea.io/sdk/gitea" + "gitea-issue/giteaClient" + "time" +) + +type GetCreateIssueProxy struct { + Title string `json:"title"` + Body string `json:"body"` + Labels []*gitea.Label `json:"labels"` + Closed *time.Time `json:"closed"` +} + +type PostCreateIssueProxy struct { + Title string `json:"title"` + Body string `json:"body"` + Labels []*gitea.Label `json:"labels"` +} + +func (c PostCreateIssueProxy) TransformToGiteaCreateIssueOption() (gitea.CreateIssueOption, error){ + giteaUser, err := giteaClient.GetUserInfo() + if( err != nil){ + return gitea.CreateIssueOption{}, err; + } + labels := []int64{} + + for _, label := range c.Labels{ + labels = append(labels, label.ID) + } + giteaObject := gitea.CreateIssueOption{ + Title: c.Title, + Body: c.Body, + Assignee: giteaUser.UserName, + Labels: labels, + Closed: false, + } + + return giteaObject, nil +} + +func TransformFromGitea(issue *gitea.Issue) (GetCreateIssueProxy) { + + giteaIssue := GetCreateIssueProxy{ + Title: issue.Title, + Body: issue.Body, + Labels: issue.Labels, + Closed: issue.Closed, + } + return giteaIssue +} \ No newline at end of file