Skip to content

Commit 5cd14c9

Browse files
Merge pull request #16 from DoWithLogic/refactor/config-and-sql-base
chore: update config retrieve mechanism and sql base from sql to sqlx
2 parents 59a394f + ae5274c commit 5cd14c9

File tree

13 files changed

+68
-61
lines changed

13 files changed

+68
-61
lines changed

cmd/main.go cmd/api/main.go

File renamed without changes.

cmd/scheduler/.keep

Whitespace-only changes.

config/config-local.yaml.example

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
server:
2-
name: golang-clean-architecture
3-
version: "v0.0.1"
4-
rpc_port: 8080
5-
rest_port: 9090
6-
debug: false
7-
env: local
8-
read_time_out: 5s
9-
write_time_out: 5s
1+
Server:
2+
Name: golang-clean-architecture
3+
Version: "v0.0.1"
4+
RPCPort: 8080
5+
RESTPort: 9090
6+
Debug: false
7+
Env: local
8+
ReadTimeOut: 5s
9+
WriteTimeOut: 5s
1010

11-
database:
12-
host: localhost
13-
port: 3306
14-
name: users
15-
user: mysql
16-
password: pwd
11+
Database:
12+
Host: localhost
13+
Port: 3306
14+
Name: users
15+
User: mysql
16+
Password: pwd
1717

18-
authentication:
19-
key: DoWithLogic!@#
20-
secret_key: s3cr#tK3y!@#v001
21-
salt_key: s4ltK3y!@#ddv001
18+
Authentication:
19+
Key: DoWithLogic!@#
20+
SecretKey: s3cr#tK3y!@#v001
21+
SaltKey: s4ltK3y!@#ddv001

config/config.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,28 @@ type (
1515
}
1616

1717
DatabaseConfig struct {
18-
Host string `mapstructure:"host"`
19-
Port int `mapstructure:"port"`
20-
Name string `mapstructure:"name"`
21-
User string `mapstructure:"user"`
22-
Password string `mapstructure:"password"`
18+
Host string
19+
Port int
20+
Name string
21+
User string
22+
Password string
2323
}
2424

2525
ServerConfig struct {
26-
Name string `mapstructure:"name"`
27-
Version string `mapstructure:"version"`
28-
RPCPort string `mapstructure:"rpc_port"`
29-
RESTPort string `mapstructure:"rest_port"`
30-
Debug bool `mapstructure:"debug"`
31-
Environment string `mapstructure:"env"`
32-
ReadTimeout time.Duration `mapstructure:"read_time_out"`
33-
WriteTimeout time.Duration `mapstructure:"write_time_out"`
26+
Name string
27+
Version string
28+
RPCPort string
29+
RESTPort string
30+
Debug bool
31+
Environment string
32+
ReadTimeout time.Duration
33+
WriteTimeout time.Duration
3434
}
3535

3636
AuthenticationConfig struct {
37-
Key string `mapstructure:"key"`
38-
SecretKey string `mapstructure:"secret_key"`
39-
SaltKey string `mapstructure:"salt_key"`
37+
Key string
38+
SecretKey string
39+
SaltKey string
4040
}
4141
)
4242

internal/users/delivery/kafka/.keep

Whitespace-only changes.

internal/users/delivery/kafka/read.txt

-1
This file was deleted.

internal/users/delivery/rpc/.keep

Whitespace-only changes.

internal/users/delivery/rpc/read.txt

-1
This file was deleted.
+10-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
package dtos
22

3-
import "github.com/invopop/validation"
3+
import (
4+
"github.com/DoWithLogic/golang-clean-architecture/pkg/apperror"
5+
"github.com/DoWithLogic/golang-clean-architecture/pkg/constant"
6+
"github.com/invopop/validation"
7+
)
48

59
type UpdateUserStatusRequest struct {
610
UserID int64 `json:"-"`
7-
Status int `json:"status"`
11+
Status string `json:"status"`
812
UpdateBy string `json:"-"`
913
}
1014

1115
func (ussp UpdateUserStatusRequest) Validate() error {
16+
if ussp.Status != constant.UserInactive && ussp.Status != constant.UserActive {
17+
return apperror.ErrStatusValue
18+
}
19+
1220
return validation.ValidateStruct(&ussp,
1321
validation.Field(&ussp.UserID, validation.NotNil),
1422
validation.Field(&ussp.UpdateBy, validation.NotNil),
15-
validation.Field(&ussp.Status, validation.Required),
1623
)
1724
}

internal/users/repository/repository.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func NewRepository(c *sqlx.DB, l *zerolog.Logger) Repository {
3737

3838
// Atomic implements vendor.Repository for transaction query
3939
func (r *repository) Atomic(ctx context.Context, opt *sql.TxOptions, repo func(tx Repository) error) error {
40-
txConn, err := r.db.BeginTx(ctx, opt)
40+
txConn, err := r.db.BeginTxx(ctx, opt)
4141
if err != nil {
4242
r.log.Z().Err(err).Msg("[repository]Atomic.BeginTxx")
4343

@@ -122,8 +122,8 @@ func (repo *repository) GetUserByID(ctx context.Context, userID int64, options .
122122
query += " FOR UPDATE"
123123
}
124124

125-
if err = new(datasource.DataSource).QuerySQL(repo.conn.QueryContext(ctx, query, args...)).Scan(row); err != nil {
126-
repo.log.Z().Err(err).Msg("[repository]GetUserByID.QueryContext")
125+
if err = new(datasource.DataSource).QuerySQL(repo.conn.QueryxContext(ctx, query, args...)).Scan(row); err != nil {
126+
repo.log.Z().Err(err).Msg("[repository]GetUserByID.QueryxContext")
127127
return userData, err
128128
}
129129

@@ -159,9 +159,9 @@ func (repo *repository) IsUserExist(ctx context.Context, email string) bool {
159159
}
160160
}
161161

162-
err := new(datasource.DataSource).QuerySQL(repo.conn.QueryContext(ctx, repository_query.IsUserExist, args...)).Scan(row)
162+
err := new(datasource.DataSource).QuerySQL(repo.conn.QueryxContext(ctx, repository_query.IsUserExist, args...)).Scan(row)
163163
if err != nil {
164-
repo.log.Z().Err(err).Msg("[repository]IsUserExist.QueryContext")
164+
repo.log.Z().Err(err).Msg("[repository]IsUserExist.QueryxContext")
165165

166166
return false
167167
}
@@ -182,9 +182,9 @@ func (repo *repository) GetUserByEmail(ctx context.Context, email string) (userD
182182
}
183183
}
184184

185-
err = new(datasource.DataSource).QuerySQL(repo.conn.QueryContext(ctx, repository_query.GetUserByEmail, args...)).Scan(row)
185+
err = new(datasource.DataSource).QuerySQL(repo.conn.QueryxContext(ctx, repository_query.GetUserByEmail, args...)).Scan(row)
186186
if err != nil {
187-
repo.log.Z().Err(err).Msg("[repository]GetUserByID.QueryContext")
187+
repo.log.Z().Err(err).Msg("[repository]GetUserByID.QueryxContext")
188188
return entities.Users{}, err
189189
}
190190

pkg/apperror/errors.go

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ var (
88
ErrInvalidPassword = errors.New("invalid password")
99
ErrFailedGenerateJWT = errors.New("failed generate access token")
1010
ErrInvalidIsActive = errors.New("invalid is_active")
11+
ErrStatusValue = errors.New("status should be 0 or 1")
1112
)

pkg/constant/constant.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ const (
77
)
88

99
const (
10-
UserInactive = iota
11-
UserActive
10+
UserInactive = "0"
11+
UserActive = "1"
1212
)
1313

14-
var MapStatus = map[int]bool{
14+
var MapStatus = map[string]bool{
1515
UserInactive: false,
1616
UserActive: true,
1717
}

pkg/datasource/sql.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@ import (
1010

1111
"github.com/DoWithLogic/golang-clean-architecture/pkg/otel/zerolog"
1212
"github.com/DoWithLogic/golang-clean-architecture/pkg/utils"
13+
"github.com/jmoiron/sqlx"
1314
)
1415

1516
type (
1617
Conn interface {
17-
BeginTx(ctx context.Context, opts *sql.TxOptions) (tx *sql.Tx, err error)
18+
BeginTxx(ctx context.Context, opts *sql.TxOptions) (*sqlx.Tx, error)
1819
PingContext(ctx context.Context) (err error)
1920
io.Closer
2021
ConnTx
2122
}
2223

2324
ConnTx interface {
24-
ExecContext(ctx context.Context, query string, args ...interface{}) (res sql.Result, err error)
25-
PrepareContext(ctx context.Context, query string) (stmt *sql.Stmt, err error)
26-
QueryContext(ctx context.Context, query string, args ...interface{}) (rows *sql.Rows, err error)
27-
QueryRowContext(ctx context.Context, query string, args ...interface{}) (row *sql.Row)
25+
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
26+
PreparexContext(ctx context.Context, query string) (*sqlx.Stmt, error)
27+
QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)
28+
QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row
2829
}
2930

3031
Exec interface {
@@ -41,17 +42,17 @@ type (
4142
}
4243

4344
query struct {
44-
sqlRows *sql.Rows
45+
sqlRows *sqlx.Rows
4546
err error
4647
}
4748

4849
DataSource struct{}
4950
)
5051

5152
var (
52-
_ Conn = (*sql.Conn)(nil)
53-
_ Conn = (*sql.DB)(nil)
54-
_ ConnTx = (*sql.Tx)(nil)
53+
_ Conn = (*sqlx.Conn)(nil)
54+
_ Conn = (*sqlx.DB)(nil)
55+
_ ConnTx = (*sqlx.Tx)(nil)
5556
log = zerolog.NewZeroLog(context.Background(), os.Stdout)
5657
)
5758

@@ -174,11 +175,11 @@ func (DataSource) ExecSQL(sqlResult sql.Result, err error) exec {
174175
return exec{sqlResult, err}
175176
}
176177

177-
func (DataSource) QuerySQL(sqlRows *sql.Rows, err error) Query {
178+
func (DataSource) QuerySQL(sqlRows *sqlx.Rows, err error) Query {
178179
return query{sqlRows, err}
179180
}
180181

181-
func (DataSource) EndTx(tx *sql.Tx, err error) error {
182+
func (DataSource) EndTx(tx *sqlx.Tx, err error) error {
182183
if tx == nil {
183184
log.Z().Err(ErrInvalidTransaction).Msg("[database:EndTx]")
184185

0 commit comments

Comments
 (0)