diff --git a/internal/postgres/tag.go b/internal/postgres/tag.go index f810266..6b963f7 100644 --- a/internal/postgres/tag.go +++ b/internal/postgres/tag.go @@ -62,6 +62,29 @@ func DeleteTag(ctx context.Context, db *gorm.DB, tagName models.AnthroveTagName) return nil } +func GetAllTagByTagsType(ctx context.Context, db *gorm.DB, tagType models.TagType) ([]models.Tag, error) { + var tags []models.Tag + + if tagType == "" { + return nil, &otterError.EntityValidationFailed{Reason: "tagType cannot be empty"} + } + + result := db.WithContext(ctx).Find(&tags).Where("tag_type = ?", tagType) + + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return nil, &otterError.NoDataFound{} + } + return nil, result.Error + } + + log.WithFields(log.Fields{ + "tags_length": len(tags), + }).Trace("database: got tag") + + return tags, nil +} + func CreateTagAndReferenceToPost(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, tag *models.Tag) error { if anthrovePostID == "" { diff --git a/internal/postgres/tag_test.go b/internal/postgres/tag_test.go index 88eabcd..a976bd4 100644 --- a/internal/postgres/tag_test.go +++ b/internal/postgres/tag_test.go @@ -1019,3 +1019,97 @@ func TestDeleteTag(t *testing.T) { }) } } + +func TestGetAllTagByTagType(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + t.Fatalf("Could not start PostgreSQL container: %v", err) + } + defer container.Terminate(ctx) + + // Setup Test + + validTags := []models.Tag{ + { + Name: "JayTheFerret", + Type: models.Character, + }, + { + Name: "SoXX", + Type: models.Character, + }, + { + Name: "Alphyron", + Type: models.Character, + }, + { + Name: "Dragon", + Type: models.Species, + }, + } + + for _, tag := range validTags { + err = CreateTag(ctx, gormDB, models.AnthroveTagName(tag.Name), tag.Type) + if err != nil { + t.Fatal(err) + } + } + + // Test + type args struct { + ctx context.Context + db *gorm.DB + tagType models.TagType + } + tests := []struct { + name string + args args + want []models.Tag + wantErr bool + }{ + { + name: "Test 1: Get Data", + args: args{ + ctx: ctx, + db: gormDB, + tagType: models.Character, + }, + want: validTags, + wantErr: false, + }, + { + name: "Test 2: invalid Tag Type", + args: args{ + ctx: ctx, + db: gormDB, + tagType: "aa", + }, + want: validTags, + wantErr: false, + }, + { + name: "Test 3: No Tag Type", + args: args{ + ctx: ctx, + db: gormDB, + tagType: "", + }, + want: nil, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := GetAllTagByTagsType(tt.args.ctx, tt.args.db, tt.args.tagType) + if (err != nil) != tt.wantErr { + t.Errorf("GetAllTagByTagsType() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !checkTag(got, tt.want) { + t.Errorf("GetAllTagByTagsType() got = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go index 0acb71e..2692907 100644 --- a/pkg/database/postgres.go +++ b/pkg/database/postgres.go @@ -206,9 +206,8 @@ func (p *postgresqlConnection) CreateTag(ctx context.Context, tagName models.Ant 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) GetAllTagsByTagType(ctx context.Context, tagType models.TagType) ([]models.Tag, error) { + return postgres.GetAllTagByTagsType(ctx, p.db, tagType) } func (p *postgresqlConnection) DeleteTag(ctx context.Context, tagName models.AnthroveTagName) error { diff --git a/pkg/database/postgres_test.go b/pkg/database/postgres_test.go index 88b10ba..f00602e 100644 --- a/pkg/database/postgres_test.go +++ b/pkg/database/postgres_test.go @@ -3143,3 +3143,97 @@ func Test_postgresqlConnection_DeleteTag(t *testing.T) { }) } } + +func Test_postgresqlConnection_GetAllTagsByTagType(t *testing.T) { + // Setup trow away container + ctx := context.Background() + container, gormDB, err := test.StartPostgresContainer(ctx) + if err != nil { + t.Fatalf("Could not start PostgreSQL container: %v", err) + } + defer container.Terminate(ctx) + + // Setup Test + + validTags := []models.Tag{ + { + Name: "JayTheFerret", + Type: models.Character, + }, + { + Name: "SoXX", + Type: models.Character, + }, + { + Name: "Alphyron", + Type: models.Character, + }, + { + Name: "Dragon", + Type: models.Species, + }, + } + + for _, tag := range validTags { + err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(tag.Name), tag.Type) + if err != nil { + t.Fatal(err) + } + } + + // Test + type args struct { + ctx context.Context + tagType models.TagType + } + tests := []struct { + name string + args args + want []models.Tag + wantErr bool + }{ + { + name: "Test 1: Get Data", + args: args{ + ctx: ctx, + tagType: models.Character, + }, + want: validTags, + wantErr: false, + }, + { + name: "Test 2: invalid Tag Type", + args: args{ + ctx: ctx, + tagType: "aa", + }, + want: validTags, + wantErr: false, + }, + { + name: "Test 3: No Tag Type", + args: args{ + ctx: ctx, + tagType: "", + }, + want: nil, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &postgresqlConnection{ + db: gormDB, + debug: true, + } + got, err := p.GetAllTagsByTagType(tt.args.ctx, tt.args.tagType) + if (err != nil) != tt.wantErr { + t.Errorf("GetAllTagsByTagType() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("GetAllTagsByTagType() got = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/pkg/database/tag.go b/pkg/database/tag.go index ecaf3d5..44fa553 100644 --- a/pkg/database/tag.go +++ b/pkg/database/tag.go @@ -12,7 +12,7 @@ type Tag interface { // GetAllTags retrieves all tags. GetAllTags(ctx context.Context) ([]models.Tag, error) - GetAllTagByTagType(ctx context.Context, tagType models.TagType) ([]models.Tag, error) + GetAllTagsByTagType(ctx context.Context, tagType models.TagType) ([]models.Tag, error) DeleteTag(ctx context.Context, tagName models.AnthroveTagName) error }