From 61e77097a8adfaf49cec69b3821599dc623dcf1c Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 11 Dec 2022 09:49:28 -0800 Subject: [PATCH] updated queue to return a router and also renamed it as it will hold all queue info later on --- routes/discordQueue.go | 29 -------------------- routes/queue.go | 60 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 29 deletions(-) delete mode 100644 routes/discordQueue.go create mode 100644 routes/queue.go diff --git a/routes/discordQueue.go b/routes/discordQueue.go deleted file mode 100644 index 2efdb44..0000000 --- a/routes/discordQueue.go +++ /dev/null @@ -1,29 +0,0 @@ -package routes - -import ( - "encoding/json" - "net/http" -) - -// GetDiscordQueue -// @Summary Returns the top 100 entries from the queue to be processed. -// @Produce application/json -// @Tags Debug, Discord, Queue -// @Router /discord/queue [get] -func (s *Server) GetDiscordQueue(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - - res, err := s.Db.ListDiscordQueueItems(*s.ctx, 100) - if err != nil { - w.Write([]byte(err.Error())) - panic(err) - } - - bres, err := json.Marshal(res) - if err != nil { - w.Write([]byte(err.Error())) - panic(err) - } - - w.Write(bres) -} diff --git a/routes/queue.go b/routes/queue.go new file mode 100644 index 0000000..27d47c4 --- /dev/null +++ b/routes/queue.go @@ -0,0 +1,60 @@ +package routes + +import ( + "encoding/json" + "net/http" + + "github.com/go-chi/chi/v5" + "github.com/jtom38/newsbot/collector/domain/models" +) + +func (s *Server) GetQueueRouter() http.Handler { + r := chi.NewRouter() + + r.Get("/discord/webhooks", s.ListDiscordWebhookQueue) + + return r +} + +type ListDiscordWebHooksQueueResults struct { + ApiStatusModel + Payload []models.DiscordQueueDto `json:"payload"` +} + +// GetDiscordQueue +// @Summary Returns the top 100 entries from the queue to be processed. +// @Produce application/json +// @Tags Queue +// @Router /queue/discord/webhooks [get] +// @Success 200 {object} ListDiscordWebHooksQueueResults "ok" +func (s *Server) ListDiscordWebhookQueue(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + + p := ListDiscordWebHooksQueueResults{ + ApiStatusModel: ApiStatusModel{ + Message: "OK", + StatusCode: http.StatusOK, + }, + } + + // Get the raw resp from sql + res, err := s.Db.ListDiscordQueueItems(*s.ctx, 100) + if err != nil { + s.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + + // convert to dto + for _, item := range res { + p.Payload = append(p.Payload, models.ConvertToDiscordQueueDto(item)) + } + + // convert to json + b, err := json.Marshal(p) + if err != nil { + s.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Write(b) +}