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
This commit is contained in:
James Tombleson 2023-01-31 12:39:54 -08:00 committed by GitHub
parent 90e739a56e
commit 8704680e82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 58 additions and 59 deletions

View File

@ -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),

View File

@ -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,

View File

@ -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,

View File

@ -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

View File

@ -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,

View File

@ -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])

View File

@ -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(&params)
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)
}

View File

@ -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,

View File

@ -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

View File

@ -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)
}

View File

@ -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