refactor: reorganized code structure

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2024-06-28 12:15:51 +02:00
parent 5de00e6861
commit 4a0b7d71a9
10 changed files with 89 additions and 94 deletions

View File

@ -133,11 +133,11 @@ func TestCheckUserToPostLink(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
got, err := CheckReferenceBetweenUserAndPost(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID)
if (err != nil) != tt.wantErr {
t.Errorf("CheckReferenceBetweenUserAndPost() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("CheckIfUserHasPostAsFavorite() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("CheckReferenceBetweenUserAndPost() got = %v, want %v", got, tt.want)
t.Errorf("CheckIfUserHasPostAsFavorite() got = %v, want %v", got, tt.want)
}
})
}
@ -266,11 +266,11 @@ func TestCheckUserToPostLinkWithNoData(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
got, err := CheckReferenceBetweenUserAndPost(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID)
if (err != nil) != tt.wantErr {
t.Errorf("CheckReferenceBetweenUserAndPost() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("CheckIfUserHasPostAsFavorite() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("CheckReferenceBetweenUserAndPost() got = %v, want %v", got, tt.want)
t.Errorf("CheckIfUserHasPostAsFavorite() got = %v, want %v", got, tt.want)
}
})
}

View File

@ -169,9 +169,8 @@ func GetUserSourceLinks(ctx context.Context, db *gorm.DB, anthroveUserID models.
return userSourceMap, nil
}
func GetUserSourceBySourceID(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID) (map[string]models.UserSource, error) {
var userSources []models.UserSource
userSourceMap := make(map[string]models.UserSource)
func GetUserSourceBySourceID(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID) (*models.UserSource, error) {
var userSource *models.UserSource
if anthroveUserID == "" {
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
@ -189,7 +188,7 @@ func GetUserSourceBySourceID(ctx context.Context, db *gorm.DB, anthroveUserID mo
return nil, &otterError.EntityValidationFailed{Reason: "sourceID needs to be 25 characters long"}
}
result := db.WithContext(ctx).Model(&models.UserSource{}).InnerJoins("Source", db.Where("id = ?", sourceID)).Where("user_id = ?", string(anthroveUserID)).First(&userSources)
result := db.WithContext(ctx).Model(&models.UserSource{}).InnerJoins("Source", db.Where("id = ?", sourceID)).Where("user_id = ?", string(anthroveUserID)).First(userSource)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
@ -197,38 +196,16 @@ func GetUserSourceBySourceID(ctx context.Context, db *gorm.DB, anthroveUserID mo
return nil, result.Error
}
for _, userSource := range userSources {
var source models.Source
result = db.WithContext(ctx).Model(&models.Source{}).Where("id = ?", userSource.SourceID).First(&source)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
}
return nil, result.Error
}
userSourceMap[source.DisplayName] = models.UserSource{
UserID: userSource.AccountID,
AccountUsername: userSource.AccountUsername,
Source: models.Source{
DisplayName: source.DisplayName,
Domain: source.Domain,
Icon: source.Icon,
},
}
}
log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID,
"source_id": sourceID,
}).Trace("database: got specified user source link")
return userSourceMap, nil
return userSource, nil
}
func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]models.AnthroveUserID, error) {
func GetAllUsers(ctx context.Context, db *gorm.DB) ([]models.User, error) {
var users []models.User
var userIDs []models.AnthroveUserID
result := db.WithContext(ctx).Model(&models.User{}).Find(&users)
if result.Error != nil {
@ -238,15 +215,11 @@ func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]models.AnthroveU
return nil, result.Error
}
for _, user := range users {
userIDs = append(userIDs, user.ID)
}
log.WithFields(log.Fields{
"anthrove_user_id_count": len(userIDs),
"anthrove_user_id_count": len(users),
}).Trace("database: got all anthrove user IDs")
return userIDs, nil
return users, nil
}
func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) {

View File

@ -244,13 +244,13 @@ func TestGetAllAnthroveUserIDs(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetAllAnthroveUserIDs(tt.args.ctx, tt.args.db)
got, err := GetAllUsers(tt.args.ctx, tt.args.db)
if (err != nil) != tt.wantErr {
t.Errorf("GetAllAnthroveUserIDs() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("GetAllUsers() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetAllAnthroveUserIDs() got = %v, want %v", got, tt.want)
t.Errorf("GetAllUsers() got = %v, want %v", got, tt.want)
}
})
}
@ -558,11 +558,11 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
got, err := GetUserFavoriteWithPagination(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.skip, tt.args.limit)
if (err != nil) != tt.wantErr {
t.Errorf("GetUserFavoriteWithPagination() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("GetAllUserFavoritesWithPagination() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetUserFavoriteWithPagination() got = %v, want %v", got, tt.want)
t.Errorf("GetAllUserFavoritesWithPagination() got = %v, want %v", got, tt.want)
}
})
}
@ -798,11 +798,11 @@ func TestGetUserSourceLinks(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
got, err := GetUserSourceLinks(tt.args.ctx, tt.args.db, tt.args.anthroveUserID)
if (err != nil) != tt.wantErr {
t.Errorf("GetUserSourceLinks() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("GetAllUserSources() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetUserSourceLinks() got = %v, want %v", got, tt.want)
t.Errorf("GetAllUserSources() got = %v, want %v", got, tt.want)
}
})
}
@ -930,11 +930,11 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
got, err := GetUserTagWitRelationToFavedPosts(tt.args.ctx, tt.args.db, tt.args.anthroveUserID)
if (err != nil) != tt.wantErr {
t.Errorf("GetUserTagWitRelationToFavedPosts() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("GetAllTagsFromUser() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetUserTagWitRelationToFavedPosts() got = %v, want %v", got, tt.want)
t.Errorf("GetAllTagsFromUser() got = %v, want %v", got, tt.want)
}
})
}

View File

@ -10,8 +10,15 @@ type OtterSpace interface {
// Connect establishes a connection to the database.
Connect(ctx context.Context, config models.DatabaseConfig) error
// User contains all function that are needed to manage the AnthroveUser
User
// Source contains all function that are needed to manage the Source
Source
// TagAlias contains all function that are needed to manage the TagAlias
TagAlias
// TagGroup contains all function that are needed to manage the TagGroup
TagGroup
}

View File

@ -97,7 +97,7 @@ func (p *postgresqlConnection) CreateReferenceBetweenUserAndPost(ctx context.Con
return postgres.CreateReferenceBetweenUserAndPost(ctx, p.db, anthroveUserID, anthrovePostID)
}
func (p *postgresqlConnection) CheckReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) (bool, error) {
func (p *postgresqlConnection) CheckIfUserHasPostAsFavorite(ctx context.Context, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) (bool, error) {
return postgres.CheckReferenceBetweenUserAndPost(ctx, p.db, anthroveUserID, anthrovePostID)
}
@ -117,23 +117,23 @@ func (p *postgresqlConnection) GetUserFavoritesCount(ctx context.Context, anthro
return postgres.GetUserFavoritesCount(ctx, p.db, anthroveUserID)
}
func (p *postgresqlConnection) GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.UserSource, error) {
func (p *postgresqlConnection) GetAllUserSources(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.UserSource, error) {
return postgres.GetUserSourceLinks(ctx, p.db, anthroveUserID)
}
func (p *postgresqlConnection) GetUserSourceBySourceID(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID) (map[string]models.UserSource, error) {
func (p *postgresqlConnection) GetUserSourceBySourceID(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID) (*models.UserSource, error) {
return postgres.GetUserSourceBySourceID(ctx, p.db, anthroveUserID, sourceID)
}
func (p *postgresqlConnection) GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error) {
return postgres.GetAllAnthroveUserIDs(ctx, p.db)
func (p *postgresqlConnection) GetAllUser(ctx context.Context) ([]models.User, error) {
return postgres.GetAllUsers(ctx, p.db)
}
func (p *postgresqlConnection) GetUserFavoriteWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) {
func (p *postgresqlConnection) GetAllUserFavoritesWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) {
return postgres.GetUserFavoriteWithPagination(ctx, p.db, anthroveUserID, skip, limit)
}
func (p *postgresqlConnection) GetUserTagWitRelationToFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) {
func (p *postgresqlConnection) GetAllTagsFromUser(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) {
return postgres.GetUserTagWitRelationToFavedPosts(ctx, p.db, anthroveUserID)
}

View File

@ -714,13 +714,13 @@ func Test_postgresqlConnection_CheckReferenceBetweenUserAndPost(t *testing.T) {
db: gormDB,
debug: true,
}
got, err := p.CheckReferenceBetweenUserAndPost(tt.args.ctx, tt.args.anthroveUserID, tt.args.anthrovePostID)
got, err := p.CheckIfUserFavouritedPost(tt.args.ctx, tt.args.anthroveUserID, tt.args.anthrovePostID)
if (err != nil) != tt.wantErr {
t.Errorf("CheckReferenceBetweenUserAndPost() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("CheckIfUserHasPostAsFavorite() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("CheckReferenceBetweenUserAndPost() got = %v, want %v", got, tt.want)
t.Errorf("CheckIfUserHasPostAsFavorite() got = %v, want %v", got, tt.want)
}
})
}
@ -1207,13 +1207,13 @@ func Test_postgresqlConnection_GetUserSourceLinks(t *testing.T) {
db: gormDB,
debug: true,
}
got, err := p.GetUserSourceLinks(tt.args.ctx, tt.args.anthroveUserID)
got, err := p.GetAllUserSources(tt.args.ctx, tt.args.anthroveUserID)
if (err != nil) != tt.wantErr {
t.Errorf("GetUserSourceLinks() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("GetAllUserSources() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetUserSourceLinks() got = %v, want %v", got, tt.want)
t.Errorf("GetAllUserSources() got = %v, want %v", got, tt.want)
}
})
}
@ -1405,11 +1405,11 @@ func Test_postgresqlConnection_GetAllAnthroveUserIDs(t *testing.T) {
}
got, err := p.GetAllAnthroveUserIDs(tt.args.ctx)
if (err != nil) != tt.wantErr {
t.Errorf("GetAllAnthroveUserIDs() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("GetAllUsers() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetAllAnthroveUserIDs() got = %v, want %v", got, tt.want)
t.Errorf("GetAllUsers() got = %v, want %v", got, tt.want)
}
})
}
@ -1540,13 +1540,13 @@ func Test_postgresqlConnection_GetUserFavoriteWithPagination(t *testing.T) {
db: gormDB,
debug: true,
}
got, err := p.GetUserFavoriteWithPagination(tt.args.ctx, tt.args.anthroveUserID, tt.args.skip, tt.args.limit)
got, err := p.GetAllUserFavoritesWithPagination(tt.args.ctx, tt.args.anthroveUserID, tt.args.skip, tt.args.limit)
if (err != nil) != tt.wantErr {
t.Errorf("GetUserFavoriteWithPagination() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("GetAllUserFavoritesWithPagination() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetUserFavoriteWithPagination() got = %v, want %v", got, tt.want)
t.Errorf("GetAllUserFavoritesWithPagination() got = %v, want %v", got, tt.want)
}
})
}
@ -1654,13 +1654,13 @@ func Test_postgresqlConnection_GetUserTagWitRelationToFavedPosts(t *testing.T) {
db: gormDB,
debug: true,
}
got, err := p.GetUserTagWitRelationToFavedPosts(tt.args.ctx, tt.args.anthroveUserID)
got, err := p.GetAllTagsFromUser(tt.args.ctx, tt.args.anthroveUserID)
if (err != nil) != tt.wantErr {
t.Errorf("GetUserTagWitRelationToFavedPosts() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("GetAllTagsFromUser() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetUserTagWitRelationToFavedPosts() got = %v, want %v", got, tt.want)
t.Errorf("GetAllTagsFromUser() got = %v, want %v", got, tt.want)
}
})
}

View File

@ -7,8 +7,11 @@ import (
)
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
GetAllTagAlias(ctx context.Context) ([]models.TagAlias, error)
GetAllTagAliasByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagAlias, error)
DeleteTagAlias(ctx context.Context, tagAliasName models.AnthroveTagAliasName) error
}

View File

@ -7,8 +7,11 @@ import (
)
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)
GetAllTagGroup(ctx context.Context) ([]models.TagGroup, error)
GetAllTagGroupByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagGroup, error)
DeleteTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName) error
}

View File

@ -7,28 +7,36 @@ import (
)
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)
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
GetAllUser(ctx context.Context) ([]models.User, error)
// GetUserFavoritesCount retrieves the count of a user's favorites.
GetUserFavoritesCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error)
// GetAllUserSources retrieves the source links of a user.
GetAllUserSources(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) (*models.UserSource, error)
// GetAllUserFavoritesWithPagination retrieves a user's favorite posts with pagination.
GetAllUserFavoritesWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error)
// GetAllTagsFromUser retrieves a user's tags through their favorite posts.
GetAllTagsFromUser(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error)
// CheckIfUserHasPostAsFavorite checks if a user-post link exists.
CheckIfUserHasPostAsFavorite(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID models.AnthrovePostID) (bool, error)
}

View File

@ -3,15 +3,16 @@ package test
import (
"context"
"database/sql"
"net/url"
"strconv"
"strings"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
migrate "github.com/rubenv/sql-migrate"
postgrescontainer "github.com/testcontainers/testcontainers-go/modules/postgres"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"net/url"
"strconv"
"strings"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
@ -88,12 +89,12 @@ func DatabaseModesFromConnectionString(ctx context.Context, pgContainer *postgre
return nil, err
}
url, err := url.Parse(connectionString)
connectionStringUrl, err := url.Parse(connectionString)
if err != nil {
return nil, err
}
split := strings.Split(url.Host, ":")
split := strings.Split(connectionStringUrl.Host, ":")
host := split[0]
port, err := strconv.Atoi(split[1])
@ -101,10 +102,10 @@ func DatabaseModesFromConnectionString(ctx context.Context, pgContainer *postgre
return nil, err
}
database := strings.TrimPrefix(url.Path, "/")
database := strings.TrimPrefix(connectionStringUrl.Path, "/")
username := url.User.Username()
password, _ := url.User.Password()
username := connectionStringUrl.User.Username()
password, _ := connectionStringUrl.User.Password()
return &models.DatabaseConfig{
Endpoint: host,