121 lines
3.2 KiB
Go
121 lines
3.2 KiB
Go
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"
|
|
"net/http"
|
|
"strconv"
|
|
"fmt"
|
|
)
|
|
|
|
func PostIssue(c *gin.Context) {
|
|
var issueProxy model.PostCreateIssueProxy
|
|
if err := c.BindJSON(&issueProxy); err != nil {
|
|
c.AsciiJSON(http.StatusBadRequest, gin.H{
|
|
"message": "CREATE_ISSUE_ERROR",
|
|
"error": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
giteaIssue, err := issueProxy.TransformToGiteaCreateIssueOption()
|
|
if err != nil {
|
|
c.AsciiJSON(http.StatusBadRequest, gin.H{
|
|
"message": "CREATE_ISSUE_ERROR",
|
|
"error": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
response, err := giteaClient.CreateIssue(giteaIssue)
|
|
if err != nil {
|
|
c.AsciiJSON(http.StatusBadRequest, gin.H{
|
|
"message": "CREATE_ISSUE_ERROR",
|
|
"error": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
c.AsciiJSON(http.StatusOK, response)
|
|
}
|
|
|
|
func PostIssueSwagger() (*swagger.Endpoint){
|
|
return endpoint.New("post", "/issues", "Create issue",
|
|
endpoint.Handler(PostIssue),
|
|
endpoint.Description("Post new issue"),
|
|
endpoint.Body(model.PostCreateIssueProxy{}, "Issue object", true),
|
|
endpoint.Tags("issues"),
|
|
endpoint.Response(http.StatusOK, gitea.Issue{}, "Gitea Issue list"),
|
|
)
|
|
}
|
|
|
|
func GetIssues(c *gin.Context) {
|
|
giteaIssues, err := giteaClient.GetIssues();
|
|
proxyIssues := []model.GetCreateIssueProxy{}
|
|
|
|
for _, issue := range giteaIssues {
|
|
proxyIssues = append(proxyIssues, model.IssueTransformFromGitea(issue))
|
|
}
|
|
|
|
if(err != nil){
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
}
|
|
c.AsciiJSON(http.StatusOK, proxyIssues)
|
|
}
|
|
|
|
func GetIssuesSwagger() (*swagger.Endpoint){
|
|
return endpoint.New("get", "/issues", "List project issues",
|
|
endpoint.Handler(GetIssues),
|
|
endpoint.Description("Get all issues"),
|
|
endpoint.Tags("issues"),
|
|
endpoint.Response(http.StatusOK, []model.GetCreateIssueProxy{}, "Gitea Issue list"),
|
|
)
|
|
}
|
|
|
|
func GetIssue(c *gin.Context) {
|
|
issueId, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
fmt.Println(fmt.Sprintf("ParseInt err: %+v", err))
|
|
c.AsciiJSON(http.StatusNotFound, err.Error())
|
|
}
|
|
issue, err := giteaClient.GetIssue(issueId)
|
|
if err != nil {
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
}
|
|
c.AsciiJSON(http.StatusOK, issue)
|
|
}
|
|
|
|
func GetIssueSwagger() (*swagger.Endpoint){
|
|
return endpoint.New("get", "/issue/:id", "Get one issue",
|
|
endpoint.Handler(GetIssue),
|
|
endpoint.Description("Get one issue"),
|
|
endpoint.Tags("issues"),
|
|
endpoint.Response(http.StatusOK, model.GetCreateIssueProxy{}, "Gitea Issue"),
|
|
)
|
|
}
|
|
|
|
func GetIssueComments(c *gin.Context) {
|
|
issueId, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
fmt.Println(fmt.Sprintf("ParseInt err: %+v", err))
|
|
c.AsciiJSON(http.StatusNotFound, err.Error())
|
|
}
|
|
issueComments, err := giteaClient.GetIssueComments(issueId)
|
|
if err != nil {
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
}
|
|
c.AsciiJSON(http.StatusOK, issueComments)
|
|
}
|
|
|
|
func GetIssueCommentsSwagger() (*swagger.Endpoint){
|
|
return endpoint.New("get", "/issue/:id/comments", "Get issue comments",
|
|
endpoint.Handler(GetIssue),
|
|
endpoint.Description("Get issue comments"),
|
|
endpoint.Tags("issues"),
|
|
endpoint.Response(http.StatusOK, []gitea.Comment{}, "Gitea issue comments"),
|
|
)
|
|
}
|