updated sources to use dto

This commit is contained in:
James Tombleson 2023-01-21 18:24:04 -08:00
parent 4fce1da63c
commit f609204f3d
8 changed files with 215 additions and 143 deletions

View File

@ -323,27 +323,6 @@ const docTemplate = `{
} }
} }
}, },
"/settings/{key}": {
"get": {
"produces": [
"application/json"
],
"tags": [
"Settings"
],
"summary": "Returns a object based on the Key that was given.",
"parameters": [
{
"type": "string",
"description": "Settings Key value",
"name": "key",
"in": "path",
"required": true
}
],
"responses": {}
}
},
"/sources": { "/sources": {
"get": { "get": {
"produces": [ "produces": [
@ -357,7 +336,7 @@ const docTemplate = `{
"200": { "200": {
"description": "ok", "description": "ok",
"schema": { "schema": {
"$ref": "#/definitions/routes.ListSourcesResults" "$ref": "#/definitions/routes.ListSources"
} }
}, },
"400": { "400": {
@ -391,7 +370,7 @@ const docTemplate = `{
"200": { "200": {
"description": "ok", "description": "ok",
"schema": { "schema": {
"$ref": "#/definitions/routes.ListSourcesResults" "$ref": "#/definitions/routes.ListSources"
} }
}, },
"400": { "400": {
@ -434,7 +413,32 @@ const docTemplate = `{
"required": true "required": true
} }
], ],
"responses": {} "responses": {
"200": {
"description": "ok",
"schema": {
"$ref": "#/definitions/routes.GetSource"
}
},
"204": {
"description": "No record found.",
"schema": {
"$ref": "#/definitions/routes.ApiError"
}
},
"400": {
"description": "Unable to query SQL.",
"schema": {
"$ref": "#/definitions/routes.ApiError"
}
},
"500": {
"description": "Failed to process data from SQL.",
"schema": {
"$ref": "#/definitions/routes.ApiError"
}
}
}
} }
}, },
"/sources/new/reddit": { "/sources/new/reddit": {
@ -527,7 +531,7 @@ const docTemplate = `{
"200": { "200": {
"description": "ok", "description": "ok",
"schema": { "schema": {
"$ref": "#/definitions/routes.GetSourceResult" "$ref": "#/definitions/routes.GetSource"
} }
}, },
"204": { "204": {
@ -1027,7 +1031,7 @@ const docTemplate = `{
} }
} }
}, },
"routes.GetSourceResult": { "routes.GetSource": {
"type": "object", "type": "object",
"properties": { "properties": {
"message": { "message": {
@ -1075,7 +1079,7 @@ const docTemplate = `{
} }
} }
}, },
"routes.ListSourcesResults": { "routes.ListSources": {
"type": "object", "type": "object",
"properties": { "properties": {
"message": { "message": {

View File

@ -314,27 +314,6 @@
} }
} }
}, },
"/settings/{key}": {
"get": {
"produces": [
"application/json"
],
"tags": [
"Settings"
],
"summary": "Returns a object based on the Key that was given.",
"parameters": [
{
"type": "string",
"description": "Settings Key value",
"name": "key",
"in": "path",
"required": true
}
],
"responses": {}
}
},
"/sources": { "/sources": {
"get": { "get": {
"produces": [ "produces": [
@ -348,7 +327,7 @@
"200": { "200": {
"description": "ok", "description": "ok",
"schema": { "schema": {
"$ref": "#/definitions/routes.ListSourcesResults" "$ref": "#/definitions/routes.ListSources"
} }
}, },
"400": { "400": {
@ -382,7 +361,7 @@
"200": { "200": {
"description": "ok", "description": "ok",
"schema": { "schema": {
"$ref": "#/definitions/routes.ListSourcesResults" "$ref": "#/definitions/routes.ListSources"
} }
}, },
"400": { "400": {
@ -425,7 +404,32 @@
"required": true "required": true
} }
], ],
"responses": {} "responses": {
"200": {
"description": "ok",
"schema": {
"$ref": "#/definitions/routes.GetSource"
}
},
"204": {
"description": "No record found.",
"schema": {
"$ref": "#/definitions/routes.ApiError"
}
},
"400": {
"description": "Unable to query SQL.",
"schema": {
"$ref": "#/definitions/routes.ApiError"
}
},
"500": {
"description": "Failed to process data from SQL.",
"schema": {
"$ref": "#/definitions/routes.ApiError"
}
}
}
} }
}, },
"/sources/new/reddit": { "/sources/new/reddit": {
@ -518,7 +522,7 @@
"200": { "200": {
"description": "ok", "description": "ok",
"schema": { "schema": {
"$ref": "#/definitions/routes.GetSourceResult" "$ref": "#/definitions/routes.GetSource"
} }
}, },
"204": { "204": {
@ -1018,7 +1022,7 @@
} }
} }
}, },
"routes.GetSourceResult": { "routes.GetSource": {
"type": "object", "type": "object",
"properties": { "properties": {
"message": { "message": {
@ -1066,7 +1070,7 @@
} }
} }
}, },
"routes.ListSourcesResults": { "routes.ListSources": {
"type": "object", "type": "object",
"properties": { "properties": {
"message": { "message": {

View File

@ -170,7 +170,7 @@ definitions:
status: status:
type: integer type: integer
type: object type: object
routes.GetSourceResult: routes.GetSource:
properties: properties:
message: message:
type: string type: string
@ -201,7 +201,7 @@ definitions:
status: status:
type: integer type: integer
type: object type: object
routes.ListSourcesResults: routes.ListSources:
properties: properties:
message: message:
type: string type: string
@ -444,20 +444,6 @@ paths:
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:
- Queue - Queue
/settings/{key}:
get:
parameters:
- description: Settings Key value
in: path
name: key
required: true
type: string
produces:
- application/json
responses: {}
summary: Returns a object based on the Key that was given.
tags:
- Settings
/sources: /sources:
get: get:
produces: produces:
@ -466,7 +452,7 @@ paths:
"200": "200":
description: ok description: ok
schema: schema:
$ref: '#/definitions/routes.ListSourcesResults' $ref: '#/definitions/routes.ListSources'
"400": "400":
description: Unable to reach SQL or Data problems description: Unable to reach SQL or Data problems
schema: schema:
@ -488,7 +474,7 @@ paths:
"200": "200":
description: ok description: ok
schema: schema:
$ref: '#/definitions/routes.GetSourceResult' $ref: '#/definitions/routes.GetSource'
"204": "204":
description: No record found. description: No record found.
schema: schema:
@ -553,7 +539,7 @@ paths:
"200": "200":
description: ok description: ok
schema: schema:
$ref: '#/definitions/routes.ListSourcesResults' $ref: '#/definitions/routes.ListSources'
"400": "400":
description: Unable to query SQL. description: Unable to query SQL.
schema: schema:
@ -580,7 +566,23 @@ paths:
type: string type: string
produces: produces:
- application/json - application/json
responses: {} responses:
"200":
description: ok
schema:
$ref: '#/definitions/routes.GetSource'
"204":
description: No record found.
schema:
$ref: '#/definitions/routes.ApiError'
"400":
description: Unable to query SQL.
schema:
$ref: '#/definitions/routes.ApiError'
"500":
description: Failed to process data from SQL.
schema:
$ref: '#/definitions/routes.ApiError'
summary: Returns a single entity by ID summary: Returns a single entity by ID
tags: tags:
- Source - Source

View File

@ -95,7 +95,7 @@ func (c DtoClient) convertArticle(i database.Article) models.ArticleDto {
func (c DtoClient) convertArticleDetails(i database.Article, s database.Source) models.ArticleDetailsDto { func (c DtoClient) convertArticleDetails(i database.Article, s database.Source) models.ArticleDetailsDto {
return models.ArticleDetailsDto{ return models.ArticleDetailsDto{
ID: i.ID, ID: i.ID,
Source: c.ConvertToSourceDto(s), Source: c.ConvertToSource(s),
Tags: c.SplitTags(i.Tags), Tags: c.SplitTags(i.Tags),
Title: i.Title, Title: i.Title,
Url: i.Url, Url: i.Url,

View File

@ -1,11 +1,70 @@
package dto package dto
import ( import (
"context"
"strings"
"github.com/google/uuid"
"github.com/jtom38/newsbot/collector/database" "github.com/jtom38/newsbot/collector/database"
"github.com/jtom38/newsbot/collector/domain/models" "github.com/jtom38/newsbot/collector/domain/models"
) )
func (c DtoClient) ConvertToSourceDto(i database.Source) models.SourceDto { func (c DtoClient) ListSources(ctx context.Context, limit int32) ([]models.SourceDto, error) {
var res []models.SourceDto
items, err := c.db.ListSources(ctx, limit)
if err != nil {
return res, err
}
for _, item := range items {
res = append(res, c.ConvertToSource(item))
}
return res, nil
}
func (c DtoClient) ListSourcesBySource(ctx context.Context, sourceName string) ([]models.SourceDto, error) {
var res []models.SourceDto
items, err := c.db.ListSourcesBySource(ctx, strings.ToLower(sourceName))
if err != nil {
return res, err
}
for _, item := range items {
res = append(res, c.ConvertToSource(item))
}
return res, nil
}
func (c DtoClient) GetSourceById(ctx context.Context, id uuid.UUID) (models.SourceDto, error) {
var res models.SourceDto
item, err := c.db.GetSourceByID(ctx, id)
if err != nil {
return res, err
}
return c.ConvertToSource(item), nil
}
func (c DtoClient) GetSourceByNameAndSource(ctx context.Context, name, source string) (models.SourceDto, error) {
var res models.SourceDto
item, err := c.db.GetSourceByNameAndSource(ctx, database.GetSourceByNameAndSourceParams{
Name: name,
Source: source,
})
if err != nil {
return res, err
}
return c.ConvertToSource(item), nil
}
func (c DtoClient) ConvertToSource(i database.Source) models.SourceDto {
var deleted bool var deleted bool
if !i.Deleted.Valid { if !i.Deleted.Valid {
deleted = true deleted = true

View File

@ -82,7 +82,7 @@ func (s *Server) MountRoutes() {
s.Router.Mount("/api/queue", s.GetQueueRouter()) s.Router.Mount("/api/queue", s.GetQueueRouter())
s.Router.Mount("/api/discord/webhooks", s.DiscordWebHookRouter()) s.Router.Mount("/api/discord/webhooks", s.DiscordWebHookRouter())
s.Router.Get("/api/settings", s.getSettings) //s.Router.Get("/api/settings", s.getSettings)
s.Router.Mount("/api/sources", s.GetSourcesRouter()) s.Router.Mount("/api/sources", s.GetSourcesRouter())
s.Router.Mount("/api/subscriptions", s.GetSubscriptionsRouter()) s.Router.Mount("/api/subscriptions", s.GetSubscriptionsRouter())

View File

@ -8,13 +8,15 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
) )
// GetSettings
// @Summary Returns a object based on the Key that was given.
// @Param key path string true "Settings Key value"
// @Produce application/json
// @Tags Settings
// @Router /settings/{key} [get]
func (s *Server) getSettings(w http.ResponseWriter, r *http.Request) { func (s *Server) getSettings(w http.ResponseWriter, r *http.Request) {
// GetSettings
// @Summary Returns a object based on the Key that was given.
// @Param key path string true "Settings Key value"
// @Produce application/json
// @Tags Settings
// @Router /settings/{key} [get]
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
//var item model.Sources //var item model.Sources

View File

@ -1,10 +1,8 @@
package routes package routes
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/http" "net/http"
"strings" "strings"
@ -35,12 +33,12 @@ func (s *Server) GetSourcesRouter() http.Handler {
return r return r
} }
type ListSourcesResults struct { type ListSources struct {
ApiStatusModel ApiStatusModel
Payload []models.SourceDto `json:"payload"` Payload []models.SourceDto `json:"payload"`
} }
type GetSourceResult struct { type GetSource struct {
ApiStatusModel ApiStatusModel
Payload models.SourceDto `json:"payload"` Payload models.SourceDto `json:"payload"`
} }
@ -50,8 +48,8 @@ type GetSourceResult struct {
// @Produce application/json // @Produce application/json
// @Tags Source // @Tags Source
// @Router /sources [get] // @Router /sources [get]
// @Success 200 {object} ListSourcesResults "ok" // @Success 200 {object} ListSources "ok"
// @Failure 400 {object} ApiError "Unable to reach SQL or Data problems" // @Failure 400 {object} ApiError "Unable to reach SQL or Data problems"
func (s *Server) listSources(w http.ResponseWriter, r *http.Request) { func (s *Server) listSources(w http.ResponseWriter, r *http.Request) {
//TODO Add top? //TODO Add top?
/* /*
@ -63,8 +61,8 @@ func (s *Server) listSources(w http.ResponseWriter, r *http.Request) {
res, err := s.Db.ListSources(*s.ctx, int32(topInt)) res, err := s.Db.ListSources(*s.ctx, int32(topInt))
*/ */
w.Header().Set("Content-Type", "application/json") w.Header().Set(HeaderContentType, ApplicationJson)
result := ListSourcesResults{ result := ListSources{
ApiStatusModel: ApiStatusModel{ ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK, StatusCode: http.StatusOK,
Message: "OK", Message: "OK",
@ -72,18 +70,13 @@ func (s *Server) listSources(w http.ResponseWriter, r *http.Request) {
} }
// Default way of showing all sources // Default way of showing all sources
res, err := s.Db.ListSources(*s.ctx, 50) items, err := s.dto.ListSources(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
} }
var dto []models.SourceDto result.Payload = items
for _, item := range res {
dto = append(dto, models.ConvertToSourceDto(item))
}
result.Payload = dto
bResult, err := json.Marshal(result) bResult, err := json.Marshal(result)
if err != nil { if err != nil {
@ -100,9 +93,9 @@ func (s *Server) listSources(w http.ResponseWriter, r *http.Request) {
// @Produce application/json // @Produce application/json
// @Tags Source // @Tags Source
// @Router /sources/by/source [get] // @Router /sources/by/source [get]
// @Success 200 {object} ListSourcesResults "ok" // @Success 200 {object} ListSources "ok"
// @Failure 400 {object} ApiError "Unable to query SQL." // @Failure 400 {object} ApiError "Unable to query SQL."
// @Failure 500 {object} ApiError "Problems with data." // @Failure 500 {object} ApiError "Problems with data."
func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) { func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) {
//TODO Add top? //TODO Add top?
/* /*
@ -113,9 +106,9 @@ func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) {
} }
res, err := s.Db.ListSources(*s.ctx, int32(topInt)) res, err := s.Db.ListSources(*s.ctx, int32(topInt))
*/ */
w.Header().Set("Content-Type", "application/json") w.Header().Set(HeaderContentType, ApplicationJson)
result := ListSourcesResults{ result := ListSources{
ApiStatusModel: ApiStatusModel{ ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK, StatusCode: http.StatusOK,
Message: "OK", Message: "OK",
@ -126,15 +119,13 @@ func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) {
_source := query["source"][0] _source := query["source"][0]
// Shows the list by Sources.source // Shows the list by Sources.source
res, err := s.Db.ListSourcesBySource(*s.ctx, strings.ToLower(_source)) res, err := s.dto.ListSourcesBySource(r.Context(), _source)
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 { result.Payload = res
result.Payload = append(result.Payload, models.ConvertToSourceDto(item))
}
bResult, err := json.Marshal(result) bResult, err := json.Marshal(result)
if err != nil { if err != nil {
@ -151,12 +142,19 @@ func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) {
// @Produce application/json // @Produce application/json
// @Tags Source // @Tags Source
// @Router /sources/{id} [get] // @Router /sources/{id} [get]
// @Success 200 {object} GetSourceResult "ok" // @Success 200 {object} GetSource "ok"
// @Failure 204 {object} ApiError "No record found." // @Failure 204 {object} ApiError "No record found."
// @Failure 400 {object} ApiError "Unable to query SQL." // @Failure 400 {object} ApiError "Unable to query SQL."
// @Failure 500 {object} ApiError "Failed to process data from SQL." // @Failure 500 {object} ApiError "Failed to process data from SQL."
func (s *Server) getSources(w http.ResponseWriter, r *http.Request) { func (s *Server) getSources(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") payload := GetSource{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
},
}
w.Header().Set(HeaderContentType, ApplicationJson)
id := chi.URLParam(r, "ID") id := chi.URLParam(r, "ID")
uuid, err := uuid.Parse(id) uuid, err := uuid.Parse(id)
@ -165,21 +163,13 @@ func (s *Server) getSources(w http.ResponseWriter, r *http.Request) {
return return
} }
res, err := s.Db.GetSourceByID(*s.ctx, uuid) res, err := s.dto.GetSourceById(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
} }
dto := models.ConvertToSourceDto(res) payload.Payload = res
payload := GetSourceResult{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
},
Payload: dto,
}
bResult, err := json.Marshal(payload) bResult, err := json.Marshal(payload)
if err != nil { if err != nil {
@ -197,35 +187,46 @@ func (s *Server) getSources(w http.ResponseWriter, r *http.Request) {
// @Produce application/json // @Produce application/json
// @Tags Source // @Tags Source
// @Router /sources/by/sourceAndName [get] // @Router /sources/by/sourceAndName [get]
// @Success 200 {object} GetSource "ok"
// @Failure 204 {object} ApiError "No record found."
// @Failure 400 {object} ApiError "Unable to query SQL."
// @Failure 500 {object} ApiError "Failed to process data from SQL."
func (s *Server) GetSourceBySourceAndName(w http.ResponseWriter, r *http.Request) { func (s *Server) GetSourceBySourceAndName(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query() p := GetSource{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
},
}
query := r.URL.Query()
name := query["name"][0] name := query["name"][0]
if name == "" { if name == "" {
http.Error(w, "Parameter 'name' was missing in the query.", http.StatusInternalServerError) s.WriteError(w, "Parameter 'name' was missing in the query.", http.StatusInternalServerError)
return return
} }
source := query["source"][0] source := query["source"][0]
if source == "" { if source == "" {
http.Error(w, "The parameter 'source' was missing in the query.", http.StatusInternalServerError) s.WriteError(w, "The parameter 'source' was missing in the query.", http.StatusInternalServerError)
return return
} }
item, err := s.Db.GetSourceByNameAndSource(context.Background(), database.GetSourceByNameAndSourceParams{ item, err := s.dto.GetSourceByNameAndSource(r.Context(), name, source)
Name: name,
Source: source,
})
if err != nil { if err != nil {
http.Error(w, "Unable to find the requested record.", http.StatusInternalServerError) s.WriteError(w, "Unable to find the requested record.", http.StatusInternalServerError)
return
} }
p.Payload = item
bResult, err := json.Marshal(item) bResult, err := json.Marshal(item)
if err != nil { if err != nil {
log.Panicln(err) s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set(HeaderContentType, ApplicationJson)
w.Write(bResult) w.Write(bResult)
} }
@ -297,7 +298,7 @@ func (s *Server) newYoutubeSource(w http.ResponseWriter, r *http.Request) {
_name := query["name"][0] _name := query["name"][0]
_url := query["url"][0] _url := query["url"][0]
//_tags := query["tags"][0] //_tags := query["tags"][0]
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
if _url == "" { if _url == "" {
s.WriteError(w, "url is missing a value", http.StatusBadRequest) s.WriteError(w, "url is missing a value", http.StatusBadRequest)
@ -348,7 +349,7 @@ func (s *Server) newYoutubeSource(w http.ResponseWriter, r *http.Request) {
// @Router /sources/new/twitch [post] // @Router /sources/new/twitch [post]
func (s *Server) newTwitchSource(w http.ResponseWriter, r *http.Request) { func (s *Server) newTwitchSource(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
query := r.URL.Query() query := r.URL.Query()
_name := query["name"][0] _name := query["name"][0]
@ -409,8 +410,8 @@ func (s *Server) deleteSources(w http.ResponseWriter, r *http.Request) {
return return
} }
p := ApiStatusModel { p := ApiStatusModel{
Message: "OK", Message: "OK",
StatusCode: http.StatusOK, StatusCode: http.StatusOK,
} }
@ -419,7 +420,7 @@ func (s *Server) deleteSources(w http.ResponseWriter, r *http.Request) {
s.WriteError(w, err.Error(), http.StatusInternalServerError) s.WriteError(w, err.Error(), http.StatusInternalServerError)
return return
} }
w.Write(b) w.Write(b)
} }
@ -446,8 +447,8 @@ func (s *Server) disableSource(w http.ResponseWriter, r *http.Request) {
s.WriteError(w, err.Error(), http.StatusInternalServerError) s.WriteError(w, err.Error(), http.StatusInternalServerError)
} }
p := ApiStatusModel { p := ApiStatusModel{
Message: "OK", Message: "OK",
StatusCode: http.StatusOK, StatusCode: http.StatusOK,
} }
@ -456,7 +457,7 @@ func (s *Server) disableSource(w http.ResponseWriter, r *http.Request) {
s.WriteError(w, err.Error(), http.StatusInternalServerError) s.WriteError(w, err.Error(), http.StatusInternalServerError)
return return
} }
w.Write(b) w.Write(b)
} }
@ -483,8 +484,8 @@ func (s *Server) enableSource(w http.ResponseWriter, r *http.Request) {
s.WriteError(w, err.Error(), http.StatusInternalServerError) s.WriteError(w, err.Error(), http.StatusInternalServerError)
} }
p := ApiStatusModel { p := ApiStatusModel{
Message: "OK", Message: "OK",
StatusCode: http.StatusOK, StatusCode: http.StatusOK,
} }
@ -493,6 +494,6 @@ func (s *Server) enableSource(w http.ResponseWriter, r *http.Request) {
s.WriteError(w, err.Error(), http.StatusInternalServerError) s.WriteError(w, err.Error(), http.StatusInternalServerError)
return return
} }
w.Write(b) w.Write(b)
} }