Last active
December 23, 2021 11:58
-
-
Save avrebarra/64b064a7d53abe2d2c01301e7b8dd2c0 to your computer and use it in GitHub Desktop.
avrebarra.github.io/simple-trick-to-know-if-your-code-is-readable/
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
var ( | |
TableName = "transaction" | |
) | |
type Input struct { | |
TrxID string `validate:"-"` | |
ReferenceID string `validate:"-"` | |
UserID string `validate:"-"` | |
Status string `validate:"-"` | |
Limit int `validate:"required"` | |
Offset int `validate:"-"` | |
} | |
func (p *Postgre) Find(ctx context.Context, i Input) (o []Transaction, tot int64, err error) { | |
if err = validator.Validate(i); err != nil { | |
return | |
} | |
type Filter struct { | |
TransactionID string | |
ReferenceID string | |
Status string | |
UserID string | |
} | |
f := Filter{ | |
TransactionID: i.TrxID, | |
ReferenceID: i.ReferenceID, | |
Status: i.Status, | |
UserID: i.UserID, | |
} | |
t := TableName | |
err = p.Config.GORM. | |
WithContext(ctx). | |
Table(t). | |
Where(f). | |
Count(&tot). | |
Error | |
if err != nil { | |
return | |
} | |
if tot == 0 { | |
o = []Transaction{} | |
return | |
} | |
output := []Postgre_Transaction{} | |
err = p.Config.GORM. | |
WithContext(ctx). | |
Limit(i.Limit). | |
Offset(i.Offset). | |
Where(f). | |
Find(&output). | |
Error | |
if errors.Is(err, gorm.ErrRecordNotFound) { | |
err = fmt.Errorf("%w: %s", NotFoundError, err) | |
} | |
if err != nil { | |
return | |
} | |
o = []Transaction{} | |
for _, v := range output { | |
o = append(o, v.ToCommon()) | |
} | |
return | |
} |
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
var ( | |
TableNameTransaction = "transaction" | |
) | |
type FindInput struct { | |
TrxID string `validate:"-"` | |
ReferenceID string `validate:"-"` | |
UserID string `validate:"-"` | |
Status string `validate:"-"` | |
PaginationLimit int `validate:"required"` | |
PaginationOffset int `validate:"-"` | |
} | |
// Find will select and count data according to input filter from storage. | |
func (p *Postgre) Find(ctx context.Context, in FindInput) (out []Transaction, total int64, err error) { | |
// prep and validate input | |
if err = validator.Validate(in); err != nil { | |
err = fmt.Errorf("bad input: %w", err) | |
return | |
} | |
// prepare query filter | |
type QueryFilter struct { | |
TransactionID string | |
ReferenceID string | |
Status string | |
UserID string | |
} | |
filter := QueryFilter{ | |
TransactionID: in.TrxID, | |
ReferenceID: in.ReferenceID, | |
Status: in.Status, | |
UserID: in.UserID, | |
} | |
// perform counting | |
tablename := TableNameTransaction | |
err = p.Config.GORM. | |
WithContext(ctx). | |
Table(tablename). | |
Where(filter). | |
Count(&total). | |
Error | |
if err != nil { | |
err = fmt.Errorf("count failure: %w", err) | |
return | |
} | |
if total == 0 { | |
// return empty results, no error | |
out = []Transaction{} | |
return | |
} | |
// perform query | |
trxs := []Postgre_Transaction{} | |
err = p.Config.GORM. | |
WithContext(ctx). | |
Limit(in.PaginationLimit). | |
Offset(in.PaginationOffset). | |
Where(filter). | |
Find(&trxs). | |
Error | |
if errors.Is(err, gorm.ErrRecordNotFound) { | |
err = fmt.Errorf("%w: %s", NotFoundError, err) | |
} | |
if err != nil { | |
err = fmt.Errorf("query failure: %w", err) | |
return | |
} | |
// build output | |
out = []Transaction{} | |
for _, v := range trxs { | |
out = append(out, v.ToCommon()) | |
} | |
return | |
} |
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
type Filter struct { | |
TransactionID string | |
ReferenceID string | |
Status string | |
UserID string | |
} | |
f := Filter{ | |
TransactionID: i.TrxID, | |
ReferenceID: i.ReferenceID, | |
Status: i.Status, | |
UserID: i.UserID, | |
} | |
t := TableName | |
err = p.Config.GORM. | |
WithContext(ctx). | |
Table(t). | |
Where(f). | |
Count(&tot). | |
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
var ( | |
TableName = "transaction" | |
) |
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
output := []Postgre_Transaction{} | |
err = p.Config.GORM. | |
WithContext(ctx). | |
Limit(i.Limit). | |
Offset(i.Offset). | |
Where(f). | |
Find(&output). | |
Error | |
if errors.Is(err, gorm.ErrRecordNotFound) { | |
err = fmt.Errorf("%w: %s", NotFoundError, err) | |
} | |
if err != nil { | |
return | |
} | |
o = []Transaction{} | |
for _, v := range output { | |
o = append(o, v.ToCommon()) | |
} | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment