refactor: changed CreateTag to align with other functions

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2024-07-01 22:06:36 +02:00
parent 47fda2994b
commit 1b40c6145a
7 changed files with 69 additions and 37 deletions

View File

@ -10,13 +10,17 @@ import (
"gorm.io/gorm" "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 { if tagName == "" {
return &otterError.EntityValidationFailed{Reason: "Tag is nil"} 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 result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) { if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.EntityAlreadyExists{} return &otterError.EntityAlreadyExists{}
@ -29,8 +33,8 @@ func CreateTag(ctx context.Context, db *gorm.DB, tag *models.Tag) error {
} }
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"tag_name": tag.Name, "tag_name": tagName,
"tag_type": tag.Type, "tag_type": tagType,
}).Trace("database: created tag node") }).Trace("database: created tag node")
return nil return nil

View File

@ -128,7 +128,7 @@ func TestGetTags(t *testing.T) {
} }
for _, tag := range tags { for _, tag := range tags {
err = CreateTag(ctx, gormDB, &tag) err = CreateTag(ctx, gormDB, models.AnthroveTagName(tag.Name), tag.Type)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -204,7 +204,8 @@ func TestCreateTag(t *testing.T) {
type args struct { type args struct {
ctx context.Context ctx context.Context
db *gorm.DB db *gorm.DB
tag *models.Tag tagName models.AnthroveTagName
tagType models.TagType
} }
tests := []struct { tests := []struct {
name string name string
@ -216,7 +217,8 @@ func TestCreateTag(t *testing.T) {
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
tag: &validTag, tagName: models.AnthroveTagName(validTag.Name),
tagType: validTag.Type,
}, },
wantErr: false, wantErr: false,
}, },
@ -225,7 +227,8 @@ func TestCreateTag(t *testing.T) {
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
tag: &validTag, tagName: models.AnthroveTagName(validTag.Name),
tagType: validTag.Type,
}, },
wantErr: true, wantErr: true,
}, },
@ -234,14 +237,15 @@ func TestCreateTag(t *testing.T) {
args: args{ args: args{
ctx: ctx, ctx: ctx,
db: gormDB, db: gormDB,
tag: &invalidTag, tagName: models.AnthroveTagName(invalidTag.Name),
tagType: invalidTag.Type,
}, },
wantErr: true, wantErr: true,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { 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) t.Errorf("CreateTag() error = %v, wantErr %v", err, tt.wantErr)
} }
}) })
@ -269,7 +273,7 @@ func TestCreateTagAlias(t *testing.T) {
Type: models.Character, Type: models.Character,
} }
err = CreateTag(ctx, gormDB, validTag) err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type)
if err != nil { if err != nil {
t.Fatal(err) 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 { if err != nil {
t.Fatal(err) 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -540,7 +544,7 @@ func TestDeleteTagAlias(t *testing.T) {
Type: models.Character, Type: models.Character,
} }
err = CreateTag(ctx, gormDB, validTag) err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -618,7 +622,7 @@ func TestCreateTagGroup(t *testing.T) {
Type: models.Character, Type: models.Character,
} }
err = CreateTag(ctx, gormDB, validTag) err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type)
if err != nil { if err != nil {
t.Fatal(err) 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 { if err != nil {
t.Fatal(err) 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -889,7 +893,7 @@ func TestDeleteTagGroup(t *testing.T) {
Type: models.Character, Type: models.Character,
} }
err = CreateTag(ctx, gormDB, validTag) err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -19,6 +19,9 @@ type OtterSpace interface {
// Source contains all function that are needed to manage the Source // Source contains all function that are needed to manage the Source
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 contains all function that are needed to manage the TagAlias
TagAlias TagAlias

View File

@ -202,6 +202,20 @@ func (p *postgresqlConnection) DeleteTagGroup(ctx context.Context, tagGroupName
return postgres.DeleteTagGroup(ctx, p.db, 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 // HELPER
func (p *postgresqlConnection) migrateDatabase(dbPool *gorm.DB) error { func (p *postgresqlConnection) migrateDatabase(dbPool *gorm.DB) error {

View File

@ -1704,7 +1704,7 @@ func Test_postgresqlConnection_GetAllTags(t *testing.T) {
} }
for _, tag := range tags { for _, tag := range tags {
err = postgres.CreateTag(ctx, gormDB, &tag) err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(tag.Name), tag.Type)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -2043,7 +2043,7 @@ func Test_postgresqlConnection_CreateTagAlias(t *testing.T) {
Type: models.Character, Type: models.Character,
} }
err = postgres.CreateTag(ctx, gormDB, validTag) err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type)
if err != nil { if err != nil {
t.Fatal(err) 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 { if err != nil {
t.Fatal(err) 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -2312,7 +2312,7 @@ func Test_postgresqlConnection_DeleteTagAlias(t *testing.T) {
Type: models.Character, Type: models.Character,
} }
err = postgres.CreateTag(ctx, gormDB, validTag) err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -2392,7 +2392,7 @@ func Test_postgresqlConnection_CreateTagGroup(t *testing.T) {
Type: models.Character, Type: models.Character,
} }
err = postgres.CreateTag(ctx, gormDB, validTag) err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type)
if err != nil { if err != nil {
t.Fatal(err) 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 { if err != nil {
t.Fatal(err) 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -2661,7 +2661,7 @@ func Test_postgresqlConnection_DeleteTagGroup(t *testing.T) {
Type: models.Character, Type: models.Character,
} }
err = postgres.CreateTag(ctx, gormDB, validTag) err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -7,6 +7,12 @@ import (
) )
type Tag interface { type Tag interface {
CreateTag(ctx context.Context, tagName models.AnthroveTagName, tagType models.TagType) error
// GetAllTags retrieves all tags. // GetAllTags retrieves all tags.
GetAllTags(ctx context.Context) ([]models.Tag, error) 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
} }

View File

@ -12,6 +12,7 @@ type AnthroveTagAliasName string
type AnthroveTagID string type AnthroveTagID string
type AnthroveScrapeTimeInterval int type AnthroveScrapeTimeInterval int
type AnthroveUserLastScrapeTime time.Time type AnthroveUserLastScrapeTime time.Time
type AnthroveTagName string
type Rating string type Rating string
type TagType string type TagType string