subscriptions moved to dto

This commit is contained in:
James Tombleson 2023-01-21 18:49:16 -08:00
parent f609204f3d
commit 71eadcfbe3
6 changed files with 138 additions and 71 deletions

View File

@ -620,7 +620,7 @@ const docTemplate = `{
"200": { "200": {
"description": "ok", "description": "ok",
"schema": { "schema": {
"$ref": "#/definitions/routes.ListSubscriptionResults" "$ref": "#/definitions/routes.ListSubscriptions"
} }
}, },
"400": { "400": {
@ -660,7 +660,7 @@ const docTemplate = `{
"200": { "200": {
"description": "ok", "description": "ok",
"schema": { "schema": {
"$ref": "#/definitions/routes.ListSubscriptionResults" "$ref": "#/definitions/routes.ListSubscriptions"
} }
} }
} }
@ -688,7 +688,7 @@ const docTemplate = `{
"200": { "200": {
"description": "ok", "description": "ok",
"schema": { "schema": {
"$ref": "#/definitions/routes.ListSubscriptionResults" "$ref": "#/definitions/routes.ListSubscriptions"
} }
}, },
"400": { "400": {
@ -933,7 +933,7 @@ const docTemplate = `{
} }
} }
}, },
"models.SubscriptionDetails": { "models.SubscriptionDetailsDto": {
"type": "object", "type": "object",
"properties": { "properties": {
"discordwebhook": { "discordwebhook": {
@ -1105,7 +1105,7 @@ const docTemplate = `{
"payload": { "payload": {
"type": "array", "type": "array",
"items": { "items": {
"$ref": "#/definitions/models.SubscriptionDetails" "$ref": "#/definitions/models.SubscriptionDetailsDto"
} }
}, },
"status": { "status": {
@ -1113,7 +1113,7 @@ const docTemplate = `{
} }
} }
}, },
"routes.ListSubscriptionResults": { "routes.ListSubscriptions": {
"type": "object", "type": "object",
"properties": { "properties": {
"message": { "message": {

View File

@ -611,7 +611,7 @@
"200": { "200": {
"description": "ok", "description": "ok",
"schema": { "schema": {
"$ref": "#/definitions/routes.ListSubscriptionResults" "$ref": "#/definitions/routes.ListSubscriptions"
} }
}, },
"400": { "400": {
@ -651,7 +651,7 @@
"200": { "200": {
"description": "ok", "description": "ok",
"schema": { "schema": {
"$ref": "#/definitions/routes.ListSubscriptionResults" "$ref": "#/definitions/routes.ListSubscriptions"
} }
} }
} }
@ -679,7 +679,7 @@
"200": { "200": {
"description": "ok", "description": "ok",
"schema": { "schema": {
"$ref": "#/definitions/routes.ListSubscriptionResults" "$ref": "#/definitions/routes.ListSubscriptions"
} }
}, },
"400": { "400": {
@ -924,7 +924,7 @@
} }
} }
}, },
"models.SubscriptionDetails": { "models.SubscriptionDetailsDto": {
"type": "object", "type": "object",
"properties": { "properties": {
"discordwebhook": { "discordwebhook": {
@ -1096,7 +1096,7 @@
"payload": { "payload": {
"type": "array", "type": "array",
"items": { "items": {
"$ref": "#/definitions/models.SubscriptionDetails" "$ref": "#/definitions/models.SubscriptionDetailsDto"
} }
}, },
"status": { "status": {
@ -1104,7 +1104,7 @@
} }
} }
}, },
"routes.ListSubscriptionResults": { "routes.ListSubscriptions": {
"type": "object", "type": "object",
"properties": { "properties": {
"message": { "message": {

View File

@ -107,7 +107,7 @@ definitions:
value: value:
type: string type: string
type: object type: object
models.SubscriptionDetails: models.SubscriptionDetailsDto:
properties: properties:
discordwebhook: discordwebhook:
$ref: '#/definitions/models.DiscordWebHooksDto' $ref: '#/definitions/models.DiscordWebHooksDto'
@ -218,12 +218,12 @@ definitions:
type: string type: string
payload: payload:
items: items:
$ref: '#/definitions/models.SubscriptionDetails' $ref: '#/definitions/models.SubscriptionDetailsDto'
type: array type: array
status: status:
type: integer type: integer
type: object type: object
routes.ListSubscriptionResults: routes.ListSubscriptions:
properties: properties:
message: message:
type: string type: string
@ -640,7 +640,7 @@ paths:
"200": "200":
description: ok description: ok
schema: schema:
$ref: '#/definitions/routes.ListSubscriptionResults' $ref: '#/definitions/routes.ListSubscriptions'
"400": "400":
description: Unable to reach SQL. description: Unable to reach SQL.
schema: schema:
@ -666,7 +666,7 @@ paths:
"200": "200":
description: ok description: ok
schema: schema:
$ref: '#/definitions/routes.ListSubscriptionResults' $ref: '#/definitions/routes.ListSubscriptions'
summary: Returns the top 100 entries from the queue to be processed. summary: Returns the top 100 entries from the queue to be processed.
tags: tags:
- Subscription - Subscription
@ -684,7 +684,7 @@ paths:
"200": "200":
description: ok description: ok
schema: schema:
$ref: '#/definitions/routes.ListSubscriptionResults' $ref: '#/definitions/routes.ListSubscriptions'
"400": "400":
description: Unable to reach SQL or Data problems description: Unable to reach SQL or Data problems
schema: schema:

View File

@ -117,7 +117,7 @@ func ConvertToSubscriptionDto(i database.Subscription) SubscriptionDto {
return c return c
} }
type SubscriptionDetails struct { type SubscriptionDetailsDto struct {
ID uuid.UUID `json:"id"` ID uuid.UUID `json:"id"`
Source SourceDto `json:"source"` Source SourceDto `json:"source"`
DiscordWebHook DiscordWebHooksDto `json:"discordwebhook"` DiscordWebHook DiscordWebHooksDto `json:"discordwebhook"`

91
dto/subscriptions.go Normal file
View File

@ -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,
}
}

View File

@ -24,38 +24,41 @@ func (s *Server) GetSubscriptionsRouter() http.Handler {
return r return r
} }
type ListSubscriptionResults struct { type ListSubscriptions struct {
ApiStatusModel ApiStatusModel
Payload []models.SubscriptionDto `json:"payload"` Payload []models.SubscriptionDto `json:"payload"`
} }
type GetSubscription struct {
ApiStatusModel
Payload models.SubscriptionDto `json:"payload"`
}
// GetSubscriptions // GetSubscriptions
// @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
// @Tags Subscription // @Tags Subscription
// @Router /subscriptions [get] // @Router /subscriptions [get]
// @Success 200 {object} ListSubscriptionResults "ok" // @Success 200 {object} ListSubscriptions "ok"
// @Failure 400 {object} ApiError "Unable to reach SQL." // @Failure 400 {object} ApiError "Unable to reach SQL."
// @Failure 500 {object} ApiError "Failed to process data from SQL." // @Failure 500 {object} ApiError "Failed to process data from SQL."
func (s *Server) ListSubscriptions(w http.ResponseWriter, r *http.Request) { func (s *Server) ListSubscriptions(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
payload := ListSubscriptionResults{ payload := ListSubscriptions{
ApiStatusModel: ApiStatusModel{ ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK, StatusCode: http.StatusOK,
Message: "OK", Message: "OK",
}, },
} }
res, err := s.Db.ListSubscriptions(*s.ctx, 50) res, err := s.dto.ListSubscriptions(r.Context(), 50)
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusBadRequest) s.WriteError(w, err.Error(), http.StatusBadRequest)
return return
} }
for _, item := range res { payload.Payload = res
payload.Payload = append(payload.Payload, models.ConvertToSubscriptionDto(item))
}
bres, err := json.Marshal(payload) bres, err := json.Marshal(payload)
if err != nil { if err != nil {
@ -68,7 +71,7 @@ func (s *Server) ListSubscriptions(w http.ResponseWriter, r *http.Request) {
type ListSubscriptionDetails struct { type ListSubscriptionDetails struct {
ApiStatusModel ApiStatusModel
Payload []models.SubscriptionDetails `json:"payload"` Payload []models.SubscriptionDetailsDto `json:"payload"`
} }
// ListSubscriptionDetails // ListSubscriptionDetails
@ -77,7 +80,7 @@ type ListSubscriptionDetails struct {
// @Tags Subscription // @Tags Subscription
// @Router /subscriptions/details [get] // @Router /subscriptions/details [get]
// @Success 200 {object} ListSubscriptionDetails "ok" // @Success 200 {object} ListSubscriptionDetails "ok"
func (s Server) ListSubscriptionDetails(w http.ResponseWriter, t *http.Request) { func (s Server) ListSubscriptionDetails(w http.ResponseWriter, r *http.Request) {
w.Header().Set(HeaderContentType, ApplicationJson) w.Header().Set(HeaderContentType, ApplicationJson)
payload := ListSubscriptionDetails{ payload := ListSubscriptionDetails{
ApiStatusModel: ApiStatusModel{ 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 { if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError) s.WriteError(w, err.Error(), http.StatusInternalServerError)
return return
} }
for _, item := range res { payload.Payload = 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) b, err := json.Marshal(payload)
if err != nil { if err != nil {
@ -120,24 +105,19 @@ func (s Server) ListSubscriptionDetails(w http.ResponseWriter, t *http.Request)
w.Write(b) w.Write(b)
} }
type GetSubscriptionResults struct {
ApiStatusModel
Payload models.SubscriptionDto `json:"payload"`
}
// GetSubscriptionsByDiscordId // GetSubscriptionsByDiscordId
// @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 query string true "id"
// @Tags Subscription // @Tags Subscription
// @Router /subscriptions/by/discordId [get] // @Router /subscriptions/by/discordId [get]
// @Success 200 {object} ListSubscriptionResults "ok" // @Success 200 {object} ListSubscriptions "ok"
// @Failure 400 {object} ApiError "Unable to reach SQL or Data problems" // @Failure 400 {object} ApiError "Unable to reach SQL or Data problems"
// @Failure 500 {object} ApiError "Data problems" // @Failure 500 {object} ApiError "Data problems"
func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Request) { 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{ ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK, StatusCode: http.StatusOK,
Message: "OK", Message: "OK",
@ -157,15 +137,13 @@ func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Requ
return return
} }
res, err := s.Db.GetSubscriptionsByDiscordWebHookId(*s.ctx, uuid) res, err := s.dto.ListSubscriptionsByDiscordWebhookId(r.Context(), uuid)
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusNoContent) s.WriteError(w, err.Error(), http.StatusNoContent)
return return
} }
for _, item := range res { p.Payload = res
p.Payload = append(p.Payload, models.ConvertToSubscriptionDto(item))
}
bres, err := json.Marshal(p) bres, err := json.Marshal(p)
if err != nil { if err != nil {
@ -182,11 +160,11 @@ func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Requ
// @Param id query string true "id" // @Param id query string true "id"
// @Tags Subscription // @Tags Subscription
// @Router /subscriptions/by/SourceId [get] // @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) { func (s *Server) GetSubscriptionsBySourceId(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
p := ListSubscriptionResults{ p := ListSubscriptions{
ApiStatusModel: ApiStatusModel{ ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK, StatusCode: http.StatusOK,
Message: "OK", Message: "OK",
@ -206,15 +184,13 @@ func (s *Server) GetSubscriptionsBySourceId(w http.ResponseWriter, r *http.Reque
return return
} }
res, err := s.Db.GetSubscriptionsByDiscordWebHookId(*s.ctx, uuid) res, err := s.dto.ListSubscriptionsBySourceId(r.Context(), uuid)
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusNoContent) s.WriteError(w, err.Error(), http.StatusNoContent)
return return
} }
for _, item := range res { p.Payload = res
p.Payload = append(p.Payload, models.ConvertToSubscriptionDto(item))
}
bres, err := json.Marshal(p) bres, err := json.Marshal(p)
if err != nil { if err != nil {