diff --git a/internal/services/dto/articles.go b/internal/services/dto/articles.go deleted file mode 100644 index d144780..0000000 --- a/internal/services/dto/articles.go +++ /dev/null @@ -1,140 +0,0 @@ -// The converter package lives between the database calls and the API calls. -// This way if any new methods like RPC calls are added later, the API does not need to be reworked as much -package dto - -import ( - "context" - "strings" - - "git.jamestombleson.com/jtom38/newsbot-api/internal/database" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain/models" - "github.com/google/uuid" -) - -type DtoClient struct { - db *database.Queries -} - -func NewDtoClient(db *database.Queries) *DtoClient { - return &DtoClient{ - db: db, - } -} - -func (c *DtoClient) ListArticles(ctx context.Context, limit, page int) ([]models.ArticleDto, error) { - var res []models.ArticleDto - - a, err := c.db.ListArticles(ctx, database.ListArticlesParams{ - Limit: int32(limit), - Offset: int32(limit * page), - }) - if err != nil { - return res, err - } - - for _, article := range a { - res = append(res, c.convertArticle(article)) - } - return res, nil -} - -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{ - Limit: limit, - Offset: page * limit, - }) - if err != nil { - return res, err - } - - for _, article := range a { - res = append(res, c.convertArticle(article)) - } - - return res, nil -} - -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 - } - - return c.convertArticle(a), nil -} - -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 - } - - s, err := c.db.GetSourceByID(ctx, a.Sourceid) - if err != nil { - return models.ArticleDetailsDto{}, err - } - - res := c.convertArticleDetails(a, s) - - return res, nil -} - -func (c *DtoClient) ListNewArticlesBySourceId(ctx context.Context, SourceID uuid.UUID, limit, page int) ([]models.ArticleDto, error) { - var res []models.ArticleDto - a, err := c.db.ListNewArticlesBySourceId(ctx, database.ListNewArticlesBySourceIdParams{ - Sourceid: SourceID, - Limit: int32(limit), - Offset: int32(limit * page), - }) - if err != nil { - return res, err - } - - for _, article := range a { - res = append(res, c.convertArticle(article)) - } - - return res, nil -} - -func (c *DtoClient) convertArticle(i database.Article) models.ArticleDto { - return models.ArticleDto{ - ID: i.ID, - Source: i.Sourceid, - Tags: c.SplitTags(i.Tags), - Title: i.Title, - Url: i.Url, - Pubdate: i.Pubdate, - Video: i.Video.String, - Videoheight: i.Videoheight, - Videowidth: i.Videoheight, - Thumbnail: i.Thumbnail, - Description: i.Description, - Authorname: i.Authorname.String, - Authorimage: i.Authorimage.String, - } -} - -func (c *DtoClient) convertArticleDetails(i database.Article, s database.Source) models.ArticleDetailsDto { - return models.ArticleDetailsDto{ - ID: i.ID, - Source: c.ConvertToSource(s), - Tags: c.SplitTags(i.Tags), - Title: i.Title, - Url: i.Url, - Pubdate: i.Pubdate, - Video: i.Video.String, - Videoheight: i.Videoheight, - Videowidth: i.Videoheight, - Thumbnail: i.Thumbnail, - Description: i.Description, - Authorname: i.Authorname.String, - Authorimage: i.Authorimage.String, - } -} - -func (c DtoClient) SplitTags(t string) []string { - return strings.Split(t, ", ") -} diff --git a/internal/services/dto/discordwebhooks.go b/internal/services/dto/discordwebhooks.go deleted file mode 100644 index 881083c..0000000 --- a/internal/services/dto/discordwebhooks.go +++ /dev/null @@ -1,63 +0,0 @@ -package dto - -import ( - "context" - - "git.jamestombleson.com/jtom38/newsbot-api/internal/database" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain/models" - "github.com/google/uuid" -) - -func (c *DtoClient) ListDiscordWebHooks(ctx context.Context, total int32) ([]models.DiscordWebHooksDto, error) { - var res []models.DiscordWebHooksDto - - items, err := c.db.ListDiscordWebhooks(ctx, total) - if err != nil { - return res, nil - } - - for _, item := range items { - res = append(res, c.ConvertDiscordWebhook(item)) - } - - return res, nil -} - -func (c *DtoClient) GetDiscordWebhook(ctx context.Context, id uuid.UUID) (models.DiscordWebHooksDto, error) { - var res models.DiscordWebHooksDto - - item, err := c.db.GetDiscordWebHooksByID(ctx, id) - if err != nil { - return res, err - } - - return c.ConvertDiscordWebhook(item), nil -} - -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{ - Server: server, - Channel: channel, - }) - if err != nil { - return res, err - } - - for _, item := range items { - res = append(res, c.ConvertDiscordWebhook(item)) - } - - return res, nil -} - -func (c *DtoClient) ConvertDiscordWebhook(i database.Discordwebhook) models.DiscordWebHooksDto { - return models.DiscordWebHooksDto{ - ID: i.ID, - Url: i.Url, - Server: i.Server, - Channel: i.Channel, - Enabled: i.Enabled, - } -} diff --git a/internal/services/dto/queue.go b/internal/services/dto/queue.go deleted file mode 100644 index ce61fc4..0000000 --- a/internal/services/dto/queue.go +++ /dev/null @@ -1,42 +0,0 @@ -package dto - -import ( - "context" - - "git.jamestombleson.com/jtom38/newsbot-api/internal/database" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain/models" -) - -func (c *DtoClient) ListDiscordWebhookQueue(ctx context.Context, limit int32) { - -} - -func (c *DtoClient) ListDiscordWebhookQueueDetails(ctx context.Context, limit int32) ([]models.DiscordQueueDetailsDto, error) { - var res []models.DiscordQueueDetailsDto - - items, err := c.db.ListDiscordQueueItems(ctx, limit) - if err != nil { - return res, err - } - - for _, item := range items { - article, err := c.GetArticleDetails(ctx, item.ID) - if err != nil { - return res, err - } - - res = append(res, models.DiscordQueueDetailsDto{ - ID: item.ID, - Article: article, - }) - } - - return res, nil -} - -func (c *DtoClient) ConvertToDiscordQueueDto(i database.Discordqueue) models.DiscordQueueDto { - return models.DiscordQueueDto{ - ID: i.ID, - Articleid: i.Articleid, - } -} diff --git a/internal/services/dto/sources.go b/internal/services/dto/sources.go deleted file mode 100644 index 8bdae27..0000000 --- a/internal/services/dto/sources.go +++ /dev/null @@ -1,85 +0,0 @@ -package dto - -import ( - "context" - "strings" - - "git.jamestombleson.com/jtom38/newsbot-api/internal/database" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain/models" - "github.com/google/uuid" -) - -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 - } - - return models.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: c.SplitTags(i.Tags), - Deleted: deleted, - } -} diff --git a/internal/services/dto/subscriptions.go b/internal/services/dto/subscriptions.go deleted file mode 100644 index 6d0f24b..0000000 --- a/internal/services/dto/subscriptions.go +++ /dev/null @@ -1,91 +0,0 @@ -package dto - -import ( - "context" - - "git.jamestombleson.com/jtom38/newsbot-api/internal/database" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain/models" - "github.com/google/uuid" -) - -func (c *DtoClient) ListSubscriptions(ctx context.Context, limit int32) ([]models.SubscriptionDto, error) { - var res []models.SubscriptionDto - - items, err := c.db.ListSubscriptions(ctx, limit) - if err != nil { - return res, err - } - - for _, item := range items { - res = append(res, c.ConvertSubscription(item)) - } - - return res, nil -} - -func (c *DtoClient) ListSubscriptionDetails(ctx context.Context, limit int32) ([]models.SubscriptionDetailsDto, error) { - var res []models.SubscriptionDetailsDto - - items, err := c.ListSubscriptions(ctx, limit) - if err != nil { - return res, err - } - - for _, item := range items { - dwh, err := c.GetDiscordWebhook(ctx, item.DiscordWebhookId) - if err != nil { - return res, err - } - - source, err := c.GetSourceById(ctx, item.SourceId) - if err != nil { - return res, err - } - - res = append(res, models.SubscriptionDetailsDto{ - ID: item.ID, - Source: source, - DiscordWebHook: dwh, - }) - } - - return res, nil -} - -func (c *DtoClient) ListSubscriptionsByDiscordWebhookId(ctx context.Context, id uuid.UUID) ([]models.SubscriptionDto, error) { - var res []models.SubscriptionDto - - items, err := c.db.GetSubscriptionsByDiscordWebHookId(ctx, id) - if err != nil { - return res, err - } - - for _, item := range items { - res = append(res, c.ConvertSubscription(item)) - } - - return res, nil -} - -func (c *DtoClient) ListSubscriptionsBySourceId(ctx context.Context, id uuid.UUID) ([]models.SubscriptionDto, error) { - var res []models.SubscriptionDto - - items, err := c.db.GetSubscriptionsBySourceID(ctx, id) - if err != nil { - return res, err - } - - for _, item := range items { - res = append(res, c.ConvertSubscription(item)) - } - - return res, nil -} - -func (c *DtoClient) ConvertSubscription(i database.Subscription) models.SubscriptionDto { - return models.SubscriptionDto{ - ID: i.ID, - DiscordWebhookId: i.Discordwebhookid, - SourceId: i.Sourceid, - } -}