Added discord management that was missing (#27)

This commit is contained in:
James Tombleson 2022-08-21 20:02:45 -07:00 committed by GitHub
parent b082c904ea
commit ac8eeaea01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 314 additions and 52 deletions

View File

@ -261,6 +261,15 @@ func (q *Queries) DeleteSubscription(ctx context.Context, id uuid.UUID) error {
return err 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 const disableSource = `-- name: DisableSource :exec
Update Sources Set Enabled = FALSE where ID = $1 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 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 const enableSource = `-- name: EnableSource :exec
Update Sources Set Enabled = TRUE where ID = $1 Update Sources Set Enabled = TRUE where ID = $1
` `

View File

@ -78,6 +78,11 @@ Select * From discordwebhooks LIMIT $1;
-- name: DeleteDiscordWebHooks :exec -- name: DeleteDiscordWebHooks :exec
Delete From discordwebhooks Where ID = $1; 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 */ /* Icons */

View File

@ -305,29 +305,6 @@ const docTemplate = `{
"responses": {} "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": { "/discord/webhooks/new": {
"post": { "post": {
"tags": [ "tags": [
@ -362,6 +339,87 @@ const docTemplate = `{
"responses": {} "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}": { "/hello/{who}": {
"get": { "get": {
"produces": [ "produces": [

View File

@ -296,29 +296,6 @@
"responses": {} "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": { "/discord/webhooks/new": {
"post": { "post": {
"tags": [ "tags": [
@ -353,6 +330,87 @@
"responses": {} "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}": { "/hello/{who}": {
"get": { "get": {
"produces": [ "produces": [

View File

@ -205,11 +205,24 @@ paths:
- Config - Config
- Discord - Discord
- Webhook - 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: get:
parameters: parameters:
- description: id - description: id
in: query in: path
name: id name: id
required: true required: true
type: string type: string
@ -221,6 +234,34 @@ paths:
- Config - Config
- Discord - Discord
- Webhook - 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: /discord/webhooks/new:
post: post:
parameters: parameters:

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"strings" "strings"
"github.com/go-chi/chi/v5"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/jtom38/newsbot/collector/database" "github.com/jtom38/newsbot/collector/database"
) )
@ -33,12 +34,12 @@ func (s *Server) GetDiscordWebHooks(w http.ResponseWriter, r *http.Request) {
w.Write(bres) w.Write(bres)
} }
// GetDiscorWebHooksById // GetDiscordWebHook
// @Summary Returns the top 100 entries from the queue to be processed. // @Summary Returns the top 100 entries from the queue to be processed.
// @Produce application/json // @Produce application/json
// @Param id query string true "id" // @Param id path string true "id"
// @Tags Config, Discord, Webhook // @Tags Config, Discord, Webhook
// @Router /discord/webhooks/byId [get] // @Router /discord/webhooks/{id} [get]
func (s *Server) GetDiscordWebHooksById(w http.ResponseWriter, r *http.Request) { func (s *Server) GetDiscordWebHooksById(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") 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.Header().Set("Content-Type", "application/json")
w.Write(bJson) 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)
}
}

View File

@ -86,7 +86,13 @@ func (s *Server) MountRoutes() {
/* Discord WebHooks */ /* Discord WebHooks */
s.Router.Post("/api/discord/webhooks/new", s.NewDiscordWebHook) s.Router.Post("/api/discord/webhooks/new", s.NewDiscordWebHook)
s.Router.Get("/api/discord/webhooks", s.GetDiscordWebHooks) 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 */ /* Settings */
s.Router.Get("/api/settings", s.getSettings) s.Router.Get("/api/settings", s.getSettings)