Skip to content

Commit

Permalink
refactor: change from coderabbit review
Browse files Browse the repository at this point in the history
  • Loading branch information
Aldiwildan77 committed Dec 10, 2023
1 parent 063fbd7 commit 1a46d94
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 27 deletions.
7 changes: 4 additions & 3 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 19 additions & 4 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)

type DatabaseDialect string
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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
}
}
8 changes: 1 addition & 7 deletions donut.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
11 changes: 5 additions & 6 deletions instance.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"log"
"os"
"time"
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions presentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}()

Expand All @@ -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
Expand Down

0 comments on commit 1a46d94

Please sign in to comment.