From 1a46d94fba2f02cbda0996c6a2f971152c0024b0 Mon Sep 17 00:00:00 2001 From: aldiwildan77 Date: Sun, 10 Dec 2023 12:21:03 +0700 Subject: [PATCH] refactor: change from coderabbit review --- application.go | 7 ++++--- database.go | 23 +++++++++++++++++++---- donut.go | 8 +------- dto.go | 2 +- instance.go | 11 +++++------ main.go | 2 +- presentation.go | 10 ++++++++-- repository.go | 6 +++--- 8 files changed, 42 insertions(+), 27 deletions(-) diff --git a/application.go b/application.go index c7c8ed7..1952998 100644 --- a/application.go +++ b/application.go @@ -3,9 +3,10 @@ package main import "fmt" type ApplicationConfig struct { - Host string `env:"APPLICATION_HOST" envDefault:"localhost"` - Port int `env:"APPLICATION_PORT" envDefault:"8080"` - TZ string `env:"APPLICATION_TZ" envDefault:"Asia/Jakarta"` + Host string `env:"APPLICATION_HOST" envDefault:"localhost"` + Port int `env:"APPLICATION_PORT" envDefault:"8080"` + TZ string `env:"APPLICATION_TZ" envDefault:"Asia/Jakarta"` + GracefulShutdownTimeout int `env:"APPLICATION_GRACEFUL_SHUTDOWN_TIMEOUT" envDefault:"10"` } func (cfg ApplicationConfig) Address() string { diff --git a/database.go b/database.go index 821c2ec..4d97fba 100644 --- a/database.go +++ b/database.go @@ -6,6 +6,7 @@ import ( "gorm.io/driver/mysql" "gorm.io/driver/postgres" "gorm.io/gorm" + "gorm.io/gorm/logger" ) type DatabaseDialect string @@ -22,10 +23,11 @@ type DatabaseConfig struct { Password string `env:"DATABASE_PASSWORD"` Schema string `env:"DATABASE_SCHEMA"` Debug bool `env:"DATABASE_DEBUG" envDefault:"false"` + LogLevel string `env:"DATABASE_LOG_LEVEL" envDefault:"info" enum:"silent,error,warn,info"` Dialect string `env:"DATABASE_DIALECT"` } -func (d DatabaseConfig) GetDialector() gorm.Dialector { +func (d DatabaseConfig) GetDialector() (gorm.Dialector, error) { switch d.Dialect { case string(DialectMySQL): dsn := fmt.Sprintf( @@ -36,7 +38,7 @@ func (d DatabaseConfig) GetDialector() gorm.Dialector { d.Port, d.Schema, ) - return mysql.Open(dsn) + return mysql.Open(dsn), nil case string(DialectPostgres): dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Jakarta", d.Host, @@ -45,8 +47,21 @@ func (d DatabaseConfig) GetDialector() gorm.Dialector { d.Schema, d.Port, ) - return postgres.Open(dsn) + return postgres.Open(dsn), nil default: - return nil + return nil, fmt.Errorf("unsupported database dialect: %s", d.Dialect) + } +} + +func (d DatabaseConfig) GetLogLevel() logger.LogLevel { + switch d.LogLevel { + case "error": + return logger.Error + case "warn": + return logger.Warn + case "info": + return logger.Info + default: + return logger.Silent } } diff --git a/donut.go b/donut.go index bf62e4a..cdec12d 100644 --- a/donut.go +++ b/donut.go @@ -87,7 +87,6 @@ func (dc *donutCall) Call(ctx context.Context, matchMakerSerial string, people P return dc.repo.UpdateStatusMatchMakerUsers(ctx, matchMakerUsersEntities) } -} func (dc *donutCall) Start(ctx context.Context, matchMakerSerial string) error { matchMaker, err := dc.repo.GetMatchMakerBySerial(ctx, matchMakerSerial) @@ -173,11 +172,7 @@ func (dc *donutCall) Pair(ctx context.Context, matchMakerSerial string) error { matchMakerUsersEntities := make(MatchMakerUserEntities, 0) - for length >= 0 { - if length <= 0 { - break - } - + for length > 0 { switch { case length == 3: people, matchMakerUsersEntities = processThreeWayCall(people, matchMakerSerial, matchMakerUsersEntities) @@ -189,7 +184,6 @@ func (dc *donutCall) Pair(ctx context.Context, matchMakerSerial string) error { matchMakerUsersEntities = processTwoWayCall(length, people, matchMakerSerial, matchMakerUsersEntities) length -= 2 } - } return dc.repo.UpdateSerialMatchMakerUsers(ctx, matchMakerUsersEntities) diff --git a/dto.go b/dto.go index 6268ad6..9137a64 100644 --- a/dto.go +++ b/dto.go @@ -72,7 +72,7 @@ func (m *MatchMakerUser) FromEntity(entity *MatchMakerUserEntity) *MatchMakerUse MatchMakerSerial: entity.MatchMakerSerial, Serial: entity.Serial, UserReference: entity.UserReference, - Status: MatchMakerUserStatusPending, + Status: entity.Status, } } diff --git a/instance.go b/instance.go index 837bf4f..25857cf 100644 --- a/instance.go +++ b/instance.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "log" "os" "time" @@ -13,20 +12,20 @@ import ( func NewDatabaseInstance(conf *Config) (*gorm.DB, error) { gormConf := new(gorm.Config) - if conf.DatabaseConfig.Debug { + if conf.DatabaseConfig.LogLevel != "" { gormConf.Logger = logger.New( log.New(os.Stdout, "\r\n", log.LstdFlags), logger.Config{ SlowThreshold: time.Second, - LogLevel: logger.Silent, + LogLevel: conf.DatabaseConfig.GetLogLevel(), Colorful: true, }, ) } - dialector := conf.DatabaseConfig.GetDialector() - if dialector == nil { - return nil, fmt.Errorf("unsupported database dialect, %s", conf.DatabaseConfig.Dialect) + dialector, err := conf.DatabaseConfig.GetDialector() + if err != nil { + return nil, err } instance, err := gorm.Open(dialector, gormConf) diff --git a/main.go b/main.go index d280d65..bd27e18 100644 --- a/main.go +++ b/main.go @@ -60,7 +60,7 @@ func main() { log.Info().Msg("server is shutting down") // Create a context with a timeout - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(cfg.ApplicationConfig.GracefulShutdownTimeout)) defer cancel() // Shutdown the server gracefully diff --git a/presentation.go b/presentation.go index 05fccfa..9ece0d5 100644 --- a/presentation.go +++ b/presentation.go @@ -9,6 +9,10 @@ import ( ) func parseCreateMatchMakerRequest(req *connect.Request[donutv1.CreateMatchMakerRequest]) (parsed *MatchMakerEntity) { + if req.Msg.MatchMaker == nil { + return nil + } + return parsed.Build( WithMatchMakerEntityName(req.Msg.MatchMaker.GetName()), WithMatchMakerEntityDescription(req.Msg.MatchMaker.GetDescription()), @@ -41,10 +45,12 @@ func parseGetMatchMakerInformationResponse(info *MatchMakerInformation) *connect func parseRegisterPeopleRequest(req *donutv1.RegisterPeopleRequest) (parsed MatchMakerUserEntities) { entity := &MatchMakerUserEntity{} - return append(parsed, entity.Build( + entity.Build( WithMatchMakerUserEntityMatchMakerSerial(req.GetMatchmakerSerial()), WithMatchMakerUserEntityUserReference(req.GetReference()), - )) + WithMatchMakerUserEntityStatus(MatchMakerUserStatusPending), + ) + return append(parsed, entity) } func parseUnRegisterPeopleRequest(req *donutv1.UnRegisterPeopleRequest) (parsed MatchMakerUserEntities) { diff --git a/repository.go b/repository.go index 80736d7..c9236c7 100644 --- a/repository.go +++ b/repository.go @@ -56,8 +56,9 @@ func (r *donutRepository) UpdateStatusMatchMakerUsers(ctx context.Context, match return } - if err := trx.Commit(); err != nil { - return err + err := trx.Commit() + if err != nil { + return } }() @@ -77,7 +78,6 @@ func (r *donutRepository) UpdateStatusMatchMakerUsers(ctx context.Context, match return nil } -} func (r *donutRepository) DeleteMatchMakerUsers(ctx context.Context, matchMakerUsers MatchMakerUserEntities) error { return r.db.WithContext(ctx).Delete(MatchMakerUsers{}.FromEntities(matchMakerUsers)).Error