-
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
fd81e9d
commit 536abb5
Showing
2 changed files
with
47 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,43 @@ | ||
//go:build tutorial_5 | ||
// +build tutorial_5 | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/ditrit/badaas-orm-tutorial/conditions" | ||
"github.com/ditrit/badaas-orm-tutorial/models" | ||
"github.com/ditrit/badaas/orm" | ||
"go.uber.org/fx" | ||
"gorm.io/gorm" | ||
) | ||
|
||
// Target: get all cities whose name is 'Paris' and that are the capital of their country | ||
func tutorial(db *gorm.DB, shutdowner fx.Shutdowner) { | ||
cities, err := orm.NewQuery[models.City]( | ||
db, | ||
conditions.City.NameIs().Eq("Paris"), | ||
conditions.City.Country( | ||
conditions.Country.CapitalIdIs().Dynamic().Eq(conditions.City.ID), | ||
), | ||
).Find() | ||
|
||
// SQL executed: | ||
// SELECT cities.* FROM cities | ||
// INNER JOIN countries Country ON | ||
// Country.id = cities.country_id AND Country.capital_id = cities.id AND Country.deleted_at IS NULL | ||
// WHERE cities.name = "Paris" AND cities.deleted_at IS NULL | ||
|
||
if err != nil { | ||
log.Panicln(err) | ||
} | ||
|
||
log.Println("Cities named 'Paris' that are the capital of their country are:") | ||
for i, city := range cities { | ||
fmt.Printf("\t%v: %+v\n", i+1, city) | ||
} | ||
|
||
shutdowner.Shutdown() | ||
} |