- Config: try ENV_FILE, .env, ../.env for loading; trim trailing slash from BaseURL - Log BASE_URL at server startup for verification - .env.example: document BASE_URL - Tasks, projects, tags, migrations and related API/handlers Made-with: Cursor
74 lines
1.7 KiB
Go
74 lines
1.7 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/calendarapi/internal/middleware"
|
|
"github.com/calendarapi/internal/models"
|
|
"github.com/calendarapi/internal/service"
|
|
"github.com/calendarapi/internal/utils"
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
type TaskWebhookHandler struct {
|
|
webhookSvc *service.TaskWebhookService
|
|
}
|
|
|
|
func NewTaskWebhookHandler(webhookSvc *service.TaskWebhookService) *TaskWebhookHandler {
|
|
return &TaskWebhookHandler{webhookSvc: webhookSvc}
|
|
}
|
|
|
|
func (h *TaskWebhookHandler) List(w http.ResponseWriter, r *http.Request) {
|
|
userID, _ := middleware.GetUserID(r.Context())
|
|
|
|
webhooks, err := h.webhookSvc.List(r.Context(), userID)
|
|
if err != nil {
|
|
utils.WriteError(w, err)
|
|
return
|
|
}
|
|
|
|
utils.WriteList(w, webhooks, models.PageInfo{Limit: len(webhooks)})
|
|
}
|
|
|
|
func (h *TaskWebhookHandler) Create(w http.ResponseWriter, r *http.Request) {
|
|
userID, _ := middleware.GetUserID(r.Context())
|
|
|
|
var req struct {
|
|
URL string `json:"url"`
|
|
Events []string `json:"events"`
|
|
Secret *string `json:"secret"`
|
|
}
|
|
if err := utils.DecodeJSON(r, &req); err != nil {
|
|
utils.WriteError(w, err)
|
|
return
|
|
}
|
|
|
|
webhook, err := h.webhookSvc.Create(r.Context(), userID, service.TaskWebhookCreateRequest{
|
|
URL: req.URL,
|
|
Events: req.Events,
|
|
Secret: req.Secret,
|
|
})
|
|
if err != nil {
|
|
utils.WriteError(w, err)
|
|
return
|
|
}
|
|
|
|
utils.WriteJSON(w, http.StatusOK, map[string]interface{}{"webhook": webhook})
|
|
}
|
|
|
|
func (h *TaskWebhookHandler) Delete(w http.ResponseWriter, r *http.Request) {
|
|
userID, _ := middleware.GetUserID(r.Context())
|
|
webhookID, err := utils.ValidateUUID(chi.URLParam(r, "id"))
|
|
if err != nil {
|
|
utils.WriteError(w, err)
|
|
return
|
|
}
|
|
|
|
if err := h.webhookSvc.Delete(r.Context(), userID, webhookID); err != nil {
|
|
utils.WriteError(w, err)
|
|
return
|
|
}
|
|
|
|
utils.WriteOK(w)
|
|
}
|