insert prices change

This commit is contained in:
nquidox 2026-04-02 17:57:48 +03:00
parent db9684debc
commit 544c5348f1
3 changed files with 10 additions and 10 deletions

View file

@ -2,6 +2,7 @@ package merch
import (
"context"
log "github.com/sirupsen/logrus"
"merch-api/internal/common"
"merch-api/pkg/utils"
)
@ -30,6 +31,7 @@ func (p *provider) GetTasks(ctx context.Context) ([]common.Task, error) {
}
func (p *provider) InsertPrices(ctx context.Context, rawPrices []common.Result) error {
log.WithField("payload", rawPrices).Debugf("%v Inserting prices", providerLogHeader)
if rawPrices == nil || len(rawPrices) == 0 {
logDebug(providerLogHeader, "no prices given")
return nil
@ -56,15 +58,13 @@ func (p *provider) InsertPrices(ctx context.Context, rawPrices []common.Result)
var insertPrices []Price
for _, rawPrice := range rawPrices {
insertPrices = append(insertPrices, Price{
CreatedAt: now,
UpdatedAt: p.utils.NullTimeFromNow(now),
MerchId: uuidMap[rawPrice.MerchUuid],
OriginId: originIds[rawPrice.OriginName],
Price: int(rawPrice.Price),
})
}
if err = p.service.repo.insertPrices(ctx, insertPrices); err != nil {
if err = p.service.repo.insertPrices(ctx, now, p.utils.NullTimeFromNow(now), insertPrices); err != nil {
logErr(serviceLogHeader, err)
return err
}

View file

@ -170,7 +170,7 @@ func (r *repo) getMerchUuidMap(ctx context.Context, merchUuids []string) (map[st
return nil, err
}
var merchUuidMap map[string]int64
merchUuidMap := make(map[string]int64)
for rows.Next() {
var (
uuid string

View file

@ -8,7 +8,7 @@ import (
)
type Prices interface {
insertPrices(ctx context.Context, prices []Price) error
insertPrices(ctx context.Context, now time.Time, updatedAt sql.NullTime, prices []Price) error
getPricesWithDays(ctx context.Context, userId int64, days time.Time) ([]Price, error)
getDistinctPrices(ctx context.Context, userId int64, merchUuid string, days time.Time) ([]Price, error)
@ -17,7 +17,7 @@ type Prices interface {
deleteZeroPrices(ctx context.Context, userId int64, now sql.NullTime, list []int64) error
}
func (r *repo) insertPrices(ctx context.Context, prices []Price) error {
func (r *repo) insertPrices(ctx context.Context, now time.Time, updatedAt sql.NullTime, prices []Price) error {
q := `
INSERT INTO merch_prices (created_at, updated_at, merch_id, origin_id, price)
SELECT $1, $2, src.merch_id, src.origin_id, src.price
@ -40,7 +40,7 @@ func (r *repo) insertPrices(ctx context.Context, prices []Price) error {
priceValues = append(priceValues, price.Price)
}
_, err := r.db.Exec(ctx, q, merchIds, originIds, priceValues)
_, err := r.db.Exec(ctx, q, now, updatedAt, merchIds, originIds, priceValues)
if err != nil {
return err
}