updated db, added dto for ListSources, and added delete source

This commit is contained in:
James Tombleson 2022-12-07 22:47:10 -08:00
parent ff4075383a
commit bbcfeb8a8a
11 changed files with 100 additions and 35 deletions

45
database/dto.go Normal file
View File

@ -0,0 +1,45 @@
package database
import (
"strings"
"github.com/google/uuid"
)
type SourceDto struct {
ID uuid.UUID `json:"id"`
Site string `json:"site"`
Name string `json:"name"`
Source string `json:"source"`
Type string `json:"type"`
Value string `json:"value"`
Enabled bool `json:"enabled"`
Url string `json:"url"`
Tags []string `json:"tags"`
Deleted bool `json:"deleted"`
}
func ConvertToSourceDto(i Source) SourceDto {
var deleted bool
if !i.Deleted.Valid {
deleted = true
}
return SourceDto{
ID: i.ID,
Site: i.Site,
Name: i.Name,
Source: i.Source,
Type: i.Type,
Value: i.Value.String,
Enabled: i.Enabled,
Url: i.Url,
Tags: splitTags(i.Tags),
Deleted: deleted,
}
}
func splitTags(t string) []string {
items := strings.Split(t, ", ")
return items
}

View File

@ -0,0 +1,11 @@
-- +goose Up
-- +goose StatementBegin
SELECT 'up SQL query';
ALTER TABLE sources Add COLUMN Deleted BOOLEAN;
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
SELECT 'down SQL query';
ALTER TABLE sources Drop Deleted Deleted BOOLEAN;
-- +goose StatementEnd

View File

@ -63,6 +63,7 @@ type Source struct {
Enabled bool Enabled bool
Url string Url string
Tags string Tags string
Deleted sql.NullBool
} }
type Subscription struct { type Subscription struct {

View File

@ -244,7 +244,7 @@ func (q *Queries) DeleteSetting(ctx context.Context, id uuid.UUID) error {
} }
const deleteSource = `-- name: DeleteSource :exec const deleteSource = `-- name: DeleteSource :exec
DELETE From sources where id = $1 UPDATE Sources Set Disabled = TRUE where id = $1
` `
func (q *Queries) DeleteSource(ctx context.Context, id uuid.UUID) error { func (q *Queries) DeleteSource(ctx context.Context, id uuid.UUID) error {
@ -351,7 +351,7 @@ func (q *Queries) GetArticleByUrl(ctx context.Context, url string) (Article, err
} }
const getArticlesBySource = `-- name: GetArticlesBySource :many const getArticlesBySource = `-- name: GetArticlesBySource :many
select articles.id, sourceid, articles.tags, title, articles.url, pubdate, video, videoheight, videowidth, thumbnail, description, authorname, authorimage, sources.id, site, name, source, type, value, enabled, sources.url, sources.tags from articles select articles.id, sourceid, articles.tags, title, articles.url, pubdate, video, videoheight, videowidth, thumbnail, description, authorname, authorimage, sources.id, site, name, source, type, value, enabled, sources.url, sources.tags, deleted from articles
INNER join sources on articles.sourceid=Sources.ID INNER join sources on articles.sourceid=Sources.ID
where site = $1 where site = $1
` `
@ -379,6 +379,7 @@ type GetArticlesBySourceRow struct {
Enabled bool Enabled bool
Url_2 string Url_2 string
Tags_2 string Tags_2 string
Deleted sql.NullBool
} }
func (q *Queries) GetArticlesBySource(ctx context.Context, site string) ([]GetArticlesBySourceRow, error) { func (q *Queries) GetArticlesBySource(ctx context.Context, site string) ([]GetArticlesBySourceRow, error) {
@ -413,6 +414,7 @@ func (q *Queries) GetArticlesBySource(ctx context.Context, site string) ([]GetAr
&i.Enabled, &i.Enabled,
&i.Url_2, &i.Url_2,
&i.Tags_2, &i.Tags_2,
&i.Deleted,
); err != nil { ); err != nil {
return nil, err return nil, err
} }
@ -740,7 +742,7 @@ func (q *Queries) GetSettingByValue(ctx context.Context, value string) (Setting,
} }
const getSourceByID = `-- name: GetSourceByID :one const getSourceByID = `-- name: GetSourceByID :one
Select id, site, name, source, type, value, enabled, url, tags From Sources where ID = $1 Limit 1 Select id, site, name, source, type, value, enabled, url, tags, deleted From Sources where ID = $1 Limit 1
` `
func (q *Queries) GetSourceByID(ctx context.Context, id uuid.UUID) (Source, error) { func (q *Queries) GetSourceByID(ctx context.Context, id uuid.UUID) (Source, error) {
@ -756,12 +758,13 @@ func (q *Queries) GetSourceByID(ctx context.Context, id uuid.UUID) (Source, erro
&i.Enabled, &i.Enabled,
&i.Url, &i.Url,
&i.Tags, &i.Tags,
&i.Deleted,
) )
return i, err return i, err
} }
const getSourceByName = `-- name: GetSourceByName :one const getSourceByName = `-- name: GetSourceByName :one
Select id, site, name, source, type, value, enabled, url, tags from Sources where name = $1 Limit 1 Select id, site, name, source, type, value, enabled, url, tags, deleted from Sources where name = $1 Limit 1
` `
func (q *Queries) GetSourceByName(ctx context.Context, name string) (Source, error) { func (q *Queries) GetSourceByName(ctx context.Context, name string) (Source, error) {
@ -777,12 +780,13 @@ func (q *Queries) GetSourceByName(ctx context.Context, name string) (Source, err
&i.Enabled, &i.Enabled,
&i.Url, &i.Url,
&i.Tags, &i.Tags,
&i.Deleted,
) )
return i, err return i, err
} }
const getSourceByNameAndSource = `-- name: GetSourceByNameAndSource :one const getSourceByNameAndSource = `-- name: GetSourceByNameAndSource :one
Select id, site, name, source, type, value, enabled, url, tags from Sources WHERE name = $1 and source = $2 Select id, site, name, source, type, value, enabled, url, tags, deleted from Sources WHERE name = $1 and source = $2
` `
type GetSourceByNameAndSourceParams struct { type GetSourceByNameAndSourceParams struct {
@ -803,6 +807,7 @@ func (q *Queries) GetSourceByNameAndSource(ctx context.Context, arg GetSourceByN
&i.Enabled, &i.Enabled,
&i.Url, &i.Url,
&i.Tags, &i.Tags,
&i.Deleted,
) )
return i, err return i, err
} }
@ -1038,7 +1043,7 @@ func (q *Queries) ListDiscordWebhooks(ctx context.Context, limit int32) ([]Disco
} }
const listSources = `-- name: ListSources :many const listSources = `-- name: ListSources :many
Select id, site, name, source, type, value, enabled, url, tags From Sources Limit $1 Select id, site, name, source, type, value, enabled, url, tags, deleted From Sources Limit $1
` `
func (q *Queries) ListSources(ctx context.Context, limit int32) ([]Source, error) { func (q *Queries) ListSources(ctx context.Context, limit int32) ([]Source, error) {
@ -1060,6 +1065,7 @@ func (q *Queries) ListSources(ctx context.Context, limit int32) ([]Source, error
&i.Enabled, &i.Enabled,
&i.Url, &i.Url,
&i.Tags, &i.Tags,
&i.Deleted,
); err != nil { ); err != nil {
return nil, err return nil, err
} }
@ -1075,7 +1081,7 @@ func (q *Queries) ListSources(ctx context.Context, limit int32) ([]Source, error
} }
const listSourcesBySource = `-- name: ListSourcesBySource :many const listSourcesBySource = `-- name: ListSourcesBySource :many
Select id, site, name, source, type, value, enabled, url, tags From Sources where Source = $1 Select id, site, name, source, type, value, enabled, url, tags, deleted From Sources where Source = $1
` `
func (q *Queries) ListSourcesBySource(ctx context.Context, source string) ([]Source, error) { func (q *Queries) ListSourcesBySource(ctx context.Context, source string) ([]Source, error) {
@ -1097,6 +1103,7 @@ func (q *Queries) ListSourcesBySource(ctx context.Context, source string) ([]Sou
&i.Enabled, &i.Enabled,
&i.Url, &i.Url,
&i.Tags, &i.Tags,
&i.Deleted,
); err != nil { ); err != nil {
return nil, err return nil, err
} }

View File

@ -160,7 +160,7 @@ Select * From Sources Limit $1;
Select * From Sources where Source = $1; Select * From Sources where Source = $1;
-- name: DeleteSource :exec -- name: DeleteSource :exec
DELETE From sources where id = $1; UPDATE Sources Set Disabled = TRUE where id = $1;
-- name: DisableSource :exec -- name: DisableSource :exec
Update Sources Set Enabled = FALSE where ID = $1; Update Sources Set Enabled = FALSE where ID = $1;

View File

@ -49,7 +49,8 @@ Create Table Sources (
Value TEXT, Value TEXT,
Enabled BOOLEAN NOT NULL, Enabled BOOLEAN NOT NULL,
Url TEXT NOT NULL, Url TEXT NOT NULL,
Tags TEXT NOT NULL Tags TEXT NOT NULL,
Deleted BOOLEAN
); );
/* This table is used to track what the Web Hook wants to have sent by Source */; /* This table is used to track what the Web Hook wants to have sent by Source */;

View File

@ -250,12 +250,11 @@ const docTemplate = `{
], ],
"responses": {} "responses": {}
}, },
"delete": { "post": {
"tags": [ "tags": [
"Config",
"Source" "Source"
], ],
"summary": "Deletes a record by ID.", "summary": "Marks a source as deleted based on its ID value.",
"parameters": [ "parameters": [
{ {
"type": "string", "type": "string",

View File

@ -241,12 +241,11 @@
], ],
"responses": {} "responses": {}
}, },
"delete": { "post": {
"tags": [ "tags": [
"Config",
"Source" "Source"
], ],
"summary": "Deletes a record by ID.", "summary": "Marks a source as deleted based on its ID value.",
"parameters": [ "parameters": [
{ {
"type": "string", "type": "string",

View File

@ -66,18 +66,6 @@ paths:
- Config - Config
- Source - Source
/config/sources/{id}: /config/sources/{id}:
delete:
parameters:
- description: id
in: path
name: id
required: true
type: string
responses: {}
summary: Deletes a record by ID.
tags:
- Config
- Source
get: get:
parameters: parameters:
- description: uuid - description: uuid
@ -92,6 +80,17 @@ paths:
tags: tags:
- Config - Config
- Source - Source
post:
parameters:
- description: id
in: path
name: id
required: true
type: string
responses: {}
summary: Marks a source as deleted based on its ID value.
tags:
- Source
/config/sources/{id}/disable: /config/sources/{id}/disable:
post: post:
parameters: parameters:

View File

@ -102,11 +102,7 @@ func (s *Server) MountRoutes() {
/* Source Routes */ /* Source Routes */
s.Router.Get("/api/config/sources", s.listSources) s.Router.Get("/api/config/sources", s.listSources)
s.Router.Get("/api/config/sources/by/source", s.listSourcesBySource) s.Router.Get("/api/config/sources/by/source", s.listSourcesBySource)
/* Reddit Source Routes */
s.Router.Post("/api/config/sources/new/reddit", s.newRedditSource) s.Router.Post("/api/config/sources/new/reddit", s.newRedditSource)
s.Router.Post("/api/config/sources/new/youtube", s.newYoutubeSource) s.Router.Post("/api/config/sources/new/youtube", s.newYoutubeSource)
s.Router.Post("/api/config/sources/new/twitch", s.newTwitchSource) s.Router.Post("/api/config/sources/new/twitch", s.newTwitchSource)
s.Router.Route("/api/config/sources/{ID}", func(r chi.Router) { s.Router.Route("/api/config/sources/{ID}", func(r chi.Router) {
@ -114,6 +110,7 @@ func (s *Server) MountRoutes() {
r.Delete("/", s.deleteSources) r.Delete("/", s.deleteSources)
r.Post("/disable", s.disableSource) r.Post("/disable", s.disableSource)
r.Post("/enable", s.enableSource) r.Post("/enable", s.enableSource)
//r.Post("/delete", )
}) })
s.Router.Get("/api/config/sources/by/sourceAndName", s.GetSourceBySourceAndName) s.Router.Get("/api/config/sources/by/sourceAndName", s.GetSourceBySourceAndName)

View File

@ -35,7 +35,13 @@ func (s *Server) listSources(w http.ResponseWriter, r *http.Request) {
http.Error(w, "url is missing a value", http.StatusBadRequest) http.Error(w, "url is missing a value", http.StatusBadRequest)
return return
} }
bResult, err := json.Marshal(res)
var dto []database.SourceDto
for _, item := range res {
dto = append(dto, database.ConvertToSourceDto(item))
}
bResult, err := json.Marshal(dto)
if err != nil { if err != nil {
http.Error(w, "unable to convert to json", http.StatusBadRequest) http.Error(w, "unable to convert to json", http.StatusBadRequest)
return return
@ -283,10 +289,10 @@ func (s *Server) newTwitchSource(w http.ResponseWriter, r *http.Request) {
} }
// DeleteSource // DeleteSource
// @Summary Deletes a record by ID. // @Summary Marks a source as deleted based on its ID value.
// @Param id path string true "id" // @Param id path string true "id"
// @Tags Config, Source // @Tags Source
// @Router /config/sources/{id} [delete] // @Router /config/sources/{id} [POST]
func (s *Server) deleteSources(w http.ResponseWriter, r *http.Request) { func (s *Server) deleteSources(w http.ResponseWriter, r *http.Request) {
//var item model.Sources = model.Sources{} //var item model.Sources = model.Sources{}