postgresql
This commit is contained in:
25
cmd/cli/goose.go
Normal file
25
cmd/cli/goose.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gitea.home.4it.me/dilap54/platiparser/gorm"
|
||||
"github.com/pressly/goose"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func init() {
|
||||
commands = append(commands, gooseCommand)
|
||||
}
|
||||
|
||||
var gooseCommand = &cli.Command{
|
||||
Name: "goose",
|
||||
Action: func(c *cli.Context) error {
|
||||
db, err := gorm.GetDB().DB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := goose.Run(c.Args().First(), db, "./migrations", c.Args().Tail()...); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
30
cmd/cli/main.go
Normal file
30
cmd/cli/main.go
Normal file
@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func init() {
|
||||
godotenv.Load(".env")
|
||||
}
|
||||
|
||||
var commands = make([]*cli.Command, 0)
|
||||
|
||||
func main() {
|
||||
s := &cli.App{
|
||||
Name: "ricapi",
|
||||
Commands: commands,
|
||||
Before: func(c *cli.Context) error {
|
||||
godotenv.Load(".env")
|
||||
return nil
|
||||
},
|
||||
}
|
||||
err := s.Run(os.Args)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
@ -7,16 +7,25 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.home.4it.me/dilap54/platiparser/gorm"
|
||||
"gitea.home.4it.me/dilap54/platiparser/plati"
|
||||
"github.com/joho/godotenv"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
)
|
||||
|
||||
func init() {
|
||||
godotenv.Load(".env")
|
||||
}
|
||||
|
||||
func main() {
|
||||
categories := openCategories("./categories.json").Content
|
||||
categories = filterBySubstring("Gift", categories)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
db := gorm.GetDB()
|
||||
platiCli := plati.New()
|
||||
|
||||
// printNames(categories)
|
||||
@ -27,10 +36,40 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if len(goods) == 0 {
|
||||
continue
|
||||
}
|
||||
gormGoods := convertGoodsToGorm(c, goods)
|
||||
if err := db.Create(gormGoods).Error; err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
json.NewEncoder(os.Stdout).Encode(goods)
|
||||
}
|
||||
}
|
||||
|
||||
func convertGoodsToGorm(cat *plati.Category, goods []*plati.Good) []*gorm.Good {
|
||||
out := make([]*gorm.Good, 0, len(goods))
|
||||
|
||||
for _, g := range goods {
|
||||
gormGood := gorm.Good{
|
||||
ID: uuid.NewV4().String(),
|
||||
Name: g.Name,
|
||||
IDC: cat.ID,
|
||||
IDR: cat.ParentID,
|
||||
Goodlink: g.GoodLink,
|
||||
Seller: g.Seller,
|
||||
Sellerlink: g.SellerLink,
|
||||
Sellerrating: g.SellerRating,
|
||||
Sold: g.Sold,
|
||||
Price: g.Price,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
out = append(out, &gormGood)
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func filterBySubstring(substring string, categories plati.Categories) plati.Categories {
|
||||
out := make(plati.Categories, 0)
|
||||
for _, c := range categories {
|
Reference in New Issue
Block a user