mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
added query logger
This commit is contained in:
@@ -18,7 +18,6 @@ type UsersService struct {
|
||||
Db *pg.DB
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Create
|
||||
|
||||
@@ -82,7 +81,11 @@ func (us *UsersService) Login(ctx context.Context, loginBody *models.Login) (*mo
|
||||
exceptionReturn := new(models.Exception)
|
||||
tokenPayload := new(models.Token)
|
||||
|
||||
db.Model(check).Where("? = ?", pg.Ident("email"), loginBody.Email).Select()
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
tx.Model(check).Where("? = ?", pg.Ident("email"), loginBody.Email).Select()
|
||||
|
||||
if check.Email == "" {
|
||||
exceptionReturn.Message = "Email not found"
|
||||
exceptionReturn.ErrorCode = "400103"
|
||||
@@ -109,6 +112,8 @@ func (us *UsersService) Login(ctx context.Context, loginBody *models.Login) (*mo
|
||||
|
||||
tokenPayload.Token = token
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return tokenPayload, exceptionReturn
|
||||
}
|
||||
|
||||
|
||||
@@ -3,15 +3,43 @@ package db
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
)
|
||||
|
||||
type LoggerHook struct {
|
||||
QueryLogger *log.Logger
|
||||
}
|
||||
|
||||
func (e *LoggerHook) BeforeQuery(ctx context.Context, qe *pg.QueryEvent) (context.Context, error) {
|
||||
return context.Background(), nil
|
||||
}
|
||||
func (e *LoggerHook) AfterQuery(ctx context.Context, qe *pg.QueryEvent) error {
|
||||
if qe.Err != nil {
|
||||
e.QueryLogger.Println(qe.Err)
|
||||
} else {
|
||||
fmtdQry, err := qe.FormattedQuery()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
e.QueryLogger.Println(string(fmtdQry))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateConnection(ctx context.Context, dbUrl string) *pg.DB {
|
||||
opt, err := pg.ParseURL(dbUrl)
|
||||
common.CheckError(err)
|
||||
file, err := os.OpenFile("querys.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
|
||||
common.CheckError(err)
|
||||
QueryLogger := log.New(file, "Query: ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||
conn := pg.Connect(opt)
|
||||
loggerHook := LoggerHook{QueryLogger}
|
||||
conn.AddQueryHook(&loggerHook)
|
||||
db := conn.WithContext(ctx)
|
||||
|
||||
fmt.Println("Successfully connected!")
|
||||
|
||||
Reference in New Issue
Block a user