From 47fda2994ba528a420836de04d26d7b73e73e9a5 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 1 Jul 2024 21:38:09 +0200 Subject: [PATCH] fix: fixed issues in PR Signed-off-by: SoXX --- internal/postgres/post.go | 2 +- internal/postgres/post_test.go | 6 +++--- pkg/database/postgres.go | 16 ++++++++++++---- pkg/database/postgres_test.go | 4 ++-- pkg/models/api.go | 5 +++++ pkg/models/userFavorite.go | 4 ---- 6 files changed, 23 insertions(+), 14 deletions(-) create mode 100644 pkg/models/api.go diff --git a/internal/postgres/post.go b/internal/postgres/post.go index 3679132..6c08cf3 100644 --- a/internal/postgres/post.go +++ b/internal/postgres/post.go @@ -59,7 +59,7 @@ func GetPostByAnthroveID(ctx context.Context, db *gorm.DB, anthrovePostID models return &post, nil } -func GetPostByURL(ctx context.Context, db *gorm.DB, sourceURL string) (*models.Post, error) { +func GetPostBySourceURL(ctx context.Context, db *gorm.DB, sourceURL string) (*models.Post, error) { if sourceURL == "" { return nil, &otterError.EntityValidationFailed{Reason: "sourceURL is not set"} diff --git a/internal/postgres/post_test.go b/internal/postgres/post_test.go index f679eaa..bb525c8 100644 --- a/internal/postgres/post_test.go +++ b/internal/postgres/post_test.go @@ -249,13 +249,13 @@ func TestGetPostBySourceURL(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := GetPostByURL(tt.args.ctx, tt.args.db, tt.args.sourceURL) + got, err := GetPostBySourceURL(tt.args.ctx, tt.args.db, tt.args.sourceURL) if (err != nil) != tt.wantErr { - t.Errorf("GetPostByURL() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("GetPostBySourceURL() error = %v, wantErr %v", err, tt.wantErr) return } if !checkPost(got, tt.want) { - t.Errorf("GetPostByURL() got = %v, want %v", got, tt.want) + t.Errorf("GetPostBySourceURL() got = %v, want %v", got, tt.want) } }) } diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go index 6d3dd07..90080c0 100644 --- a/pkg/database/postgres.go +++ b/pkg/database/postgres.go @@ -15,7 +15,7 @@ import ( log "github.com/sirupsen/logrus" gormPostgres "gorm.io/driver/postgres" "gorm.io/gorm" - logger2 "gorm.io/gorm/logger" + gormLogger "gorm.io/gorm/logger" ) //go:embed migrations/*.sql @@ -34,6 +34,8 @@ func NewPostgresqlConnection() OtterSpace { func (p *postgresqlConnection) Connect(_ context.Context, config models.DatabaseConfig) error { var localSSL string + var logLevel gormLogger.LogLevel + if config.SSL { localSSL = "require" } else { @@ -45,9 +47,15 @@ func (p *postgresqlConnection) Connect(_ context.Context, config models.Database dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s", config.Endpoint, config.Username, config.Password, config.Database, config.Port, localSSL, config.Timezone) var err error - dbLogger := logger2.New(log2.New(os.Stdout, "\r\n", log2.LstdFlags), logger2.Config{ + if p.debug { + logLevel = gormLogger.Info + } else { + logLevel = gormLogger.Silent + } + + dbLogger := gormLogger.New(log2.New(os.Stdout, "\r\n", log2.LstdFlags), gormLogger.Config{ SlowThreshold: 200 * time.Millisecond, - LogLevel: logger2.Info, + LogLevel: logLevel, IgnoreRecordNotFoundError: true, Colorful: true, }) @@ -105,7 +113,7 @@ func (p *postgresqlConnection) GetPostByAnthroveID(ctx context.Context, anthrove } func (p *postgresqlConnection) GetPostByURL(ctx context.Context, sourceUrl string) (*models.Post, error) { - return postgres.GetPostByURL(ctx, p.db, sourceUrl) + return postgres.GetPostBySourceURL(ctx, p.db, sourceUrl) } func (p *postgresqlConnection) GetPostBySourceID(ctx context.Context, sourceID models.AnthroveSourceID) (*models.Post, error) { diff --git a/pkg/database/postgres_test.go b/pkg/database/postgres_test.go index 93534cc..41af09f 100644 --- a/pkg/database/postgres_test.go +++ b/pkg/database/postgres_test.go @@ -887,11 +887,11 @@ func Test_postgresqlConnection_GetPostByURL(t *testing.T) { } got, err := p.GetPostByURL(tt.args.ctx, tt.args.sourceUrl) if (err != nil) != tt.wantErr { - t.Errorf("GetPostByURL() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("GetPostBySourceURL() error = %v, wantErr %v", err, tt.wantErr) return } if !checkPost(got, tt.want) { - t.Errorf("GetPostByURL() got = %v, want %v", got, tt.want) + t.Errorf("GetPostBySourceURL() got = %v, want %v", got, tt.want) } }) } diff --git a/pkg/models/api.go b/pkg/models/api.go new file mode 100644 index 0000000..37adbd0 --- /dev/null +++ b/pkg/models/api.go @@ -0,0 +1,5 @@ +package models + +type FavoriteList struct { + Posts []Post `json:"posts,omitempty"` +} diff --git a/pkg/models/userFavorite.go b/pkg/models/userFavorite.go index 3bebe91..028b907 100644 --- a/pkg/models/userFavorite.go +++ b/pkg/models/userFavorite.go @@ -11,7 +11,3 @@ type UserFavorites struct { func (UserFavorites) TableName() string { return "UserFavorites" } - -type FavoriteList struct { - Posts []Post `json:"posts,omitempty"` -}