diff --git a/Makefile b/Makefile index baf9b6e..26eb7d0 100644 --- a/Makefile +++ b/Makefile @@ -8,4 +8,7 @@ tutorial_3: go run -tags=tutorial_3 . tutorial_4: - go run -tags=tutorial_4 . \ No newline at end of file + go run -tags=tutorial_4 . + +tutorial_5: + go run -tags=tutorial_5 . \ No newline at end of file diff --git a/tutorial_5.go b/tutorial_5.go new file mode 100644 index 0000000..ff351b6 --- /dev/null +++ b/tutorial_5.go @@ -0,0 +1,45 @@ +//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" + "github.com/ditrit/badaas/orm/dynamic" + "github.com/ditrit/badaas/orm/model" + "go.uber.org/fx" +) + +// Target: get all cities whose name is 'Paris' and that are the capital of their country +func tutorial(crudCityService orm.CRUDService[models.City, model.UUID], shutdowner fx.Shutdowner) { + cities, err := crudCityService.Query( + conditions.CityName(orm.Eq("Paris")), + conditions.CityCountry( + conditions.CountryCapitalId( + dynamic.Eq(conditions.CityIdField), + ), + ), + ) + + // 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() +}