refactor: added deletion of tags

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2024-07-01 22:15:43 +02:00
parent 1b40c6145a
commit a842d64f1c
4 changed files with 165 additions and 4 deletions

View File

@ -13,11 +13,11 @@ import (
func CreateTag(ctx context.Context, db *gorm.DB, tagName models.AnthroveTagName, tagType models.TagType) error { func CreateTag(ctx context.Context, db *gorm.DB, tagName models.AnthroveTagName, tagType models.TagType) error {
if tagName == "" { if tagName == "" {
return &otterError.EntityValidationFailed{Reason: "tagName is empty"} return &otterError.EntityValidationFailed{Reason: "tagName cannot be empty"}
} }
if tagType == "" { if tagType == "" {
return &otterError.EntityValidationFailed{Reason: "tagType is empty"} return &otterError.EntityValidationFailed{Reason: "tagType cannot be empty"}
} }
result := db.WithContext(ctx).Create(&models.Tag{Name: string(tagName), Type: tagType}) result := db.WithContext(ctx).Create(&models.Tag{Name: string(tagName), Type: tagType})
@ -40,6 +40,28 @@ func CreateTag(ctx context.Context, db *gorm.DB, tagName models.AnthroveTagName,
return nil return nil
} }
func DeleteTag(ctx context.Context, db *gorm.DB, tagName models.AnthroveTagName) error {
if tagName == "" {
return &otterError.EntityValidationFailed{Reason: "tagName cannot be empty"}
}
result := db.WithContext(ctx).Delete(&models.Tag{Name: string(tagName)})
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &otterError.NoDataFound{}
}
return result.Error
}
log.WithFields(log.Fields{
"tag_name": tagName,
}).Trace("database: deleted tag")
return nil
}
func CreateTagAndReferenceToPost(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, tag *models.Tag) error { func CreateTagAndReferenceToPost(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, tag *models.Tag) error {
if anthrovePostID == "" { if anthrovePostID == "" {

View File

@ -949,3 +949,73 @@ func TestDeleteTagGroup(t *testing.T) {
}) })
} }
} }
func TestDeleteTag(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
validTagID := models.AnthroveTagID("toothless")
validTag := &models.Tag{
Name: string(validTagID),
Type: models.Character,
}
err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type)
if err != nil {
t.Fatal(err)
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
tagName models.AnthroveTagName
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 1: Valid TagName",
args: args{
ctx: ctx,
db: gormDB,
tagName: models.AnthroveTagName(validTagID),
},
wantErr: false,
},
{
name: "Test 2: Invalid TagName",
args: args{
ctx: ctx,
db: gormDB,
tagName: models.AnthroveTagName("aaa"),
},
wantErr: false,
},
{
name: "Test 3: No TagName",
args: args{
ctx: ctx,
db: gormDB,
tagName: "",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeleteTag(tt.args.ctx, tt.args.db, tt.args.tagName); (err != nil) != tt.wantErr {
t.Errorf("DeleteTag() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

View File

@ -212,8 +212,7 @@ func (p *postgresqlConnection) GetAllTagByTagType(ctx context.Context, tagType m
} }
func (p *postgresqlConnection) DeleteTag(ctx context.Context, tagName models.AnthroveTagName) error { func (p *postgresqlConnection) DeleteTag(ctx context.Context, tagName models.AnthroveTagName) error {
//TODO implement me return postgres.DeleteTag(ctx, p.db, tagName)
panic("implement me")
} }
// HELPER // HELPER

View File

@ -3073,3 +3073,73 @@ func Test_postgresqlConnection_UpdateUserSourceValidation(t *testing.T) {
}) })
} }
} }
func Test_postgresqlConnection_DeleteTag(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
validTagID := models.AnthroveTagID("toothless")
validTag := &models.Tag{
Name: string(validTagID),
Type: models.Character,
}
err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type)
if err != nil {
t.Fatal(err)
}
// Test
type args struct {
ctx context.Context
tagName models.AnthroveTagName
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 1: Valid TagName",
args: args{
ctx: ctx,
tagName: models.AnthroveTagName(validTagID),
},
wantErr: false,
},
{
name: "Test 2: Invalid TagName",
args: args{
ctx: ctx,
tagName: models.AnthroveTagName("aaa"),
},
wantErr: false,
},
{
name: "Test 3: No TagName",
args: args{
ctx: ctx,
tagName: "",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &postgresqlConnection{
db: gormDB,
debug: true,
}
if err := p.DeleteTag(tt.args.ctx, tt.args.tagName); (err != nil) != tt.wantErr {
t.Errorf("DeleteTag() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}