added subscription details route

This commit is contained in:
James Tombleson 2023-01-21 12:20:33 -08:00
parent 8d99ec9155
commit 4ab7854ba2
12 changed files with 303 additions and 58 deletions

View File

@ -1,6 +1,6 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.13.0
// sqlc v1.16.0
package database

View File

@ -1,6 +1,6 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.13.0
// sqlc v1.16.0
package database

View File

@ -1,6 +1,6 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.13.0
// sqlc v1.16.0
// source: query.sql
package database

View File

@ -97,7 +97,6 @@ const docTemplate = `{
"application/json"
],
"tags": [
"Config",
"Discord",
"Webhook"
],
@ -111,7 +110,6 @@ const docTemplate = `{
"application/json"
],
"tags": [
"Config",
"Discord",
"Webhook"
],
@ -138,7 +136,6 @@ const docTemplate = `{
"/discord/webhooks/new": {
"post": {
"tags": [
"Config",
"Discord",
"Webhook"
],
@ -175,7 +172,6 @@ const docTemplate = `{
"application/json"
],
"tags": [
"Config",
"Discord",
"Webhook"
],
@ -193,7 +189,6 @@ const docTemplate = `{
},
"delete": {
"tags": [
"Config",
"Discord",
"Webhook"
],
@ -211,7 +206,6 @@ const docTemplate = `{
},
"patch": {
"tags": [
"Config",
"Discord",
"Webhook"
],
@ -231,7 +225,6 @@ const docTemplate = `{
"/discord/webhooks/{id}/disable": {
"post": {
"tags": [
"Config",
"Discord",
"Webhook"
],
@ -251,7 +244,6 @@ const docTemplate = `{
"/discord/webhooks/{id}/enable": {
"post": {
"tags": [
"Config",
"Discord",
"Webhook"
],
@ -711,6 +703,25 @@ const docTemplate = `{
}
}
},
"/subscriptions/details": {
"get": {
"produces": [
"application/json"
],
"tags": [
"Subscription"
],
"summary": "Returns the top 50 entries with full deatils on the source and output.",
"responses": {
"200": {
"description": "ok",
"schema": {
"$ref": "#/definitions/routes.ListSubscriptionDetails"
}
}
}
}
},
"/subscriptions/discord/webhook/delete": {
"delete": {
"tags": [
@ -720,8 +731,8 @@ const docTemplate = `{
"parameters": [
{
"type": "string",
"description": "Id",
"name": "Id",
"description": "id",
"name": "id",
"in": "query",
"required": true
}
@ -729,7 +740,7 @@ const docTemplate = `{
"responses": {}
}
},
"/subscriptions/new/discord/webhook": {
"/subscriptions/discord/webhook/new": {
"post": {
"tags": [
"Subscription"
@ -767,6 +778,26 @@ const docTemplate = `{
}
}
},
"models.DiscordWebHooksDto": {
"type": "object",
"properties": {
"ID": {
"type": "string"
},
"channel": {
"type": "string"
},
"enabled": {
"type": "boolean"
},
"server": {
"type": "string"
},
"url": {
"type": "string"
}
}
},
"models.SourceDto": {
"type": "object",
"properties": {
@ -805,6 +836,20 @@ const docTemplate = `{
}
}
},
"models.SubscriptionDetails": {
"type": "object",
"properties": {
"discordwebhook": {
"$ref": "#/definitions/models.DiscordWebHooksDto"
},
"id": {
"type": "string"
},
"source": {
"$ref": "#/definitions/models.SourceDto"
}
}
},
"models.SubscriptionDto": {
"type": "object",
"properties": {
@ -878,6 +923,23 @@ const docTemplate = `{
}
}
},
"routes.ListSubscriptionDetails": {
"type": "object",
"properties": {
"message": {
"type": "string"
},
"payload": {
"type": "array",
"items": {
"$ref": "#/definitions/models.SubscriptionDetails"
}
},
"status": {
"type": "integer"
}
}
},
"routes.ListSubscriptionResults": {
"type": "object",
"properties": {

View File

@ -88,7 +88,6 @@
"application/json"
],
"tags": [
"Config",
"Discord",
"Webhook"
],
@ -102,7 +101,6 @@
"application/json"
],
"tags": [
"Config",
"Discord",
"Webhook"
],
@ -129,7 +127,6 @@
"/discord/webhooks/new": {
"post": {
"tags": [
"Config",
"Discord",
"Webhook"
],
@ -166,7 +163,6 @@
"application/json"
],
"tags": [
"Config",
"Discord",
"Webhook"
],
@ -184,7 +180,6 @@
},
"delete": {
"tags": [
"Config",
"Discord",
"Webhook"
],
@ -202,7 +197,6 @@
},
"patch": {
"tags": [
"Config",
"Discord",
"Webhook"
],
@ -222,7 +216,6 @@
"/discord/webhooks/{id}/disable": {
"post": {
"tags": [
"Config",
"Discord",
"Webhook"
],
@ -242,7 +235,6 @@
"/discord/webhooks/{id}/enable": {
"post": {
"tags": [
"Config",
"Discord",
"Webhook"
],
@ -702,6 +694,25 @@
}
}
},
"/subscriptions/details": {
"get": {
"produces": [
"application/json"
],
"tags": [
"Subscription"
],
"summary": "Returns the top 50 entries with full deatils on the source and output.",
"responses": {
"200": {
"description": "ok",
"schema": {
"$ref": "#/definitions/routes.ListSubscriptionDetails"
}
}
}
}
},
"/subscriptions/discord/webhook/delete": {
"delete": {
"tags": [
@ -711,8 +722,8 @@
"parameters": [
{
"type": "string",
"description": "Id",
"name": "Id",
"description": "id",
"name": "id",
"in": "query",
"required": true
}
@ -720,7 +731,7 @@
"responses": {}
}
},
"/subscriptions/new/discord/webhook": {
"/subscriptions/discord/webhook/new": {
"post": {
"tags": [
"Subscription"
@ -758,6 +769,26 @@
}
}
},
"models.DiscordWebHooksDto": {
"type": "object",
"properties": {
"ID": {
"type": "string"
},
"channel": {
"type": "string"
},
"enabled": {
"type": "boolean"
},
"server": {
"type": "string"
},
"url": {
"type": "string"
}
}
},
"models.SourceDto": {
"type": "object",
"properties": {
@ -796,6 +827,20 @@
}
}
},
"models.SubscriptionDetails": {
"type": "object",
"properties": {
"discordwebhook": {
"$ref": "#/definitions/models.DiscordWebHooksDto"
},
"id": {
"type": "string"
},
"source": {
"$ref": "#/definitions/models.SourceDto"
}
}
},
"models.SubscriptionDto": {
"type": "object",
"properties": {
@ -869,6 +914,23 @@
}
}
},
"routes.ListSubscriptionDetails": {
"type": "object",
"properties": {
"message": {
"type": "string"
},
"payload": {
"type": "array",
"items": {
"$ref": "#/definitions/models.SubscriptionDetails"
}
},
"status": {
"type": "integer"
}
}
},
"routes.ListSubscriptionResults": {
"type": "object",
"properties": {

View File

@ -7,6 +7,19 @@ definitions:
id:
type: string
type: object
models.DiscordWebHooksDto:
properties:
ID:
type: string
channel:
type: string
enabled:
type: boolean
server:
type: string
url:
type: string
type: object
models.SourceDto:
properties:
deleted:
@ -32,6 +45,15 @@ definitions:
value:
type: string
type: object
models.SubscriptionDetails:
properties:
discordwebhook:
$ref: '#/definitions/models.DiscordWebHooksDto'
id:
type: string
source:
$ref: '#/definitions/models.SourceDto'
type: object
models.SubscriptionDto:
properties:
discordwebhookid:
@ -79,6 +101,17 @@ definitions:
status:
type: integer
type: object
routes.ListSubscriptionDetails:
properties:
message:
type: string
payload:
items:
$ref: '#/definitions/models.SubscriptionDetails'
type: array
status:
type: integer
type: object
routes.ListSubscriptionResults:
properties:
message:
@ -154,7 +187,6 @@ paths:
responses: {}
summary: Returns the top 100 entries from the queue to be processed.
tags:
- Config
- Discord
- Webhook
/discord/webhooks/{id}:
@ -168,7 +200,6 @@ paths:
responses: {}
summary: Deletes a record by ID.
tags:
- Config
- Discord
- Webhook
get:
@ -183,7 +214,6 @@ paths:
responses: {}
summary: Returns the top 100 entries from the queue to be processed.
tags:
- Config
- Discord
- Webhook
patch:
@ -196,7 +226,6 @@ paths:
responses: {}
summary: Updates a valid discord webhook ID based on the body given.
tags:
- Config
- Discord
- Webhook
/discord/webhooks/{id}/disable:
@ -210,7 +239,6 @@ paths:
responses: {}
summary: Disables a Webhook from being used.
tags:
- Config
- Discord
- Webhook
/discord/webhooks/{id}/enable:
@ -224,7 +252,6 @@ paths:
responses: {}
summary: Enables a source to continue processing.
tags:
- Config
- Discord
- Webhook
/discord/webhooks/by/serverAndChannel:
@ -245,7 +272,6 @@ paths:
responses: {}
summary: Returns all the known web hooks based on the Server and Channel given.
tags:
- Config
- Discord
- Webhook
/discord/webhooks/new:
@ -269,7 +295,6 @@ paths:
responses: {}
summary: Creates a new record for a discord web hook to post data to.
tags:
- Config
- Discord
- Webhook
/hello/{who}:
@ -564,19 +589,31 @@ paths:
summary: Returns the top 100 entries from the queue to be processed.
tags:
- Subscription
/subscriptions/details:
get:
produces:
- application/json
responses:
"200":
description: ok
schema:
$ref: '#/definitions/routes.ListSubscriptionDetails'
summary: Returns the top 50 entries with full deatils on the source and output.
tags:
- Subscription
/subscriptions/discord/webhook/delete:
delete:
parameters:
- description: Id
- description: id
in: query
name: Id
name: id
required: true
type: string
responses: {}
summary: Removes a Discord WebHook Subscription based on the Subscription ID.
tags:
- Subscription
/subscriptions/new/discord/webhook:
/subscriptions/discord/webhook/new:
post:
parameters:
- description: discordWebHookId

View File

@ -8,6 +8,24 @@ import (
"github.com/jtom38/newsbot/collector/database"
)
type DiscordWebHooksDto struct {
ID uuid.UUID `json:"ID"`
Url string `json:"url"`
Server string `json:"server"`
Channel string `json:"channel"`
Enabled bool `json:"enabled"`
}
func ConvertToDiscordWebhookDto(i database.Discordwebhook) DiscordWebHooksDto {
return DiscordWebHooksDto{
ID: i.ID,
Url: i.Url,
Server: i.Server,
Channel: i.Channel,
Enabled: i.Enabled,
}
}
type SourceDto struct {
ID uuid.UUID `json:"id"`
Site string `json:"site"`
@ -68,6 +86,12 @@ func ConvertToSubscriptionDto(i database.Subscription) SubscriptionDto {
return c
}
type SubscriptionDetails struct {
ID uuid.UUID `json:"id"`
Source SourceDto `json:"source"`
DiscordWebHook DiscordWebHooksDto `json:"discordwebhook"`
}
func splitTags(t string) []string {
items := strings.Split(t, ", ")
return items

View File

@ -14,7 +14,7 @@ import (
// @Tags Articles
// @Router /articles [get]
func (s *Server) listArticles(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", ApplicationJson)
res, err := s.Db.ListArticlesByDate(*s.ctx, 50)
if err != nil {

View File

@ -19,6 +19,7 @@ import (
// @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 {

View File

@ -23,6 +23,12 @@ type Server struct {
ctx *context.Context
}
const (
HeaderContentType = "Content-Type"
ApplicationJson = "application/json"
)
var (
ErrIdValueMissing string = "id value is missing"
ErrValueNotUuid string = "a value given was expected to be a uuid but was not correct."

View File

@ -2,7 +2,6 @@ package routes
import (
"encoding/json"
"log"
"net/http"
"github.com/go-chi/chi/v5"
@ -16,29 +15,28 @@ import (
// @Tags Settings
// @Router /settings/{key} [get]
func (s *Server) getSettings(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
//var item model.Sources
id := chi.URLParam(r, "ID")
uuid, err := uuid.Parse(id)
if err != nil {
panic(err)
s.WriteError(w, err.Error(), http.StatusBadRequest)
return
}
res, err := s.Db.GetSourceByID(*s.ctx, uuid)
if err != nil {
panic(err)
s.WriteError(w, err.Error(), http.StatusNotFound)
return
}
//itemId := fmt.Sprint(item.ID)
//if id != itemId {
// log.Panicln("Unable to find the requested record. Either unable to access SQL or the record does not exist.")
//}
bResult, err := json.Marshal(res)
if err != nil {
log.Panicln(err)
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(bResult)
}

View File

@ -15,6 +15,7 @@ func (s *Server) GetSubscriptionsRouter() http.Handler {
r := chi.NewRouter()
r.Get("/", s.ListSubscriptions)
r.Get("/details", s.ListSubscriptionDetails)
r.Get("/by/discordId", s.GetSubscriptionsByDiscordId)
r.Get("/by/sourceId", s.GetSubscriptionsBySourceId)
r.Post("/discord/webhook/new", s.newDiscordWebHookSubscription)
@ -25,7 +26,7 @@ func (s *Server) GetSubscriptionsRouter() http.Handler {
type ListSubscriptionResults struct {
ApiStatusModel
Payload []models.SubscriptionDto `json:"payload"`
Payload []models.SubscriptionDto `json:"payload"`
}
// GetSubscriptions
@ -46,7 +47,7 @@ func (s *Server) ListSubscriptions(w http.ResponseWriter, r *http.Request) {
},
}
res, err := s.Db.ListSubscriptions(*s.ctx, 100)
res, err := s.Db.ListSubscriptions(*s.ctx, 50)
if err != nil {
s.WriteError(w, err.Error(), http.StatusBadRequest)
return
@ -65,9 +66,63 @@ func (s *Server) ListSubscriptions(w http.ResponseWriter, r *http.Request) {
w.Write(bres)
}
type ListSubscriptionDetails struct {
ApiStatusModel
Payload []models.SubscriptionDetails `json:"payload"`
}
// ListSubscriptionDetails
// @Summary Returns the top 50 entries with full deatils on the source and output.
// @Produce application/json
// @Tags Subscription
// @Router /subscriptions/details [get]
// @Success 200 {object} ListSubscriptionDetails "ok"
func (s Server) ListSubscriptionDetails(w http.ResponseWriter, t *http.Request) {
w.Header().Set(HeaderContentType, ApplicationJson)
payload := ListSubscriptionDetails{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
Message: "OK",
},
}
res, err := s.Db.ListSubscriptions(t.Context(), 50)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
for _, item := range res {
dwh, err := s.Db.GetDiscordWebHooksByID(t.Context(), item.Discordwebhookid)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
source, err := s.Db.GetSourceByID(t.Context(), item.Sourceid)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
payload.Payload = append(payload.Payload, models.SubscriptionDetails{
ID: item.ID,
DiscordWebHook: models.ConvertToDiscordWebhookDto(dwh),
Source: models.ConvertToSourceDto(source),
})
}
b, err := json.Marshal(payload)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
}
w.Write(b)
}
type GetSubscriptionResults struct {
ApiStatusModel
Payload models.SubscriptionDto `json:"payload"`
Payload models.SubscriptionDto `json:"payload"`
}
// GetSubscriptionsByDiscordId
@ -82,10 +137,10 @@ type GetSubscriptionResults struct {
func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
p := ListSubscriptionResults {
p := ListSubscriptionResults{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
Message: "OK",
Message: "OK",
},
}
@ -131,10 +186,10 @@ func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Requ
func (s *Server) GetSubscriptionsBySourceId(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
p := ListSubscriptionResults {
p := ListSubscriptionResults{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
Message: "OK",
Message: "OK",
},
}
@ -175,7 +230,7 @@ func (s *Server) GetSubscriptionsBySourceId(w http.ResponseWriter, r *http.Reque
// @Param discordWebHookId query string true "discordWebHookId"
// @Param sourceId query string true "sourceId"
// @Tags Subscription
// @Router /subscriptions/new/discord/webhook [post]
// @Router /subscriptions/discord/webhook/new [post]
func (s *Server) newDiscordWebHookSubscription(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
@ -239,20 +294,20 @@ func (s *Server) newDiscordWebHookSubscription(w http.ResponseWriter, r *http.Re
// DeleteDiscordWebHookSubscription
// @Summary Removes a Discord WebHook Subscription based on the Subscription ID.
// @Param Id query string true "Id"
// @Param id query string true "id"
// @Tags Subscription
// @Router /subscriptions/discord/webhook/delete [delete]
func (s *Server) DeleteDiscordWebHookSubscription(w http.ResponseWriter, r *http.Request) {
var ErrMissingSubscriptionID string = "the request was missing a 'Id'"
query := r.URL.Query()
id := query["Id"][0]
id := query["id"][0]
if id == "" {
s.WriteError(w, ErrMissingSubscriptionID, http.StatusBadRequest)
return
}
uid, err := uuid.Parse(query["Id"][0])
uid, err := uuid.Parse(query["id"][0])
if err != nil {
s.WriteError(w, err.Error(), http.StatusBadRequest)
return