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"
)
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

View File

@ -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)
}

View File

@ -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

View File

@ -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 {

View File

@ -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)
}

View File

@ -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
}

View File

@ -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