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": {
"get": {
"produces": [
@ -357,7 +336,7 @@ const docTemplate = `{
"200": {
"description": "ok",
"schema": {
"$ref": "#/definitions/routes.ListSourcesResults"
"$ref": "#/definitions/routes.ListSources"
}
},
"400": {
@ -391,7 +370,7 @@ const docTemplate = `{
"200": {
"description": "ok",
"schema": {
"$ref": "#/definitions/routes.ListSourcesResults"
"$ref": "#/definitions/routes.ListSources"
}
},
"400": {
@ -434,7 +413,32 @@ const docTemplate = `{
"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": {
@ -527,7 +531,7 @@ const docTemplate = `{
"200": {
"description": "ok",
"schema": {
"$ref": "#/definitions/routes.GetSourceResult"
"$ref": "#/definitions/routes.GetSource"
}
},
"204": {
@ -1027,7 +1031,7 @@ const docTemplate = `{
}
}
},
"routes.GetSourceResult": {
"routes.GetSource": {
"type": "object",
"properties": {
"message": {
@ -1075,7 +1079,7 @@ const docTemplate = `{
}
}
},
"routes.ListSourcesResults": {
"routes.ListSources": {
"type": "object",
"properties": {
"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": {
"get": {
"produces": [
@ -348,7 +327,7 @@
"200": {
"description": "ok",
"schema": {
"$ref": "#/definitions/routes.ListSourcesResults"
"$ref": "#/definitions/routes.ListSources"
}
},
"400": {
@ -382,7 +361,7 @@
"200": {
"description": "ok",
"schema": {
"$ref": "#/definitions/routes.ListSourcesResults"
"$ref": "#/definitions/routes.ListSources"
}
},
"400": {
@ -425,7 +404,32 @@
"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": {
@ -518,7 +522,7 @@
"200": {
"description": "ok",
"schema": {
"$ref": "#/definitions/routes.GetSourceResult"
"$ref": "#/definitions/routes.GetSource"
}
},
"204": {
@ -1018,7 +1022,7 @@
}
}
},
"routes.GetSourceResult": {
"routes.GetSource": {
"type": "object",
"properties": {
"message": {
@ -1066,7 +1070,7 @@
}
}
},
"routes.ListSourcesResults": {
"routes.ListSources": {
"type": "object",
"properties": {
"message": {

View File

@ -170,7 +170,7 @@ definitions:
status:
type: integer
type: object
routes.GetSourceResult:
routes.GetSource:
properties:
message:
type: string
@ -201,7 +201,7 @@ definitions:
status:
type: integer
type: object
routes.ListSourcesResults:
routes.ListSources:
properties:
message:
type: string
@ -444,20 +444,6 @@ paths:
summary: Returns the top 100 entries from the queue to be processed.
tags:
- 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:
get:
produces:
@ -466,7 +452,7 @@ paths:
"200":
description: ok
schema:
$ref: '#/definitions/routes.ListSourcesResults'
$ref: '#/definitions/routes.ListSources'
"400":
description: Unable to reach SQL or Data problems
schema:
@ -488,7 +474,7 @@ paths:
"200":
description: ok
schema:
$ref: '#/definitions/routes.GetSourceResult'
$ref: '#/definitions/routes.GetSource'
"204":
description: No record found.
schema:
@ -553,7 +539,7 @@ paths:
"200":
description: ok
schema:
$ref: '#/definitions/routes.ListSourcesResults'
$ref: '#/definitions/routes.ListSources'
"400":
description: Unable to query SQL.
schema:
@ -580,7 +566,23 @@ paths:
type: string
produces:
- 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
tags:
- 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 {
return models.ArticleDetailsDto{
ID: i.ID,
Source: c.ConvertToSourceDto(s),
Source: c.ConvertToSource(s),
Tags: c.SplitTags(i.Tags),
Title: i.Title,
Url: i.Url,

View File

@ -1,11 +1,70 @@
package dto
import (
"context"
"strings"
"github.com/google/uuid"
"github.com/jtom38/newsbot/collector/database"
"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
if !i.Deleted.Valid {
deleted = true

View File

@ -82,7 +82,7 @@ func (s *Server) MountRoutes() {
s.Router.Mount("/api/queue", s.GetQueueRouter())
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/subscriptions", s.GetSubscriptionsRouter())

View File

@ -8,13 +8,15 @@ import (
"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) {
// 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")
//var item model.Sources

View File

@ -1,10 +1,8 @@
package routes
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
@ -35,12 +33,12 @@ func (s *Server) GetSourcesRouter() http.Handler {
return r
}
type ListSourcesResults struct {
type ListSources struct {
ApiStatusModel
Payload []models.SourceDto `json:"payload"`
}
type GetSourceResult struct {
type GetSource struct {
ApiStatusModel
Payload models.SourceDto `json:"payload"`
}
@ -50,8 +48,8 @@ type GetSourceResult struct {
// @Produce application/json
// @Tags Source
// @Router /sources [get]
// @Success 200 {object} ListSourcesResults "ok"
// @Failure 400 {object} ApiError "Unable to reach SQL or Data problems"
// @Success 200 {object} ListSources "ok"
// @Failure 400 {object} ApiError "Unable to reach SQL or Data problems"
func (s *Server) listSources(w http.ResponseWriter, r *http.Request) {
//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))
*/
w.Header().Set("Content-Type", "application/json")
result := ListSourcesResults{
w.Header().Set(HeaderContentType, ApplicationJson)
result := ListSources{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
Message: "OK",
@ -72,18 +70,13 @@ func (s *Server) listSources(w http.ResponseWriter, r *http.Request) {
}
// 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 {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
var dto []models.SourceDto
for _, item := range res {
dto = append(dto, models.ConvertToSourceDto(item))
}
result.Payload = dto
result.Payload = items
bResult, err := json.Marshal(result)
if err != nil {
@ -100,9 +93,9 @@ func (s *Server) listSources(w http.ResponseWriter, r *http.Request) {
// @Produce application/json
// @Tags Source
// @Router /sources/by/source [get]
// @Success 200 {object} ListSourcesResults "ok"
// @Failure 400 {object} ApiError "Unable to query SQL."
// @Failure 500 {object} ApiError "Problems with data."
// @Success 200 {object} ListSources "ok"
// @Failure 400 {object} ApiError "Unable to query SQL."
// @Failure 500 {object} ApiError "Problems with data."
func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) {
//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))
*/
w.Header().Set("Content-Type", "application/json")
w.Header().Set(HeaderContentType, ApplicationJson)
result := ListSourcesResults{
result := ListSources{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
Message: "OK",
@ -126,15 +119,13 @@ func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) {
_source := query["source"][0]
// 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 {
s.WriteError(w, err.Error(), http.StatusBadRequest)
return
}
for _, item := range res {
result.Payload = append(result.Payload, models.ConvertToSourceDto(item))
}
result.Payload = res
bResult, err := json.Marshal(result)
if err != nil {
@ -151,12 +142,19 @@ func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) {
// @Produce application/json
// @Tags Source
// @Router /sources/{id} [get]
// @Success 200 {object} GetSourceResult "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."
// @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) 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")
uuid, err := uuid.Parse(id)
@ -165,21 +163,13 @@ func (s *Server) getSources(w http.ResponseWriter, r *http.Request) {
return
}
res, err := s.Db.GetSourceByID(*s.ctx, uuid)
res, err := s.dto.GetSourceById(r.Context(), uuid)
if err != nil {
s.WriteError(w, err.Error(), http.StatusNoContent)
return
}
dto := models.ConvertToSourceDto(res)
payload := GetSourceResult{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
},
Payload: dto,
}
payload.Payload = res
bResult, err := json.Marshal(payload)
if err != nil {
@ -197,35 +187,46 @@ func (s *Server) getSources(w http.ResponseWriter, r *http.Request) {
// @Produce application/json
// @Tags Source
// @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) {
query := r.URL.Query()
p := GetSource{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
},
}
query := r.URL.Query()
name := query["name"][0]
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
}
source := query["source"][0]
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
}
item, err := s.Db.GetSourceByNameAndSource(context.Background(), database.GetSourceByNameAndSourceParams{
Name: name,
Source: source,
})
item, err := s.dto.GetSourceByNameAndSource(r.Context(), name, source)
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)
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)
}
@ -409,8 +410,8 @@ func (s *Server) deleteSources(w http.ResponseWriter, r *http.Request) {
return
}
p := ApiStatusModel {
Message: "OK",
p := ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
}
@ -446,8 +447,8 @@ func (s *Server) disableSource(w http.ResponseWriter, r *http.Request) {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
}
p := ApiStatusModel {
Message: "OK",
p := ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
}
@ -483,8 +484,8 @@ func (s *Server) enableSource(w http.ResponseWriter, r *http.Request) {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
}
p := ApiStatusModel {
Message: "OK",
p := ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
}