41 lines
1.1 KiB
Go
41 lines
1.1 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/samber/lo"
|
|
"github.com/uptrace/bun"
|
|
|
|
"git.lehouerou.net/laurent/sorarebuddy/model"
|
|
)
|
|
|
|
type CountryRepository struct {
|
|
*Repository[model.Country]
|
|
}
|
|
|
|
func NewCountryRepository(db *bun.DB) *CountryRepository {
|
|
return &CountryRepository{
|
|
Repository: NewRepository[model.Country](db, []string{"slug"}),
|
|
}
|
|
}
|
|
|
|
func (r *CountryRepository) GetBySlug(ctx context.Context, slug string) (model.Country, error) {
|
|
var res model.Country
|
|
err := r.db.NewSelect().Model(&res).Where("slug = ?", slug).Scan(ctx)
|
|
if err != nil {
|
|
return model.Country{}, errors.Wrapf(err, "getting country by slug : %s", slug)
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func (r *CountryRepository) GetCountrySlugsNotInDb(ctx context.Context, countrySlugs []string) ([]string, error) {
|
|
var countries []model.Country
|
|
err := r.db.NewSelect().Model(&countries).Where("slug IN (?)", bun.In(countrySlugs)).Scan(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
diff, _ := lo.Difference(countrySlugs, lo.Map(countries, func(c model.Country, index int) string { return c.Slug }))
|
|
return diff, nil
|
|
}
|