I have 2 tables: Actors and Films. I need to get actors with all their films by changeable filters - they can be present or absent, a single value or a slice of possible values.
I suspect that the way I made it work (with the db object) smells bad.
Any suggestions?
var (
orderBy string
count uint
where = map[string]interface{}{}
data []ActorsWithFilms
db = ms.db.Table("actor")
)
if filter.ActorName != "" {
where["actor_name"] = filter.ActorName
}
if filter.UpdatedFrom != "" {
db = db.Where("actor.updated_at >= ?", filter.UpdatedFrom)
}
if len(filter.FilmNames) != 0 {
db = db.Where("film.name in (?)", filter.FilmNames)
}
if len(filter.Statuses) != 0 {
db = db.Where("actor.status in (?)", filter.Statuses)
}
err := db.
Joins("join film on film.actorID = actor.id").
Where(where).
Preload("Film").
Order(orderBy).
Limit(filter.GetLimit()).
Offset(filter.Offset).
Find(&data).
Count(&count).
Error