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" "github.com/ectrc/snow/aid"
p "github.com/ectrc/snow/person" p "github.com/ectrc/snow/person"
"github.com/ectrc/snow/storage"
) )
var ( var (
@ -115,15 +116,7 @@ func NewFortnitePerson(displayName string, everything bool) *p.Person {
person.AthenaProfile.Attributes.AddAttribute(p.NewAttribute("active_loadout_index", 0)).Save() person.AthenaProfile.Attributes.AddAttribute(p.NewAttribute("active_loadout_index", 0)).Save()
if everything { if everything {
for _, item := range Cosmetics.Items { GiveEverything(person)
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()
}
} }
person.Save() person.Save()
@ -132,13 +125,24 @@ func NewFortnitePerson(displayName string, everything bool) *p.Person {
} }
func GiveEverything(person *p.Person) { func GiveEverything(person *p.Person) {
items := make([]storage.DB_Item, 0)
for _, item := range Cosmetics.Items { for _, item := range Cosmetics.Items {
if strings.Contains(strings.ToLower(item.ID), "random") { if strings.Contains(strings.ToLower(item.ID), "random") {
continue continue
} }
has := person.AthenaProfile.Items.GetItemByTemplateID(item.ID)
if has != nil {
continue
}
item := p.NewItem(item.Type.BackendValue + ":" + item.ID, 1) item := p.NewItem(item.Type.BackendValue + ":" + item.ID, 1)
item.HasSeen = true 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{ return &storage.DB_Item{
ProfileID: profileId,
ID: i.ID, ID: i.ID,
ProfileID: profileId,
TemplateID: i.TemplateID, TemplateID: i.TemplateID,
Quantity: i.Quantity, Quantity: i.Quantity,
Favorite: i.Favorite, Favorite: i.Favorite,

View File

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

View File

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

View File

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