Skip to content

Commit

Permalink
add tutorial 5
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoLiberali committed Aug 23, 2023
1 parent 0718c30 commit 2f70aa1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ tutorial_3:
go run -tags=tutorial_3 .

tutorial_4:
go run -tags=tutorial_4 .
go run -tags=tutorial_4 .

tutorial_5:
go run -tags=tutorial_5 .
45 changes: 45 additions & 0 deletions tutorial_5.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 2f70aa1

Please sign in to comment.