pcgamedb/db/import.go
nite07 ed12329be7 add import
fix organize log
change crawl task trigger body
fix CleanOrphanGamesInGameInfos delete all data
2024-11-17 13:29:04 +08:00

46 lines
880 B
Go

package db
import (
"context"
"encoding/json"
"os"
"github.com/nitezs/pcgamedb/model"
)
func ImportGameInfo(filePath string) error {
gameInfo := []*model.GameInfo{}
data, err := os.ReadFile(filePath)
if err != nil {
return err
}
err = json.Unmarshal(data, &gameInfo)
if err != nil {
return err
}
docs := make([]interface{}, len(gameInfo))
for i, v := range gameInfo {
docs[i] = v
}
_, err = GameInfoCollection.InsertMany(context.Background(), docs)
return err
}
func ImportGameItem(filePath string) error {
gameItem := []*model.GameItem{}
data, err := os.ReadFile(filePath)
if err != nil {
return err
}
err = json.Unmarshal(data, &gameItem)
if err != nil {
return err
}
docs := make([]interface{}, len(gameItem))
for i, v := range gameItem {
docs[i] = v
}
_, err = GameItemCollection.InsertMany(context.Background(), docs)
return err
}