Skip to content

Instantly share code, notes, and snippets.

@Solution
Created May 13, 2021 13:05
Show Gist options
  • Save Solution/45211581836f74266ef71394bf975199 to your computer and use it in GitHub Desktop.
Save Solution/45211581836f74266ef71394bf975199 to your computer and use it in GitHub Desktop.
type QueryObject struct {
query *gorm.DB
}
func NewQueryObject(database *gorm.DB) *QueryObject {
return &QueryObject{query: database.Model(&User{})}
}
func (qo *QueryObject) FilterByUsername(username string) *QueryObject {
qo.query = qo.query.Where("username = ?", username)
return qo
}
func (qo *QueryObject) FilterByActive() *QueryObject {
qo.query = qo.query.Where("active = ?", true)
return qo
}
func (qo *QueryObject) GetQuery() *gorm.DB {
return qo.query
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment