refactor(postgres): a lot of things happened here
Some checks failed
Gitea Build Check / Build (push) Failing after 6m7s

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2024-06-27 21:37:29 +02:00
parent a71f759763
commit da4fda3597
3 changed files with 57 additions and 36 deletions

View File

@ -73,6 +73,10 @@ func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUs
return result.Error return result.Error
} }
if result.RowsAffected == 0 {
return &otterError.NoDataWritten{}
}
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID, "anthrove_user_id": anthroveUserID,
"source_id": sourceID, "source_id": sourceID,
@ -301,23 +305,32 @@ func GetUserTagWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthrov
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort} return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
} }
result := db.WithContext(ctx).Raw( rows, err := db.WithContext(ctx).Raw(
`WITH user_posts AS ( `WITH user_posts AS (
SELECT post_id FROM "UserFavorites" WHERE user_id = $1 SELECT post_id FROM "UserFavorites" WHERE user_id = $1
) )
SELECT post_tags.tag_name AS tag_name, count(*) AS count, (SELECT tag_type FROM "Tag" WHERE "Tag".name = post_tags.tag_name LIMIT 1) AS tag_type FROM post_tags, user_posts WHERE post_tags.post_id IN (user_posts.post_id) GROUP BY post_tags.tag_name`, anthroveUserID).Scan(&queryUserFavorites) SELECT post_tags.tag_name AS tag_name, count(*) AS count, (SELECT tag_type FROM "Tag" WHERE "Tag".name = post_tags.tag_name LIMIT 1) AS tag_type FROM post_tags, user_posts WHERE post_tags.post_id IN (user_posts.post_id) GROUP BY post_tags.tag_name ORDER BY tag_type DESC, tag_name DESC`, anthroveUserID).Rows()
if result.Error != nil { if err != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{} return nil, &otterError.NoDataFound{}
} }
return nil, result.Error return nil, err
} }
var userFavoritesFrequency = make([]models.TagsWithFrequency, len(queryUserFavorites)) var userFavoritesFrequency = make([]models.TagsWithFrequency, 0)
for i, query := range queryUserFavorites { defer rows.Close()
userFavoritesFrequency[i].Frequency = query.count for rows.Next() {
userFavoritesFrequency[i].Tags.Name = query.tagName var tagName string
userFavoritesFrequency[i].Tags.Type = query.tagType var count int64
var tagType string
rows.Scan(&tagName, &count, &tagType)
userFavoritesFrequency = append(userFavoritesFrequency, models.TagsWithFrequency{
Frequency: count,
Tags: models.Tag{
Name: tagName,
Type: models.TagType(tagType),
},
})
} }
log.WithFields(log.Fields{ log.WithFields(log.Fields{

View File

@ -870,15 +870,15 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) {
{ {
Frequency: 1, Frequency: 1,
Tags: models.Tag{ Tags: models.Tag{
Name: tags[1].Name, Name: tags[2].Name,
Type: tags[1].Type, Type: tags[2].Type,
}, },
}, },
{ {
Frequency: 1, Frequency: 1,
Tags: models.Tag{ Tags: models.Tag{
Name: tags[2].Name, Name: tags[1].Name,
Type: tags[2].Type, Type: tags[1].Type,
}, },
}, },
} }

View File

@ -469,6 +469,7 @@ func Test_postgresqlConnection_CreateReferenceBetweenPostAndSource(t *testing.T)
ctx context.Context ctx context.Context
anthrovePostID models.AnthrovePostID anthrovePostID models.AnthrovePostID
sourceDomain models.AnthroveSourceDomain sourceDomain models.AnthroveSourceDomain
postURl models.AnthrovePostURL
} }
tests := []struct { tests := []struct {
name string name string
@ -481,6 +482,7 @@ func Test_postgresqlConnection_CreateReferenceBetweenPostAndSource(t *testing.T)
ctx: ctx, ctx: ctx,
anthrovePostID: post.ID, anthrovePostID: post.ID,
sourceDomain: "e621.net", sourceDomain: "e621.net",
postURl: "http://e621.net/post/eeasd",
}, },
wantErr: false, wantErr: false,
}, },
@ -490,6 +492,7 @@ func Test_postgresqlConnection_CreateReferenceBetweenPostAndSource(t *testing.T)
ctx: ctx, ctx: ctx,
anthrovePostID: "123456", anthrovePostID: "123456",
sourceDomain: "e621.net", sourceDomain: "e621.net",
postURl: "",
}, },
wantErr: true, wantErr: true,
}, },
@ -499,6 +502,7 @@ func Test_postgresqlConnection_CreateReferenceBetweenPostAndSource(t *testing.T)
ctx: ctx, ctx: ctx,
anthrovePostID: "1234", anthrovePostID: "1234",
sourceDomain: "fa.banana", sourceDomain: "fa.banana",
postURl: "",
}, },
wantErr: true, wantErr: true,
}, },
@ -507,7 +511,7 @@ func Test_postgresqlConnection_CreateReferenceBetweenPostAndSource(t *testing.T)
args: args{ args: args{
ctx: ctx, ctx: ctx,
anthrovePostID: "696969", anthrovePostID: "696969",
sourceDomain: "hehe.funny.number", postURl: "",
}, },
wantErr: true, wantErr: true,
}} }}
@ -517,7 +521,7 @@ func Test_postgresqlConnection_CreateReferenceBetweenPostAndSource(t *testing.T)
db: gormDB, db: gormDB,
debug: true, debug: true,
} }
if err := p.CreateReferenceBetweenPostAndSource(tt.args.ctx, tt.args.anthrovePostID, tt.args.sourceDomain); (err != nil) != tt.wantErr { if err := p.CreateReferenceBetweenPostAndSource(tt.args.ctx, tt.args.anthrovePostID, tt.args.sourceDomain, tt.args.postURl); (err != nil) != tt.wantErr {
t.Errorf("CreateReferenceBetweenPostAndSource() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("CreateReferenceBetweenPostAndSource() error = %v, wantErr %v", err, tt.wantErr)
} }
}) })
@ -830,7 +834,7 @@ func Test_postgresqlConnection_GetPostByURL(t *testing.T) {
t.Fatal("Could not create source", err) t.Fatal("Could not create source", err)
} }
err = postgres.CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain)) err = postgres.CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "https://e62asdwad.com/asdas")
if err != nil { if err != nil {
t.Fatal("Could not create source reference", err) t.Fatal("Could not create source reference", err)
} }
@ -850,7 +854,7 @@ func Test_postgresqlConnection_GetPostByURL(t *testing.T) {
name: "Test 1: Valid sourceUrl", name: "Test 1: Valid sourceUrl",
args: args{ args: args{
ctx: ctx, ctx: ctx,
sourceUrl: source.Domain, sourceUrl: "https://e62asdwad.com/asdas",
}, },
want: post, want: post,
wantErr: false, wantErr: false,
@ -931,7 +935,7 @@ func Test_postgresqlConnection_GetPostBySourceID(t *testing.T) {
t.Fatal("Could not create source", err) t.Fatal("Could not create source", err)
} }
err = postgres.CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain)) err = postgres.CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "https://easd15aed.de/asd")
if err != nil { if err != nil {
t.Fatal("Could not create source reference", err) t.Fatal("Could not create source reference", err)
} }
@ -1228,30 +1232,34 @@ func Test_postgresqlConnection_GetUserSourceBySourceID(t *testing.T) {
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
invalidUserID := models.AnthroveUserID("XXX") invalidUserID := models.AnthroveUserID("XXX")
validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1"))
expectedResult := make(map[string]models.UserSource) expectedResult := make(map[string]models.UserSource)
source := &models.Source{
BaseModel: models.BaseModel[models.AnthroveSourceID]{
ID: validSourceID,
},
DisplayName: "e621",
Domain: "e621.net",
}
expectedResult["e621"] = models.UserSource{ expectedResult["e621"] = models.UserSource{
UserID: "e1", UserID: "e1",
AccountUsername: "euser", AccountUsername: "euser",
Source: models.Source{ Source: models.Source{
DisplayName: "e621", DisplayName: source.DisplayName,
Domain: "e621.net", Domain: source.Domain,
Icon: source.Icon,
}, },
} }
source := &models.Source{
BaseModel: models.BaseModel[models.AnthroveSourceID]{
ID: expectedResult["e621"].Source.ID,
},
DisplayName: expectedResult["e621"].Source.DisplayName,
Domain: expectedResult["e621"].Source.Domain,
}
err = postgres.CreateSource(ctx, gormDB, source) err = postgres.CreateSource(ctx, gormDB, source)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validUserID, models.AnthroveSourceID(expectedResult["e621"].SourceID), expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -1605,15 +1613,15 @@ func Test_postgresqlConnection_GetUserTagWitRelationToFavedPosts(t *testing.T) {
{ {
Frequency: 1, Frequency: 1,
Tags: models.Tag{ Tags: models.Tag{
Name: tags[1].Name, Name: tags[2].Name,
Type: tags[1].Type, Type: tags[2].Type,
}, },
}, },
{ {
Frequency: 1, Frequency: 1,
Tags: models.Tag{ Tags: models.Tag{
Name: tags[2].Name, Name: tags[1].Name,
Type: tags[2].Type, Type: tags[1].Type,
}, },
}, },
} }