From 71eadcfbe354c6e862e3d31d8ff8c3766a41d141 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sat, 21 Jan 2023 18:49:16 -0800 Subject: [PATCH] subscriptions moved to dto --- docs/docs.go | 12 +++--- docs/swagger.json | 12 +++--- docs/swagger.yaml | 12 +++--- domain/models/dto.go | 2 +- dto/subscriptions.go | 91 +++++++++++++++++++++++++++++++++++++++++ routes/subscriptions.go | 80 +++++++++++++----------------------- 6 files changed, 138 insertions(+), 71 deletions(-) create mode 100644 dto/subscriptions.go diff --git a/docs/docs.go b/docs/docs.go index f32858f..f8eba2b 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -620,7 +620,7 @@ const docTemplate = `{ "200": { "description": "ok", "schema": { - "$ref": "#/definitions/routes.ListSubscriptionResults" + "$ref": "#/definitions/routes.ListSubscriptions" } }, "400": { @@ -660,7 +660,7 @@ const docTemplate = `{ "200": { "description": "ok", "schema": { - "$ref": "#/definitions/routes.ListSubscriptionResults" + "$ref": "#/definitions/routes.ListSubscriptions" } } } @@ -688,7 +688,7 @@ const docTemplate = `{ "200": { "description": "ok", "schema": { - "$ref": "#/definitions/routes.ListSubscriptionResults" + "$ref": "#/definitions/routes.ListSubscriptions" } }, "400": { @@ -933,7 +933,7 @@ const docTemplate = `{ } } }, - "models.SubscriptionDetails": { + "models.SubscriptionDetailsDto": { "type": "object", "properties": { "discordwebhook": { @@ -1105,7 +1105,7 @@ const docTemplate = `{ "payload": { "type": "array", "items": { - "$ref": "#/definitions/models.SubscriptionDetails" + "$ref": "#/definitions/models.SubscriptionDetailsDto" } }, "status": { @@ -1113,7 +1113,7 @@ const docTemplate = `{ } } }, - "routes.ListSubscriptionResults": { + "routes.ListSubscriptions": { "type": "object", "properties": { "message": { diff --git a/docs/swagger.json b/docs/swagger.json index c8cca54..4eed4d9 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -611,7 +611,7 @@ "200": { "description": "ok", "schema": { - "$ref": "#/definitions/routes.ListSubscriptionResults" + "$ref": "#/definitions/routes.ListSubscriptions" } }, "400": { @@ -651,7 +651,7 @@ "200": { "description": "ok", "schema": { - "$ref": "#/definitions/routes.ListSubscriptionResults" + "$ref": "#/definitions/routes.ListSubscriptions" } } } @@ -679,7 +679,7 @@ "200": { "description": "ok", "schema": { - "$ref": "#/definitions/routes.ListSubscriptionResults" + "$ref": "#/definitions/routes.ListSubscriptions" } }, "400": { @@ -924,7 +924,7 @@ } } }, - "models.SubscriptionDetails": { + "models.SubscriptionDetailsDto": { "type": "object", "properties": { "discordwebhook": { @@ -1096,7 +1096,7 @@ "payload": { "type": "array", "items": { - "$ref": "#/definitions/models.SubscriptionDetails" + "$ref": "#/definitions/models.SubscriptionDetailsDto" } }, "status": { @@ -1104,7 +1104,7 @@ } } }, - "routes.ListSubscriptionResults": { + "routes.ListSubscriptions": { "type": "object", "properties": { "message": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index a93fa33..3996964 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -107,7 +107,7 @@ definitions: value: type: string type: object - models.SubscriptionDetails: + models.SubscriptionDetailsDto: properties: discordwebhook: $ref: '#/definitions/models.DiscordWebHooksDto' @@ -218,12 +218,12 @@ definitions: type: string payload: items: - $ref: '#/definitions/models.SubscriptionDetails' + $ref: '#/definitions/models.SubscriptionDetailsDto' type: array status: type: integer type: object - routes.ListSubscriptionResults: + routes.ListSubscriptions: properties: message: type: string @@ -640,7 +640,7 @@ paths: "200": description: ok schema: - $ref: '#/definitions/routes.ListSubscriptionResults' + $ref: '#/definitions/routes.ListSubscriptions' "400": description: Unable to reach SQL. schema: @@ -666,7 +666,7 @@ paths: "200": description: ok schema: - $ref: '#/definitions/routes.ListSubscriptionResults' + $ref: '#/definitions/routes.ListSubscriptions' summary: Returns the top 100 entries from the queue to be processed. tags: - Subscription @@ -684,7 +684,7 @@ paths: "200": description: ok schema: - $ref: '#/definitions/routes.ListSubscriptionResults' + $ref: '#/definitions/routes.ListSubscriptions' "400": description: Unable to reach SQL or Data problems schema: diff --git a/domain/models/dto.go b/domain/models/dto.go index b0f3510..4775204 100644 --- a/domain/models/dto.go +++ b/domain/models/dto.go @@ -117,7 +117,7 @@ func ConvertToSubscriptionDto(i database.Subscription) SubscriptionDto { return c } -type SubscriptionDetails struct { +type SubscriptionDetailsDto struct { ID uuid.UUID `json:"id"` Source SourceDto `json:"source"` DiscordWebHook DiscordWebHooksDto `json:"discordwebhook"` diff --git a/dto/subscriptions.go b/dto/subscriptions.go new file mode 100644 index 0000000..7a08ae6 --- /dev/null +++ b/dto/subscriptions.go @@ -0,0 +1,91 @@ +package dto + +import ( + "context" + + "github.com/google/uuid" + "github.com/jtom38/newsbot/collector/database" + "github.com/jtom38/newsbot/collector/domain/models" +) + +func (c DtoClient) ListSubscriptions(ctx context.Context, limit int32) ([]models.SubscriptionDto, error) { + var res []models.SubscriptionDto + + items, err := c.db.ListSubscriptions(ctx, limit) + if err != nil { + return res, err + } + + for _, item := range items { + res = append(res, c.ConvertSubscription(item)) + } + + return res, nil +} + +func (c DtoClient) ListSubscriptionDetails(ctx context.Context, limit int32) ([]models.SubscriptionDetailsDto, error) { + var res []models.SubscriptionDetailsDto + + items, err := c.ListSubscriptions(ctx, limit) + if err != nil { + return res, err + } + + for _, item := range items { + dwh, err := c.GetDiscordWebhook(ctx, item.DiscordWebhookId) + if err != nil { + return res, err + } + + source, err := c.GetSourceById(ctx, item.SourceId) + if err != nil { + return res, err + } + + res = append(res, models.SubscriptionDetailsDto{ + ID: item.ID, + Source: source, + DiscordWebHook: dwh, + }) + } + + return res, nil +} + +func (c DtoClient) ListSubscriptionsByDiscordWebhookId(ctx context.Context, id uuid.UUID) ([]models.SubscriptionDto, error) { + var res []models.SubscriptionDto + + items, err := c.db.GetSubscriptionsByDiscordWebHookId(ctx, id) + if err != nil { + return res, err + } + + for _, item := range items { + res = append(res, c.ConvertSubscription(item)) + } + + return res, nil +} + +func (c DtoClient) ListSubscriptionsBySourceId(ctx context.Context, id uuid.UUID) ([]models.SubscriptionDto, error) { + var res []models.SubscriptionDto + + items, err := c.db.GetSubscriptionsBySourceID(ctx, id) + if err != nil { + return res, err + } + + for _, item := range items { + res = append(res, c.ConvertSubscription(item)) + } + + return res, nil +} + +func (c DtoClient) ConvertSubscription(i database.Subscription) models.SubscriptionDto { + return models.SubscriptionDto{ + ID: i.ID, + DiscordWebhookId: i.Discordwebhookid, + SourceId: i.Sourceid, + } +} diff --git a/routes/subscriptions.go b/routes/subscriptions.go index 169e6d4..029a797 100644 --- a/routes/subscriptions.go +++ b/routes/subscriptions.go @@ -24,38 +24,41 @@ func (s *Server) GetSubscriptionsRouter() http.Handler { return r } -type ListSubscriptionResults struct { +type ListSubscriptions struct { ApiStatusModel Payload []models.SubscriptionDto `json:"payload"` } +type GetSubscription struct { + ApiStatusModel + Payload models.SubscriptionDto `json:"payload"` +} + // GetSubscriptions // @Summary Returns the top 100 entries from the queue to be processed. // @Produce application/json // @Tags Subscription // @Router /subscriptions [get] -// @Success 200 {object} ListSubscriptionResults "ok" -// @Failure 400 {object} ApiError "Unable to reach SQL." -// @Failure 500 {object} ApiError "Failed to process data from SQL." +// @Success 200 {object} ListSubscriptions "ok" +// @Failure 400 {object} ApiError "Unable to reach SQL." +// @Failure 500 {object} ApiError "Failed to process data from SQL." func (s *Server) ListSubscriptions(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - payload := ListSubscriptionResults{ + payload := ListSubscriptions{ ApiStatusModel: ApiStatusModel{ StatusCode: http.StatusOK, Message: "OK", }, } - res, err := s.Db.ListSubscriptions(*s.ctx, 50) + res, err := s.dto.ListSubscriptions(r.Context(), 50) if err != nil { s.WriteError(w, err.Error(), http.StatusBadRequest) return } - for _, item := range res { - payload.Payload = append(payload.Payload, models.ConvertToSubscriptionDto(item)) - } + payload.Payload = res bres, err := json.Marshal(payload) if err != nil { @@ -68,7 +71,7 @@ func (s *Server) ListSubscriptions(w http.ResponseWriter, r *http.Request) { type ListSubscriptionDetails struct { ApiStatusModel - Payload []models.SubscriptionDetails `json:"payload"` + Payload []models.SubscriptionDetailsDto `json:"payload"` } // ListSubscriptionDetails @@ -76,8 +79,8 @@ type ListSubscriptionDetails struct { // @Produce application/json // @Tags Subscription // @Router /subscriptions/details [get] -// @Success 200 {object} ListSubscriptionDetails "ok" -func (s Server) ListSubscriptionDetails(w http.ResponseWriter, t *http.Request) { +// @Success 200 {object} ListSubscriptionDetails "ok" +func (s Server) ListSubscriptionDetails(w http.ResponseWriter, r *http.Request) { w.Header().Set(HeaderContentType, ApplicationJson) payload := ListSubscriptionDetails{ ApiStatusModel: ApiStatusModel{ @@ -86,31 +89,13 @@ func (s Server) ListSubscriptionDetails(w http.ResponseWriter, t *http.Request) }, } - res, err := s.Db.ListSubscriptions(t.Context(), 50) + res, err := s.dto.ListSubscriptionDetails(r.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), - }) - } + payload.Payload = res b, err := json.Marshal(payload) if err != nil { @@ -120,24 +105,19 @@ func (s Server) ListSubscriptionDetails(w http.ResponseWriter, t *http.Request) w.Write(b) } -type GetSubscriptionResults struct { - ApiStatusModel - Payload models.SubscriptionDto `json:"payload"` -} - // GetSubscriptionsByDiscordId // @Summary Returns the top 100 entries from the queue to be processed. // @Produce application/json // @Param id query string true "id" // @Tags Subscription // @Router /subscriptions/by/discordId [get] -// @Success 200 {object} ListSubscriptionResults "ok" -// @Failure 400 {object} ApiError "Unable to reach SQL or Data problems" -// @Failure 500 {object} ApiError "Data problems" +// @Success 200 {object} ListSubscriptions "ok" +// @Failure 400 {object} ApiError "Unable to reach SQL or Data problems" +// @Failure 500 {object} ApiError "Data problems" func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") + w.Header().Set(HeaderContentType, ApplicationJson) - p := ListSubscriptionResults{ + p := ListSubscriptions{ ApiStatusModel: ApiStatusModel{ StatusCode: http.StatusOK, Message: "OK", @@ -157,15 +137,13 @@ func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Requ return } - res, err := s.Db.GetSubscriptionsByDiscordWebHookId(*s.ctx, uuid) + res, err := s.dto.ListSubscriptionsByDiscordWebhookId(r.Context(), uuid) if err != nil { s.WriteError(w, err.Error(), http.StatusNoContent) return } - for _, item := range res { - p.Payload = append(p.Payload, models.ConvertToSubscriptionDto(item)) - } + p.Payload = res bres, err := json.Marshal(p) if err != nil { @@ -182,11 +160,11 @@ func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Requ // @Param id query string true "id" // @Tags Subscription // @Router /subscriptions/by/SourceId [get] -// @Success 200 {object} ListSubscriptionResults "ok" +// @Success 200 {object} ListSubscriptions "ok" func (s *Server) GetSubscriptionsBySourceId(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - p := ListSubscriptionResults{ + p := ListSubscriptions{ ApiStatusModel: ApiStatusModel{ StatusCode: http.StatusOK, Message: "OK", @@ -206,15 +184,13 @@ func (s *Server) GetSubscriptionsBySourceId(w http.ResponseWriter, r *http.Reque return } - res, err := s.Db.GetSubscriptionsByDiscordWebHookId(*s.ctx, uuid) + res, err := s.dto.ListSubscriptionsBySourceId(r.Context(), uuid) if err != nil { s.WriteError(w, err.Error(), http.StatusNoContent) return } - for _, item := range res { - p.Payload = append(p.Payload, models.ConvertToSubscriptionDto(item)) - } + p.Payload = res bres, err := json.Marshal(p) if err != nil {