bulk create items to prevent blocking db

This commit is contained in:
eccentric 2023-12-17 00:43:17 +00:00
parent 352913535d
commit 25c2e59b16
5 changed files with 25 additions and 12 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/ectrc/snow/aid"
p "github.com/ectrc/snow/person"
"github.com/ectrc/snow/storage"
)
var (
@ -115,15 +116,7 @@ func NewFortnitePerson(displayName string, everything bool) *p.Person {
person.AthenaProfile.Attributes.AddAttribute(p.NewAttribute("active_loadout_index", 0)).Save()
if everything {
for _, item := range Cosmetics.Items {
if strings.Contains(strings.ToLower(item.ID), "random") {
continue
}
item := p.NewItem(item.Type.BackendValue + ":" + item.ID, 1)
item.HasSeen = true
person.AthenaProfile.Items.AddItem(item).Save()
}
GiveEverything(person)
}
person.Save()
@ -132,13 +125,24 @@ func NewFortnitePerson(displayName string, everything bool) *p.Person {
}
func GiveEverything(person *p.Person) {
items := make([]storage.DB_Item, 0)
for _, item := range Cosmetics.Items {
if strings.Contains(strings.ToLower(item.ID), "random") {
continue
}
has := person.AthenaProfile.Items.GetItemByTemplateID(item.ID)
if has != nil {
continue
}
item := p.NewItem(item.Type.BackendValue + ":" + item.ID, 1)
item.HasSeen = true
person.AthenaProfile.Items.AddItem(item).Save()
person.AthenaProfile.Items.AddItem(item)
items = append(items, *item.ToDatabase(person.AthenaProfile.ID))
}
storage.Repo.BulkCreateItems(&items)
}

View File

@ -172,8 +172,8 @@ func (i *Item) ToDatabase(profileId string) *storage.DB_Item {
}
return &storage.DB_Item{
ProfileID: profileId,
ID: i.ID,
ProfileID: profileId,
TemplateID: i.TemplateID,
Quantity: i.Quantity,
Favorite: i.Favorite,

View File

@ -171,6 +171,10 @@ func (s *PostgresStorage) SaveItem(item *DB_Item) {
s.Postgres.Save(item)
}
func (s *PostgresStorage) BulkCreateItems(items *[]DB_Item) {
s.Postgres.Create(items)
}
func (s *PostgresStorage) DeleteItem(itemId string) {
s.Postgres.Delete(&DB_Item{}, "id = ?", itemId)
}

View File

@ -22,6 +22,7 @@ type Storage interface {
DeleteProfile(profileId string)
SaveItem(item *DB_Item)
BulkCreateItems(items *[]DB_Item)
DeleteItem(itemId string)
SaveVariant(variant *DB_VariantChannel)
@ -118,6 +119,10 @@ func (r *Repository) SaveItem(item *DB_Item) {
r.Storage.SaveItem(item)
}
func (r *Repository) BulkCreateItems(items *[]DB_Item) {
r.Storage.BulkCreateItems(items)
}
func (r *Repository) DeleteItem(itemId string) {
r.Storage.DeleteItem(itemId)
}

View File

@ -171,7 +171,7 @@ type DB_SeasonStat struct {
XP int
Tier int
Stars int
LevelClainmed int
LevelClaimed int
TierClaimed int
}