From a2147294d5d9a9e16796d862a3814fc5b1546d21 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Wed, 7 Dec 2022 22:48:31 -0800 Subject: [PATCH] updated db, added dto for ListSources, and added delete source (#35) --- database/dto.go | 45 +++++++++++++++++++ .../20221207213427_source_delete.sql | 11 +++++ database/models.go | 1 + database/query.sql.go | 21 ++++++--- database/schema/query.sql | 2 +- database/schema/schema.sql | 3 +- docs/docs.go | 5 +-- docs/swagger.json | 5 +-- docs/swagger.yaml | 23 +++++----- routes/server.go | 5 +-- routes/sources.go | 14 ++++-- 11 files changed, 100 insertions(+), 35 deletions(-) create mode 100644 database/dto.go create mode 100644 database/migrations/20221207213427_source_delete.sql diff --git a/database/dto.go b/database/dto.go new file mode 100644 index 0000000..7b4427e --- /dev/null +++ b/database/dto.go @@ -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 +} diff --git a/database/migrations/20221207213427_source_delete.sql b/database/migrations/20221207213427_source_delete.sql new file mode 100644 index 0000000..3130bda --- /dev/null +++ b/database/migrations/20221207213427_source_delete.sql @@ -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 diff --git a/database/models.go b/database/models.go index 34c3a97..6624941 100644 --- a/database/models.go +++ b/database/models.go @@ -63,6 +63,7 @@ type Source struct { Enabled bool Url string Tags string + Deleted sql.NullBool } type Subscription struct { diff --git a/database/query.sql.go b/database/query.sql.go index 1a0cd0d..390e13e 100644 --- a/database/query.sql.go +++ b/database/query.sql.go @@ -244,7 +244,7 @@ func (q *Queries) DeleteSetting(ctx context.Context, id uuid.UUID) error { } 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 { @@ -351,7 +351,7 @@ func (q *Queries) GetArticleByUrl(ctx context.Context, url string) (Article, err } 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 where site = $1 ` @@ -379,6 +379,7 @@ type GetArticlesBySourceRow struct { Enabled bool Url_2 string Tags_2 string + Deleted sql.NullBool } 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.Url_2, &i.Tags_2, + &i.Deleted, ); err != nil { return nil, err } @@ -740,7 +742,7 @@ func (q *Queries) GetSettingByValue(ctx context.Context, value string) (Setting, } 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) { @@ -756,12 +758,13 @@ func (q *Queries) GetSourceByID(ctx context.Context, id uuid.UUID) (Source, erro &i.Enabled, &i.Url, &i.Tags, + &i.Deleted, ) return i, err } 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) { @@ -777,12 +780,13 @@ func (q *Queries) GetSourceByName(ctx context.Context, name string) (Source, err &i.Enabled, &i.Url, &i.Tags, + &i.Deleted, ) return i, err } 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 { @@ -803,6 +807,7 @@ func (q *Queries) GetSourceByNameAndSource(ctx context.Context, arg GetSourceByN &i.Enabled, &i.Url, &i.Tags, + &i.Deleted, ) return i, err } @@ -1038,7 +1043,7 @@ func (q *Queries) ListDiscordWebhooks(ctx context.Context, limit int32) ([]Disco } 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) { @@ -1060,6 +1065,7 @@ func (q *Queries) ListSources(ctx context.Context, limit int32) ([]Source, error &i.Enabled, &i.Url, &i.Tags, + &i.Deleted, ); err != nil { return nil, err } @@ -1075,7 +1081,7 @@ func (q *Queries) ListSources(ctx context.Context, limit int32) ([]Source, error } 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) { @@ -1097,6 +1103,7 @@ func (q *Queries) ListSourcesBySource(ctx context.Context, source string) ([]Sou &i.Enabled, &i.Url, &i.Tags, + &i.Deleted, ); err != nil { return nil, err } diff --git a/database/schema/query.sql b/database/schema/query.sql index bff9dbd..4c28e94 100644 --- a/database/schema/query.sql +++ b/database/schema/query.sql @@ -160,7 +160,7 @@ Select * From Sources Limit $1; Select * From Sources where Source = $1; -- name: DeleteSource :exec -DELETE From sources where id = $1; +UPDATE Sources Set Disabled = TRUE where id = $1; -- name: DisableSource :exec Update Sources Set Enabled = FALSE where ID = $1; diff --git a/database/schema/schema.sql b/database/schema/schema.sql index e65a0f4..280f877 100644 --- a/database/schema/schema.sql +++ b/database/schema/schema.sql @@ -49,7 +49,8 @@ Create Table Sources ( Value TEXT, Enabled BOOLEAN 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 */; diff --git a/docs/docs.go b/docs/docs.go index d8c6d49..0178183 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -250,12 +250,11 @@ const docTemplate = `{ ], "responses": {} }, - "delete": { + "post": { "tags": [ - "Config", "Source" ], - "summary": "Deletes a record by ID.", + "summary": "Marks a source as deleted based on its ID value.", "parameters": [ { "type": "string", diff --git a/docs/swagger.json b/docs/swagger.json index 85c271b..b8b53e9 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -241,12 +241,11 @@ ], "responses": {} }, - "delete": { + "post": { "tags": [ - "Config", "Source" ], - "summary": "Deletes a record by ID.", + "summary": "Marks a source as deleted based on its ID value.", "parameters": [ { "type": "string", diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 753bec9..fe4860b 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -66,18 +66,6 @@ paths: - Config - Source /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: parameters: - description: uuid @@ -92,6 +80,17 @@ paths: tags: - Config - 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: post: parameters: diff --git a/routes/server.go b/routes/server.go index 18057f2..a68082e 100644 --- a/routes/server.go +++ b/routes/server.go @@ -102,11 +102,7 @@ func (s *Server) MountRoutes() { /* Source Routes */ s.Router.Get("/api/config/sources", s.listSources) 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/youtube", s.newYoutubeSource) s.Router.Post("/api/config/sources/new/twitch", s.newTwitchSource) s.Router.Route("/api/config/sources/{ID}", func(r chi.Router) { @@ -114,6 +110,7 @@ func (s *Server) MountRoutes() { r.Delete("/", s.deleteSources) r.Post("/disable", s.disableSource) r.Post("/enable", s.enableSource) + //r.Post("/delete", ) }) s.Router.Get("/api/config/sources/by/sourceAndName", s.GetSourceBySourceAndName) diff --git a/routes/sources.go b/routes/sources.go index 9fa37f8..9586dc3 100644 --- a/routes/sources.go +++ b/routes/sources.go @@ -35,7 +35,13 @@ func (s *Server) listSources(w http.ResponseWriter, r *http.Request) { http.Error(w, "url is missing a value", http.StatusBadRequest) 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 { http.Error(w, "unable to convert to json", http.StatusBadRequest) return @@ -283,10 +289,10 @@ func (s *Server) newTwitchSource(w http.ResponseWriter, r *http.Request) { } // DeleteSource -// @Summary Deletes a record by ID. +// @Summary Marks a source as deleted based on its ID value. // @Param id path string true "id" -// @Tags Config, Source -// @Router /config/sources/{id} [delete] +// @Tags Source +// @Router /config/sources/{id} [POST] func (s *Server) deleteSources(w http.ResponseWriter, r *http.Request) { //var item model.Sources = model.Sources{}