feat: add list of currencies
This commit is contained in:
@@ -5,11 +5,13 @@ import (
|
||||
"git.ma-al.com/goc_daniel/b2b/app/model"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/query/filters"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/query/find"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type UICurrencyRepo interface {
|
||||
CreateConversionRate(currencyRate *model.CurrencyRate) error
|
||||
Get(id uint) (*model.Currency, error)
|
||||
Find(langId uint, p find.Paging, filt *filters.FiltersList) (*find.Found[model.Currency], error)
|
||||
}
|
||||
|
||||
type CurrencyRepo struct{}
|
||||
@@ -25,19 +27,12 @@ func (repo *CurrencyRepo) CreateConversionRate(currencyRate *model.CurrencyRate)
|
||||
func (repo *CurrencyRepo) Get(id uint) (*model.Currency, error) {
|
||||
var currency model.Currency
|
||||
|
||||
err := db.DB.Table("b2b_currencies c").
|
||||
Select("c.*, r.conversion_rate").
|
||||
Joins(`
|
||||
LEFT JOIN b2b_currency_rates r
|
||||
ON r.b2b_id_currency = c.id
|
||||
AND r.created_at = (
|
||||
SELECT MAX(created_at)
|
||||
FROM b2b_currency_rates
|
||||
WHERE b2b_id_currency = c.id
|
||||
)
|
||||
`).
|
||||
Where("c.id = ?", id).
|
||||
Scan(¤cy).Error
|
||||
err := db.DB.
|
||||
Model(&model.Currency{}).
|
||||
Scopes(WithLatestRate()).
|
||||
Select("b2b_currencies.*, r.conversion_rate").
|
||||
Where("b2b_currencies.id = ?", id).
|
||||
First(¤cy).Error
|
||||
|
||||
return ¤cy, err
|
||||
}
|
||||
@@ -46,8 +41,24 @@ func (repo *CurrencyRepo) Find(langId uint, p find.Paging, filt *filters.Filters
|
||||
|
||||
found, err := find.Paginate[model.Currency](langId, p, db.DB.
|
||||
Model(&model.Currency{}).
|
||||
Scopes(WithLatestRate()).
|
||||
Select("b2b_currencies.*, r.conversion_rate").
|
||||
Scopes(filt.All()...),
|
||||
)
|
||||
|
||||
return &found, err
|
||||
}
|
||||
|
||||
func WithLatestRate() func(db *gorm.DB) *gorm.DB {
|
||||
return func(db *gorm.DB) *gorm.DB {
|
||||
return db.Joins(`
|
||||
LEFT JOIN b2b_currency_rates r
|
||||
ON r.b2b_id_currency = b2b_currencies.id
|
||||
AND r.created_at = (
|
||||
SELECT MAX(created_at)
|
||||
FROM b2b_currency_rates
|
||||
WHERE b2b_id_currency = b2b_currencies.id
|
||||
)
|
||||
`)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user