insert prices method
This commit is contained in:
parent
acb8b8d4a0
commit
6a21576c1c
3 changed files with 55 additions and 9 deletions
|
|
@ -48,5 +48,5 @@ type Price struct {
|
|||
DeletedAt sql.NullTime `json:"deleted_at" gorm:"column:deleted_at"`
|
||||
MerchUuid string `json:"merch_uuid" gorm:"column:merch_uuid"`
|
||||
Price int `json:"price" gorm:"column:price"`
|
||||
Origin Origin `json:"origin" gorm:"column:origin"`
|
||||
Origin Origin `json:"origin" gorm:"column:origin;type:integer"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,10 @@
|
|||
package merch
|
||||
|
||||
import "encoding/json"
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Origin int
|
||||
|
||||
|
|
@ -25,3 +29,16 @@ func (o Origin) String() string {
|
|||
func (o Origin) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(o.String())
|
||||
}
|
||||
|
||||
func parseOrigin(s string) (Origin, bool) {
|
||||
for i, name := range Origins {
|
||||
if name == strings.ToLower(s) {
|
||||
return Origin((i + 1) * 1000), true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func (o Origin) Value() (driver.Value, error) {
|
||||
return int(o), nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
package merch
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
"merch-parser-api/internal/shared"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Link struct {
|
||||
|
|
@ -15,8 +18,8 @@ type TaskProvider struct {
|
|||
}
|
||||
|
||||
type TaskRepository interface {
|
||||
GetLinks() (*Link, error)
|
||||
InsertPrices() error
|
||||
getLinks() (*Link, error)
|
||||
insertPrices(prices []Price) error
|
||||
}
|
||||
|
||||
type TaskRepo struct {
|
||||
|
|
@ -34,7 +37,7 @@ func NewTaskRepository(db *gorm.DB) TaskRepository {
|
|||
}
|
||||
|
||||
func (p *TaskProvider) PrepareTasks() (map[string]shared.Task, error) {
|
||||
getLinks, err := p.repo.GetLinks()
|
||||
getLinks, err := p.repo.getLinks()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -67,11 +70,37 @@ func (p *TaskProvider) PrepareTasks() (map[string]shared.Task, error) {
|
|||
return taskMap, nil
|
||||
}
|
||||
|
||||
func (p *TaskProvider) InsertPrices([]shared.TaskResult) error {
|
||||
func (p *TaskProvider) InsertPrices(prices []shared.TaskResult) error {
|
||||
if len(prices) == 0 {
|
||||
log.WithField("msg", "no prices received").Debug("Merch provider | Insert prices")
|
||||
return nil
|
||||
}
|
||||
|
||||
var insertPrices []Price
|
||||
for _, item := range prices {
|
||||
origin, ok := parseOrigin(item.Origin)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
insertPrices = append(insertPrices, Price{
|
||||
CreatedAt: time.Now().UTC(),
|
||||
UpdatedAt: sql.NullTime{Time: time.Time{}, Valid: false},
|
||||
DeletedAt: sql.NullTime{Time: time.Time{}, Valid: false},
|
||||
MerchUuid: item.MerchUuid,
|
||||
Price: int(item.Price),
|
||||
Origin: origin,
|
||||
})
|
||||
}
|
||||
|
||||
if err := p.repo.insertPrices(insertPrices); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *TaskRepo) GetLinks() (*Link, error) {
|
||||
func (r *TaskRepo) getLinks() (*Link, error) {
|
||||
var surugayaList []Surugaya
|
||||
if err := r.db.Model(&Surugaya{}).Find(&surugayaList).Error; err != nil {
|
||||
return nil, err
|
||||
|
|
@ -88,6 +117,6 @@ func (r *TaskRepo) GetLinks() (*Link, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (r *TaskRepo) InsertPrices() error {
|
||||
return nil
|
||||
func (r *TaskRepo) insertPrices(prices []Price) error {
|
||||
return r.db.Model(&Price{}).Create(&prices).Error
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue