updated discordwebhooks to use dto

This commit is contained in:
James Tombleson 2023-01-21 17:38:17 -08:00
parent 663cbc4e37
commit 176459f17d
6 changed files with 428 additions and 205 deletions

View File

@ -158,7 +158,14 @@ const docTemplate = `{
"required": true
}
],
"responses": {}
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/routes.ListDiscordWebhooks"
}
}
}
}
},
"/discord/webhooks/new": {
@ -194,6 +201,63 @@ const docTemplate = `{
"responses": {}
}
},
"/discord/webhooks/{ID}": {
"delete": {
"tags": [
"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": [
"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": [
"Discord",
"Webhook"
],
"summary": "Enables a source to continue processing.",
"parameters": [
{
"type": "string",
"description": "id",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {}
}
},
"/discord/webhooks/{id}": {
"get": {
"produces": [
@ -213,24 +277,14 @@ const docTemplate = `{
"required": true
}
],
"responses": {}
},
"delete": {
"tags": [
"Discord",
"Webhook"
],
"summary": "Deletes a record by ID.",
"parameters": [
{
"type": "string",
"description": "id",
"name": "id",
"in": "path",
"required": true
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/routes.GetDiscordWebhook"
}
}
],
"responses": {}
}
},
"patch": {
"tags": [
@ -250,44 +304,6 @@ const docTemplate = `{
"responses": {}
}
},
"/discord/webhooks/{id}/disable": {
"post": {
"tags": [
"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": [
"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": [
@ -1042,6 +1058,20 @@ const docTemplate = `{
}
}
},
"routes.GetDiscordWebhook": {
"type": "object",
"properties": {
"message": {
"type": "string"
},
"payload": {
"$ref": "#/definitions/models.DiscordWebHooksDto"
},
"status": {
"type": "integer"
}
}
},
"routes.GetSourceResult": {
"type": "object",
"properties": {
@ -1073,6 +1103,23 @@ const docTemplate = `{
}
}
},
"routes.ListDiscordWebhooks": {
"type": "object",
"properties": {
"message": {
"type": "string"
},
"payload": {
"type": "array",
"items": {
"$ref": "#/definitions/models.DiscordWebHooksDto"
}
},
"status": {
"type": "integer"
}
}
},
"routes.ListSourcesResults": {
"type": "object",
"properties": {

View File

@ -149,7 +149,14 @@
"required": true
}
],
"responses": {}
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/routes.ListDiscordWebhooks"
}
}
}
}
},
"/discord/webhooks/new": {
@ -185,6 +192,63 @@
"responses": {}
}
},
"/discord/webhooks/{ID}": {
"delete": {
"tags": [
"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": [
"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": [
"Discord",
"Webhook"
],
"summary": "Enables a source to continue processing.",
"parameters": [
{
"type": "string",
"description": "id",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {}
}
},
"/discord/webhooks/{id}": {
"get": {
"produces": [
@ -204,24 +268,14 @@
"required": true
}
],
"responses": {}
},
"delete": {
"tags": [
"Discord",
"Webhook"
],
"summary": "Deletes a record by ID.",
"parameters": [
{
"type": "string",
"description": "id",
"name": "id",
"in": "path",
"required": true
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/routes.GetDiscordWebhook"
}
}
],
"responses": {}
}
},
"patch": {
"tags": [
@ -241,44 +295,6 @@
"responses": {}
}
},
"/discord/webhooks/{id}/disable": {
"post": {
"tags": [
"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": [
"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": [
@ -1033,6 +1049,20 @@
}
}
},
"routes.GetDiscordWebhook": {
"type": "object",
"properties": {
"message": {
"type": "string"
},
"payload": {
"$ref": "#/definitions/models.DiscordWebHooksDto"
},
"status": {
"type": "integer"
}
}
},
"routes.GetSourceResult": {
"type": "object",
"properties": {
@ -1064,6 +1094,23 @@
}
}
},
"routes.ListDiscordWebhooks": {
"type": "object",
"properties": {
"message": {
"type": "string"
},
"payload": {
"type": "array",
"items": {
"$ref": "#/definitions/models.DiscordWebHooksDto"
}
},
"status": {
"type": "integer"
}
}
},
"routes.ListSourcesResults": {
"type": "object",
"properties": {

View File

@ -161,6 +161,15 @@ definitions:
status:
type: integer
type: object
routes.GetDiscordWebhook:
properties:
message:
type: string
payload:
$ref: '#/definitions/models.DiscordWebHooksDto'
status:
type: integer
type: object
routes.GetSourceResult:
properties:
message:
@ -181,6 +190,17 @@ definitions:
status:
type: integer
type: object
routes.ListDiscordWebhooks:
properties:
message:
type: string
payload:
items:
$ref: '#/definitions/models.DiscordWebHooksDto'
type: array
status:
type: integer
type: object
routes.ListSourcesResults:
properties:
message:
@ -295,7 +315,7 @@ paths:
tags:
- Discord
- Webhook
/discord/webhooks/{id}:
/discord/webhooks/{ID}:
delete:
parameters:
- description: id
@ -308,6 +328,33 @@ paths:
tags:
- 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:
- 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:
- Discord
- Webhook
/discord/webhooks/{id}:
get:
parameters:
- description: id
@ -317,7 +364,11 @@ paths:
type: string
produces:
- application/json
responses: {}
responses:
"200":
description: OK
schema:
$ref: '#/definitions/routes.GetDiscordWebhook'
summary: Returns the top 100 entries from the queue to be processed.
tags:
- Discord
@ -334,32 +385,6 @@ paths:
tags:
- 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:
- 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:
- Discord
- Webhook
/discord/webhooks/by/serverAndChannel:
get:
parameters:
@ -375,7 +400,11 @@ paths:
type: string
produces:
- application/json
responses: {}
responses:
"200":
description: OK
schema:
$ref: '#/definitions/routes.ListDiscordWebhooks'
summary: Returns all the known web hooks based on the Server and Channel given.
tags:
- Discord

63
dto/discordwebhooks.go Normal file
View File

@ -0,0 +1,63 @@
package dto
import (
"context"
"github.com/google/uuid"
"github.com/jtom38/newsbot/collector/database"
"github.com/jtom38/newsbot/collector/domain/models"
)
func (c DtoClient) ListDiscordWebHooks(ctx context.Context, total int32) ([]models.DiscordWebHooksDto, error) {
var res []models.DiscordWebHooksDto
items, err := c.db.ListDiscordWebhooks(ctx, total)
if err != nil {
return res, nil
}
for _, item := range items {
res = append(res, c.ConvertDiscordWebhook(item))
}
return res, nil
}
func (c DtoClient) GetDiscordWebhook(ctx context.Context, id uuid.UUID) (models.DiscordWebHooksDto, error) {
var res models.DiscordWebHooksDto
item, err := c.db.GetDiscordWebHooksByID(ctx, id)
if err != nil {
return res, err
}
return c.ConvertDiscordWebhook(item), nil
}
func (c DtoClient) GetDiscordWebHookByServerAndChannel(ctx context.Context, server, channel string) ([]models.DiscordWebHooksDto, error) {
var res []models.DiscordWebHooksDto
items, err := c.db.GetDiscordWebHooksByServerAndChannel(ctx, database.GetDiscordWebHooksByServerAndChannelParams{
Server: server,
Channel: channel,
})
if err != nil {
return res, err
}
for _, item := range items {
res = append(res, c.ConvertDiscordWebhook(item))
}
return res, nil
}
func (c DtoClient) ConvertDiscordWebhook(i database.Discordwebhook) models.DiscordWebHooksDto {
return models.DiscordWebHooksDto{
ID: i.ID,
Url: i.Url,
Server: i.Server,
Channel: i.Channel,
Enabled: i.Enabled,
}
}

View File

@ -1,7 +1,6 @@
package routes
import (
"context"
"encoding/json"
"log"
"net/http"
@ -10,63 +9,106 @@ import (
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
"github.com/jtom38/newsbot/collector/database"
"github.com/jtom38/newsbot/collector/domain/models"
)
// GetDiscordWebHooks
func (s Server) DiscordWebHookRouter() http.Handler {
r := chi.NewRouter()
r.Get("/", s.ListDiscordWebHooks)
r.Post("/new", s.NewDiscordWebHook)
r.Get("/by/serverAndChannel", s.GetDiscordWebHooksByServerAndChannel)
r.Route("/{ID}", func(r chi.Router) {
r.Get("/", s.GetDiscordWebHooksById)
r.Delete("/", s.deleteDiscordWebHook)
r.Post("/disable", s.disableDiscordWebHook)
r.Post("/enable", s.enableDiscordWebHook)
})
return r
}
type ListDiscordWebhooks struct {
ApiStatusModel
Payload []models.DiscordWebHooksDto `json:"payload"`
}
// ListDiscordWebhooks
// @Summary Returns the top 100 entries from the queue to be processed.
// @Produce application/json
// @Tags Discord, Webhook
// @Router /discord/webhooks [get]
func (s *Server) GetDiscordWebHooks(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
res, err := s.Db.ListDiscordWebhooks(*s.ctx, 100)
if err != nil {
w.Write([]byte(err.Error()))
panic(err)
func (s *Server) ListDiscordWebHooks(w http.ResponseWriter, r *http.Request) {
p := ListDiscordWebhooks{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
},
}
bres, err := json.Marshal(res)
w.Header().Set(HeaderContentType, ApplicationJson)
res, err := s.dto.ListDiscordWebHooks(r.Context(), 50)
if err != nil {
w.Write([]byte(err.Error()))
panic(err)
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
p.Payload = res
bres, err := json.Marshal(p)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bres)
}
type GetDiscordWebhook struct {
ApiStatusModel
Payload models.DiscordWebHooksDto `json:"payload"`
}
// GetDiscordWebHook
// @Summary Returns the top 100 entries from the queue to be processed.
// @Produce application/json
// @Param id path string true "id"
// @Tags Discord, Webhook
// @Router /discord/webhooks/{id} [get]
// @Success 200 {object} GetDiscordWebhook "OK"
func (s *Server) GetDiscordWebHooksById(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
p := GetDiscordWebhook{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
},
}
w.Header().Set(HeaderContentType, ApplicationJson)
_id := chi.URLParam(r, "ID")
if _id == "" {
http.Error(w, "id is missing", http.StatusBadRequest)
s.WriteError(w, "id is missing", http.StatusBadRequest)
return
}
uuid, err := uuid.Parse(_id)
if err != nil {
http.Error(w, "unable to parse id value", http.StatusBadRequest)
s.WriteError(w, "unable to parse id value", http.StatusBadRequest)
return
}
res, err := s.Db.GetDiscordWebHooksByID(*s.ctx, uuid)
res, err := s.dto.GetDiscordWebhook(r.Context(), uuid)
if err != nil {
http.Error(w, "no record found", http.StatusBadRequest)
s.WriteError(w, "no record found", http.StatusBadRequest)
return
}
p.Payload = res
bres, err := json.Marshal(res)
bres, err := json.Marshal(p)
if err != nil {
http.Error(w, "unable to convert to json", http.StatusBadRequest)
panic(err)
s.WriteError(w, "unable to convert to json", http.StatusBadRequest)
return
}
w.Write(bres)
@ -79,41 +121,47 @@ func (s *Server) GetDiscordWebHooksById(w http.ResponseWriter, r *http.Request)
// @Param channel query string true "memes"
// @Tags Discord, Webhook
// @Router /discord/webhooks/by/serverAndChannel [get]
// @Success 200 {object} ListDiscordWebhooks "OK"
func (s *Server) GetDiscordWebHooksByServerAndChannel(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
p := ListDiscordWebhooks{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
},
}
w.Header().Set(HeaderContentType, ApplicationJson)
query := r.URL.Query()
_server := query["server"][0]
if _server == "" {
http.Error(w, "ID is missing", http.StatusInternalServerError)
s.WriteError(w, "ID is missing", http.StatusInternalServerError)
return
}
_channel := query["channel"][0]
if _channel == "" {
http.Error(w, "Channel is missing", http.StatusInternalServerError)
s.WriteError(w, "Channel is missing", http.StatusInternalServerError)
return
}
res, err := s.Db.GetDiscordWebHooksByServerAndChannel(context.Background(), database.GetDiscordWebHooksByServerAndChannelParams{
Server: _server,
Channel: _channel,
})
res, err := s.dto.GetDiscordWebHookByServerAndChannel(r.Context(), _server, _channel)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
bres, err := json.Marshal(res)
p.Payload = res
bres, err := json.Marshal(p)
if err != nil {
http.Error(w, "unable to convert to json", http.StatusInternalServerError)
panic(err)
s.WriteError(w, "unable to convert to json", http.StatusInternalServerError)
return
}
w.Write(bres)
}
// NewDiscordWebHook
// @Summary Creates a new record for a discord web hook to post data to.
// @Param url query string true "url"
@ -162,23 +210,25 @@ func (s *Server) NewDiscordWebHook(w http.ResponseWriter, r *http.Request) {
// @Summary Disables a Webhook from being used.
// @Param id path string true "id"
// @Tags Discord, Webhook
// @Router /discord/webhooks/{id}/disable [post]
// @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)
s.WriteError(w, err.Error(), http.StatusBadRequest)
return
}
// 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)
s.WriteError(w, err.Error(), http.StatusBadRequest)
return
}
err = s.Db.DisableDiscordWebHook(*s.ctx, uuid)
if err != nil {
log.Panic(err)
s.WriteError(w, err.Error(), http.StatusInternalServerError)
}
}
@ -186,23 +236,23 @@ func (s *Server) disableDiscordWebHook(w http.ResponseWriter, r *http.Request) {
// @Summary Enables a source to continue processing.
// @Param id path string true "id"
// @Tags Discord, Webhook
// @Router /discord/webhooks/{id}/enable [post]
// @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)
s.WriteError(w, err.Error(), http.StatusBadRequest)
}
// 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)
s.WriteError(w, err.Error(), http.StatusBadRequest)
}
err = s.Db.EnableDiscordWebHook(*s.ctx, uuid)
if err != nil {
log.Panic(err)
s.WriteError(w, err.Error(), http.StatusInternalServerError)
}
}
@ -210,26 +260,26 @@ func (s *Server) enableDiscordWebHook(w http.ResponseWriter, r *http.Request) {
// @Summary Deletes a record by ID.
// @Param id path string true "id"
// @Tags Discord, Webhook
// @Router /discord/webhooks/{id} [delete]
// @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)
s.WriteError(w, err.Error(), http.StatusBadRequest)
}
// 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)
s.WriteError(w, err.Error(), http.StatusBadRequest)
}
// Delete the record
err = s.Db.DeleteDiscordWebHooks(*s.ctx, uuid)
if err != nil {
log.Panic(err)
s.WriteError(w, err.Error(), http.StatusInternalServerError)
}
}

View File

@ -80,21 +80,8 @@ func (s *Server) MountRoutes() {
s.Router.Mount("/api/articles", s.GetArticleRouter())
s.Router.Mount("/api/queue", s.GetQueueRouter())
s.Router.Mount("/api/discord/webhooks", s.DiscordWebHookRouter())
/* Discord WebHooks */
s.Router.Get("/api/discord/webhooks", s.GetDiscordWebHooks)
s.Router.Post("/api/discord/webhooks/new", s.NewDiscordWebHook)
//s.Router.Get("/api/discord/webhooks/byId", s.GetDiscordWebHooksById)
s.Router.Get("/api/discord/webhooks/by/serverAndChannel", s.GetDiscordWebHooksByServerAndChannel)
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)
s.Router.Mount("/api/sources", s.GetSourcesRouter())