-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c25d76
commit e01becd
Showing
7 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package database | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/jmoiron/sqlx" | ||
"github.com/opentracing/opentracing-go" | ||
"github.com/voltgizerz/POS-restaurant/config" | ||
"github.com/voltgizerz/POS-restaurant/pkg/logger" | ||
) | ||
|
||
type DatabaseOpts struct { | ||
MasterDB *sqlx.DB | ||
} | ||
|
||
// InitDatabase initializes and returns a new Database instance | ||
func InitDatabase(ctx context.Context, cfg config.Database) *DatabaseOpts { | ||
span, ctx := opentracing.StartSpanFromContext(ctx, "database.InitDatabase") | ||
defer span.Finish() | ||
|
||
return &DatabaseOpts{ | ||
MasterDB: connectMySQL(ctx, cfg), | ||
} | ||
} | ||
|
||
func connectMySQL(ctx context.Context, cfg config.Database) *sqlx.DB { | ||
span, _ := opentracing.StartSpanFromContext(ctx, "database.connectMySQL") | ||
defer span.Finish() | ||
|
||
db, err := sqlx.Connect("mysql", cfg.Host) | ||
if err != nil { | ||
logger.LogStdErr.Fatalf("[connectMySQL] failed connect err: %s", err.Error()) | ||
} | ||
|
||
return db | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,11 @@ | ||
package interfaces | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/voltgizerz/POS-restaurant/internal/app/entity" | ||
) | ||
|
||
type IUserRepository interface { | ||
GetUser(ctx context.Context, int64 int64) (*entity.User, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package repository | ||
|
||
import ( | ||
"github.com/voltgizerz/POS-restaurant/database" | ||
) | ||
|
||
type RepositoryOpts struct { | ||
Database *database.DatabaseOpts | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package repository | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/jmoiron/sqlx" | ||
"github.com/opentracing/opentracing-go" | ||
"github.com/sirupsen/logrus" | ||
"github.com/voltgizerz/POS-restaurant/internal/app/entity" | ||
"github.com/voltgizerz/POS-restaurant/internal/app/interfaces" | ||
"github.com/voltgizerz/POS-restaurant/pkg/logger" | ||
) | ||
|
||
type UserRepository struct { | ||
MasterDB *sqlx.DB | ||
} | ||
|
||
func NewUserRepository(opts RepositoryOpts) interfaces.IUserRepository { | ||
return &UserRepository{ | ||
MasterDB: opts.Database.MasterDB, | ||
} | ||
} | ||
|
||
func (r *UserRepository) GetUser(ctx context.Context, userID int64) (*entity.User, error) { | ||
span, _ := opentracing.StartSpanFromContext(ctx, "repo.UserRepository.GetUser") | ||
defer span.Finish() | ||
|
||
user := entity.User{} | ||
err := r.MasterDB.Get(&user, "SELECT * FROM users WHERE id=$1", userID) | ||
if err != nil { | ||
logger.LogStdErr.WithFields(logrus.Fields{ | ||
"user_id": userID, | ||
"error": err, | ||
}).Error("[UserRepository] error on GetUser") | ||
|
||
return nil, err | ||
} | ||
|
||
return &user, nil | ||
} |