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": {
"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": {

View File

@ -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": {

View File

@ -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:

View File

@ -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"`

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
}
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 {