From 94c2bdb312c1f4660e630de398722da05baf5a0d Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Thu, 9 May 2024 18:59:50 -0700 Subject: [PATCH 1/6] entity was moved to its own page --- internal/entity/entity.go | 140 ++++++++++++++++++++++++ internal/services/input/ffxiv.go | 12 +- internal/services/input/ffxiv_test.go | 5 +- internal/services/input/reddit.go | 31 +++--- internal/services/input/reddit_test.go | 5 +- internal/services/input/rss.go | 14 +-- internal/services/input/rss_test.go | 7 +- internal/services/input/twitch.go | 12 +- internal/services/input/twitch_test.go | 7 +- internal/services/input/youtube.go | 19 ++-- internal/services/input/youtube_test.go | 5 +- 11 files changed, 201 insertions(+), 56 deletions(-) create mode 100644 internal/entity/entity.go diff --git a/internal/entity/entity.go b/internal/entity/entity.go new file mode 100644 index 0000000..107a9d6 --- /dev/null +++ b/internal/entity/entity.go @@ -0,0 +1,140 @@ +package entity + +import ( + "time" +) + +// This links a source to a discord webhook. +// It is owned by a user so they can remove the link +type AlertDiscordEntity struct { + ID int64 + CreatedAt time.Time + UpdatedAt time.Time + DeletedAt time.Time + UserID int64 + SourceID int64 + DiscordWebHookId int64 +} + +type ArticleEntity struct { + ID int64 + CreatedAt time.Time + UpdatedAt time.Time + DeletedAt time.Time + SourceID int64 + Tags string + Title string + Url string + PubDate time.Time + IsVideo bool + Thumbnail string + Description string + AuthorName string + AuthorImageUrl string +} + +type DiscordQueueEntity struct { + ID int64 + CreatedAt time.Time + UpdatedAt time.Time + DeletedAt time.Time + ArticleId int64 + SourceId int64 +} + +type DiscordWebHookEntity struct { + ID int64 + CreatedAt time.Time + UpdatedAt time.Time + DeletedAt time.Time + UserID int64 + Url string + Server string + Channel string + Enabled bool +} + +type IconEntity struct { + ID int64 + CreatedAt time.Time + UpdatedAt time.Time + DeletedAt time.Time + FileName string + Site string +} + +type SettingEntity struct { + ID int64 + CreatedAt time.Time + UpdatedAt time.Time + DeletedAt time.Time + Key string + Value string + Options string +} + +type SourceEntity struct { + ID int64 + CreatedAt time.Time + UpdatedAt time.Time + DeletedAt time.Time + + // Who will collect from it. Used + // domain.SourceCollector... + Source string + + // Human Readable value to state what is getting collected + DisplayName string + + // Tells the parser where to look for data + Url string + + // Static tags for this defined record + Tags string + + // If the record is disabled, then it will be skipped on processing + Enabled bool +} + +//type SubscriptionEntity struct { +// ID int64 +// CreatedAt time.Time +// UpdatedAt time.Time +// DeletedAt time.Time +// UserID int64 +// SourceID int64 +// //SourceType string +// //SourceName string +// DiscordID int64 +// //DiscordName string +//} + +// This defines what sources a user wants to follow. +// These will show up for the user as a front page +type UserSourceSubscriptionEntity struct { + ID int64 + CreatedAt time.Time + UpdatedAt time.Time + DeletedAt time.Time + UserID int64 + SourceID int64 +} + +type UserEntity struct { + ID int64 + CreatedAt time.Time + UpdatedAt time.Time + DeletedAt time.Time + Username string + Hash string + Scopes string +} + +type RefreshTokenEntity struct { + ID int64 + CreatedAt time.Time + UpdatedAt time.Time + DeletedAt time.Time + Username string + Token string +} diff --git a/internal/services/input/ffxiv.go b/internal/services/input/ffxiv.go index 7072548..0cd4cba 100644 --- a/internal/services/input/ffxiv.go +++ b/internal/services/input/ffxiv.go @@ -12,7 +12,7 @@ import ( "github.com/go-rod/rod/lib/launcher" "github.com/google/uuid" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/entity" "git.jamestombleson.com/jtom38/newsbot-api/internal/services/cache" ) @@ -24,7 +24,7 @@ const ( ) type FFXIVClient struct { - record domain.SourceEntity + record entity.SourceEntity //SourceID uint //Url string //Region string @@ -32,15 +32,15 @@ type FFXIVClient struct { cacheGroup string } -func NewFFXIVClient(Record domain.SourceEntity) FFXIVClient { +func NewFFXIVClient(Record entity.SourceEntity) FFXIVClient { return FFXIVClient{ record: Record, cacheGroup: "ffxiv", } } -func (fc *FFXIVClient) CheckSource() ([]domain.ArticleEntity, error) { - var articles []domain.ArticleEntity +func (fc *FFXIVClient) CheckSource() ([]entity.ArticleEntity, error) { + var articles []entity.ArticleEntity parser := fc.GetBrowser() defer parser.Close() @@ -96,7 +96,7 @@ func (fc *FFXIVClient) CheckSource() ([]domain.ArticleEntity, error) { return articles, err } - article := domain.ArticleEntity{ + article := entity.ArticleEntity{ SourceID: fc.record.ID, Tags: tags, Title: title, diff --git a/internal/services/input/ffxiv_test.go b/internal/services/input/ffxiv_test.go index f2fde75..4fe035e 100644 --- a/internal/services/input/ffxiv_test.go +++ b/internal/services/input/ffxiv_test.go @@ -3,11 +3,12 @@ package input_test import ( "testing" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/entity" ffxiv "git.jamestombleson.com/jtom38/newsbot-api/internal/services/input" ) -var FFXIVRecord domain.SourceEntity = domain.SourceEntity{ +var FFXIVRecord entity.SourceEntity = entity.SourceEntity{ ID: 9999, DisplayName: "Final Fantasy XIV - NA", Source: domain.SourceCollectorFfxiv, diff --git a/internal/services/input/reddit.go b/internal/services/input/reddit.go index 0a6aab7..bfd8844 100644 --- a/internal/services/input/reddit.go +++ b/internal/services/input/reddit.go @@ -9,6 +9,7 @@ import ( "time" "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/entity" "git.jamestombleson.com/jtom38/newsbot-api/internal/services" "github.com/go-rod/rod" "github.com/go-rod/rod/lib/launcher" @@ -16,7 +17,7 @@ import ( type RedditClient struct { config RedditConfig - record domain.SourceEntity + record entity.SourceEntity } type RedditConfig struct { @@ -25,7 +26,7 @@ type RedditConfig struct { PullNSFW string } -func NewRedditClient(Record domain.SourceEntity) *RedditClient { +func NewRedditClient(Record entity.SourceEntity) *RedditClient { rc := RedditClient{ record: Record, } @@ -86,10 +87,10 @@ func (rc *RedditClient) GetContent() (domain.RedditJsonContent, error) { return items, nil } -func (rc *RedditClient) ConvertToArticles(items domain.RedditJsonContent) []domain.ArticleEntity { - var redditArticles []domain.ArticleEntity +func (rc *RedditClient) ConvertToArticles(items domain.RedditJsonContent) []entity.ArticleEntity { + var redditArticles []entity.ArticleEntity for _, item := range items.Data.Children { - var article domain.ArticleEntity + var article entity.ArticleEntity article, err := rc.convertToArticle(item.Data) if err != nil { log.Printf("[Reddit] %v", err) @@ -102,8 +103,8 @@ func (rc *RedditClient) ConvertToArticles(items domain.RedditJsonContent) []doma // ConvertToArticle() will take the reddit model struct and convert them over to Article structs. // This data can be passed to the database. -func (rc *RedditClient) convertToArticle(source domain.RedditPost) (domain.ArticleEntity, error) { - var item domain.ArticleEntity +func (rc *RedditClient) convertToArticle(source domain.RedditPost) (entity.ArticleEntity, error) { + var item entity.ArticleEntity if source.Content == "" && source.Url != "" { item = rc.convertPicturePost(source) @@ -129,8 +130,8 @@ func (rc *RedditClient) convertToArticle(source domain.RedditPost) (domain.Artic return item, nil } -func (rc *RedditClient) convertPicturePost(source domain.RedditPost) domain.ArticleEntity { - var item = domain.ArticleEntity{ +func (rc *RedditClient) convertPicturePost(source domain.RedditPost) entity.ArticleEntity { + var item = entity.ArticleEntity{ SourceID: rc.record.ID, Title: source.Title, Tags: fmt.Sprintf("%v", rc.record.Tags), @@ -145,8 +146,8 @@ func (rc *RedditClient) convertPicturePost(source domain.RedditPost) domain.Arti return item } -func (rc *RedditClient) convertTextPost(source domain.RedditPost) domain.ArticleEntity { - var item = domain.ArticleEntity{ +func (rc *RedditClient) convertTextPost(source domain.RedditPost) entity.ArticleEntity { + var item = entity.ArticleEntity{ SourceID: rc.record.ID, Tags: "a", Title: source.Title, @@ -158,8 +159,8 @@ func (rc *RedditClient) convertTextPost(source domain.RedditPost) domain.Article return item } -func (rc *RedditClient) convertVideoPost(source domain.RedditPost) domain.ArticleEntity { - var item = domain.ArticleEntity{ +func (rc *RedditClient) convertVideoPost(source domain.RedditPost) entity.ArticleEntity { + var item = entity.ArticleEntity{ SourceID: rc.record.ID, Tags: "a", Title: source.Title, @@ -172,8 +173,8 @@ func (rc *RedditClient) convertVideoPost(source domain.RedditPost) domain.Articl } // This post is nothing more then a redirect to another location. -func (rc *RedditClient) convertRedirectPost(source domain.RedditPost) domain.ArticleEntity { - var item = domain.ArticleEntity{ +func (rc *RedditClient) convertRedirectPost(source domain.RedditPost) entity.ArticleEntity { + var item = entity.ArticleEntity{ SourceID: rc.record.ID, Tags: "a", Title: source.Title, diff --git a/internal/services/input/reddit_test.go b/internal/services/input/reddit_test.go index bb7eb76..de13971 100644 --- a/internal/services/input/reddit_test.go +++ b/internal/services/input/reddit_test.go @@ -3,11 +3,12 @@ package input_test import ( "testing" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/entity" "git.jamestombleson.com/jtom38/newsbot-api/internal/services/input" ) -var RedditRecord domain.SourceEntity = domain.SourceEntity{ +var RedditRecord entity.SourceEntity = entity.SourceEntity{ ID: 9999, DisplayName: "dadjokes", Source: domain.SourceCollectorRss, diff --git a/internal/services/input/rss.go b/internal/services/input/rss.go index e5dbccd..8a844d8 100644 --- a/internal/services/input/rss.go +++ b/internal/services/input/rss.go @@ -3,19 +3,19 @@ package input import ( "strings" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/entity" "github.com/mmcdole/gofeed" ) type FeedInput interface { - GetArticles() (domain.ArticleEntity, error) + GetArticles() (entity.ArticleEntity, error) } type rssClient struct { - SourceRecord domain.SourceEntity + SourceRecord entity.SourceEntity } -func NewRssClient(sourceRecord domain.SourceEntity) rssClient { +func NewRssClient(sourceRecord entity.SourceEntity) rssClient { client := rssClient{ SourceRecord: sourceRecord, } @@ -23,7 +23,7 @@ func NewRssClient(sourceRecord domain.SourceEntity) rssClient { return client } -func (rc rssClient) GetArticles() ([]domain.ArticleEntity, error) { +func (rc rssClient) GetArticles() ([]entity.ArticleEntity, error) { parser := gofeed.NewParser() feed, err := parser.ParseURL(rc.SourceRecord.Url) if err != nil { @@ -31,9 +31,9 @@ func (rc rssClient) GetArticles() ([]domain.ArticleEntity, error) { } sourceTags := strings.Split(rc.SourceRecord.Tags, ",") - var articles []domain.ArticleEntity + var articles []entity.ArticleEntity for _, post := range feed.Items { - article := domain.ArticleEntity{ + article := entity.ArticleEntity{ SourceID: rc.SourceRecord.ID, Title: post.Title, Description: post.Content, diff --git a/internal/services/input/rss_test.go b/internal/services/input/rss_test.go index b03e798..a2a0065 100644 --- a/internal/services/input/rss_test.go +++ b/internal/services/input/rss_test.go @@ -3,11 +3,12 @@ package input_test import ( "testing" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/entity" "git.jamestombleson.com/jtom38/newsbot-api/internal/services/input" ) -var rssRecord = domain.SourceEntity{ +var rssRecord = entity.SourceEntity{ ID: 1, DisplayName: "ArsTechnica", Url: "https://feeds.arstechnica.com/arstechnica/index", @@ -27,7 +28,7 @@ func TestRssGetFeed(t *testing.T) { } func TestRssAgainstGita(t *testing.T) { - client := input.NewRssClient(domain.SourceEntity{ + client := input.NewRssClient(entity.SourceEntity{ ID: 2, DisplayName: "Gitea - Newsbot-api", Source: domain.SourceCollectorRss, diff --git a/internal/services/input/twitch.go b/internal/services/input/twitch.go index 317db56..652fa13 100644 --- a/internal/services/input/twitch.go +++ b/internal/services/input/twitch.go @@ -6,13 +6,13 @@ import ( "strings" "time" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/entity" "git.jamestombleson.com/jtom38/newsbot-api/internal/services" "github.com/nicklaw5/helix/v2" ) type TwitchClient struct { - SourceRecord domain.SourceEntity + SourceRecord entity.SourceEntity // config monitorClips string @@ -71,7 +71,7 @@ func initTwitchApi(ClientId string, ClientSecret string) (helix.Client, error) { } // This will let you replace the bound source record to keep the same session alive. -func (tc *TwitchClient) ReplaceSourceRecord(source domain.SourceEntity) { +func (tc *TwitchClient) ReplaceSourceRecord(source entity.SourceEntity) { tc.SourceRecord = source } @@ -86,8 +86,8 @@ func (tc *TwitchClient) Login() error { return nil } -func (tc *TwitchClient) GetContent() ([]domain.ArticleEntity, error) { - var items []domain.ArticleEntity +func (tc *TwitchClient) GetContent() ([]entity.ArticleEntity, error) { + var items []entity.ArticleEntity user, err := tc.GetUserDetails() if err != nil { @@ -100,7 +100,7 @@ func (tc *TwitchClient) GetContent() ([]domain.ArticleEntity, error) { } for _, video := range posts { - var article domain.ArticleEntity + var article entity.ArticleEntity AuthorName, err := tc.ExtractAuthor(video) if err != nil { diff --git a/internal/services/input/twitch_test.go b/internal/services/input/twitch_test.go index df06ad3..0107811 100644 --- a/internal/services/input/twitch_test.go +++ b/internal/services/input/twitch_test.go @@ -4,17 +4,18 @@ import ( "log" "testing" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/entity" "git.jamestombleson.com/jtom38/newsbot-api/internal/services/input" ) -var TwitchSourceRecord = domain.SourceEntity{ +var TwitchSourceRecord = entity.SourceEntity{ ID: 9999, DisplayName: "nintendo", Source: domain.SourceCollectorTwitch, } -var TwitchInvalidRecord = domain.SourceEntity{ +var TwitchInvalidRecord = entity.SourceEntity{ ID: 9999, DisplayName: "EvilNintendo", Source: domain.SourceCollectorTwitch, diff --git a/internal/services/input/youtube.go b/internal/services/input/youtube.go index 1bfdfe2..1ec35c6 100644 --- a/internal/services/input/youtube.go +++ b/internal/services/input/youtube.go @@ -11,11 +11,11 @@ import ( "github.com/go-rod/rod/lib/launcher" "github.com/mmcdole/gofeed" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/entity" ) type YoutubeClient struct { - record domain.SourceEntity + record entity.SourceEntity // internal variables at time of collection channelID string @@ -25,7 +25,7 @@ type YoutubeClient struct { //debug bool // cache config - cacheGroup string + //cacheGroup string } var ( @@ -36,17 +36,16 @@ var ( const YOUTUBE_FEED_URL string = "https://www.youtube.com/feeds/videos.xml?channel_id=" -func NewYoutubeClient(Record domain.SourceEntity) YoutubeClient { +func NewYoutubeClient(Record entity.SourceEntity) YoutubeClient { yc := YoutubeClient{ - record: Record, - cacheGroup: "youtube", + record: Record, } return yc } // CheckSource will go and run all the commands needed to process a source. -func (yc *YoutubeClient) GetContent() ([]domain.ArticleEntity, error) { - var items []domain.ArticleEntity +func (yc *YoutubeClient) GetContent() ([]entity.ArticleEntity, error) { + var items []entity.ArticleEntity docParser, err := yc.GetParser(yc.record.Url) if err != nil { return items, err @@ -246,7 +245,7 @@ func (yc *YoutubeClient) CheckUriCache(uri *string) bool { return false } -func (yc *YoutubeClient) ConvertToArticle(item *gofeed.Item) domain.ArticleEntity { +func (yc *YoutubeClient) ConvertToArticle(item *gofeed.Item) entity.ArticleEntity { parser, err := yc.GetParser(item.Link) if err != nil { log.Printf("[YouTube] Unable to process %v, submit this link as an issue.\n", item.Link) @@ -264,7 +263,7 @@ func (yc *YoutubeClient) ConvertToArticle(item *gofeed.Item) domain.ArticleEntit log.Printf("[YouTube] %v", msg) } - var article = domain.ArticleEntity{ + var article = entity.ArticleEntity{ SourceID: yc.record.ID, Tags: tags, Title: item.Title, diff --git a/internal/services/input/youtube_test.go b/internal/services/input/youtube_test.go index 7c1a75f..0337e58 100644 --- a/internal/services/input/youtube_test.go +++ b/internal/services/input/youtube_test.go @@ -3,11 +3,12 @@ package input_test import ( "testing" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/entity" "git.jamestombleson.com/jtom38/newsbot-api/internal/services/input" ) -var YouTubeRecord = domain.SourceEntity{ +var YouTubeRecord = entity.SourceEntity{ ID: 9999, DisplayName: "dadjokes", Source: domain.SourceCollectorReddit, -- 2.45.2 From 6bccbce91b4a7ef9c357e6cd8e61da8ae92797b8 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Thu, 9 May 2024 19:08:25 -0700 Subject: [PATCH 2/6] domain was broken up for external usage --- {internal/domain => domain}/const.go | 0 {internal/domain => domain}/dto.go | 0 {internal/domain => domain}/requests.go | 0 {internal/domain => domain}/responses.go | 0 {internal/domain => domain}/scopes.go | 0 internal/domain/entity.go | 140 ---------------------- internal/{services => dtoconv}/dtoconv.go | 17 +-- 7 files changed, 10 insertions(+), 147 deletions(-) rename {internal/domain => domain}/const.go (100%) rename {internal/domain => domain}/dto.go (100%) rename {internal/domain => domain}/requests.go (100%) rename {internal/domain => domain}/responses.go (100%) rename {internal/domain => domain}/scopes.go (100%) delete mode 100644 internal/domain/entity.go rename internal/{services => dtoconv}/dtoconv.go (65%) diff --git a/internal/domain/const.go b/domain/const.go similarity index 100% rename from internal/domain/const.go rename to domain/const.go diff --git a/internal/domain/dto.go b/domain/dto.go similarity index 100% rename from internal/domain/dto.go rename to domain/dto.go diff --git a/internal/domain/requests.go b/domain/requests.go similarity index 100% rename from internal/domain/requests.go rename to domain/requests.go diff --git a/internal/domain/responses.go b/domain/responses.go similarity index 100% rename from internal/domain/responses.go rename to domain/responses.go diff --git a/internal/domain/scopes.go b/domain/scopes.go similarity index 100% rename from internal/domain/scopes.go rename to domain/scopes.go diff --git a/internal/domain/entity.go b/internal/domain/entity.go deleted file mode 100644 index 6983fb5..0000000 --- a/internal/domain/entity.go +++ /dev/null @@ -1,140 +0,0 @@ -package domain - -import ( - "time" -) - -// This links a source to a discord webhook. -// It is owned by a user so they can remove the link -type AlertDiscordEntity struct { - ID int64 - CreatedAt time.Time - UpdatedAt time.Time - DeletedAt time.Time - UserID int64 - SourceID int64 - DiscordWebHookId int64 -} - -type ArticleEntity struct { - ID int64 - CreatedAt time.Time - UpdatedAt time.Time - DeletedAt time.Time - SourceID int64 - Tags string - Title string - Url string - PubDate time.Time - IsVideo bool - Thumbnail string - Description string - AuthorName string - AuthorImageUrl string -} - -type DiscordQueueEntity struct { - ID int64 - CreatedAt time.Time - UpdatedAt time.Time - DeletedAt time.Time - ArticleId int64 - SourceId int64 -} - -type DiscordWebHookEntity struct { - ID int64 - CreatedAt time.Time - UpdatedAt time.Time - DeletedAt time.Time - UserID int64 - Url string - Server string - Channel string - Enabled bool -} - -type IconEntity struct { - ID int64 - CreatedAt time.Time - UpdatedAt time.Time - DeletedAt time.Time - FileName string - Site string -} - -type SettingEntity struct { - ID int64 - CreatedAt time.Time - UpdatedAt time.Time - DeletedAt time.Time - Key string - Value string - Options string -} - -type SourceEntity struct { - ID int64 - CreatedAt time.Time - UpdatedAt time.Time - DeletedAt time.Time - - // Who will collect from it. Used - // domain.SourceCollector... - Source string - - // Human Readable value to state what is getting collected - DisplayName string - - // Tells the parser where to look for data - Url string - - // Static tags for this defined record - Tags string - - // If the record is disabled, then it will be skipped on processing - Enabled bool -} - -//type SubscriptionEntity struct { -// ID int64 -// CreatedAt time.Time -// UpdatedAt time.Time -// DeletedAt time.Time -// UserID int64 -// SourceID int64 -// //SourceType string -// //SourceName string -// DiscordID int64 -// //DiscordName string -//} - -// This defines what sources a user wants to follow. -// These will show up for the user as a front page -type UserSourceSubscriptionEntity struct { - ID int64 - CreatedAt time.Time - UpdatedAt time.Time - DeletedAt time.Time - UserID int64 - SourceID int64 -} - -type UserEntity struct { - ID int64 - CreatedAt time.Time - UpdatedAt time.Time - DeletedAt time.Time - Username string - Hash string - Scopes string -} - -type RefreshTokenEntity struct { - ID int64 - CreatedAt time.Time - UpdatedAt time.Time - DeletedAt time.Time - Username string - Token string -} diff --git a/internal/services/dtoconv.go b/internal/dtoconv/dtoconv.go similarity index 65% rename from internal/services/dtoconv.go rename to internal/dtoconv/dtoconv.go index 6505dc5..17faa4c 100644 --- a/internal/services/dtoconv.go +++ b/internal/dtoconv/dtoconv.go @@ -1,8 +1,11 @@ package services -import "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" +import ( + "git.jamestombleson.com/jtom38/newsbot-api/domain" + internal "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" +) -func ArticlesToDto(items []domain.ArticleEntity) []domain.ArticleDto { +func ArticlesToDto(items []internal.ArticleEntity) []domain.ArticleDto { var dtos []domain.ArticleDto for _, item := range items { dtos = append(dtos, ArticleToDto(item)) @@ -10,7 +13,7 @@ func ArticlesToDto(items []domain.ArticleEntity) []domain.ArticleDto { return dtos } -func ArticleToDto(item domain.ArticleEntity) domain.ArticleDto { +func ArticleToDto(item internal.ArticleEntity) domain.ArticleDto { return domain.ArticleDto{ ID: item.ID, SourceID: item.SourceID, @@ -26,7 +29,7 @@ func ArticleToDto(item domain.ArticleEntity) domain.ArticleDto { } } -func DiscordWebhooksToDto(items []domain.DiscordWebHookEntity) []domain.DiscordWebHookDto{ +func DiscordWebhooksToDto(items []internal.DiscordWebHookEntity) []domain.DiscordWebHookDto{ var dtos []domain.DiscordWebHookDto for _, item := range items { dtos = append(dtos, DiscordWebhookToDto(item)) @@ -34,7 +37,7 @@ func DiscordWebhooksToDto(items []domain.DiscordWebHookEntity) []domain.DiscordW return dtos } -func DiscordWebhookToDto(item domain.DiscordWebHookEntity) domain.DiscordWebHookDto { +func DiscordWebhookToDto(item internal.DiscordWebHookEntity) domain.DiscordWebHookDto { return domain.DiscordWebHookDto{ ID: item.ID, Server: item.Server, @@ -44,7 +47,7 @@ func DiscordWebhookToDto(item domain.DiscordWebHookEntity) domain.DiscordWebHook } } -func SourcesToDto(items []domain.SourceEntity) []domain.SourceDto { +func SourcesToDto(items []internal.SourceEntity) []domain.SourceDto { var dtos []domain.SourceDto for _, item := range items { dtos = append(dtos, SourceToDto(item)) @@ -52,7 +55,7 @@ func SourcesToDto(items []domain.SourceEntity) []domain.SourceDto { return dtos } -func SourceToDto(item domain.SourceEntity) domain.SourceDto { +func SourceToDto(item internal.SourceEntity) domain.SourceDto { return domain.SourceDto{ ID: item.ID, Source: item.Source, -- 2.45.2 From e9e208371afa76ee0d75210efac81f4716a74544 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Thu, 9 May 2024 19:08:43 -0700 Subject: [PATCH 3/6] handlers got updated for the new dtoconv package --- internal/dtoconv/dtoconv.go | 16 +++++++------- internal/handler/v1/articles.go | 14 ++++++------ internal/handler/v1/auth.go | 2 +- internal/handler/v1/discordwebhooks.go | 18 ++++++++-------- internal/handler/v1/handler.go | 2 +- internal/handler/v1/jwt.go | 2 +- internal/handler/v1/sources.go | 30 +++++++++++++------------- 7 files changed, 42 insertions(+), 42 deletions(-) diff --git a/internal/dtoconv/dtoconv.go b/internal/dtoconv/dtoconv.go index 17faa4c..153f800 100644 --- a/internal/dtoconv/dtoconv.go +++ b/internal/dtoconv/dtoconv.go @@ -1,11 +1,11 @@ -package services +package dtoconv import ( "git.jamestombleson.com/jtom38/newsbot-api/domain" - internal "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/entity" ) -func ArticlesToDto(items []internal.ArticleEntity) []domain.ArticleDto { +func ArticlesToDto(items []entity.ArticleEntity) []domain.ArticleDto { var dtos []domain.ArticleDto for _, item := range items { dtos = append(dtos, ArticleToDto(item)) @@ -13,7 +13,7 @@ func ArticlesToDto(items []internal.ArticleEntity) []domain.ArticleDto { return dtos } -func ArticleToDto(item internal.ArticleEntity) domain.ArticleDto { +func ArticleToDto(item entity.ArticleEntity) domain.ArticleDto { return domain.ArticleDto{ ID: item.ID, SourceID: item.SourceID, @@ -29,7 +29,7 @@ func ArticleToDto(item internal.ArticleEntity) domain.ArticleDto { } } -func DiscordWebhooksToDto(items []internal.DiscordWebHookEntity) []domain.DiscordWebHookDto{ +func DiscordWebhooksToDto(items []entity.DiscordWebHookEntity) []domain.DiscordWebHookDto{ var dtos []domain.DiscordWebHookDto for _, item := range items { dtos = append(dtos, DiscordWebhookToDto(item)) @@ -37,7 +37,7 @@ func DiscordWebhooksToDto(items []internal.DiscordWebHookEntity) []domain.Discor return dtos } -func DiscordWebhookToDto(item internal.DiscordWebHookEntity) domain.DiscordWebHookDto { +func DiscordWebhookToDto(item entity.DiscordWebHookEntity) domain.DiscordWebHookDto { return domain.DiscordWebHookDto{ ID: item.ID, Server: item.Server, @@ -47,7 +47,7 @@ func DiscordWebhookToDto(item internal.DiscordWebHookEntity) domain.DiscordWebHo } } -func SourcesToDto(items []internal.SourceEntity) []domain.SourceDto { +func SourcesToDto(items []entity.SourceEntity) []domain.SourceDto { var dtos []domain.SourceDto for _, item := range items { dtos = append(dtos, SourceToDto(item)) @@ -55,7 +55,7 @@ func SourcesToDto(items []internal.SourceEntity) []domain.SourceDto { return dtos } -func SourceToDto(item internal.SourceEntity) domain.SourceDto { +func SourceToDto(item entity.SourceEntity) domain.SourceDto { return domain.SourceDto{ ID: item.ID, Source: item.Source, diff --git a/internal/handler/v1/articles.go b/internal/handler/v1/articles.go index b77f4d9..73b916a 100644 --- a/internal/handler/v1/articles.go +++ b/internal/handler/v1/articles.go @@ -4,8 +4,8 @@ import ( "net/http" "strconv" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" - "git.jamestombleson.com/jtom38/newsbot-api/internal/services" + "git.jamestombleson.com/jtom38/newsbot-api/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/dtoconv" "github.com/labstack/echo/v4" ) @@ -41,7 +41,7 @@ func (s *Handler) listArticles(c echo.Context) error { return s.WriteError(c, err, http.StatusInternalServerError) } - resp.Payload = services.ArticlesToDto(res) + resp.Payload = dtoconv.ArticlesToDto(res) return c.JSON(http.StatusOK, resp) } @@ -79,7 +79,7 @@ func (s *Handler) getArticle(c echo.Context) error { } var dtos []domain.ArticleDto - dtos = append(dtos, services.ArticleToDto(item)) + dtos = append(dtos, dtoconv.ArticleToDto(item)) p.Payload = dtos return c.JSON(http.StatusOK, p) @@ -123,8 +123,8 @@ func (s *Handler) getArticleDetails(c echo.Context) error { return s.WriteError(c, err, http.StatusInternalServerError) } - p.Payload.Article = services.ArticleToDto(article) - p.Payload.Source = services.SourceToDto(source) + p.Payload.Article = dtoconv.ArticleToDto(article) + p.Payload.Source = dtoconv.SourceToDto(source) return c.JSON(http.StatusOK, p) } @@ -168,6 +168,6 @@ func (s *Handler) ListArticlesBySourceId(c echo.Context) error { return c.JSON(http.StatusInternalServerError, err) } - p.Payload = services.ArticlesToDto(items) + p.Payload = dtoconv.ArticlesToDto(items) return c.JSON(http.StatusOK, p) } diff --git a/internal/handler/v1/auth.go b/internal/handler/v1/auth.go index f73f57e..61a9ad6 100644 --- a/internal/handler/v1/auth.go +++ b/internal/handler/v1/auth.go @@ -5,7 +5,7 @@ import ( "strings" "time" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/domain" "git.jamestombleson.com/jtom38/newsbot-api/internal/repository" "github.com/labstack/echo/v4" diff --git a/internal/handler/v1/discordwebhooks.go b/internal/handler/v1/discordwebhooks.go index 4328904..944616b 100644 --- a/internal/handler/v1/discordwebhooks.go +++ b/internal/handler/v1/discordwebhooks.go @@ -5,8 +5,8 @@ import ( "strconv" "strings" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" - "git.jamestombleson.com/jtom38/newsbot-api/internal/services" + "git.jamestombleson.com/jtom38/newsbot-api/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/dtoconv" "github.com/labstack/echo/v4" ) @@ -35,7 +35,7 @@ func (s *Handler) ListDiscordWebHooks(c echo.Context) error { if err != nil { return c.JSON(http.StatusInternalServerError, err) } - p.Payload = services.DiscordWebhooksToDto(res) + p.Payload = dtoconv.DiscordWebhooksToDto(res) return c.JSON(http.StatusOK, p) } @@ -71,7 +71,7 @@ func (s *Handler) GetDiscordWebHooksById(c echo.Context) error { return s.WriteError(c, err, http.StatusInternalServerError) } var dtos []domain.DiscordWebHookDto - dtos = append(dtos, services.DiscordWebhookToDto(res)) + dtos = append(dtos, dtoconv.DiscordWebhookToDto(res)) p.Payload = dtos return c.JSON(http.StatusOK, p) } @@ -114,7 +114,7 @@ func (s *Handler) GetDiscordWebHooksByServerAndChannel(c echo.Context) error { return s.WriteError(c, err, http.StatusInternalServerError) } - p.Payload = services.DiscordWebhooksToDto(res) + p.Payload = dtoconv.DiscordWebhooksToDto(res) return c.JSON(http.StatusOK, p) } @@ -180,7 +180,7 @@ func (s *Handler) NewDiscordWebHook(c echo.Context) error { } var dtos []domain.DiscordWebHookDto - dtos = append(dtos, services.DiscordWebhookToDto(item)) + dtos = append(dtos, dtoconv.DiscordWebhookToDto(item)) return c.JSON(http.StatusOK, domain.DiscordWebhookResponse{ BaseResponse: domain.BaseResponse{ @@ -239,7 +239,7 @@ func (s *Handler) disableDiscordWebHook(c echo.Context) error { } var dtos []domain.DiscordWebHookDto - dtos = append(dtos, services.DiscordWebhookToDto(item)) + dtos = append(dtos, dtoconv.DiscordWebhookToDto(item)) return c.JSON(http.StatusOK, domain.DiscordWebhookResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, @@ -290,7 +290,7 @@ func (s *Handler) enableDiscordWebHook(c echo.Context) error { } var dtos []domain.DiscordWebHookDto - dtos = append(dtos, services.DiscordWebhookToDto(item)) + dtos = append(dtos, dtoconv.DiscordWebhookToDto(item)) return c.JSON(http.StatusOK, domain.DiscordWebhookResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, @@ -344,7 +344,7 @@ func (s *Handler) deleteDiscordWebHook(c echo.Context) error { } var dtos []domain.DiscordWebHookDto - dtos = append(dtos, services.DiscordWebhookToDto(item)) + dtos = append(dtos, dtoconv.DiscordWebhookToDto(item)) return c.JSON(http.StatusOK, domain.DiscordWebhookResponse{ BaseResponse: domain.BaseResponse{ Message: ResponseMessageSuccess, diff --git a/internal/handler/v1/handler.go b/internal/handler/v1/handler.go index d3e76ad..671479e 100644 --- a/internal/handler/v1/handler.go +++ b/internal/handler/v1/handler.go @@ -13,7 +13,7 @@ import ( swagger "github.com/swaggo/echo-swagger" _ "git.jamestombleson.com/jtom38/newsbot-api/docs" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/domain" "git.jamestombleson.com/jtom38/newsbot-api/internal/services" ) diff --git a/internal/handler/v1/jwt.go b/internal/handler/v1/jwt.go index 1a8d565..fba6795 100644 --- a/internal/handler/v1/jwt.go +++ b/internal/handler/v1/jwt.go @@ -5,7 +5,7 @@ import ( "strings" "time" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/domain" "github.com/golang-jwt/jwt/v5" "github.com/labstack/echo/v4" ) diff --git a/internal/handler/v1/sources.go b/internal/handler/v1/sources.go index 000cfde..ce66b22 100644 --- a/internal/handler/v1/sources.go +++ b/internal/handler/v1/sources.go @@ -6,8 +6,8 @@ import ( "strconv" "strings" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" - "git.jamestombleson.com/jtom38/newsbot-api/internal/services" + "git.jamestombleson.com/jtom38/newsbot-api/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/dtoconv" "github.com/labstack/echo/v4" ) @@ -43,7 +43,7 @@ func (s *Handler) listSources(c echo.Context) error { return s.WriteError(c, err, http.StatusInternalServerError) } - resp.Payload = services.SourcesToDto(items) + resp.Payload = dtoconv.SourcesToDto(items) return c.JSON(http.StatusOK, resp) } @@ -88,7 +88,7 @@ func (s *Handler) listSourcesBySource(c echo.Context) error { }) } - resp.Payload = services.SourcesToDto(items) + resp.Payload = dtoconv.SourcesToDto(items) return c.JSON(http.StatusOK, resp) } @@ -127,7 +127,7 @@ func (s *Handler) getSource(c echo.Context) error { } var dto []domain.SourceDto - dto = append(dto, services.SourceToDto(item)) + dto = append(dto, dtoconv.SourceToDto(item)) resp.Payload = dto return c.JSON(http.StatusOK, resp) } @@ -169,7 +169,7 @@ func (s *Handler) GetSourceBySourceAndName(c echo.Context) error { } var dto []domain.SourceDto - dto = append(dto, services.SourceToDto(item)) + dto = append(dto, dtoconv.SourceToDto(item)) resp.Payload = dto return c.JSON(http.StatusOK, resp) } @@ -224,7 +224,7 @@ func (s *Handler) newRedditSource(c echo.Context) error { } var dto []domain.SourceDto - dto = append(dto, services.SourceToDto(item)) + dto = append(dto, dtoconv.SourceToDto(item)) resp.Payload = dto return c.JSON(http.StatusOK, resp) } @@ -264,7 +264,7 @@ func (s *Handler) newYoutubeSource(c echo.Context) error { item, err := s.repo.Sources.GetBySourceAndName(c.Request().Context(), domain.SourceCollectorYoutube, param.Name) if err == nil { var dto []domain.SourceDto - dto = append(dto, services.SourceToDto(item)) + dto = append(dto, dtoconv.SourceToDto(item)) resp.Payload = dto return c.JSON(http.StatusOK, resp) } @@ -282,7 +282,7 @@ func (s *Handler) newYoutubeSource(c echo.Context) error { item, err = s.repo.Sources.GetBySourceAndName(c.Request().Context(), domain.SourceCollectorYoutube, param.Name) if err == nil { var dto []domain.SourceDto - dto = append(dto, services.SourceToDto(item)) + dto = append(dto, dtoconv.SourceToDto(item)) resp.Payload = dto return c.JSON(http.StatusOK, resp) } @@ -323,7 +323,7 @@ func (s *Handler) newTwitchSource(c echo.Context) error { item, err := s.repo.Sources.GetBySourceAndName(c.Request().Context(), domain.SourceCollectorTwitch, param.Name) if err == nil { var dto []domain.SourceDto - dto = append(dto, services.SourceToDto(item)) + dto = append(dto, dtoconv.SourceToDto(item)) resp.Payload = dto return c.JSON(http.StatusOK, resp) } @@ -341,7 +341,7 @@ func (s *Handler) newTwitchSource(c echo.Context) error { item, _ = s.repo.Sources.GetBySourceAndName(c.Request().Context(), domain.SourceCollectorTwitch, param.Name) var dto []domain.SourceDto - dto = append(dto, services.SourceToDto(item)) + dto = append(dto, dtoconv.SourceToDto(item)) resp.Payload = dto return c.JSON(http.StatusOK, resp) @@ -399,7 +399,7 @@ func (s *Handler) newRssSource(c echo.Context) error { } var dto []domain.SourceDto - dto = append(dto, services.SourceToDto(item)) + dto = append(dto, dtoconv.SourceToDto(item)) resp.Payload = dto return c.JSON(http.StatusOK, resp) } @@ -446,7 +446,7 @@ func (s *Handler) deleteSources(c echo.Context) error { } var items []domain.SourceDto - items = append(items, services.SourceToDto(item)) + items = append(items, dtoconv.SourceToDto(item)) return c.JSON(http.StatusOK, domain.SourcesResponse{ BaseResponse: domain.BaseResponse{ @@ -499,7 +499,7 @@ func (s *Handler) disableSource(c echo.Context) error { } var dto []domain.SourceDto - dto = append(dto, services.SourceToDto(item)) + dto = append(dto, dtoconv.SourceToDto(item)) resp.Payload = dto return c.JSON(http.StatusOK, resp) } @@ -547,7 +547,7 @@ func (s *Handler) enableSource(c echo.Context) error { } var dto []domain.SourceDto - dto = append(dto, services.SourceToDto(item)) + dto = append(dto, dtoconv.SourceToDto(item)) resp.Payload = dto return c.JSON(http.StatusOK, resp) } -- 2.45.2 From 80da61db8c6d9eea91c1b77ec2f37e9a58ce30d1 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Thu, 9 May 2024 19:08:57 -0700 Subject: [PATCH 4/6] repositories now use the new entity package --- internal/repository/alertDiscord.go | 16 ++--- internal/repository/alertDiscord_test.go | 2 +- internal/repository/article.go | 62 +++++++++---------- internal/repository/discordWebHooks.go | 40 ++++++------ internal/repository/refreshTokens.go | 20 +++--- internal/repository/source.go | 56 ++++++++--------- internal/repository/source_test.go | 2 +- internal/repository/userSourceSubscription.go | 28 ++++----- internal/repository/users.go | 26 ++++---- 9 files changed, 126 insertions(+), 126 deletions(-) diff --git a/internal/repository/alertDiscord.go b/internal/repository/alertDiscord.go index e6ef6de..466d542 100644 --- a/internal/repository/alertDiscord.go +++ b/internal/repository/alertDiscord.go @@ -7,7 +7,7 @@ import ( "fmt" "time" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/entity" "github.com/huandu/go-sqlbuilder" ) @@ -16,7 +16,7 @@ type AlertDiscordRepo interface { SoftDelete(ctx context.Context, id int64) (int64, error) Restore(ctx context.Context, id int64) (int64, error) Delete(ctx context.Context, id int64) (int64, error) - ListByUser(ctx context.Context, page, limit int, userId int64) ([]domain.AlertDiscordEntity, error) + ListByUser(ctx context.Context, page, limit int, userId int64) ([]entity.AlertDiscordEntity, error) } type alertDiscordRepository struct { @@ -61,7 +61,7 @@ func (r alertDiscordRepository) Delete(ctx context.Context, id int64) (int64, er return deleteFromTable(ctx, r.conn, "AlertDiscord", id) } -func (r alertDiscordRepository) ListByUser(ctx context.Context, page, limit int, userId int64) ([]domain.AlertDiscordEntity, error) { +func (r alertDiscordRepository) ListByUser(ctx context.Context, page, limit int, userId int64) ([]entity.AlertDiscordEntity, error) { builder := sqlbuilder.NewSelectBuilder() builder.Select("*") builder.From("AlertDiscord") @@ -74,19 +74,19 @@ func (r alertDiscordRepository) ListByUser(ctx context.Context, page, limit int, query, args := builder.Build() rows, err := r.conn.QueryContext(ctx, query, args...) if err != nil { - return []domain.AlertDiscordEntity{}, err + return []entity.AlertDiscordEntity{}, err } data := r.processRows(rows) if len(data) == 0 { - return []domain.AlertDiscordEntity{}, errors.New(ErrUserNotFound) + return []entity.AlertDiscordEntity{}, errors.New(ErrUserNotFound) } return data, nil } -func (ur alertDiscordRepository) processRows(rows *sql.Rows) []domain.AlertDiscordEntity { - items := []domain.AlertDiscordEntity{} +func (ur alertDiscordRepository) processRows(rows *sql.Rows) []entity.AlertDiscordEntity { + items := []entity.AlertDiscordEntity{} for rows.Next() { var id int64 @@ -105,7 +105,7 @@ func (ur alertDiscordRepository) processRows(rows *sql.Rows) []domain.AlertDisco fmt.Println(err) } - item := domain.AlertDiscordEntity{ + item := entity.AlertDiscordEntity{ ID: id, CreatedAt: createdAt, UpdatedAt: updatedAt, diff --git a/internal/repository/alertDiscord_test.go b/internal/repository/alertDiscord_test.go index e9cadcb..a9683a2 100644 --- a/internal/repository/alertDiscord_test.go +++ b/internal/repository/alertDiscord_test.go @@ -5,7 +5,7 @@ import ( "testing" "time" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/domain" "git.jamestombleson.com/jtom38/newsbot-api/internal/repository" ) diff --git a/internal/repository/article.go b/internal/repository/article.go index 928a957..a11e518 100644 --- a/internal/repository/article.go +++ b/internal/repository/article.go @@ -7,7 +7,7 @@ import ( "fmt" "time" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/entity" "github.com/huandu/go-sqlbuilder" ) @@ -17,14 +17,14 @@ const ( ) type ArticlesRepo interface { - GetById(ctx context.Context, id int64) (domain.ArticleEntity, error) - GetByUrl(ctx context.Context, url string) (domain.ArticleEntity, error) - ListTop(ctx context.Context, limit int) ([]domain.ArticleEntity, error) - ListByPage(ctx context.Context, page, limit int) ([]domain.ArticleEntity, error) - ListByPublishDate(ctx context.Context, page, limit int, orderBy string) ([]domain.ArticleEntity, error) - ListBySource(ctx context.Context, page, limit, sourceId int, orderBy string) ([]domain.ArticleEntity, error) + GetById(ctx context.Context, id int64) (entity.ArticleEntity, error) + GetByUrl(ctx context.Context, url string) (entity.ArticleEntity, error) + ListTop(ctx context.Context, limit int) ([]entity.ArticleEntity, error) + ListByPage(ctx context.Context, page, limit int) ([]entity.ArticleEntity, error) + ListByPublishDate(ctx context.Context, page, limit int, orderBy string) ([]entity.ArticleEntity, error) + ListBySource(ctx context.Context, page, limit, sourceId int, orderBy string) ([]entity.ArticleEntity, error) Create(ctx context.Context, sourceId int64, tags, title, url, thumbnailUrl, description, authorName, authorImageUrl string, pubDate time.Time, isVideo bool) (int64, error) - CreateFromEntity(ctx context.Context, entity domain.ArticleEntity) (int64, error) + CreateFromEntity(ctx context.Context, entity entity.ArticleEntity) (int64, error) } type ArticleRepository struct { @@ -41,7 +41,7 @@ func NewArticleRepository(conn *sql.DB) ArticleRepository { } } -func (ar ArticleRepository) GetById(ctx context.Context, id int64) (domain.ArticleEntity, error) { +func (ar ArticleRepository) GetById(ctx context.Context, id int64) (entity.ArticleEntity, error) { builder := sqlbuilder.NewSelectBuilder() builder.Select("*") builder.From("articles").Where( @@ -52,18 +52,18 @@ func (ar ArticleRepository) GetById(ctx context.Context, id int64) (domain.Artic query, args := builder.Build() rows, err := ar.conn.QueryContext(ctx, query, args...) if err != nil { - return domain.ArticleEntity{}, err + return entity.ArticleEntity{}, err } data := ar.processRows(rows) if len(data) == 0 { - return domain.ArticleEntity{}, errors.New(ErrUserNotFound) + return entity.ArticleEntity{}, errors.New(ErrUserNotFound) } return data[0], nil } -func (ar ArticleRepository) GetByUrl(ctx context.Context, url string) (domain.ArticleEntity, error) { +func (ar ArticleRepository) GetByUrl(ctx context.Context, url string) (entity.ArticleEntity, error) { builder := sqlbuilder.NewSelectBuilder() builder.Select("*") builder.From("articles").Where( @@ -74,18 +74,18 @@ func (ar ArticleRepository) GetByUrl(ctx context.Context, url string) (domain.Ar query, args := builder.Build() rows, err := ar.conn.QueryContext(ctx, query, args...) if err != nil { - return domain.ArticleEntity{}, err + return entity.ArticleEntity{}, err } data := ar.processRows(rows) if len(data) == 0 { - return domain.ArticleEntity{}, errors.New(ErrUserNotFound) + return entity.ArticleEntity{}, errors.New(ErrUserNotFound) } return data[0], nil } -func (ar ArticleRepository) ListTop(ctx context.Context, limit int) ([]domain.ArticleEntity, error) { +func (ar ArticleRepository) ListTop(ctx context.Context, limit int) ([]entity.ArticleEntity, error) { builder := sqlbuilder.NewSelectBuilder() builder.Select("*") builder.From("articles") @@ -94,18 +94,18 @@ func (ar ArticleRepository) ListTop(ctx context.Context, limit int) ([]domain.Ar query, args := builder.Build() rows, err := ar.conn.QueryContext(ctx, query, args...) if err != nil { - return []domain.ArticleEntity{}, err + return []entity.ArticleEntity{}, err } data := ar.processRows(rows) if len(data) == 0 { - return []domain.ArticleEntity{}, errors.New(ErrUserNotFound) + return []entity.ArticleEntity{}, errors.New(ErrUserNotFound) } return data, nil } -func (ar ArticleRepository) ListByPage(ctx context.Context, page, limit int) ([]domain.ArticleEntity, error) { +func (ar ArticleRepository) ListByPage(ctx context.Context, page, limit int) ([]entity.ArticleEntity, error) { builder := sqlbuilder.NewSelectBuilder() builder.Select("*") builder.From("articles") @@ -116,18 +116,18 @@ func (ar ArticleRepository) ListByPage(ctx context.Context, page, limit int) ([] query, args := builder.Build() rows, err := ar.conn.QueryContext(ctx, query, args...) if err != nil { - return []domain.ArticleEntity{}, err + return []entity.ArticleEntity{}, err } data := ar.processRows(rows) if len(data) == 0 { - return []domain.ArticleEntity{}, errors.New(ErrUserNotFound) + return []entity.ArticleEntity{}, errors.New(ErrUserNotFound) } return data, nil } -func (ar ArticleRepository) ListByPublishDate(ctx context.Context, page, limit int, orderBy string) ([]domain.ArticleEntity, error) { +func (ar ArticleRepository) ListByPublishDate(ctx context.Context, page, limit int, orderBy string) ([]entity.ArticleEntity, error) { builder := sqlbuilder.NewSelectBuilder() builder.Select("*") builder.From("articles") @@ -140,17 +140,17 @@ func (ar ArticleRepository) ListByPublishDate(ctx context.Context, page, limit i query, args := builder.Build() rows, err := ar.conn.QueryContext(ctx, query, args...) if err != nil { - return []domain.ArticleEntity{}, err + return []entity.ArticleEntity{}, err } data := ar.processRows(rows) if len(data) == 0 { - return []domain.ArticleEntity{}, errors.New(ErrUserNotFound) + return []entity.ArticleEntity{}, errors.New(ErrUserNotFound) } return data, nil } -func (ar ArticleRepository) ListBySource(ctx context.Context, page, limit, sourceId int, orderBy string) ([]domain.ArticleEntity, error) { +func (ar ArticleRepository) ListBySource(ctx context.Context, page, limit, sourceId int, orderBy string) ([]entity.ArticleEntity, error) { builder := sqlbuilder.NewSelectBuilder() builder.Select("*") builder.From("articles") @@ -167,12 +167,12 @@ func (ar ArticleRepository) ListBySource(ctx context.Context, page, limit, sourc query, args := builder.Build() rows, err := ar.conn.QueryContext(ctx, query, args...) if err != nil { - return []domain.ArticleEntity{}, err + return []entity.ArticleEntity{}, err } data := ar.processRows(rows) if len(data) == 0 { - return []domain.ArticleEntity{}, errors.New(ErrUserNotFound) + return []entity.ArticleEntity{}, errors.New(ErrUserNotFound) } return data, nil } @@ -193,12 +193,12 @@ func (ar ArticleRepository) Create(ctx context.Context, sourceId int64, tags, ti return 1, nil } -func (ar ArticleRepository) CreateFromEntity(ctx context.Context, entity domain.ArticleEntity) (int64, error) { +func (ar ArticleRepository) CreateFromEntity(ctx context.Context, entity entity.ArticleEntity) (int64, error) { dt := time.Now() queryBuilder := sqlbuilder.NewInsertBuilder() queryBuilder.InsertInto("articles") queryBuilder.Cols("UpdatedAt", "CreatedAt", "DeletedAt", "SourceId", "Tags", "Title", "Url", "PubDate", "IsVideo", "ThumbnailUrl", "Description", "AuthorName", "AuthorImageUrl") - queryBuilder.Values(dt, dt, timeZero, entity.SourceID, entity.Tags, entity.Title, entity.Url, entity.PubDate, entity.IsVideo, entity.Thumbnail, entity.Description, entity.AuthorName, entity.AuthorImageUrl) + queryBuilder.Values(dt, dt, timeZero, entity.SourceID, entity.Tags, entity.Title, entity.Url, entity.PubDate, entity.IsVideo, entity.Thumbnail, entity.Description, entity.AuthorName, entity.AuthorImageUrl) query, args := queryBuilder.Build() _, err := ar.conn.ExecContext(ctx, query, args...) @@ -209,8 +209,8 @@ func (ar ArticleRepository) CreateFromEntity(ctx context.Context, entity domain. return 1, nil } -func (ur ArticleRepository) processRows(rows *sql.Rows) []domain.ArticleEntity { - items := []domain.ArticleEntity{} +func (ur ArticleRepository) processRows(rows *sql.Rows) []entity.ArticleEntity { + items := []entity.ArticleEntity{} for rows.Next() { var id int64 @@ -237,7 +237,7 @@ func (ur ArticleRepository) processRows(rows *sql.Rows) []domain.ArticleEntity { fmt.Println(err) } - item := domain.ArticleEntity{ + item := entity.ArticleEntity{ ID: id, CreatedAt: createdAt, UpdatedAt: updatedAt, diff --git a/internal/repository/discordWebHooks.go b/internal/repository/discordWebHooks.go index b87ef86..25455b7 100644 --- a/internal/repository/discordWebHooks.go +++ b/internal/repository/discordWebHooks.go @@ -5,7 +5,7 @@ import ( "database/sql" "time" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/entity" "github.com/huandu/go-sqlbuilder" ) @@ -16,10 +16,10 @@ type DiscordWebHookRepo interface { SoftDelete(ctx context.Context, id int64) (int64, error) Restore(ctx context.Context, id int64) (int64, error) Delete(ctx context.Context, id int64) (int64, error) - GetById(ctx context.Context, id int64) (domain.DiscordWebHookEntity, error) - GetByUrl(ctx context.Context, url string) (domain.DiscordWebHookEntity, error) - ListByServerName(ctx context.Context, name string) ([]domain.DiscordWebHookEntity, error) - ListByServerAndChannel(ctx context.Context, server, channel string) ([]domain.DiscordWebHookEntity, error) + GetById(ctx context.Context, id int64) (entity.DiscordWebHookEntity, error) + GetByUrl(ctx context.Context, url string) (entity.DiscordWebHookEntity, error) + ListByServerName(ctx context.Context, name string) ([]entity.DiscordWebHookEntity, error) + ListByServerAndChannel(ctx context.Context, server, channel string) ([]entity.DiscordWebHookEntity, error) } type discordWebHookRepository struct { @@ -100,7 +100,7 @@ func (r discordWebHookRepository) Delete(ctx context.Context, id int64) (int64, return deleteFromTable(ctx, r.conn, "DiscordWebHooks", id) } -func (r discordWebHookRepository) GetById(ctx context.Context, id int64) (domain.DiscordWebHookEntity, error) { +func (r discordWebHookRepository) GetById(ctx context.Context, id int64) (entity.DiscordWebHookEntity, error) { builder := sqlbuilder.NewSelectBuilder() builder.Select("*") builder.From("DiscordWebHooks").Where( @@ -111,18 +111,18 @@ func (r discordWebHookRepository) GetById(ctx context.Context, id int64) (domain query, args := builder.Build() rows, err := r.conn.QueryContext(ctx, query, args...) if err != nil { - return domain.DiscordWebHookEntity{}, err + return entity.DiscordWebHookEntity{}, err } data, err := r.processRows(rows) if len(data) == 0 { - return domain.DiscordWebHookEntity{}, err + return entity.DiscordWebHookEntity{}, err } return data[0], nil } -func (r discordWebHookRepository) GetByUrl(ctx context.Context, url string) (domain.DiscordWebHookEntity, error) { +func (r discordWebHookRepository) GetByUrl(ctx context.Context, url string) (entity.DiscordWebHookEntity, error) { builder := sqlbuilder.NewSelectBuilder() builder.Select("*") builder.From("DiscordWebHooks").Where( @@ -133,18 +133,18 @@ func (r discordWebHookRepository) GetByUrl(ctx context.Context, url string) (dom query, args := builder.Build() rows, err := r.conn.QueryContext(ctx, query, args...) if err != nil { - return domain.DiscordWebHookEntity{}, err + return entity.DiscordWebHookEntity{}, err } data, err := r.processRows(rows) if len(data) == 0 { - return domain.DiscordWebHookEntity{}, err + return entity.DiscordWebHookEntity{}, err } return data[0], nil } -func (r discordWebHookRepository) ListByServerName(ctx context.Context, name string) ([]domain.DiscordWebHookEntity, error) { +func (r discordWebHookRepository) ListByServerName(ctx context.Context, name string) ([]entity.DiscordWebHookEntity, error) { builder := sqlbuilder.NewSelectBuilder() builder.Select("*") builder.From("DiscordWebHooks").Where( @@ -154,18 +154,18 @@ func (r discordWebHookRepository) ListByServerName(ctx context.Context, name str query, args := builder.Build() rows, err := r.conn.QueryContext(ctx, query, args...) if err != nil { - return []domain.DiscordWebHookEntity{}, err + return []entity.DiscordWebHookEntity{}, err } data, err := r.processRows(rows) if len(data) == 0 { - return []domain.DiscordWebHookEntity{}, err + return []entity.DiscordWebHookEntity{}, err } return data, nil } -func (r discordWebHookRepository) ListByServerAndChannel(ctx context.Context, server, channel string) ([]domain.DiscordWebHookEntity, error) { +func (r discordWebHookRepository) ListByServerAndChannel(ctx context.Context, server, channel string) ([]entity.DiscordWebHookEntity, error) { builder := sqlbuilder.NewSelectBuilder() builder.Select("*") builder.From("DiscordWebHooks").Where( @@ -176,19 +176,19 @@ func (r discordWebHookRepository) ListByServerAndChannel(ctx context.Context, se query, args := builder.Build() rows, err := r.conn.QueryContext(ctx, query, args...) if err != nil { - return []domain.DiscordWebHookEntity{}, err + return []entity.DiscordWebHookEntity{}, err } data, err := r.processRows(rows) if len(data) == 0 { - return []domain.DiscordWebHookEntity{}, err + return []entity.DiscordWebHookEntity{}, err } return data, nil } -func (r discordWebHookRepository) processRows(rows *sql.Rows) ([]domain.DiscordWebHookEntity, error) { - items := []domain.DiscordWebHookEntity{} +func (r discordWebHookRepository) processRows(rows *sql.Rows) ([]entity.DiscordWebHookEntity, error) { + items := []entity.DiscordWebHookEntity{} for rows.Next() { var id int64 @@ -209,7 +209,7 @@ func (r discordWebHookRepository) processRows(rows *sql.Rows) ([]domain.DiscordW return items, err } - item := domain.DiscordWebHookEntity{ + item := entity.DiscordWebHookEntity{ ID: id, CreatedAt: createdAt, UpdatedAt: updatedAt, diff --git a/internal/repository/refreshTokens.go b/internal/repository/refreshTokens.go index ec7fc6f..382a69a 100644 --- a/internal/repository/refreshTokens.go +++ b/internal/repository/refreshTokens.go @@ -7,7 +7,7 @@ import ( "fmt" "time" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/entity" "github.com/huandu/go-sqlbuilder" ) @@ -17,7 +17,7 @@ const ( type RefreshToken interface { Create(ctx context.Context, username string, token string) (int64, error) - GetByUsername(ctx context.Context, name string) (domain.RefreshTokenEntity, error) + GetByUsername(ctx context.Context, name string) (entity.RefreshTokenEntity, error) DeleteById(ctx context.Context, id int64) (int64, error) } @@ -47,7 +47,7 @@ func (rt RefreshTokenRepository) Create(ctx context.Context, username string, to return 1, nil } -func (rt RefreshTokenRepository) GetByUsername(ctx context.Context, name string) (domain.RefreshTokenEntity, error) { +func (rt RefreshTokenRepository) GetByUsername(ctx context.Context, name string) (entity.RefreshTokenEntity, error) { builder := sqlbuilder.NewSelectBuilder() builder.Select("*").From(refreshTokenTableName).Where( builder.E("Username", name), @@ -56,12 +56,12 @@ func (rt RefreshTokenRepository) GetByUsername(ctx context.Context, name string) query, args := builder.Build() rows, err := rt.connection.QueryContext(ctx, query, args...) if err != nil { - return domain.RefreshTokenEntity{}, err + return entity.RefreshTokenEntity{}, err } data := rt.processRows(rows) if len(data) == 0 { - return domain.RefreshTokenEntity{}, errors.New("no token found for user") + return entity.RefreshTokenEntity{}, errors.New("no token found for user") } return data[0], nil @@ -83,8 +83,8 @@ func (rt RefreshTokenRepository) DeleteById(ctx context.Context, id int64) (int6 return rows.RowsAffected() } -func (rd RefreshTokenRepository) processRows(rows *sql.Rows) []domain.RefreshTokenEntity { - items := []domain.RefreshTokenEntity{} +func (rd RefreshTokenRepository) processRows(rows *sql.Rows) []entity.RefreshTokenEntity { + items := []entity.RefreshTokenEntity{} for rows.Next() { var id int64 @@ -99,15 +99,15 @@ func (rd RefreshTokenRepository) processRows(rows *sql.Rows) []domain.RefreshTok fmt.Println(err) } - item := domain.RefreshTokenEntity{ + item := entity.RefreshTokenEntity{ ID: id, Username: username, Token: token, CreatedAt: createdAt, UpdatedAt: updatedAt, } - - if (deletedAt.Valid) { + + if deletedAt.Valid { item.DeletedAt = deletedAt.Time } diff --git a/internal/repository/source.go b/internal/repository/source.go index b534c4b..203672d 100644 --- a/internal/repository/source.go +++ b/internal/repository/source.go @@ -5,18 +5,18 @@ import ( "database/sql" "time" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/entity" "github.com/huandu/go-sqlbuilder" ) type Sources interface { Create(ctx context.Context, source, displayName, url, tags string, enabled bool) (int64, error) - GetById(ctx context.Context, id int64) (domain.SourceEntity, error) - GetByDisplayName(ctx context.Context, displayName string) (domain.SourceEntity, error) - GetBySource(ctx context.Context, source string) (domain.SourceEntity, error) - GetBySourceAndName(ctx context.Context, source, name string) (domain.SourceEntity, error) - List(ctx context.Context, page, limit int) ([]domain.SourceEntity, error) - ListBySource(ctx context.Context, page, limit int, source string) ([]domain.SourceEntity, error) + GetById(ctx context.Context, id int64) (entity.SourceEntity, error) + GetByDisplayName(ctx context.Context, displayName string) (entity.SourceEntity, error) + GetBySource(ctx context.Context, source string) (entity.SourceEntity, error) + GetBySourceAndName(ctx context.Context, source, name string) (entity.SourceEntity, error) + List(ctx context.Context, page, limit int) ([]entity.SourceEntity, error) + ListBySource(ctx context.Context, page, limit int, source string) ([]entity.SourceEntity, error) Enable(ctx context.Context, id int64) (int64, error) Disable(ctx context.Context, id int64) (int64, error) SoftDelete(ctx context.Context, id int64) (int64, error) @@ -50,7 +50,7 @@ func (r sourceRepository) Create(ctx context.Context, source, displayName, url, return 1, nil } -func (r sourceRepository) GetById(ctx context.Context, id int64) (domain.SourceEntity, error) { +func (r sourceRepository) GetById(ctx context.Context, id int64) (entity.SourceEntity, error) { b := sqlbuilder.NewSelectBuilder() b.Select("*") b.From("Sources").Where( @@ -61,18 +61,18 @@ func (r sourceRepository) GetById(ctx context.Context, id int64) (domain.SourceE rows, err := r.conn.QueryContext(ctx, query, args...) if err != nil { - return domain.SourceEntity{}, err + return entity.SourceEntity{}, err } data, err := r.processRows(rows) if len(data) == 0 { - return domain.SourceEntity{}, err + return entity.SourceEntity{}, err } return data[0], nil } -func (r sourceRepository) GetByDisplayName(ctx context.Context, displayName string) (domain.SourceEntity, error) { +func (r sourceRepository) GetByDisplayName(ctx context.Context, displayName string) (entity.SourceEntity, error) { b := sqlbuilder.NewSelectBuilder() b.Select("*") b.From("Sources").Where( @@ -83,18 +83,18 @@ func (r sourceRepository) GetByDisplayName(ctx context.Context, displayName stri rows, err := r.conn.QueryContext(ctx, query, args...) if err != nil { - return domain.SourceEntity{}, err + return entity.SourceEntity{}, err } data, err := r.processRows(rows) if len(data) == 0 { - return domain.SourceEntity{}, err + return entity.SourceEntity{}, err } return data[0], nil } -func (r sourceRepository) GetBySource(ctx context.Context, source string) (domain.SourceEntity, error) { +func (r sourceRepository) GetBySource(ctx context.Context, source string) (entity.SourceEntity, error) { b := sqlbuilder.NewSelectBuilder() b.Select("*") b.From("Sources").Where( @@ -105,18 +105,18 @@ func (r sourceRepository) GetBySource(ctx context.Context, source string) (domai rows, err := r.conn.QueryContext(ctx, query, args...) if err != nil { - return domain.SourceEntity{}, err + return entity.SourceEntity{}, err } data, err := r.processRows(rows) if len(data) == 0 { - return domain.SourceEntity{}, err + return entity.SourceEntity{}, err } return data[0], nil } -func (r sourceRepository) GetBySourceAndName(ctx context.Context, source, name string) (domain.SourceEntity, error) { +func (r sourceRepository) GetBySourceAndName(ctx context.Context, source, name string) (entity.SourceEntity, error) { b := sqlbuilder.NewSelectBuilder() b.Select("*") b.From("Sources").Where( @@ -128,18 +128,18 @@ func (r sourceRepository) GetBySourceAndName(ctx context.Context, source, name s rows, err := r.conn.QueryContext(ctx, query, args...) if err != nil { - return domain.SourceEntity{}, err + return entity.SourceEntity{}, err } data, err := r.processRows(rows) if len(data) == 0 { - return domain.SourceEntity{}, err + return entity.SourceEntity{}, err } return data[0], nil } -func (r sourceRepository) List(ctx context.Context, page, limit int) ([]domain.SourceEntity, error) { +func (r sourceRepository) List(ctx context.Context, page, limit int) ([]entity.SourceEntity, error) { builder := sqlbuilder.NewSelectBuilder() builder.Select("*") builder.From("Sources") @@ -149,18 +149,18 @@ func (r sourceRepository) List(ctx context.Context, page, limit int) ([]domain.S query, args := builder.Build() rows, err := r.conn.QueryContext(ctx, query, args...) if err != nil { - return []domain.SourceEntity{}, err + return []entity.SourceEntity{}, err } data, err := r.processRows(rows) if len(data) == 0 { - return []domain.SourceEntity{}, err + return []entity.SourceEntity{}, err } return data, nil } -func (r sourceRepository) ListBySource(ctx context.Context, page, limit int, source string) ([]domain.SourceEntity, error) { +func (r sourceRepository) ListBySource(ctx context.Context, page, limit int, source string) ([]entity.SourceEntity, error) { builder := sqlbuilder.NewSelectBuilder() builder.Select("*") builder.From("Sources") @@ -173,12 +173,12 @@ func (r sourceRepository) ListBySource(ctx context.Context, page, limit int, sou query, args := builder.Build() rows, err := r.conn.QueryContext(ctx, query, args...) if err != nil { - return []domain.SourceEntity{}, err + return []entity.SourceEntity{}, err } data, err := r.processRows(rows) if len(data) == 0 { - return []domain.SourceEntity{}, err + return []entity.SourceEntity{}, err } return data, nil @@ -236,8 +236,8 @@ func (r sourceRepository) Delete(ctx context.Context, id int64) (int64, error) { return deleteFromTable(ctx, r.conn, "Sources", id) } -func (r sourceRepository) processRows(rows *sql.Rows) ([]domain.SourceEntity, error) { - items := []domain.SourceEntity{} +func (r sourceRepository) processRows(rows *sql.Rows) ([]entity.SourceEntity, error) { + items := []entity.SourceEntity{} for rows.Next() { var id int64 @@ -258,7 +258,7 @@ func (r sourceRepository) processRows(rows *sql.Rows) ([]domain.SourceEntity, er return items, err } - item := domain.SourceEntity{ + item := entity.SourceEntity{ ID: id, CreatedAt: createdAt, UpdatedAt: updatedAt, diff --git a/internal/repository/source_test.go b/internal/repository/source_test.go index d262322..08c7ad0 100644 --- a/internal/repository/source_test.go +++ b/internal/repository/source_test.go @@ -4,7 +4,7 @@ import ( "context" "testing" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/domain" "git.jamestombleson.com/jtom38/newsbot-api/internal/repository" ) diff --git a/internal/repository/userSourceSubscription.go b/internal/repository/userSourceSubscription.go index 7a9a5c2..94b4bd8 100644 --- a/internal/repository/userSourceSubscription.go +++ b/internal/repository/userSourceSubscription.go @@ -7,7 +7,7 @@ import ( "fmt" "time" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/entity" "github.com/huandu/go-sqlbuilder" ) @@ -16,7 +16,7 @@ type UserSourceRepo interface { SoftDelete(ctx context.Context, id int64) (int64, error) Restore(ctx context.Context, id int64) (int64, error) Delete(ctx context.Context, id int64) (int64, error) - ListByUser(ctx context.Context, page, limit int, userId int64) ([]domain.UserSourceSubscriptionEntity, error) + ListByUser(ctx context.Context, page, limit int, userId int64) ([]entity.UserSourceSubscriptionEntity, error) } type userSourceRepository struct { @@ -61,7 +61,7 @@ func (r userSourceRepository) Delete(ctx context.Context, id int64) (int64, erro return deleteFromTable(ctx, r.conn, "UserSourceSubscriptions", id) } -func (r userSourceRepository) ListByUser(ctx context.Context, page, limit int, userId int64) ([]domain.UserSourceSubscriptionEntity, error) { +func (r userSourceRepository) ListByUser(ctx context.Context, page, limit int, userId int64) ([]entity.UserSourceSubscriptionEntity, error) { builder := sqlbuilder.NewSelectBuilder() builder.Select("*") builder.From("UserSourceSubscriptions") @@ -74,19 +74,19 @@ func (r userSourceRepository) ListByUser(ctx context.Context, page, limit int, u query, args := builder.Build() rows, err := r.conn.QueryContext(ctx, query, args...) if err != nil { - return []domain.UserSourceSubscriptionEntity{}, err + return []entity.UserSourceSubscriptionEntity{}, err } data := r.processRows(rows) if len(data) == 0 { - return []domain.UserSourceSubscriptionEntity{}, errors.New(ErrUserNotFound) + return []entity.UserSourceSubscriptionEntity{}, errors.New(ErrUserNotFound) } return data, nil } -func (ur userSourceRepository) processRows(rows *sql.Rows) []domain.UserSourceSubscriptionEntity { - items := []domain.UserSourceSubscriptionEntity{} +func (ur userSourceRepository) processRows(rows *sql.Rows) []entity.UserSourceSubscriptionEntity { + items := []entity.UserSourceSubscriptionEntity{} for rows.Next() { var id int64 @@ -104,13 +104,13 @@ func (ur userSourceRepository) processRows(rows *sql.Rows) []domain.UserSourceSu fmt.Println(err) } - item := domain.UserSourceSubscriptionEntity{ - ID: id, - CreatedAt: createdAt, - UpdatedAt: updatedAt, - DeletedAt: deletedAt, - UserID: userId, - SourceID: sourceId, + item := entity.UserSourceSubscriptionEntity{ + ID: id, + CreatedAt: createdAt, + UpdatedAt: updatedAt, + DeletedAt: deletedAt, + UserID: userId, + SourceID: sourceId, } items = append(items, item) diff --git a/internal/repository/users.go b/internal/repository/users.go index c6a4d1e..fbad225 100644 --- a/internal/repository/users.go +++ b/internal/repository/users.go @@ -7,7 +7,7 @@ import ( "fmt" "time" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/entity" "github.com/huandu/go-sqlbuilder" "golang.org/x/crypto/bcrypt" @@ -19,9 +19,9 @@ const ( ) type Users interface { - GetByName(ctx context.Context, name string) (domain.UserEntity, error) + GetByName(ctx context.Context, name string) (entity.UserEntity, error) Create(ctx context.Context, name, password, scope string) (int64, error) - Update(ctx context.Context, id int, entity domain.UserEntity) error + Update(ctx context.Context, id int, entity entity.UserEntity) error UpdatePassword(ctx context.Context, name, password string) error CheckUserHash(ctx context.Context, name, password string) error UpdateScopes(ctx context.Context, name, scope string) error @@ -38,7 +38,7 @@ type userRepository struct { connection *sql.DB } -func (ur userRepository) GetByName(ctx context.Context, name string) (domain.UserEntity, error) { +func (ur userRepository) GetByName(ctx context.Context, name string) (entity.UserEntity, error) { builder := sqlbuilder.NewSelectBuilder() builder.Select("*").From("users").Where( builder.E("Name", name), @@ -47,18 +47,18 @@ func (ur userRepository) GetByName(ctx context.Context, name string) (domain.Use rows, err := ur.connection.QueryContext(ctx, query, args...) if err != nil { - return domain.UserEntity{}, err + return entity.UserEntity{}, err } data := ur.processRows(rows) if len(data) == 0 { - return domain.UserEntity{}, errors.New(ErrUserNotFound) + return entity.UserEntity{}, errors.New(ErrUserNotFound) } return data[0], nil } -func (ur userRepository) Create(ctx context.Context,name, password, scope string) (int64, error) { +func (ur userRepository) Create(ctx context.Context, name, password, scope string) (int64, error) { passwordBytes := []byte(password) hash, err := bcrypt.GenerateFromPassword(passwordBytes, bcrypt.DefaultCost) if err != nil { @@ -80,7 +80,7 @@ func (ur userRepository) Create(ctx context.Context,name, password, scope string return 1, nil } -func (ur userRepository) Update(ctx context.Context, id int, entity domain.UserEntity) error { +func (ur userRepository) Update(ctx context.Context, id int, entity entity.UserEntity) error { return errors.New("not implemented") } @@ -98,7 +98,7 @@ func (ur userRepository) UpdatePassword(ctx context.Context, name, password stri // If the hash matches what we have in the database, an error will not be returned. // If the user does not exist or the hash does not match, an error will be returned -func (ur userRepository) CheckUserHash(ctx context.Context,name, password string) error { +func (ur userRepository) CheckUserHash(ctx context.Context, name, password string) error { record, err := ur.GetByName(ctx, name) if err != nil { return err @@ -112,7 +112,7 @@ func (ur userRepository) CheckUserHash(ctx context.Context,name, password string return nil } -func (ur userRepository) UpdateScopes(ctx context.Context,name, scope string) error { +func (ur userRepository) UpdateScopes(ctx context.Context, name, scope string) error { builder := sqlbuilder.NewUpdateBuilder() builder.Update("users") builder.Set( @@ -130,8 +130,8 @@ func (ur userRepository) UpdateScopes(ctx context.Context,name, scope string) er return nil } -func (ur userRepository) processRows(rows *sql.Rows) []domain.UserEntity { - items := []domain.UserEntity{} +func (ur userRepository) processRows(rows *sql.Rows) []entity.UserEntity { + items := []entity.UserEntity{} for rows.Next() { var id int64 @@ -146,7 +146,7 @@ func (ur userRepository) processRows(rows *sql.Rows) []domain.UserEntity { fmt.Println(err) } - item := domain.UserEntity{ + item := entity.UserEntity{ ID: id, UpdatedAt: updatedAt, Username: username, -- 2.45.2 From d7f2eca4c388b6532a534f03ac68670f4d6786d8 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Thu, 9 May 2024 19:09:11 -0700 Subject: [PATCH 5/6] repo services not use entity --- internal/repositoryServices/refreshTokens.go | 6 +++--- internal/repositoryServices/userService.go | 15 ++++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/internal/repositoryServices/refreshTokens.go b/internal/repositoryServices/refreshTokens.go index ca5b800..f56ef48 100644 --- a/internal/repositoryServices/refreshTokens.go +++ b/internal/repositoryServices/refreshTokens.go @@ -5,7 +5,7 @@ import ( "database/sql" "errors" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/entity" "git.jamestombleson.com/jtom38/newsbot-api/internal/repository" "github.com/google/uuid" ) @@ -16,7 +16,7 @@ const ( type RefreshToken interface { Create(ctx context.Context, username string) (string, error) - GetByName(ctx context.Context, name string) (domain.RefreshTokenEntity, error) + GetByName(ctx context.Context, name string) (entity.RefreshTokenEntity, error) Delete(ctx context.Context, id int64) (int64, error) IsRequestValid(ctx context.Context, username, refreshToken string) error } @@ -64,7 +64,7 @@ func (rt RefreshTokenService) Create(ctx context.Context, username string) (stri } // Find the saved refresh token for a user and return it if it exists -func (rt RefreshTokenService) GetByName(ctx context.Context, name string) (domain.RefreshTokenEntity, error) { +func (rt RefreshTokenService) GetByName(ctx context.Context, name string) (entity.RefreshTokenEntity, error) { return rt.table.GetByUsername(ctx, name) } diff --git a/internal/repositoryServices/userService.go b/internal/repositoryServices/userService.go index 7a4bed2..2bd52d8 100644 --- a/internal/repositoryServices/userService.go +++ b/internal/repositoryServices/userService.go @@ -6,7 +6,8 @@ import ( "errors" "strings" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/domain" + "git.jamestombleson.com/jtom38/newsbot-api/internal/entity" "git.jamestombleson.com/jtom38/newsbot-api/internal/repository" "golang.org/x/crypto/bcrypt" @@ -21,10 +22,10 @@ const ( type UserServices interface { DoesUserExist(ctx context.Context, username string) error DoesPasswordMatchHash(ctx context.Context, username, password string) error - GetUser(ctx context.Context, username string) (domain.UserEntity, error) + GetUser(ctx context.Context, username string) (entity.UserEntity, error) AddScopes(ctx context.Context, username string, scopes []string) error RemoveScopes(ctx context.Context, username string, scopes []string) error - Create(ctx context.Context, name, password, scope string) (domain.UserEntity, error) + Create(ctx context.Context, name, password, scope string) (entity.UserEntity, error) CheckPasswordForRequirements(password string) error } @@ -63,7 +64,7 @@ func (us UserService) DoesPasswordMatchHash(ctx context.Context, username, passw return nil } -func (us UserService) GetUser(ctx context.Context, username string) (domain.UserEntity, error) { +func (us UserService) GetUser(ctx context.Context, username string) (entity.UserEntity, error) { return us.repo.GetByName(ctx, username) } @@ -124,14 +125,14 @@ func (us UserService) doesScopeExist(scopes []string, target string) bool { return false } -func (us UserService) Create(ctx context.Context, name, password, scope string) (domain.UserEntity, error) { +func (us UserService) Create(ctx context.Context, name, password, scope string) (entity.UserEntity, error) { err := us.CheckPasswordForRequirements(password) if err != nil { - return domain.UserEntity{}, err + return entity.UserEntity{}, err } us.repo.Create(ctx, name, password, domain.ScopeArticleRead) - return domain.UserEntity{}, nil + return entity.UserEntity{}, nil } func (us UserService) CheckPasswordForRequirements(password string) error { -- 2.45.2 From a818b892f4a1c1e93396d057c453ed67395c8efb Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Thu, 9 May 2024 19:09:32 -0700 Subject: [PATCH 6/6] domain path update for source const --- internal/services/cron/collectors.go | 2 +- internal/services/cron/collectors_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/services/cron/collectors.go b/internal/services/cron/collectors.go index 4d4a93d..9bae29d 100644 --- a/internal/services/cron/collectors.go +++ b/internal/services/cron/collectors.go @@ -4,7 +4,7 @@ import ( "log" "time" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/domain" "git.jamestombleson.com/jtom38/newsbot-api/internal/services/input" ) diff --git a/internal/services/cron/collectors_test.go b/internal/services/cron/collectors_test.go index 090d12d..5bd12ae 100644 --- a/internal/services/cron/collectors_test.go +++ b/internal/services/cron/collectors_test.go @@ -4,7 +4,7 @@ import ( "context" "testing" - "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-api/domain" "git.jamestombleson.com/jtom38/newsbot-api/internal/services" "git.jamestombleson.com/jtom38/newsbot-api/internal/services/cron" ) -- 2.45.2