73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/chaitin/SafeLine/internal/service"
|
|
)
|
|
|
|
// GitHubHandler provides HTTP handlers for GitHub operations.
|
|
type GitHubHandler struct {
|
|
gitHubService *service.GitHubService
|
|
pageLimit int
|
|
}
|
|
|
|
// NewGitHubHandler creates a new instance of GitHubHandler.
|
|
func NewGitHubHandler(gitHubService *service.GitHubService, pageLimit int) *GitHubHandler {
|
|
return &GitHubHandler{
|
|
gitHubService: gitHubService,
|
|
pageLimit: pageLimit,
|
|
}
|
|
}
|
|
|
|
// GetIssues handles GET requests for fetching GitHub issues.
|
|
// @Summary get issues
|
|
// @Description get issues from GitHub
|
|
// @Tags GitHub
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param q query string false "search by"
|
|
// @Success 200 {array} service.Issue
|
|
// @Router /repos/issues [get]
|
|
func (h *GitHubHandler) GetIssues(c *gin.Context) {
|
|
filter := c.Query("q")
|
|
|
|
issues, err := h.gitHubService.GetIssues(c, filter)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
if len(issues) > h.pageLimit {
|
|
c.JSON(http.StatusOK, issues[:h.pageLimit])
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, issues)
|
|
}
|
|
|
|
// GetDiscussions handles GET requests for fetching GitHub discussions.
|
|
// @Summary get discussions
|
|
// @Description get discussions from GitHub
|
|
// @Tags GitHub
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param q query string false "search by"
|
|
// @Success 200 {array} service.Discussion
|
|
// @Router /repos/discussions [get]
|
|
func (h *GitHubHandler) GetDiscussions(c *gin.Context) {
|
|
filter := c.Query("q")
|
|
|
|
discussions, err := h.gitHubService.GetDiscussions(c, filter)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if len(discussions) > h.pageLimit {
|
|
c.JSON(http.StatusOK, discussions[:h.pageLimit])
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, discussions)
|
|
}
|