Skip to content

Commit

Permalink
webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
mstgnz committed Jul 5, 2024
1 parent f19dbcd commit 27fc2a0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
22 changes: 10 additions & 12 deletions handler/web/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import (
)

type WebhookHandler struct {
webhook *services.WebhookService
request *services.RequestService
schedule *services.ScheduleService
webhook *services.WebhookService
}

func (h *WebhookHandler) HomeHandler(w http.ResponseWriter, r *http.Request) error {
Expand All @@ -42,13 +40,13 @@ func (h *WebhookHandler) CreateHandler(w http.ResponseWriter, r *http.Request) e

func (h *WebhookHandler) EditHandler(w http.ResponseWriter, r *http.Request) error {

cUser, _ := r.Context().Value(config.CKey("user")).(*models.User)
id := chi.URLParam(r, "id")
query := r.URL.Query()
query.Set("id", id)
r.URL.RawQuery = query.Encode()

_, response := h.webhook.ListService(w, r)

data, _ := response.Data["webhooks"].([]*models.Webhook)
var updatedAt = ""
if data[0].UpdatedAt != nil {
Expand All @@ -65,20 +63,20 @@ func (h *WebhookHandler) EditHandler(w http.ResponseWriter, r *http.Request) err
}

// requests
_, requests := h.request.ListService(w, r)
requestData, _ := requests.Data["requests"].([]models.Request)
request := &models.Request{}
requests, _ := request.Get(cUser.ID, 0, "")
requestList := `<select class="form-select" name="request_id">`
for _, request := range requestData {
requestList += fmt.Sprintf(`<option value="%d" %s>GET</option>`, request.ID, request.Url)
for _, request := range requests {
requestList += fmt.Sprintf(`<option value="%d">%s</option>`, request.ID, request.Url)
}
requestList += "</select>"

// schedules
_, schedules := h.schedule.ListService(w, r)
schedulesData, _ := schedules.Data["schedules"].([]models.Schedule)
schedule := &models.Schedule{}
schedules, _ := schedule.Get(0, cUser.ID, 0, 0, 0, "")
scheduleList := `<select class="form-select" name="schedule_id">`
for _, schedule := range schedulesData {
scheduleList += fmt.Sprintf(`<option value="%d" %s>GET</option>`, schedule.ID, schedule.Timing)
for _, schedule := range schedules {
scheduleList += fmt.Sprintf(`<option value="%d">%s</option>`, schedule.ID, schedule.Timing)
}
scheduleList += "</select>"

Expand Down
4 changes: 2 additions & 2 deletions models/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ func (m *Request) Get(userID, id int, url string) ([]Request, error) {
query := strings.TrimSuffix(config.App().QUERY["REQUESTS"], ";")

if id > 0 {
query += fmt.Sprintf(" AND id=%v", id)
query += fmt.Sprintf(" AND id=%d", id)
}
if url != "" {
query += fmt.Sprintf(" AND url=%v", url)
query += fmt.Sprintf(" AND url='%s'", url)
}

// prepare
Expand Down
9 changes: 5 additions & 4 deletions models/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models

import (
"fmt"
"log"
"strings"
"time"

Expand Down Expand Up @@ -58,17 +59,17 @@ func (m *Webhook) Get(id, schedule_id, request_id, user_id int) ([]*Webhook, err
query := strings.TrimSuffix(config.App().QUERY["WEBHOOKS"], ";")

if id > 0 {
query += fmt.Sprintf(" AND id=%v", id)
query += fmt.Sprintf(" AND w.id=%d", id)
}

if schedule_id > 0 {
query += fmt.Sprintf(" AND schedule_id=%v", schedule_id)
query += fmt.Sprintf(" AND w.schedule_id=%d", schedule_id)
}

if request_id > 0 {
query += fmt.Sprintf(" AND request_id=%v", request_id)
query += fmt.Sprintf(" AND w.request_id=%d", request_id)
}

log.Println(query)
// prepare
stmt, err := config.App().DB.Prepare(query)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions services/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func (h *WebhookService) ListService(w http.ResponseWriter, r *http.Request) (in
schedule_id, _ := strconv.Atoi(r.URL.Query().Get("schedule_id"))
request_id, _ := strconv.Atoi(r.URL.Query().Get("request_id"))

if schedule_id == 0 {
return http.StatusBadRequest, config.Response{Status: false, Message: "schedule_id param required"}
if id == 0 && schedule_id == 0 && request_id == 0 {
return http.StatusBadRequest, config.Response{Status: false, Message: "id, schedule_id or request_id param required"}
}

webhooks, err := webhook.Get(id, schedule_id, request_id, cUser.ID)
Expand Down

0 comments on commit 27fc2a0

Please sign in to comment.