From ac8eeaea015918780e51c5ff32f031719f2e27d9 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 21 Aug 2022 20:02:45 -0700 Subject: [PATCH] Added discord management that was missing (#27) --- database/query.sql.go | 18 +++++++ database/schema/query.sql | 5 ++ docs/docs.go | 104 +++++++++++++++++++++++++++++--------- docs/swagger.json | 104 +++++++++++++++++++++++++++++--------- docs/swagger.yaml | 45 ++++++++++++++++- routes/discordwebhooks.go | 82 ++++++++++++++++++++++++++++-- routes/server.go | 8 ++- 7 files changed, 314 insertions(+), 52 deletions(-) diff --git a/database/query.sql.go b/database/query.sql.go index 6ca1084..d950e7d 100644 --- a/database/query.sql.go +++ b/database/query.sql.go @@ -261,6 +261,15 @@ func (q *Queries) DeleteSubscription(ctx context.Context, id uuid.UUID) error { return err } +const disableDiscordWebHook = `-- name: DisableDiscordWebHook :exec +Update discordwebhooks Set Enabled = FALSE where ID = $1 +` + +func (q *Queries) DisableDiscordWebHook(ctx context.Context, id uuid.UUID) error { + _, err := q.db.ExecContext(ctx, disableDiscordWebHook, id) + return err +} + const disableSource = `-- name: DisableSource :exec Update Sources Set Enabled = FALSE where ID = $1 ` @@ -270,6 +279,15 @@ func (q *Queries) DisableSource(ctx context.Context, id uuid.UUID) error { return err } +const enableDiscordWebHook = `-- name: EnableDiscordWebHook :exec +Update discordwebhooks Set Enabled = TRUE where ID = $1 +` + +func (q *Queries) EnableDiscordWebHook(ctx context.Context, id uuid.UUID) error { + _, err := q.db.ExecContext(ctx, enableDiscordWebHook, id) + return err +} + const enableSource = `-- name: EnableSource :exec Update Sources Set Enabled = TRUE where ID = $1 ` diff --git a/database/schema/query.sql b/database/schema/query.sql index f37beb5..50c13e3 100644 --- a/database/schema/query.sql +++ b/database/schema/query.sql @@ -78,6 +78,11 @@ Select * From discordwebhooks LIMIT $1; -- name: DeleteDiscordWebHooks :exec Delete From discordwebhooks Where ID = $1; +-- name: DisableDiscordWebHook :exec +Update discordwebhooks Set Enabled = FALSE where ID = $1; + +-- name: EnableDiscordWebHook :exec +Update discordwebhooks Set Enabled = TRUE where ID = $1; /* Icons */ diff --git a/docs/docs.go b/docs/docs.go index 956818a..148aa6a 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -305,29 +305,6 @@ const docTemplate = `{ "responses": {} } }, - "/discord/webhooks/byId": { - "get": { - "produces": [ - "application/json" - ], - "tags": [ - "Config", - "Discord", - "Webhook" - ], - "summary": "Returns the top 100 entries from the queue to be processed.", - "parameters": [ - { - "type": "string", - "description": "id", - "name": "id", - "in": "query", - "required": true - } - ], - "responses": {} - } - }, "/discord/webhooks/new": { "post": { "tags": [ @@ -362,6 +339,87 @@ const docTemplate = `{ "responses": {} } }, + "/discord/webhooks/{id}": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "Config", + "Discord", + "Webhook" + ], + "summary": "Returns the top 100 entries from the queue to be processed.", + "parameters": [ + { + "type": "string", + "description": "id", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": {} + }, + "delete": { + "tags": [ + "Config", + "Discord", + "Webhook" + ], + "summary": "Deletes a record by ID.", + "parameters": [ + { + "type": "string", + "description": "id", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": {} + } + }, + "/discord/webhooks/{id}/disable": { + "post": { + "tags": [ + "Config", + "Discord", + "Webhook" + ], + "summary": "Disables a Webhook from being used.", + "parameters": [ + { + "type": "string", + "description": "id", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": {} + } + }, + "/discord/webhooks/{id}/enable": { + "post": { + "tags": [ + "Config", + "Discord", + "Webhook" + ], + "summary": "Enables a source to continue processing.", + "parameters": [ + { + "type": "string", + "description": "id", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": {} + } + }, "/hello/{who}": { "get": { "produces": [ diff --git a/docs/swagger.json b/docs/swagger.json index b1a7286..43d3593 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -296,29 +296,6 @@ "responses": {} } }, - "/discord/webhooks/byId": { - "get": { - "produces": [ - "application/json" - ], - "tags": [ - "Config", - "Discord", - "Webhook" - ], - "summary": "Returns the top 100 entries from the queue to be processed.", - "parameters": [ - { - "type": "string", - "description": "id", - "name": "id", - "in": "query", - "required": true - } - ], - "responses": {} - } - }, "/discord/webhooks/new": { "post": { "tags": [ @@ -353,6 +330,87 @@ "responses": {} } }, + "/discord/webhooks/{id}": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "Config", + "Discord", + "Webhook" + ], + "summary": "Returns the top 100 entries from the queue to be processed.", + "parameters": [ + { + "type": "string", + "description": "id", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": {} + }, + "delete": { + "tags": [ + "Config", + "Discord", + "Webhook" + ], + "summary": "Deletes a record by ID.", + "parameters": [ + { + "type": "string", + "description": "id", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": {} + } + }, + "/discord/webhooks/{id}/disable": { + "post": { + "tags": [ + "Config", + "Discord", + "Webhook" + ], + "summary": "Disables a Webhook from being used.", + "parameters": [ + { + "type": "string", + "description": "id", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": {} + } + }, + "/discord/webhooks/{id}/enable": { + "post": { + "tags": [ + "Config", + "Discord", + "Webhook" + ], + "summary": "Enables a source to continue processing.", + "parameters": [ + { + "type": "string", + "description": "id", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": {} + } + }, "/hello/{who}": { "get": { "produces": [ diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 232a1c9..40d9436 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -205,11 +205,24 @@ paths: - Config - Discord - Webhook - /discord/webhooks/byId: + /discord/webhooks/{id}: + delete: + parameters: + - description: id + in: path + name: id + required: true + type: string + responses: {} + summary: Deletes a record by ID. + tags: + - Config + - Discord + - Webhook get: parameters: - description: id - in: query + in: path name: id required: true type: string @@ -221,6 +234,34 @@ paths: - Config - Discord - Webhook + /discord/webhooks/{id}/disable: + post: + parameters: + - description: id + in: path + name: id + required: true + type: string + responses: {} + summary: Disables a Webhook from being used. + tags: + - Config + - Discord + - Webhook + /discord/webhooks/{id}/enable: + post: + parameters: + - description: id + in: path + name: id + required: true + type: string + responses: {} + summary: Enables a source to continue processing. + tags: + - Config + - Discord + - Webhook /discord/webhooks/new: post: parameters: diff --git a/routes/discordwebhooks.go b/routes/discordwebhooks.go index 5af6e0f..0168c9e 100644 --- a/routes/discordwebhooks.go +++ b/routes/discordwebhooks.go @@ -6,6 +6,7 @@ import ( "net/http" "strings" + "github.com/go-chi/chi/v5" "github.com/google/uuid" "github.com/jtom38/newsbot/collector/database" ) @@ -33,12 +34,12 @@ func (s *Server) GetDiscordWebHooks(w http.ResponseWriter, r *http.Request) { w.Write(bres) } -// GetDiscorWebHooksById +// GetDiscordWebHook // @Summary Returns the top 100 entries from the queue to be processed. // @Produce application/json -// @Param id query string true "id" +// @Param id path string true "id" // @Tags Config, Discord, Webhook -// @Router /discord/webhooks/byId [get] +// @Router /discord/webhooks/{id} [get] func (s *Server) GetDiscordWebHooksById(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") @@ -113,3 +114,78 @@ func (s *Server) NewDiscordWebHook(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.Write(bJson) } + +// DisableDiscordWebHooks +// @Summary Disables a Webhook from being used. +// @Param id path string true "id" +// @Tags Config, Discord, Webhook +// @Router /discord/webhooks/{id}/disable [post] +func (s *Server) disableDiscordWebHook(w http.ResponseWriter, r *http.Request) { + id := chi.URLParam(r, "ID") + uuid, err := uuid.Parse(id) + if err != nil { + log.Panicln(err) + } + + // Check to make sure we can find the record + _, err = s.Db.GetDiscordWebHooksByID(*s.ctx, uuid) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + } + + err = s.Db.DisableDiscordWebHook(*s.ctx, uuid) + if err != nil { + log.Panic(err) + } +} + +// EnableDiscordWebHook +// @Summary Enables a source to continue processing. +// @Param id path string true "id" +// @Tags Config, Discord, Webhook +// @Router /discord/webhooks/{id}/enable [post] +func (s *Server) enableDiscordWebHook(w http.ResponseWriter, r *http.Request) { + id := chi.URLParam(r, "ID") + uuid, err := uuid.Parse(id) + if err != nil { + log.Panicln(err) + } + + // Check to make sure we can find the record + _, err = s.Db.GetDiscordWebHooksByID(*s.ctx, uuid) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + } + + err = s.Db.EnableDiscordWebHook(*s.ctx, uuid) + if err != nil { + log.Panic(err) + } +} + +// DeleteDiscordWebHook +// @Summary Deletes a record by ID. +// @Param id path string true "id" +// @Tags Config, Discord, Webhook +// @Router /discord/webhooks/{id} [delete] +func (s *Server) deleteDiscordWebHook(w http.ResponseWriter, r *http.Request) { + //var item model.Sources = model.Sources{} + + id := chi.URLParam(r, "ID") + uuid, err := uuid.Parse(id) + if err != nil { + log.Panicln(err) + } + + // Check to make sure we can find the record + _, err = s.Db.GetDiscordQueueByID(*s.ctx, uuid) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + } + + // Delete the record + err = s.Db.DeleteDiscordWebHooks(*s.ctx, uuid) + if err != nil { + log.Panic(err) + } +} diff --git a/routes/server.go b/routes/server.go index 5e9198f..b9b3455 100644 --- a/routes/server.go +++ b/routes/server.go @@ -86,7 +86,13 @@ func (s *Server) MountRoutes() { /* Discord WebHooks */ s.Router.Post("/api/discord/webhooks/new", s.NewDiscordWebHook) s.Router.Get("/api/discord/webhooks", s.GetDiscordWebHooks) - s.Router.Get("/api/discord/webhooks/byId", s.GetDiscordWebHooksById) + //s.Router.Get("/api/discord/webhooks/byId", s.GetDiscordWebHooksById) + s.Router.Route("/api/discord/webhooks/{ID}", func(r chi.Router) { + r.Get("/", s.GetDiscordWebHooksById) + r.Delete("/", s.deleteDiscordWebHook) + r.Post("/disable", s.disableDiscordWebHook) + r.Post("/enable", s.enableDiscordWebHook) + }) /* Settings */ s.Router.Get("/api/settings", s.getSettings)