implement proper sever route handling
This commit is contained in:
@@ -1,22 +1,66 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"github.com/LazyBachelor/LazyPM/internal/service"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/LazyBachelor/LazyPM/internal/models"
|
||||
"github.com/LazyBachelor/LazyPM/internal/service"
|
||||
)
|
||||
|
||||
type IssuesHandler struct {
|
||||
Services *service.Services
|
||||
}
|
||||
|
||||
func NewIssuesHandler(services *service.Services) *IssuesHandler {
|
||||
return &IssuesHandler{
|
||||
Services: services,
|
||||
func IssuesRoutes(svc *service.Services) []Route {
|
||||
return []Route{
|
||||
{Pattern: "/issues", Handler: GetAllIssues(svc)},
|
||||
{Pattern: "POST /create-issue", Handler: CreateIssue(svc)},
|
||||
}
|
||||
}
|
||||
|
||||
func (h *IssuesHandler) HandleGetAllIssues() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func CreateIssue(svc *service.Services) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
})
|
||||
var issue models.Issue
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&issue)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid request payload", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
defer r.Body.Close()
|
||||
|
||||
err = svc.Beads.CreateIssue(r.Context(), &issue, "")
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to create issue: "+err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(issue)
|
||||
}
|
||||
}
|
||||
|
||||
func GetAllIssues(svc *service.Services) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
issues, err := svc.Beads.AllIssues(r.Context())
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to retrieve issues", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(issues)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to marshal issues", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(jsonData)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user