mirror of
https://github.com/chaitin/SafeLine.git
synced 2026-02-06 16:53:23 +08:00
feat: add github api
This commit is contained in:
73
backend/internal/handler/github.go
Normal file
73
backend/internal/handler/github.go
Normal file
@@ -0,0 +1,73 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user