From 4ab7854ba294c4f8d362171afe61637ff641a813 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sat, 21 Jan 2023 12:20:33 -0800 Subject: [PATCH] added subscription details route --- database/db.go | 2 +- database/models.go | 2 +- database/query.sql.go | 2 +- docs/docs.go | 84 ++++++++++++++++++++++++++++++++++----- docs/swagger.json | 84 ++++++++++++++++++++++++++++++++++----- docs/swagger.yaml | 59 ++++++++++++++++++++++----- domain/models/dto.go | 24 +++++++++++ routes/articles.go | 2 +- routes/discordwebhooks.go | 1 + routes/server.go | 6 +++ routes/settings.go | 18 ++++----- routes/subscriptions.go | 77 ++++++++++++++++++++++++++++++----- 12 files changed, 303 insertions(+), 58 deletions(-) diff --git a/database/db.go b/database/db.go index c048805..fa4f825 100644 --- a/database/db.go +++ b/database/db.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.13.0 +// sqlc v1.16.0 package database diff --git a/database/models.go b/database/models.go index 6624941..b743218 100644 --- a/database/models.go +++ b/database/models.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.13.0 +// sqlc v1.16.0 package database diff --git a/database/query.sql.go b/database/query.sql.go index 390e13e..96b4f9b 100644 --- a/database/query.sql.go +++ b/database/query.sql.go @@ -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 diff --git a/docs/docs.go b/docs/docs.go index dee31fc..fb33cf1 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -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": { diff --git a/docs/swagger.json b/docs/swagger.json index d890e4f..651a874 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -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": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index eb6a9f8..8eba8d7 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -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 diff --git a/domain/models/dto.go b/domain/models/dto.go index 517c08b..91a6546 100644 --- a/domain/models/dto.go +++ b/domain/models/dto.go @@ -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 diff --git a/routes/articles.go b/routes/articles.go index 1fe6acf..00e3f2c 100644 --- a/routes/articles.go +++ b/routes/articles.go @@ -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 { diff --git a/routes/discordwebhooks.go b/routes/discordwebhooks.go index 4e29b67..fd24022 100644 --- a/routes/discordwebhooks.go +++ b/routes/discordwebhooks.go @@ -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 { diff --git a/routes/server.go b/routes/server.go index 73794bf..01a874f 100644 --- a/routes/server.go +++ b/routes/server.go @@ -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." diff --git a/routes/settings.go b/routes/settings.go index 267af99..101542d 100644 --- a/routes/settings.go +++ b/routes/settings.go @@ -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) } diff --git a/routes/subscriptions.go b/routes/subscriptions.go index 04548ac..169e6d4 100644 --- a/routes/subscriptions.go +++ b/routes/subscriptions.go @@ -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