From 1b40c6145a798deb39cd77f82ff41a5170fbea59 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 1 Jul 2024 22:06:36 +0200 Subject: [PATCH] refactor: changed CreateTag to align with other functions Signed-off-by: SoXX --- internal/postgres/tag.go | 16 +++++++----- internal/postgres/tag_test.go | 48 +++++++++++++++++++---------------- pkg/database/database.go | 3 +++ pkg/database/postgres.go | 14 ++++++++++ pkg/database/postgres_test.go | 18 ++++++------- pkg/database/tag.go | 6 +++++ pkg/models/const.go | 1 + 7 files changed, 69 insertions(+), 37 deletions(-) diff --git a/internal/postgres/tag.go b/internal/postgres/tag.go index 405b5a8..bc5ccd0 100644 --- a/internal/postgres/tag.go +++ b/internal/postgres/tag.go @@ -10,13 +10,17 @@ import ( "gorm.io/gorm" ) -func CreateTag(ctx context.Context, db *gorm.DB, tag *models.Tag) error { +func CreateTag(ctx context.Context, db *gorm.DB, tagName models.AnthroveTagName, tagType models.TagType) error { - if tag == nil { - return &otterError.EntityValidationFailed{Reason: "Tag is nil"} + if tagName == "" { + return &otterError.EntityValidationFailed{Reason: "tagName is empty"} } - result := db.WithContext(ctx).Create(tag) + if tagType == "" { + return &otterError.EntityValidationFailed{Reason: "tagType is empty"} + } + + result := db.WithContext(ctx).Create(&models.Tag{Name: string(tagName), Type: tagType}) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { return &otterError.EntityAlreadyExists{} @@ -29,8 +33,8 @@ func CreateTag(ctx context.Context, db *gorm.DB, tag *models.Tag) error { } log.WithFields(log.Fields{ - "tag_name": tag.Name, - "tag_type": tag.Type, + "tag_name": tagName, + "tag_type": tagType, }).Trace("database: created tag node") return nil diff --git a/internal/postgres/tag_test.go b/internal/postgres/tag_test.go index adbc867..5a30658 100644 --- a/internal/postgres/tag_test.go +++ b/internal/postgres/tag_test.go @@ -128,7 +128,7 @@ func TestGetTags(t *testing.T) { } for _, tag := range tags { - err = CreateTag(ctx, gormDB, &tag) + err = CreateTag(ctx, gormDB, models.AnthroveTagName(tag.Name), tag.Type) if err != nil { t.Fatal(err) } @@ -202,9 +202,10 @@ func TestCreateTag(t *testing.T) { // Test type args struct { - ctx context.Context - db *gorm.DB - tag *models.Tag + ctx context.Context + db *gorm.DB + tagName models.AnthroveTagName + tagType models.TagType } tests := []struct { name string @@ -214,34 +215,37 @@ func TestCreateTag(t *testing.T) { { name: "Test 1: Valid Tag", args: args{ - ctx: ctx, - db: gormDB, - tag: &validTag, + ctx: ctx, + db: gormDB, + tagName: models.AnthroveTagName(validTag.Name), + tagType: validTag.Type, }, wantErr: false, }, { name: "Test 2: Duplicate Tag", args: args{ - ctx: ctx, - db: gormDB, - tag: &validTag, + ctx: ctx, + db: gormDB, + tagName: models.AnthroveTagName(validTag.Name), + tagType: validTag.Type, }, wantErr: true, }, { name: "Test 3: Invalid Tag", args: args{ - ctx: ctx, - db: gormDB, - tag: &invalidTag, + ctx: ctx, + db: gormDB, + tagName: models.AnthroveTagName(invalidTag.Name), + tagType: invalidTag.Type, }, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := CreateTag(tt.args.ctx, tt.args.db, tt.args.tag); (err != nil) != tt.wantErr { + if err := CreateTag(tt.args.ctx, tt.args.db, tt.args.tagName, tt.args.tagType); (err != nil) != tt.wantErr { t.Errorf("CreateTag() error = %v, wantErr %v", err, tt.wantErr) } }) @@ -269,7 +273,7 @@ func TestCreateTagAlias(t *testing.T) { Type: models.Character, } - err = CreateTag(ctx, gormDB, validTag) + err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) if err != nil { t.Fatal(err) } @@ -379,7 +383,7 @@ func TestGetAllTagAlias(t *testing.T) { }, } - err = CreateTag(ctx, gormDB, validTag) + err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) if err != nil { t.Fatal(err) } @@ -456,7 +460,7 @@ func TestGetAllTagAliasByTag(t *testing.T) { }, } - err = CreateTag(ctx, gormDB, validTag) + err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) if err != nil { t.Fatal(err) } @@ -540,7 +544,7 @@ func TestDeleteTagAlias(t *testing.T) { Type: models.Character, } - err = CreateTag(ctx, gormDB, validTag) + err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) if err != nil { t.Fatal(err) } @@ -618,7 +622,7 @@ func TestCreateTagGroup(t *testing.T) { Type: models.Character, } - err = CreateTag(ctx, gormDB, validTag) + err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) if err != nil { t.Fatal(err) } @@ -728,7 +732,7 @@ func TestGetAllTagGroup(t *testing.T) { }, } - err = CreateTag(ctx, gormDB, validTag) + err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) if err != nil { t.Fatal(err) } @@ -805,7 +809,7 @@ func TestGetAllTagGroupByTag(t *testing.T) { }, } - err = CreateTag(ctx, gormDB, validTag) + err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) if err != nil { t.Fatal(err) } @@ -889,7 +893,7 @@ func TestDeleteTagGroup(t *testing.T) { Type: models.Character, } - err = CreateTag(ctx, gormDB, validTag) + err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) if err != nil { t.Fatal(err) } diff --git a/pkg/database/database.go b/pkg/database/database.go index 0bce2bf..bc19286 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -19,6 +19,9 @@ type OtterSpace interface { // Source contains all function that are needed to manage the Source Source + // Tag contains all functions that are used to manage Tag + Tag + // TagAlias contains all function that are needed to manage the TagAlias TagAlias diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go index 90080c0..4e1b02b 100644 --- a/pkg/database/postgres.go +++ b/pkg/database/postgres.go @@ -202,6 +202,20 @@ func (p *postgresqlConnection) DeleteTagGroup(ctx context.Context, tagGroupName return postgres.DeleteTagGroup(ctx, p.db, tagGroupName) } +func (p *postgresqlConnection) CreateTag(ctx context.Context, tagName models.AnthroveTagName, tagType models.TagType) error { + return postgres.CreateTag(ctx, p.db, tagName, tagType) +} + +func (p *postgresqlConnection) GetAllTagByTagType(ctx context.Context, tagType models.TagType) ([]models.Tag, error) { + //TODO implement me + panic("implement me") +} + +func (p *postgresqlConnection) DeleteTag(ctx context.Context, tagName models.AnthroveTagName) error { + //TODO implement me + panic("implement me") +} + // HELPER func (p *postgresqlConnection) migrateDatabase(dbPool *gorm.DB) error { diff --git a/pkg/database/postgres_test.go b/pkg/database/postgres_test.go index 41af09f..3a46606 100644 --- a/pkg/database/postgres_test.go +++ b/pkg/database/postgres_test.go @@ -1704,7 +1704,7 @@ func Test_postgresqlConnection_GetAllTags(t *testing.T) { } for _, tag := range tags { - err = postgres.CreateTag(ctx, gormDB, &tag) + err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(tag.Name), tag.Type) if err != nil { t.Fatal(err) } @@ -2043,7 +2043,7 @@ func Test_postgresqlConnection_CreateTagAlias(t *testing.T) { Type: models.Character, } - err = postgres.CreateTag(ctx, gormDB, validTag) + err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) if err != nil { t.Fatal(err) } @@ -2151,7 +2151,7 @@ func Test_postgresqlConnection_GetAllTagAlias(t *testing.T) { }, } - err = postgres.CreateTag(ctx, gormDB, validTag) + err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) if err != nil { t.Fatal(err) } @@ -2228,7 +2228,7 @@ func Test_postgresqlConnection_GetAllTagAliasByTag(t *testing.T) { }, } - err = postgres.CreateTag(ctx, gormDB, validTag) + err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) if err != nil { t.Fatal(err) } @@ -2312,7 +2312,7 @@ func Test_postgresqlConnection_DeleteTagAlias(t *testing.T) { Type: models.Character, } - err = postgres.CreateTag(ctx, gormDB, validTag) + err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) if err != nil { t.Fatal(err) } @@ -2392,7 +2392,7 @@ func Test_postgresqlConnection_CreateTagGroup(t *testing.T) { Type: models.Character, } - err = postgres.CreateTag(ctx, gormDB, validTag) + err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) if err != nil { t.Fatal(err) } @@ -2500,7 +2500,7 @@ func Test_postgresqlConnection_GetAllTagGroup(t *testing.T) { }, } - err = postgres.CreateTag(ctx, gormDB, validTag) + err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) if err != nil { t.Fatal(err) } @@ -2577,7 +2577,7 @@ func Test_postgresqlConnection_GetAllTagGroupByTag(t *testing.T) { }, } - err = postgres.CreateTag(ctx, gormDB, validTag) + err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) if err != nil { t.Fatal(err) } @@ -2661,7 +2661,7 @@ func Test_postgresqlConnection_DeleteTagGroup(t *testing.T) { Type: models.Character, } - err = postgres.CreateTag(ctx, gormDB, validTag) + err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) if err != nil { t.Fatal(err) } diff --git a/pkg/database/tag.go b/pkg/database/tag.go index eb0f4a5..ecaf3d5 100644 --- a/pkg/database/tag.go +++ b/pkg/database/tag.go @@ -7,6 +7,12 @@ import ( ) type Tag interface { + CreateTag(ctx context.Context, tagName models.AnthroveTagName, tagType models.TagType) error + // GetAllTags retrieves all tags. GetAllTags(ctx context.Context) ([]models.Tag, error) + + GetAllTagByTagType(ctx context.Context, tagType models.TagType) ([]models.Tag, error) + + DeleteTag(ctx context.Context, tagName models.AnthroveTagName) error } diff --git a/pkg/models/const.go b/pkg/models/const.go index 00fc13b..7e84b0c 100644 --- a/pkg/models/const.go +++ b/pkg/models/const.go @@ -12,6 +12,7 @@ type AnthroveTagAliasName string type AnthroveTagID string type AnthroveScrapeTimeInterval int type AnthroveUserLastScrapeTime time.Time +type AnthroveTagName string type Rating string type TagType string