From bc8b5bb4a7a91517b9d505c8136eb06b2f346ab8 Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 28 Jun 2024 10:54:42 +0200 Subject: [PATCH] refactor: reorganized code structure Signed-off-by: SoXX --- pkg/database/database.go | 72 ++------------------ pkg/database/post.go | 28 ++++++++ pkg/database/{ => postgres}/postgres.go | 5 +- pkg/database/{ => postgres}/postgres_test.go | 5 +- pkg/database/source.go | 19 ++++++ pkg/database/tag.go | 12 ++++ pkg/database/tagalias.go | 14 ++++ pkg/database/taggroup.go | 14 ++++ pkg/database/user.go | 29 ++++++++ pkg/database/usersource.go | 14 ++++ 10 files changed, 141 insertions(+), 71 deletions(-) create mode 100644 pkg/database/post.go rename pkg/database/{ => postgres}/postgres.go (98%) rename pkg/database/{ => postgres}/postgres_test.go (99%) create mode 100644 pkg/database/source.go create mode 100644 pkg/database/tag.go create mode 100644 pkg/database/tagalias.go create mode 100644 pkg/database/taggroup.go create mode 100644 pkg/database/user.go create mode 100644 pkg/database/usersource.go diff --git a/pkg/database/database.go b/pkg/database/database.go index 691bd98..c721eb4 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -10,75 +10,13 @@ type OtterSpace interface { // Connect establishes a connection to the database. Connect(ctx context.Context, config models.DatabaseConfig) error - // CreateUserWithRelationToSource adds a user with a relation to a source. - CreateUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, accountId string, accountUsername string) error + User - // CreateSource adds a new source to the database. - CreateSource(ctx context.Context, anthroveSource *models.Source) error + Source - // CreatePost adds a new post to the database. - CreatePost(ctx context.Context, anthrovePost *models.Post) error + TagAlias - // CreateTagAndReferenceToPost adds a tag with a relation to a post. - CreateTagAndReferenceToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error + TagGroup - // CreateReferenceBetweenPostAndSource links a post with a source. - CreateReferenceBetweenPostAndSource(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain, postURL models.AnthrovePostURL, config models.PostReferenceConfig) error - - // CreateReferenceBetweenUserAndPost links a user with a post. - CreateReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error - - // CheckReferenceBetweenUserAndPost checks if a user-post link exists. - CheckReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID models.AnthrovePostID) (bool, error) - - // GetPostByAnthroveID retrieves a post by its Anthrove ID. - GetPostByAnthroveID(ctx context.Context, anthrovePostID models.AnthrovePostID) (*models.Post, error) - - // GetPostByURL retrieves a post by its source URL. - GetPostByURL(ctx context.Context, postURL string) (*models.Post, error) - - // GetPostBySourceID retrieves a post by its source ID. - GetPostBySourceID(ctx context.Context, sourceID models.AnthroveSourceID) (*models.Post, error) - - // GetUserFavoritesCount retrieves the count of a user's favorites. - GetUserFavoritesCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) - - // GetUserSourceLinks retrieves the source links of a user. - GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.UserSource, error) - - // GetUserSourceBySourceID retrieves a specified source link of a user. - GetUserSourceBySourceID(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID) (map[string]models.UserSource, error) - - // GetAllAnthroveUserIDs retrieves all Anthrove user IDs. - GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error) - - // GetUserFavoriteWithPagination retrieves a user's favorite posts with pagination. - GetUserFavoriteWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) - - // GetUserTagWitRelationToFavedPosts retrieves a user's tags through their favorited posts. - GetUserTagWitRelationToFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) - - // GetAllTags retrieves all tags. - GetAllTags(ctx context.Context) ([]models.Tag, error) - - // GetAllSources retrieves all sources. - GetAllSources(ctx context.Context) ([]models.Source, error) - - // GetSourceByDomain retrieves a source by its URL. - GetSourceByDomain(ctx context.Context, sourceDomain models.AnthroveSourceDomain) (*models.Source, error) - - GetAllTagAlias(ctx context.Context) ([]models.TagAlias, error) - GetAllTagAliasByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagAlias, error) - CreateTagAlias(ctx context.Context, tagAliasName models.AnthroveTagAliasName, tagID models.AnthroveTagID) error - DeleteTagAlias(ctx context.Context, tagAliasName models.AnthroveTagAliasName) error - - GetAllTagGroup(ctx context.Context) ([]models.TagGroup, error) - GetAllTagGroupByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagGroup, error) - CreateTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName, tagID models.AnthroveTagID) - DeleteTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName) error - - UpdateUserSourceScrapeTimeInterval(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, scrapeTime models.AnthroveScrapeTimeInterval) error - UpdateUserSourceLastScrapeTime(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, lastScrapeTime models.AnthroveUserLastScrapeTime) error - - UpdateUserSourceValidation(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, valid bool) error + UserSource } diff --git a/pkg/database/post.go b/pkg/database/post.go new file mode 100644 index 0000000..7edb191 --- /dev/null +++ b/pkg/database/post.go @@ -0,0 +1,28 @@ +package database + +import ( + "context" + + "git.dragse.it/anthrove/otter-space-sdk/pkg/models" +) + +type Post interface { + + // CreatePost adds a new post to the database. + CreatePost(ctx context.Context, anthrovePost *models.Post) error + + // GetPostByAnthroveID retrieves a post by its Anthrove ID. + GetPostByAnthroveID(ctx context.Context, anthrovePostID models.AnthrovePostID) (*models.Post, error) + + // GetPostByURL retrieves a post by its source URL. + GetPostByURL(ctx context.Context, postURL string) (*models.Post, error) + + // GetPostBySourceID retrieves a post by its source ID. + GetPostBySourceID(ctx context.Context, sourceID models.AnthroveSourceID) (*models.Post, error) + + // CreateTagAndReferenceToPost adds a tag with a relation to a post. + CreateTagAndReferenceToPost(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error + + // CreateReferenceBetweenPostAndSource links a post with a source. + CreateReferenceBetweenPostAndSource(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain, postURL models.AnthrovePostURL, config models.PostReferenceConfig) error +} diff --git a/pkg/database/postgres.go b/pkg/database/postgres/postgres.go similarity index 98% rename from pkg/database/postgres.go rename to pkg/database/postgres/postgres.go index fe078dc..714438a 100644 --- a/pkg/database/postgres.go +++ b/pkg/database/postgres/postgres.go @@ -1,4 +1,4 @@ -package database +package postgres import ( "context" @@ -9,6 +9,7 @@ import ( "time" "git.dragse.it/anthrove/otter-space-sdk/internal/postgres" + "git.dragse.it/anthrove/otter-space-sdk/pkg/database" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" _ "github.com/lib/pq" migrate "github.com/rubenv/sql-migrate" @@ -26,7 +27,7 @@ type postgresqlConnection struct { debug bool } -func NewPostgresqlConnection() OtterSpace { +func NewPostgresqlConnection() database.OtterSpace { return &postgresqlConnection{ db: nil, } diff --git a/pkg/database/postgres_test.go b/pkg/database/postgres/postgres_test.go similarity index 99% rename from pkg/database/postgres_test.go rename to pkg/database/postgres/postgres_test.go index c7d4996..721752a 100644 --- a/pkg/database/postgres_test.go +++ b/pkg/database/postgres/postgres_test.go @@ -1,4 +1,4 @@ -package database +package postgres import ( "context" @@ -7,6 +7,7 @@ import ( "testing" "git.dragse.it/anthrove/otter-space-sdk/internal/postgres" + "git.dragse.it/anthrove/otter-space-sdk/pkg/database" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/test" "gorm.io/gorm" @@ -16,7 +17,7 @@ func TestNewPostgresqlConnection(t *testing.T) { // Test tests := []struct { name string - want OtterSpace + want database.OtterSpace }{ { name: "Test 1: Create new postgresql connection", diff --git a/pkg/database/source.go b/pkg/database/source.go new file mode 100644 index 0000000..776c1d1 --- /dev/null +++ b/pkg/database/source.go @@ -0,0 +1,19 @@ +package database + +import ( + "context" + + "git.dragse.it/anthrove/otter-space-sdk/pkg/models" +) + +type Source interface { + + // CreateSource adds a new source to the database. + CreateSource(ctx context.Context, anthroveSource *models.Source) error + + // GetAllSources retrieves all sources. + GetAllSources(ctx context.Context) ([]models.Source, error) + + // GetSourceByDomain retrieves a source by its URL. + GetSourceByDomain(ctx context.Context, sourceDomain models.AnthroveSourceDomain) (*models.Source, error) +} diff --git a/pkg/database/tag.go b/pkg/database/tag.go new file mode 100644 index 0000000..eb0f4a5 --- /dev/null +++ b/pkg/database/tag.go @@ -0,0 +1,12 @@ +package database + +import ( + "context" + + "git.dragse.it/anthrove/otter-space-sdk/pkg/models" +) + +type Tag interface { + // GetAllTags retrieves all tags. + GetAllTags(ctx context.Context) ([]models.Tag, error) +} diff --git a/pkg/database/tagalias.go b/pkg/database/tagalias.go new file mode 100644 index 0000000..ef5f2e5 --- /dev/null +++ b/pkg/database/tagalias.go @@ -0,0 +1,14 @@ +package database + +import ( + "context" + + "git.dragse.it/anthrove/otter-space-sdk/pkg/models" +) + +type TagAlias interface { + GetAllTagAlias(ctx context.Context) ([]models.TagAlias, error) + GetAllTagAliasByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagAlias, error) + CreateTagAlias(ctx context.Context, tagAliasName models.AnthroveTagAliasName, tagID models.AnthroveTagID) error + DeleteTagAlias(ctx context.Context, tagAliasName models.AnthroveTagAliasName) error +} diff --git a/pkg/database/taggroup.go b/pkg/database/taggroup.go new file mode 100644 index 0000000..db3918a --- /dev/null +++ b/pkg/database/taggroup.go @@ -0,0 +1,14 @@ +package database + +import ( + "context" + + "git.dragse.it/anthrove/otter-space-sdk/pkg/models" +) + +type TagGroup interface { + GetAllTagGroup(ctx context.Context) ([]models.TagGroup, error) + GetAllTagGroupByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagGroup, error) + CreateTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName, tagID models.AnthroveTagID) + DeleteTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName) error +} diff --git a/pkg/database/user.go b/pkg/database/user.go new file mode 100644 index 0000000..1b72e61 --- /dev/null +++ b/pkg/database/user.go @@ -0,0 +1,29 @@ +package database + +import ( + "context" + + "git.dragse.it/anthrove/otter-space-sdk/pkg/models" +) + +type User interface { + // GetAllAnthroveUserIDs retrieves all Anthrove user IDs. + GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error) + + // CreateUserWithRelationToSource adds a user with a relation to a source. + CreateUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, accountId string, accountUsername string) error + // CreateReferenceBetweenUserAndPost links a user with a post. + CreateReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error + // CheckReferenceBetweenUserAndPost checks if a user-post link exists. + CheckReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID models.AnthrovePostID) (bool, error) + // GetUserFavoritesCount retrieves the count of a user's favorites. + GetUserFavoritesCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) + // GetUserSourceLinks retrieves the source links of a user. + GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.UserSource, error) + // GetUserSourceBySourceID retrieves a specified source link of a user. + GetUserSourceBySourceID(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID) (map[string]models.UserSource, error) + // GetUserFavoriteWithPagination retrieves a user's favorite posts with pagination. + GetUserFavoriteWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) + // GetUserTagWitRelationToFavedPosts retrieves a user's tags through their favorited posts. + GetUserTagWitRelationToFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) +} diff --git a/pkg/database/usersource.go b/pkg/database/usersource.go new file mode 100644 index 0000000..1f30a48 --- /dev/null +++ b/pkg/database/usersource.go @@ -0,0 +1,14 @@ +package database + +import ( + "context" + + "git.dragse.it/anthrove/otter-space-sdk/pkg/models" +) + +type UserSource interface { + UpdateUserSourceScrapeTimeInterval(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, scrapeTime models.AnthroveScrapeTimeInterval) error + UpdateUserSourceLastScrapeTime(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, lastScrapeTime models.AnthroveUserLastScrapeTime) error + + UpdateUserSourceValidation(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, valid bool) error +}