ApplyQuery interface
This commit is contained in:
21
app/main.go
21
app/main.go
@ -29,7 +29,18 @@ type CertFilter struct {
|
||||
IdsNot *[]string `filterfield:"field=id;operator=NOT_IN"`
|
||||
CreatedAt_Lt *time.Time `filterfield:"field=created_at;operator=LT"`
|
||||
Timestamps *[]time.Time `filterfield:"field=created_at;operator=IN"`
|
||||
// CompanyIsActive *bool `filterfield:"joins=companies;field=is_active;operator=EQ"`
|
||||
CompanyIsActive *bool
|
||||
}
|
||||
|
||||
func (f CertFilter) ApplyQuery(query *gorm.DB) *gorm.DB {
|
||||
if f.CompanyIsActive != nil {
|
||||
query = query.Joins(
|
||||
fmt.Sprintf(
|
||||
"JOIN companies ON certificates.company_id = companies.id WHERE companies.is_active = %t",
|
||||
*f.CompanyIsActive,
|
||||
))
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
type CompanyFilter struct {
|
||||
@ -99,7 +110,7 @@ func doListWithJoins(db *gorm.DB) {
|
||||
repo.Init(db, nil)
|
||||
|
||||
filter := CertFilter{
|
||||
Alive: &TRUE,
|
||||
CompanyIsActive: &FALSE,
|
||||
}
|
||||
|
||||
certs, err := repo.List(filter, nil)
|
||||
@ -203,11 +214,11 @@ func main() {
|
||||
db := db.InitDB()
|
||||
|
||||
// doMagic(db)
|
||||
doList(db)
|
||||
// doListWithJoins(db)
|
||||
// doList(db)
|
||||
doListWithJoins(db)
|
||||
// doCount(db)
|
||||
// doSave(db)
|
||||
doDelete(db)
|
||||
// doDelete(db)
|
||||
// doGet(db)
|
||||
// doExists(db)
|
||||
// inheritance.DoInheritanceTest()
|
||||
|
||||
Reference in New Issue
Block a user