mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
add cron job to sync currencies once in 24 hours
This commit is contained in:
50
pkg/service/currency.go
Normal file
50
pkg/service/currency.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"wallet-api/pkg/model"
|
||||
"wallet-api/pkg/repository"
|
||||
"wallet-api/pkg/utl/common"
|
||||
)
|
||||
|
||||
type CurrencyService struct {
|
||||
repository *repository.CurrencyRepository
|
||||
logger *log.Logger
|
||||
}
|
||||
|
||||
func NewCurrencyService(repository *repository.CurrencyRepository, logger *log.Logger) *CurrencyService {
|
||||
return &CurrencyService{
|
||||
repository: repository,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
GetFirst
|
||||
|
||||
Gets first row from Currency table.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
Returns:
|
||||
model.CurrencyModel: Currency object from database.
|
||||
*/
|
||||
func (as CurrencyService) Sync(ctx context.Context) {
|
||||
resp, err := common.Fetch[model.ExchangeBody]("GET", "https://api.exchangerate-api.com/v4/latest/euro")
|
||||
if err != nil {
|
||||
as.logger.Println(err)
|
||||
return
|
||||
}
|
||||
m := resp.Rates.(map[string]interface{})
|
||||
|
||||
rates := new([]model.Rate)
|
||||
|
||||
for k, v := range m {
|
||||
rate := new(model.Rate)
|
||||
rate.Code = k
|
||||
rate.Rate = v.(float64)
|
||||
*rates = append(*rates, *rate)
|
||||
}
|
||||
as.repository.SyncBulk(ctx, rates)
|
||||
}
|
||||
Reference in New Issue
Block a user