From 8704680e825e23a6c3a98290181c2abcec9459ae Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Tue, 31 Jan 2023 12:39:54 -0800 Subject: [PATCH] Features/route cleanup (#38) * moved route context to the request and not passed down * dtoClient now returns a pointer to help on memory * moved funcs to use the dto pointer --- dto/articles.go | 18 +++++++++--------- dto/discordwebhooks.go | 8 ++++---- dto/queue.go | 6 +++--- dto/sources.go | 10 +++++----- dto/subscriptions.go | 10 +++++----- routes/articles.go | 4 +--- routes/discordwebhooks.go | 16 ++++++++-------- routes/server.go | 17 +++++++++-------- routes/settings.go | 2 +- routes/sources.go | 22 +++++++++++----------- routes/subscriptions.go | 4 ++-- 11 files changed, 58 insertions(+), 59 deletions(-) diff --git a/dto/articles.go b/dto/articles.go index bc37498..fd8cc20 100644 --- a/dto/articles.go +++ b/dto/articles.go @@ -15,13 +15,13 @@ type DtoClient struct { db *database.Queries } -func NewDtoClient(db *database.Queries) DtoClient { - return DtoClient{ +func NewDtoClient(db *database.Queries) *DtoClient { + return &DtoClient{ db: db, } } -func (c DtoClient) ListArticles(ctx context.Context, limit int) ([]models.ArticleDto, error) { +func (c *DtoClient) ListArticles(ctx context.Context, limit int) ([]models.ArticleDto, error) { var res []models.ArticleDto a, err := c.db.ListArticles(ctx, int32(limit)) @@ -35,7 +35,7 @@ func (c DtoClient) ListArticles(ctx context.Context, limit int) ([]models.Articl return res, nil } -func (c DtoClient) ListArticlesByPage(ctx context.Context, page, limit int32 ) ([]models.ArticleDto, error) { +func (c *DtoClient) ListArticlesByPage(ctx context.Context, page, limit int32 ) ([]models.ArticleDto, error) { var res []models.ArticleDto a, err := c.db.ListArticlesByPage(ctx, database.ListArticlesByPageParams{ @@ -53,7 +53,7 @@ func (c DtoClient) ListArticlesByPage(ctx context.Context, page, limit int32 ) ( return res, nil } -func (c DtoClient) GetArticle(ctx context.Context, ID uuid.UUID) (models.ArticleDto, error) { +func (c *DtoClient) GetArticle(ctx context.Context, ID uuid.UUID) (models.ArticleDto, error) { a, err := c.db.GetArticleByID(ctx, ID) if err != nil { return models.ArticleDto{}, err @@ -62,7 +62,7 @@ func (c DtoClient) GetArticle(ctx context.Context, ID uuid.UUID) (models.Article return c.convertArticle(a), nil } -func (c DtoClient) GetArticleDetails(ctx context.Context, ID uuid.UUID) (models.ArticleDetailsDto, error) { +func (c *DtoClient) GetArticleDetails(ctx context.Context, ID uuid.UUID) (models.ArticleDetailsDto, error) { a, err := c.db.GetArticleByID(ctx, ID) if err != nil { return models.ArticleDetailsDto{}, err @@ -78,7 +78,7 @@ func (c DtoClient) GetArticleDetails(ctx context.Context, ID uuid.UUID) (models. return res, nil } -func (c DtoClient) GetArticlesBySourceId(ctx context.Context, SourceID uuid.UUID) ([]models.ArticleDto, error) { +func (c *DtoClient) GetArticlesBySourceId(ctx context.Context, SourceID uuid.UUID) ([]models.ArticleDto, error) { var res []models.ArticleDto a, err := c.db.GetArticlesBySourceId(ctx, SourceID) if err != nil { @@ -92,7 +92,7 @@ func (c DtoClient) GetArticlesBySourceId(ctx context.Context, SourceID uuid.UUID return res, nil } -func (c DtoClient) convertArticle(i database.Article) models.ArticleDto { +func (c *DtoClient) convertArticle(i database.Article) models.ArticleDto { return models.ArticleDto{ ID: i.ID, Source: i.Sourceid, @@ -110,7 +110,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{ ID: i.ID, Source: c.ConvertToSource(s), diff --git a/dto/discordwebhooks.go b/dto/discordwebhooks.go index 5ddac2a..5e20df1 100644 --- a/dto/discordwebhooks.go +++ b/dto/discordwebhooks.go @@ -8,7 +8,7 @@ import ( "github.com/jtom38/newsbot/collector/domain/models" ) -func (c DtoClient) ListDiscordWebHooks(ctx context.Context, total int32) ([]models.DiscordWebHooksDto, error) { +func (c *DtoClient) ListDiscordWebHooks(ctx context.Context, total int32) ([]models.DiscordWebHooksDto, error) { var res []models.DiscordWebHooksDto items, err := c.db.ListDiscordWebhooks(ctx, total) @@ -23,7 +23,7 @@ func (c DtoClient) ListDiscordWebHooks(ctx context.Context, total int32) ([]mode return res, nil } -func (c DtoClient) GetDiscordWebhook(ctx context.Context, id uuid.UUID) (models.DiscordWebHooksDto, error) { +func (c *DtoClient) GetDiscordWebhook(ctx context.Context, id uuid.UUID) (models.DiscordWebHooksDto, error) { var res models.DiscordWebHooksDto item, err := c.db.GetDiscordWebHooksByID(ctx, id) @@ -34,7 +34,7 @@ func (c DtoClient) GetDiscordWebhook(ctx context.Context, id uuid.UUID) (models. return c.ConvertDiscordWebhook(item), nil } -func (c DtoClient) GetDiscordWebHookByServerAndChannel(ctx context.Context, server, channel string) ([]models.DiscordWebHooksDto, error) { +func (c *DtoClient) GetDiscordWebHookByServerAndChannel(ctx context.Context, server, channel string) ([]models.DiscordWebHooksDto, error) { var res []models.DiscordWebHooksDto items, err := c.db.GetDiscordWebHooksByServerAndChannel(ctx, database.GetDiscordWebHooksByServerAndChannelParams{ @@ -52,7 +52,7 @@ func (c DtoClient) GetDiscordWebHookByServerAndChannel(ctx context.Context, serv return res, nil } -func (c DtoClient) ConvertDiscordWebhook(i database.Discordwebhook) models.DiscordWebHooksDto { +func (c *DtoClient) ConvertDiscordWebhook(i database.Discordwebhook) models.DiscordWebHooksDto { return models.DiscordWebHooksDto{ ID: i.ID, Url: i.Url, diff --git a/dto/queue.go b/dto/queue.go index d5697f2..fe2724e 100644 --- a/dto/queue.go +++ b/dto/queue.go @@ -7,11 +7,11 @@ import ( "github.com/jtom38/newsbot/collector/domain/models" ) -func (c DtoClient) ListDiscordWebhookQueue(ctx context.Context, limit int32) { +func (c *DtoClient) ListDiscordWebhookQueue(ctx context.Context, limit int32) { } -func (c DtoClient) ListDiscordWebhookQueueDetails(ctx context.Context, limit int32) ([]models.DiscordQueueDetailsDto, error) { +func (c *DtoClient) ListDiscordWebhookQueueDetails(ctx context.Context, limit int32) ([]models.DiscordQueueDetailsDto, error) { var res []models.DiscordQueueDetailsDto items, err := c.db.ListDiscordQueueItems(ctx, limit) @@ -34,7 +34,7 @@ func (c DtoClient) ListDiscordWebhookQueueDetails(ctx context.Context, limit int return res, nil } -func (c DtoClient) ConvertToDiscordQueueDto(i database.Discordqueue) models.DiscordQueueDto { +func (c *DtoClient) ConvertToDiscordQueueDto(i database.Discordqueue) models.DiscordQueueDto { return models.DiscordQueueDto{ ID: i.ID, Articleid: i.Articleid, diff --git a/dto/sources.go b/dto/sources.go index 35403a8..baab2d1 100644 --- a/dto/sources.go +++ b/dto/sources.go @@ -9,7 +9,7 @@ import ( "github.com/jtom38/newsbot/collector/domain/models" ) -func (c DtoClient) ListSources(ctx context.Context, limit int32) ([]models.SourceDto, error) { +func (c *DtoClient) ListSources(ctx context.Context, limit int32) ([]models.SourceDto, error) { var res []models.SourceDto items, err := c.db.ListSources(ctx, limit) @@ -24,7 +24,7 @@ func (c DtoClient) ListSources(ctx context.Context, limit int32) ([]models.Sourc return res, nil } -func (c DtoClient) ListSourcesBySource(ctx context.Context, sourceName string) ([]models.SourceDto, error) { +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)) @@ -39,7 +39,7 @@ func (c DtoClient) ListSourcesBySource(ctx context.Context, sourceName string) ( return res, nil } -func (c DtoClient) GetSourceById(ctx context.Context, id uuid.UUID) (models.SourceDto, error) { +func (c *DtoClient) GetSourceById(ctx context.Context, id uuid.UUID) (models.SourceDto, error) { var res models.SourceDto item, err := c.db.GetSourceByID(ctx, id) @@ -50,7 +50,7 @@ func (c DtoClient) GetSourceById(ctx context.Context, id uuid.UUID) (models.Sour return c.ConvertToSource(item), nil } -func (c DtoClient) GetSourceByNameAndSource(ctx context.Context, name, source string) (models.SourceDto, error) { +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{ @@ -64,7 +64,7 @@ func (c DtoClient) GetSourceByNameAndSource(ctx context.Context, name, source st return c.ConvertToSource(item), nil } -func (c DtoClient) ConvertToSource(i database.Source) models.SourceDto { +func (c *DtoClient) ConvertToSource(i database.Source) models.SourceDto { var deleted bool if !i.Deleted.Valid { deleted = true diff --git a/dto/subscriptions.go b/dto/subscriptions.go index 7a08ae6..522b7ac 100644 --- a/dto/subscriptions.go +++ b/dto/subscriptions.go @@ -8,7 +8,7 @@ import ( "github.com/jtom38/newsbot/collector/domain/models" ) -func (c DtoClient) ListSubscriptions(ctx context.Context, limit int32) ([]models.SubscriptionDto, error) { +func (c *DtoClient) ListSubscriptions(ctx context.Context, limit int32) ([]models.SubscriptionDto, error) { var res []models.SubscriptionDto items, err := c.db.ListSubscriptions(ctx, limit) @@ -23,7 +23,7 @@ func (c DtoClient) ListSubscriptions(ctx context.Context, limit int32) ([]models return res, nil } -func (c DtoClient) ListSubscriptionDetails(ctx context.Context, limit int32) ([]models.SubscriptionDetailsDto, error) { +func (c *DtoClient) ListSubscriptionDetails(ctx context.Context, limit int32) ([]models.SubscriptionDetailsDto, error) { var res []models.SubscriptionDetailsDto items, err := c.ListSubscriptions(ctx, limit) @@ -52,7 +52,7 @@ func (c DtoClient) ListSubscriptionDetails(ctx context.Context, limit int32) ([] return res, nil } -func (c DtoClient) ListSubscriptionsByDiscordWebhookId(ctx context.Context, id uuid.UUID) ([]models.SubscriptionDto, error) { +func (c *DtoClient) ListSubscriptionsByDiscordWebhookId(ctx context.Context, id uuid.UUID) ([]models.SubscriptionDto, error) { var res []models.SubscriptionDto items, err := c.db.GetSubscriptionsByDiscordWebHookId(ctx, id) @@ -67,7 +67,7 @@ func (c DtoClient) ListSubscriptionsByDiscordWebhookId(ctx context.Context, id u return res, nil } -func (c DtoClient) ListSubscriptionsBySourceId(ctx context.Context, id uuid.UUID) ([]models.SubscriptionDto, error) { +func (c *DtoClient) ListSubscriptionsBySourceId(ctx context.Context, id uuid.UUID) ([]models.SubscriptionDto, error) { var res []models.SubscriptionDto items, err := c.db.GetSubscriptionsBySourceID(ctx, id) @@ -82,7 +82,7 @@ func (c DtoClient) ListSubscriptionsBySourceId(ctx context.Context, id uuid.UUID return res, nil } -func (c DtoClient) ConvertSubscription(i database.Subscription) models.SubscriptionDto { +func (c *DtoClient) ConvertSubscription(i database.Subscription) models.SubscriptionDto { return models.SubscriptionDto{ ID: i.ID, DiscordWebhookId: i.Discordwebhookid, diff --git a/routes/articles.go b/routes/articles.go index 1773d2b..0282715 100644 --- a/routes/articles.go +++ b/routes/articles.go @@ -1,7 +1,6 @@ package routes import ( - "fmt" "net/http" "strconv" @@ -55,8 +54,7 @@ func (s *Server) listArticles(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() queryPage := query["page"] - fmt.Printf("queryPage: %v\n", queryPage) - + // if a page number was sent, process it if len(queryPage) == 1 { page, err := strconv.Atoi(query["page"][0]) diff --git a/routes/discordwebhooks.go b/routes/discordwebhooks.go index 97591ce..cc86cfd 100644 --- a/routes/discordwebhooks.go +++ b/routes/discordwebhooks.go @@ -169,7 +169,7 @@ func (s *Server) NewDiscordWebHook(w http.ResponseWriter, r *http.Request) { Channel: _channel, Enabled: true, } - s.Db.CreateDiscordWebHook(*s.ctx, params) + s.Db.CreateDiscordWebHook(r.Context(), params) bJson, err := json.Marshal(¶ms) if err != nil { @@ -193,13 +193,13 @@ func (s *Server) disableDiscordWebHook(w http.ResponseWriter, r *http.Request) { } // Check to make sure we can find the record - _, err = s.Db.GetDiscordWebHooksByID(*s.ctx, uuid) + _, err = s.Db.GetDiscordWebHooksByID(r.Context(), uuid) if err != nil { s.WriteError(w, err.Error(), http.StatusBadRequest) return } - err = s.Db.DisableDiscordWebHook(*s.ctx, uuid) + err = s.Db.DisableDiscordWebHook(r.Context(), uuid) if err != nil { s.WriteError(w, err.Error(), http.StatusInternalServerError) } @@ -218,12 +218,12 @@ func (s *Server) enableDiscordWebHook(w http.ResponseWriter, r *http.Request) { } // Check to make sure we can find the record - _, err = s.Db.GetDiscordWebHooksByID(*s.ctx, uuid) + _, err = s.Db.GetDiscordWebHooksByID(r.Context(), uuid) if err != nil { s.WriteError(w, err.Error(), http.StatusBadRequest) } - err = s.Db.EnableDiscordWebHook(*s.ctx, uuid) + err = s.Db.EnableDiscordWebHook(r.Context(), uuid) if err != nil { s.WriteError(w, err.Error(), http.StatusInternalServerError) } @@ -244,13 +244,13 @@ func (s *Server) deleteDiscordWebHook(w http.ResponseWriter, r *http.Request) { } // Check to make sure we can find the record - _, err = s.Db.GetDiscordQueueByID(*s.ctx, uuid) + _, err = s.Db.GetDiscordQueueByID(r.Context(), uuid) if err != nil { s.WriteError(w, err.Error(), http.StatusBadRequest) } // Delete the record - err = s.Db.DeleteDiscordWebHooks(*s.ctx, uuid) + err = s.Db.DeleteDiscordWebHooks(r.Context(), uuid) if err != nil { s.WriteError(w, err.Error(), http.StatusInternalServerError) } @@ -270,7 +270,7 @@ func (s *Server) UpdateDiscordWebHook(w http.ResponseWriter, r *http.Request) { } // Check to make sure we can find the record - _, err = s.Db.GetDiscordQueueByID(*s.ctx, uuid) + _, err = s.Db.GetDiscordQueueByID(r.Context(), uuid) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) } diff --git a/routes/server.go b/routes/server.go index 952a457..4f30e80 100644 --- a/routes/server.go +++ b/routes/server.go @@ -19,8 +19,8 @@ import ( type Server struct { Router *chi.Mux Db *database.Queries - dto dto.DtoClient - ctx *context.Context + dto *dto.DtoClient + //ctx *context.Context } const ( @@ -38,16 +38,16 @@ var ( func NewServer(ctx context.Context, db *database.Queries) *Server { s := &Server{ - ctx: &ctx, + //ctx: &ctx, Db: db, dto: dto.NewDtoClient(db), } - //db, err := openDatabase(ctx) - //if err != nil { - // panic(err) - //} - //s.Db = db + db, err := openDatabase(ctx) + if err != nil { + panic(err) + } + s.Db = db s.Router = chi.NewRouter() s.MountMiddleware() @@ -98,6 +98,7 @@ type ApiError struct { } func (s *Server) WriteError(w http.ResponseWriter, errMessage string, HttpStatusCode int) { + w.Header().Set(HeaderContentType, ApplicationJson) e := ApiError{ ApiStatusModel: &ApiStatusModel{ StatusCode: HttpStatusCode, diff --git a/routes/settings.go b/routes/settings.go index bb6aad2..8ea6e3d 100644 --- a/routes/settings.go +++ b/routes/settings.go @@ -28,7 +28,7 @@ func (s *Server) getSettings(w http.ResponseWriter, r *http.Request) { return } - res, err := s.Db.GetSourceByID(*s.ctx, uuid) + res, err := s.Db.GetSourceByID(r.Context(), uuid) if err != nil { s.WriteError(w, err.Error(), http.StatusNotFound) return diff --git a/routes/sources.go b/routes/sources.go index d0ba5f4..28c8031 100644 --- a/routes/sources.go +++ b/routes/sources.go @@ -58,7 +58,7 @@ func (s *Server) listSources(w http.ResponseWriter, r *http.Request) { if err != nil { panic(err) } - res, err := s.Db.ListSources(*s.ctx, int32(topInt)) + res, err := s.Db.ListSources(r.Context(), int32(topInt)) */ p := ListSources{ @@ -96,7 +96,7 @@ func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) { if err != nil { panic(err) } - res, err := s.Db.ListSources(*s.ctx, int32(topInt)) + res, err := s.Db.ListSources(r.Context(), int32(topInt)) */ p := ListSources{ @@ -235,7 +235,7 @@ func (s *Server) newRedditSource(w http.ResponseWriter, r *http.Request) { Url: _url, Tags: tags, } - err := s.Db.CreateSource(*s.ctx, params) + err := s.Db.CreateSource(r.Context(), params) if err != nil { s.WriteError(w, err.Error(), http.StatusInternalServerError) return @@ -291,7 +291,7 @@ func (s *Server) newYoutubeSource(w http.ResponseWriter, r *http.Request) { Url: _url, Tags: tags, } - err := s.Db.CreateSource(*s.ctx, params) + err := s.Db.CreateSource(r.Context(), params) if err != nil { s.WriteError(w, err.Error(), http.StatusInternalServerError) return @@ -330,7 +330,7 @@ func (s *Server) newTwitchSource(w http.ResponseWriter, r *http.Request) { Url: _url, Tags: tags, } - err := s.Db.CreateSource(*s.ctx, params) + err := s.Db.CreateSource(r.Context(), params) if err != nil { s.WriteError(w, err.Error(), http.StatusInternalServerError) return @@ -361,14 +361,14 @@ func (s *Server) deleteSources(w http.ResponseWriter, r *http.Request) { } // Check to make sure we can find the record - _, err = s.Db.GetSourceByID(*s.ctx, uuid) + _, err = s.Db.GetSourceByID(r.Context(), uuid) if err != nil { s.WriteError(w, err.Error(), http.StatusInternalServerError) return } // Delete the record - err = s.Db.DeleteSource(*s.ctx, uuid) + err = s.Db.DeleteSource(r.Context(), uuid) if err != nil { s.WriteError(w, err.Error(), http.StatusInternalServerError) return @@ -401,12 +401,12 @@ func (s *Server) disableSource(w http.ResponseWriter, r *http.Request) { } // Check to make sure we can find the record - _, err = s.Db.GetSourceByID(*s.ctx, uuid) + _, err = s.Db.GetSourceByID(r.Context(), uuid) if err != nil { s.WriteError(w, err.Error(), http.StatusInternalServerError) } - err = s.Db.DisableSource(*s.ctx, uuid) + err = s.Db.DisableSource(r.Context(), uuid) if err != nil { s.WriteError(w, err.Error(), http.StatusInternalServerError) } @@ -438,12 +438,12 @@ func (s *Server) enableSource(w http.ResponseWriter, r *http.Request) { } // Check to make sure we can find the record - _, err = s.Db.GetSourceByID(*s.ctx, uuid) + _, err = s.Db.GetSourceByID(r.Context(), uuid) if err != nil { s.WriteError(w, err.Error(), http.StatusInternalServerError) } - err = s.Db.EnableSource(*s.ctx, uuid) + err = s.Db.EnableSource(r.Context(), uuid) if err != nil { s.WriteError(w, err.Error(), http.StatusInternalServerError) } diff --git a/routes/subscriptions.go b/routes/subscriptions.go index 3d1deeb..3b51c46 100644 --- a/routes/subscriptions.go +++ b/routes/subscriptions.go @@ -201,7 +201,7 @@ func (s *Server) newDiscordWebHookSubscription(w http.ResponseWriter, r *http.Re } // Check if the sub already exists - _, err = s.Db.QuerySubscriptions(*s.ctx, database.QuerySubscriptionsParams{ + _, err = s.Db.QuerySubscriptions(r.Context(), database.QuerySubscriptionsParams{ Discordwebhookid: uHook, Sourceid: uSource, }) @@ -216,7 +216,7 @@ func (s *Server) newDiscordWebHookSubscription(w http.ResponseWriter, r *http.Re Discordwebhookid: uHook, Sourceid: uSource, } - err = s.Db.CreateSubscription(*s.ctx, params) + err = s.Db.CreateSubscription(r.Context(), params) if err != nil { s.WriteError(w, err.Error(), http.StatusInternalServerError) return